Optimization Problems with SciPy
$SciPy$ provides powerful optimization routines in the scipy.optimize module to solve various optimization problems.
These include finding the minimum of a function, solving equations, and linear programming.
One commonly used method is constrained optimization, where the goal is to minimize or maximize a function subject to constraints.
Example Problem: Constrained Optimization (Minimizing a Nonlinear Function)
Let’s consider an example of a constrained optimization problem, where we minimize a nonlinear function subject to some constraints.
Problem
Minimize the following objective function:
$$
f(x, y) = x^2 + y^2
$$
subject to the constraints:
- $( x + y = 1 )$ (Equality constraint)
- $( x \geq 0 )$ and $( y \geq 0 )$ (Inequality constraints)
This is a simple quadratic function, and the constraints limit the feasible region to part of the first quadrant where the variables are non-negative and sum to $1$.
Approach
We will use $SciPy$’s minimize function from the optimize module, specifying the constraints and bounds.
Code Implementation
1 | import numpy as np |
Explanation
Objective Function:
- The function we want to minimize is $( f(x, y) = x^2 + y^2 )$, which is a quadratic function.
This is represented in Python asobjective(x), wherex[0]is $( x )$ andx[1]is $( y )$.
- The function we want to minimize is $( f(x, y) = x^2 + y^2 )$, which is a quadratic function.
Constraints:
- The equality constraint is $( x + y = 1 )$, which is enforced by
constraint_eq(x). This function must return $0$ for the constraint to be satisfied (i.e., $( x + y - 1 = 0 )$).
- The equality constraint is $( x + y = 1 )$, which is enforced by
Bounds:
- The variables $( x )$ and $( y )$ must be non-negative, represented by the bounds
[(0, None), (0, None)].
- The variables $( x )$ and $( y )$ must be non-negative, represented by the bounds
Initial Guess:
- The solver needs an initial guess for the variables.
We use $( x_0 = [0.5, 0.5] )$, which is a reasonable starting point for the algorithm.
- The solver needs an initial guess for the variables.
Optimization Method:
- We use the Sequential Least Squares Programming (SLSQP) algorithm, which is appropriate for constrained optimization problems.
Result:
- The result includes the optimal values for $( x )$ and $( y )$ that minimize the objective function while satisfying the constraints.
Output
1 | Optimal solution: [0.5 0.5] |
- Optimal solution: The solver will provide the optimal values for $( x )$ and $( y )$, which should satisfy both the constraint $( x + y = 1 )$ and the non-negativity conditions $( x \geq 0 )$ and $( y \geq 0 )$.
- Objective function value: The value of $( f(x, y) = x^2 + y^2 )$ at the optimal solution.
For this specific problem, the optimal solution is expected to be $( x = 0.5 )$ and $( y = 0.5 )$, with an objective function value of:
$$
f(0.5, 0.5) = 0.5^2 + 0.5^2 = 0.5
$$
Applications
This type of constrained optimization is widely used in areas such as:
- Resource allocation: Optimizing the allocation of limited resources (e.g., time, money, materials).
- Engineering design: Minimizing energy consumption, costs, or weight while ensuring the design meets all performance constraints.
- Economics: Finding optimal production quantities subject to cost or budget constraints.







