Beginner

The objective function

Objective Function

Estimated duration
20 minutes
Related questions
3
Related algorithms
2
Status
Log in to track

Lesson objectives

  • Formulating an objective function that reflects the real goal.
  • Handling constraints with penalties.
  • Recognizing noisy and expensive objectives.

Explanation

The algorithm optimizes exactly what we ask of it, not what we mean. A carelessly formulated objective function produces "optimal" solutions with no practical value.

The objective function f: Ω → ℝ turns a solution into a number. In machine learning it is usually the validation error (1 − accuracy, 1 − Dice, or RMSE). It must be computed on separate validation data or by cross-validation, and must not touch the test data.

Constraints: they can be enforced by repair (clipping the solution to the bounds), by rejection, or by a penalty function that adds a cost proportional to the size of the violation.

Noise and cost: a single evaluation of a deep model may take hours, and its result changes with the random seed. So limited evaluation budgets are used, with shortened training (fewer epochs or partial data) as an approximate surrogate, and the best solutions are re-evaluated with more repetitions.

Feasible regionConstraint violationf̃(x) = f(x) + λ · amount of violation
The penalty function makes violating solutions worse without removing them.

Mathematics

The penalty

F(x) = f(x) + \mu\sum_i \max(0, g_i(x))^2

μ is the penalty coefficient.

A composite objective for feature selection

F(S) = \alpha\,\text{err}(S) + (1-\alpha)\frac{|S|}{D}

α≈0.99 is common in the literature.

Python code

def fs_objective(mask, err_fn, alpha=0.99):
    k = sum(mask)
    if k == 0:
        return 1.0  # empty subset is invalid
    return alpha * err_fn(mask) + (1 - alpha) * k / len(mask)

Simplified teaching code that works with NumPy only; for research experiments use a documented library and fix the random seed.

Practical exercise

In the neural network lab (feature selection mode), change α and notice how the number of selected features changes.

Common mistakes

  • Computing the objective on the test data.
  • Using accuracy alone with imbalanced data.
  • Reporting the objective value from a single run with a noisy objective.

Summary

The objective function is the real specification of the problem: formulate it carefully, keep the validation data separate from the test data, and deal with noise.

References

There are no references recorded for this lesson.

Short quiz

3 questions are chosen at random from 3 questions linked to this lesson, and the quiz is graded on the server.

Links within the lab

Algorithms
Optimization types
Related lessons
In the paths