To solve an optimization problem with a nonlinear objective function or nonlinear constraints in $Python$, we can use libraries like SciPy
’s optimize
module.
SciPy
provides various functions to handle nonlinear optimization problems, including minimize
with methods like 'SLSQP'
(Sequential Least Squares Programming), which can handle nonlinear constraints.
In this example, we’ll set up a nonlinear objective function with a nonlinear constraint and solve it using SciPy
.
Problem Setup
Suppose we want to minimize the following objective function:
$$
f(x, y) = (x - 2)^2 + (y - 1)^2
$$
This objective function represents the distance between $(x, y)$ and the point $(2, 1)$, which we aim to minimize.
We’ll also define a nonlinear constraint:
$$
g(x, y) = x^2 + y^2 - 1 \leq 0
$$
This constraint forces $(x, y)$ to lie within the unit circle centered at the origin.
Code Implementation
The following $Python$ code demonstrates how to define and solve this nonlinear optimization problem using SciPy
.
1 | from scipy.optimize import minimize, NonlinearConstraint |
Explanation of Key Components
Objective Function:
- The function
objective(vars)
takes an arrayvars
with variables $( x )$ and $( y )$ and returns the value of the objective function $(x - 2)^2 + (y - 1)^2$.
This is the function we want to minimize.
- The function
Constraint Function:
- The function
constraint(vars)
defines the constraint $( x^2 + y^2 - 1 )$, which must be less than or equal to zero for a valid solution.
In other words, the solution must lie within or on the unit circle.
- The function
Nonlinear Constraint:
- We use
NonlinearConstraint
to define the constraint, setting the bounds to-np.inf
(no lower bound) and0
(upper bound), which enforces $( x^2 + y^2 - 1 \leq 0 )$.
- We use
Optimization Solver:
- The
minimize
function fromSciPy
is used to find the minimum of the objective function. - We specify
method='SLSQP'
to handle the nonlinear constraint, and we pass the constraint as a list. - The initial guess is $([0, 0])$, which serves as a starting point for the algorithm.
- The
Output:
- If successful,
result.x
contains the optimal values for $( x )$ and $( y )$, andresult.fun
gives the minimum value of the objective function. - If the optimization fails, an error message is displayed.
- If successful,
Result
Optimal solution found: x = 0.8944272288069395 y = 0.4472135198861174 Minimum value of objective function = 1.5278640450001992
The optimization was successful, yielding an optimal solution for $( x )$ and $( y )$ within the specified constraints. The optimal values are:
- $( x = 0.894 )$
- $( y = 0.447 )$
These values minimize the objective function, achieving a minimum value of approximately $1.528$.
This result satisfies the constraint that $( x^2 + y^2 \leq 1 )$, meaning the solution lies within the unit circle as required.
Summary
This example demonstrates how to solve a constrained nonlinear optimization problem in $Python$ using SciPy
.
The SLSQP
algorithm in SciPy
‘s minimize
function is well-suited for handling nonlinear constraints and is effective for finding solutions when either the objective function or constraints are nonlinear.