Robots need to decide: where to move, how to move, how fast to go.
These decisions often boil down to solving optimization problems—minimizing a cost (e.g., energy, time, error) under physical or logical constraints.
Python’s SciPy.optimize module makes it easy to define and solve these problems with minimal overhead and maximal flexibility.
- Trajectory planning – minimize time or energy while avoiding obstacles
- Parameter tuning – auto-calibrate models and controllers
- Inverse kinematics – find joint angles that achieve a task
- System ID – fit parameters to observed robot data
- Control – MPC, LQR, and reinforcement learning all involve optimization
- Perception – SLAM, bundle adjustment, and point cloud registration are optimization problems under the hood
- Boyd & Vandenberghe – Convex Optimization
- Nocedal & Wright – Numerical Optimization
- SciPy Optimization Docs
- MIT 6.252 – Nonlinear Programming
- Stanford EE364a – Convex Optimization
- Khan Academy – Optimization with Constraints
| Task | Function |
|---|---|
| Unconstrained minimize | scipy.optimize.minimize |
| Least-squares fitting | scipy.optimize.least_squares |
| Constrained nonlinear optimization | minimize(..., constraints=...) |
| Bounds on variables | bounds= keyword |
| Global optimization | basinhopping, differential_evolution |
| Root finding | root, fsolve |
| Curve fitting | curve_fit |
pip install
python -m pip install scipyconda env
conda create -n optboot python=3.12 scipy
conda activate optbootMinimize the function f(x) = (x - 3)^2 + 5.
from scipy.optimize import minimize
f = lambda x: (x - 3)**2 + 5
res = minimize(f, x0=[0])
print("Minimum x* =", res.x, "f(x*) =", res.fun)Minimize f(x, y) = x² + 2y² + xy + x.
import numpy as np
from scipy.optimize import minimize
f = lambda v: v[0]**2 + 2*v[1]**2 + v[0]*v[1] + v[0]
res = minimize(f, x0=[1, 1])
print("opt =", res.x)Minimize f(x) = x² with bounds -1 ≤ x ≤ 2.
res = minimize(lambda x: x**2, x0=[0.5], bounds=[(-1, 2)])
print("x* =", res.x)Minimize f(x) = x[0]² + x[1]²
subject to constraint: x[0] + x[1] = 1.
cons = ({'type': 'eq', 'fun': lambda x: x[0] + x[1] - 1})
res = minimize(lambda x: x[0]**2 + x[1]**2, x0=[0.5, 0.5], constraints=cons)
print(res.x)Try changing to an inequality: 'type': 'ineq' for x[0] + x[1] ≤ 1.
Fit parameters a, b in y = a * exp(bx) to noisy data.
import numpy as np
from scipy.optimize import curve_fit
# synthetic data
x = np.linspace(0, 4, 50)
y = 2.5 * np.exp(1.3 * x) + np.random.normal(0, 1, size=x.shape)
# model
def model(x, a, b):
return a * np.exp(b * x)
popt, _ = curve_fit(model, x, y)
print("a =", popt[0], "b =", popt[1])Given end-effector target (x, y), find th1, th2 such that
x = l1*cos(th1) + l2*cos(th1+th2)
y = l1*sin(th1) + l2*sin(th1+th2)
import numpy as np
from scipy.optimize import minimize
l1, l2 = 1.0, 1.0
target = np.array([1.2, 0.8])
def objective(th):
x = l1*np.cos(th[0]) + l2*np.cos(th[0]+th[1])
y = l1*np.sin(th[0]) + l2*np.sin(th[0]+th[1])
err = np.linalg.norm([x, y] - target)
return err
res = minimize(objective, x0=[0, 0])
print("th1, th2 =", res.x)Minimize squared error from target over horizon:
import numpy as np
from scipy.optimize import minimize
N = 10
target = np.ones(N)
def cost(u):
return np.sum((u - target)**2)
bounds = [(0, 1)] * N
res = minimize(cost, x0=np.zeros(N), bounds=bounds)
print("u* =", res.x)This is a basic structure for model predictive control: optimize control input u to minimize cost over time.
- Always inspect
res.successandres.message– to check if the solver converged - Scale your variables – optimization performs better when variables are on similar scales
- Use gradients (Jacobian) if possible – speeds up and stabilizes convergence
- Visualize your objective function – surface/contour plots often reveal pathologies
- Test multiple solvers – try
'BFGS','SLSQP','trust-constr', etc. viamethod=keyword