Beginner

Introduction to optimization

Introduction to Optimization

Estimated duration
25 minutes
Related questions
5
Related algorithms
2
Status
Log in to track

Lesson objectives

  • Defining an optimization problem and its components: variables, objective function, constraints.
  • Distinguishing minimization from maximization, and continuous from discrete problems.
  • Understanding the difference between the optimal solution and a solution that is acceptable in practice.

Explanation

Almost every engineering or scientific decision is a search for the "best" option by some criterion: lowest cost, highest accuracy, shortest time. Optimization is the branch of mathematics that formulates this search precisely and develops methods to solve it.

An optimization problem has three elements. First, the decision variables x, which are what we can control, such as the learning rate or the set of selected features. Second, the objective function f(x), which measures the quality of a solution and which we usually minimize (maximization is equivalent to minimizing −f). Third, the constraints, which define the feasible region, such as variable bounds or a time budget.

Problems are classified by the nature of the variables: continuous (real numbers), discrete/integer, combinatorial (an ordering or a subset), or mixed. They are also classified by the objective function: linear or non-linear, convex or non-convex, differentiable or a "black box" about which we know only its value at each point.

Black-box problems are very common in artificial intelligence: there is no analytical formula for a model's accuracy as a function of its hyperparameters; we can only train and measure. This is where heuristics and metaheuristics, which need no derivatives, come in.

x = 0Local minimumOne-dimensional rastrigin function: many valleys that fool local search
Objective function landscape (Fitness Landscape) of a multimodal function.

Mathematics

The general form

\min_{x\in\Omega} f(x)\quad \text{s.t.}\quad g_i(x)\le 0,\ h_j(x)=0

Ω is the search space, g the inequality constraints and h the equality constraints.

Maximization as minimization

\max_x f(x) = -\min_x\,(-f(x))

Python code

import numpy as np
# Objective: sphere function, minimum 0 at x = 0
def f(x):
    return float(np.sum(np.asarray(x) ** 2))

rng = np.random.default_rng(0)
samples = rng.uniform(-5, 5, size=(1000, 2))
best = min(samples, key=f)
print('best random sample:', best, f(best))

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

Practical exercise

Try the Sphere function in two dimensions in the simulator and notice how the solutions gather around the origin. Then switch to Rastrigin and notice the many valleys.

Common mistakes

  • Confusing the objective function with the final evaluation metric on the test data.
  • Ignoring the constraints and then clipping the solutions without documenting it.
  • Assuming that any method finds the global optimum in non-convex problems.

Summary

Optimization = variables + objective function + constraints. The nature of the variables and the objective function decides the suitable method, and black-box problems are common in machine learning.

References

Related papers in the repository

Short quiz

5 questions are chosen at random from 5 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