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:
The vector 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:
- Calculus Essentials for derivatives, partial derivatives and gradients.
- Linear Algebra & Matrix Theory for matrices, eigenvalues and positive definiteness.
- Math for Machine Learning, whose continuous optimization chapter (Gradient Descent, Lagrange multipliers, Convex Optimization) is a quick first pass over part 1 and 3 of this course.
- Math for Deep Learning & LLMs, which covers the optimizers used for neural networks (momentum, Adam); this course points there instead of repeating them.
- NumPy for arrays. SciPy appears as the "production" check on our own code.
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?
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:
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 (, , ), 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: , , profit 15.
Solution
The corners of the feasible region are , , and . With profit they score 0, 15, 14 and 4.
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 is the same as minimizing .
Solution
The optimal point of is the optimal point of , and the optimal values differ only in sign: . That is why the program prints -res.fun.
Test yourself
A point that satisfies all the constraints of an optimization problem is called…
- optimal
- feasible
- stationary
- convex
How do you turn into a minimization problem?
- Minimize
- Minimize
- Minimize
- Minimize
Which part of the course covers BFGS?
- Linear programming
- Unconstrained optimization
- Integer programming
- Derivative-free methods