TExcelTAD
Home › Optimization › Optimization Intro

Optimization Intro

Almost every program that makes a decision is solving an optimization problem: a router picks the cheapest path, a scheduler packs jobs onto machines, a trading system balances return against risk, and a neural network adjusts millions of weights to lower a loss. This course is about the mathematics that makes those decisions good, fast and provably correct.

An optimization problem has three parts:

minimizef0(x)the objectivesubject tofi(x)≤0,i=1,…,minequality constraintshj(x)=0,j=1,…,pequality constraints\begin{aligned} \text{minimize} \quad & f_0(\mathbf{x}) && \text{the objective} \\ \text{subject to} \quad & f_i(\mathbf{x}) \le 0, \quad i = 1, \dots, m && \text{inequality constraints} \\ & h_j(\mathbf{x}) = 0, \quad j = 1, \dots, p && \text{equality constraints} \end{aligned}

The vector x∈Rn\mathbf{x} \in \mathbb{R}^n holds the decision variables. A point that satisfies all constraints is feasible, and a feasible point with the smallest objective value is optimal.

The reference books

The topic order follows two classic texts, used only as a map of topics:

  • Convex Optimization by Stephen Boyd and Lieven Vandenberghe (Cambridge University Press, 2004). The authors publish a free PDF at stanford.edu/~boyd/cvxbook. It sets the order of the convexity, duality and application parts.
  • Numerical Optimization by Jorge Nocedal and Stephen J. Wright (Springer, 2nd ed. 2006), whose catalogue of unconstrained methods (line search, trust regions, conjugate gradient, quasi-Newton, nonlinear least squares) sets the order of the algorithms part.

Thanks to the authors for these books, and to Boyd and Vandenberghe for making theirs freely available. All explanations, numbers, code and exercises here are original, written for programmers, with runnable Python.

What is in the course

Part Topics
1. Convex sets and functions problem forms, convex sets, separation, convex functions, smoothness, optimality conditions
2. Unconstrained optimization gradient descent, line search, Wolfe conditions, rates, Newton, BFGS, L-BFGS, conjugate gradient, trust regions, Gauss-Newton, coordinate descent
3. Constrained optimization feasible sets, Lagrange multipliers, KKT, duality, shadow prices, penalty methods, projected gradient
4. Linear programming LP forms, vertices, the simplex method, LP duality, interior-point methods, modelling tricks
5. Quadratic and conic programming QPs, active sets, SOCP and SDP
6. Nonsmooth and proximal methods subgradients, proximal operators, the lasso (ISTA, FISTA), ADMM
7. Stochastic optimization SGD, step-size schedules, variance reduction
8. Integer programming modelling with integers, branch and bound, cutting planes
9. Derivative-free methods golden-section search, Nelder-Mead, simulated annealing, genetic algorithms
10. Applications portfolios, scheduling, training ML models, choosing a solver

What you should know first

This is an advanced course. It links to the full treatment elsewhere instead of repeating it:

Here is the whole course in miniature. A small factory makes two products. Each unit of product A earns 3 and needs 1 hour of machine time and 2 kg of material; product B earns 5 and needs 2 hours and 1 kg. There are 8 hours and 10 kg available. How much of each should it make?

python · try it yourself
import numpy as np
from scipy.optimize import linprog

# maximize 3a + 5b  ->  minimize -(3a + 5b)
res = linprog(c=[-3, -5], A_ub=[[1, 2], [2, 1]], b_ub=[8, 10], bounds=[(0, None), (0, None)])
print("plan (a, b):", np.round(res.x, 4))
print("profit:", round(-res.fun, 4))
plan (a, b): [4. 2.]
profit: 22.0

The answer sits at a corner of the feasible region, where both resources are used up. That is no accident: The Geometry of Linear Programs explains why. And the same problem solved by brute force over a grid agrees:

python · try it yourself
import numpy as np

a, b = np.meshgrid(np.linspace(0, 5, 501), np.linspace(0, 4, 401))
feasible = (a + 2 * b <= 8 + 1e-9) & (2 * a + b <= 10 + 1e-9)
profit = np.where(feasible, 3 * a + 5 * b, -np.inf)
i = np.unravel_index(np.argmax(profit), profit.shape)
print("grid best (a, b):", round(float(a[i]), 2), round(float(b[i]), 2), " profit:", round(float(profit[i]), 2))
grid best (a, b): 4.0 2.0  profit: 22.0

The grid needed about 200,000 function evaluations to find what the solver found by visiting a handful of corners. Making that difference systematic is the subject of this course.

How lessons work

Each lesson explains one idea, states it in math notation (∇f(x)\nabla f(\mathbf{x}), ∇2f(x)\nabla^2 f(\mathbf{x}), L(x,λ)\mathcal{L}(\mathbf{x}, \boldsymbol{\lambda})), implements the algorithm in a few lines of NumPy, and ends with an In software note, exercises with answers and worked solutions, and a short quiz.

Tip: Our own implementations are short so that you can read them. For real work, use a tested library (scipy.optimize, CVXPY, OR-Tools, HiGHS, Gurobi). The last lesson, Choosing a Method and a Solver, maps problem types to tools.

Learning paths

Your goal Suggested route
ML engineer Math for ML → parts 1, 2, 6, 7 → Training Machine Learning Models → Training Mechanics, Evaluation & Model Improvement (optimizers in practice)
Operations research parts 1, 3, 4, 8 → Scheduling and Assignment
Quant / finance parts 1, 3, 5 → Portfolio Optimization
Numerical computing parts 2 and 3, with Numerical Methods

Exercises

Exercise 1: Name the parts

A delivery company wants the shortest total route that visits 20 shops, using at most 3 vans, each carrying at most 500 kg. Name the decision variables, the objective and the constraints.

Answer

Variables: which van visits which shops in which order. Objective: total distance. Constraints: every shop visited once, at most 3 vans, load per van at most 500 kg.

Solution

The variables are discrete (an assignment of shops to vans and an order per van), so this is an integer program, a vehicle-routing problem. The objective is the sum of the route lengths. The constraints are coverage (each shop exactly once), fleet size and capacity. Part 8 treats problems of this kind.

Exercise 2: Change the prices

In the factory example, suppose product B earns only 1 per unit instead of 5. Which corner do you expect to win?

Answer

Make only A: a=5a = 5, b=0b = 0, profit 15.

Solution

The corners of the feasible region are (0,0)(0, 0), (5,0)(5, 0), (4,2)(4, 2) and (0,4)(0, 4). With profit 3a+b3a + b they score 0, 15, 14 and 4.

python · try it yourself
from scipy.optimize import linprog
res = linprog(c=[-3, -1], A_ub=[[1, 2], [2, 1]], b_ub=[8, 10], bounds=[(0, None), (0, None)])
print([float(round(v, 4)) for v in res.x], float(round(-res.fun, 4)))
[5.0, 0.0] 15.0

Exercise 3: Minimize or maximize

Why does the example pass c=[-3, -5] to linprog?

Answer

linprog only minimizes, and maximizing ff is the same as minimizing −f-f.

Solution

The optimal point of max⁡f\max f is the optimal point of min⁡(−f)\min(-f), and the optimal values differ only in sign: max⁡f=−min⁡(−f)\max f = -\min(-f). That is why the program prints -res.fun.

Test yourself

A point that satisfies all the constraints of an optimization problem is called…

  1. optimal
  2. feasible
  3. stationary
  4. convex

How do you turn max⁡f(x)\max f(\mathbf{x}) into a minimization problem?

  1. Minimize 1/f(x)1/f(\mathbf{x})
  2. Minimize −f(x)-f(\mathbf{x})
  3. Minimize f(x)2f(\mathbf{x})^2
  4. Minimize f(−x)f(-\mathbf{x})

Which part of the course covers BFGS?

  1. Linear programming
  2. Unconstrained optimization
  3. Integer programming
  4. Derivative-free methods