Optimal Solution of a Nonlinear Constrained Optimization Problem

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from scipy.optimize import minimize, NonlinearConstraint
import numpy as np

# Define the nonlinear objective function
def objective(vars):
x, y = vars
return (x - 2)**2 + (y - 1)**2

# Define the nonlinear constraint function
def constraint(vars):
x, y = vars
return x**2 + y**2 - 1 # This should be <= 0 to satisfy the constraint

# Create a nonlinear constraint object
nonlinear_constraint = NonlinearConstraint(constraint, -np.inf, 0)

# Initial guess for the variables
initial_guess = [0, 0]

# Solve the optimization problem
result = minimize(
objective,
initial_guess,
method='SLSQP',
constraints=[nonlinear_constraint]
)

# Display the results
if result.success:
print("Optimal solution found:")
print("x =", result.x[0])
print("y =", result.x[1])
print("Minimum value of objective function =", result.fun)
else:
print("Optimization failed:", result.message)

Explanation of Key Components

  1. Objective Function:

    • The function objective(vars) takes an array vars 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.
  2. 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.
  3. Nonlinear Constraint:

    • We use NonlinearConstraint to define the constraint, setting the bounds to -np.inf (no lower bound) and 0 (upper bound), which enforces $( x^2 + y^2 - 1 \leq 0 )$.
  4. Optimization Solver:

    • The minimize function from SciPy 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.
  5. Output:

    • If successful, result.x contains the optimal values for $( x )$ and $( y )$, and result.fun gives the minimum value of the objective function.
    • If the optimization fails, an error message is displayed.

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.