Choosing a Method and a Solver
The hardest part of applied optimization is usually not the algorithm but recognising what kind of problem you have. Once you know the class, the choice of method and software follows. This lesson collects the decision rules of the whole course into one guide.
Step 1: classify the problem
Ask these questions in order:
- Are some variables discrete? Yes: mixed-integer (MILP/MIQP) or constraint programming. Try hard to keep the continuous part linear or convex quadratic.
- Are objective and constraints linear? Yes: LP.
- Is it convex? Quadratic with and linear constraints: QP. Norms, quadratic constraints: SOCP. Matrix inequalities: SDP. Otherwise general convex. Check with the composition rules of Operations That Preserve Convexity.
- Smooth and unconstrained (or only bounds)? Gradient-based local methods.
- No derivatives, or noisy/black-box? Derivative-free.
- A huge finite sum? Stochastic methods.
Step 2: pick the method
| Problem class | First choice | Alternatives |
|---|---|---|
| LP | dual simplex or interior point (HiGHS) | barrier + crossover for huge LPs |
| convex QP | interior point (Clarabel, MOSEK); OSQP for embedded/warm starts | active set (qpOASES) |
| SOCP / SDP | interior point (Clarabel, MOSEK, ECOS) | SCS (first order, large scale) |
| MILP | branch and cut (HiGHS, SCIP, Gurobi, CPLEX) | CP-SAT for scheduling-like structure |
| smooth unconstrained, small/medium | BFGS or Newton (trust region) | |
| smooth unconstrained, large | L-BFGS | nonlinear CG, Newton-CG |
| nonlinear least squares | Levenberg-Marquardt / trust-region reflective | Gauss-Newton |
| smooth + simple nonsmooth (L1, bounds) | proximal gradient / FISTA, coordinate descent | ADMM |
| smooth nonlinear constraints | SQP (SLSQP, SNOPT), interior point (IPOPT) | augmented Lagrangian |
| huge data, ML training | SGD / Adam; SAGA for convex finite sums | L-BFGS full batch |
| black box, few variables | Nelder-Mead, COBYLA, BOBYQA | pattern search |
| black box, expensive | Bayesian optimization | surrogate models |
| black box, multimodal | CMA-ES, differential evolution, annealing | multistart local search |
Step 3: let a modelling layer do the conversion
Writing matrices by hand is error-prone. A modelling layer turns a readable model into the solver's standard form:
| Tool | For |
|---|---|
| CVXPY | convex problems (LP, QP, SOCP, SDP), checks convexity |
| Pyomo, JuMP, PuLP, OR-Tools linear solver | LP/MILP and general nonlinear models |
| OR-Tools CP-SAT | scheduling, routing, combinatorial problems |
scipy.optimize |
quick work: minimize, least_squares, linprog, milp, differential_evolution |
A worked decision
"Minimize delivery cost for 50 trucks, each either used or not, with route capacities and fuel costs that rise linearly with load": binary variables (truck used) → MILP; costs and capacities are linear → MILP with linear constraints; use HiGHS through PuLP or OR-Tools, set a 1% gap tolerance for speed.
import numpy as np
from scipy.optimize import milp, LinearConstraint, Bounds
# 6 trucks (fixed cost if used, capacity), 10 tons to move; per-ton cost depends on the truck
fixed = np.array([50.0, 40.0, 70.0, 30.0, 60.0, 45.0]); cap = np.array([4.0, 3.0, 6.0, 2.0, 5.0, 3.0])
per_ton = np.array([5.0, 6.0, 3.0, 8.0, 4.0, 6.0])
# variables: used_j (binary), load_j (continuous)
c = np.r_[fixed, per_ton]
A = np.vstack([np.r_[np.zeros(6), np.ones(6)], # total load = 10
np.hstack([-np.diag(cap), np.eye(6)])]) # load_j <= cap_j * used_j
res = milp(c, constraints=LinearConstraint(A, np.r_[10, -np.inf * np.ones(6)], np.r_[10, np.zeros(6)]),
integrality=np.r_[np.ones(6), np.zeros(6)], bounds=Bounds(0, np.r_[np.ones(6), cap]),
options={"mip_rel_gap": 0.0})
used = np.nonzero(res.x[:6] > 0.5)[0]
print("trucks used:", used.tolist(), " loads:", np.round(res.x[6:][used], 2).tolist(), " cost:", round(res.fun, 2))trucks used: [0, 2] loads: [4.0, 6.0] cost: 158.0
Sanity checks before trusting any answer
- Check the status (optimal, infeasible, unbounded, iteration limit) and the reported gap or KKT residual.
- Evaluate constraints yourself at the returned point.
- For local methods on non-convex problems, run several starting points.
- Scale your problem: variables and constraints of very different magnitudes cause numerical trouble in every solver.
- Compare against a simple baseline (a heuristic, a relaxation bound, brute force on a tiny instance).
In software
- The NEOS server lets you try many commercial and academic solvers on a model without installing them; Hans Mittelmann's benchmarks compare solver speed per problem class.
Exercises
Exercise 1: Classify
Classify each: (a) minimize subject to ; (b) choose 5 of 30 sensors to maximize coverage, coverage linear in the choices; (c) fit 3 parameters of an ODE model to data.
Answer
(a) convex QP (or SOCP); (b) binary integer program; (c) nonlinear least squares.
Solution
(a) Minimizing the norm or its square is equivalent and the square is a convex quadratic. (b) "Choose 5 of 30" needs binaries with . (c) Residuals depend nonlinearly on the parameters through the ODE solution: Levenberg-Marquardt, with finite-difference or sensitivity Jacobians.
Exercise 2: Wrong tool
Someone trains a 3-variable logistic regression with a genetic algorithm. What would you suggest?
Answer
Newton's method or L-BFGS.
Solution
The problem is smooth and convex with cheap exact gradients and Hessians. A GA ignores all that structure and gives no optimality guarantee.
Exercise 3: Check the truck model (code)
Verify the truck solution: total load, capacity constraints and cost computed by hand.
Answer
Every check passes, and the cost is 158 as the solver reported: fixed costs 50 + 70 plus 4 tons at 5 and 6 tons at 3.
Solution
import numpy as np
fixed = np.array([50.0, 40.0, 70.0, 30.0, 60.0, 45.0]); cap = np.array([4.0, 3.0, 6.0, 2.0, 5.0, 3.0])
per_ton = np.array([5.0, 6.0, 3.0, 8.0, 4.0, 6.0])
used = [0, 2]; load = np.zeros(6); load[0], load[2] = 4.0, 6.0
print("load:", load.sum(), " within capacity:", bool(np.all(load <= cap)), " cost:", fixed[used].sum() + per_ton @ load)load: 10.0 within capacity: True cost: 158.0
Test yourself
A problem with binary choices and linear costs and constraints is…
- a MILP
- an SDP
- a smooth unconstrained problem
- a convex QP
For a smooth unconstrained problem with a million variables, a good first choice is…
- Newton's method with a dense Hessian
- L-BFGS
- Nelder-Mead
- branch and bound
Why use a modelling layer like CVXPY or Pyomo?
- It converts a readable model to the solver's standard form and catches modelling errors
- It is always faster than the solver
- It replaces the solver
- It guarantees integer solutions