SciPy optimize.minimize() Function



The scipy.optimize.minimize() function is used to minimize a scalar objective function. It supports various optimization algorithms which includes gradient-based methods such as BFGS, L-BFGS-B and derivative-free methods like Nelder-Mead.

This function can handle both unconstrained and constrained optimization problems. We can specify options like bounds, constraints or a custom gradient. The result includes the optimal solution, function value, success status and additional details about the optimization process.

This function is useful for solving various optimization problems such as engineering, machine learning and economics.

Syntax

Following is the syntax of the function scipy.optimize.minimize() to perform minimization in SciPy −

scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)

Parameters

Below are the parameters of the scipy.optimize.minimize() function −

  • fun (callable): The objective function to minimize.
  • x0 (array-like): Initial guess for the variables.
  • method (string, optional): Optimization method such as 'BFGS', 'Nelder-Mead', etc.
  • jac (callable, optional): Gradient of the objective function.
  • bounds (sequence, optional): Bounds for variables like [(0, 1), (-1, 1)]).
  • constraints (dict, optional): Constraints for the optimization such as {'type': 'eq', 'fun': constraint_function}.
  • tol (float, optional): Tolerance for stopping criteria.
  • callback (callable, optional): A function to monitor the optimization process.
  • options (dict, optional): Solver-specific options such as maximum iterations.

Return Value

The scipy.optimize.minimize() function returns an object with the following attributes −

  • x: The optimal solution.
  • fun: The function value at the optimum.
  • success: Boolean indicating if the optimization was successful.
  • message: A message describing the termination reason.
  • jac: The gradient at the optimum if provided.

Example of Basic Minimization

Following is an example that minimizes the quadratic function ( f(x) = x^2 + x + 2 ) using the scipy.optimize.minimize() function −

from scipy.optimize import minimize

# Objective function
def objective(x):
    return x**2 + x + 2

# Initial guess
x0 = [0]

# Minimize the function
result = minimize(objective, x0)

# Display results
print("Optimal solution:", result.x)
print("Function value at optimum:", result.fun)

Here is the output of the scipy.optimize.minimize() function for basic minimization −

Optimal solution: [-0.50000001]
Function value at optimum: 1.75

Minimization with Bounds

This example minimizes f(x) = x^2 + x + 2 ) within the bounds (0, 1) −

from scipy.optimize import minimize

# Objective function
def objective(x):
    return x**2 + x + 2

# Bounds
bounds = [(0, 1)]

# Initial guess
x0 = [0.5]

# Minimize the function with bounds
result = minimize(objective, x0, bounds=bounds)

# Display results
print("Optimal solution:", result.x)
print("Function value at optimum:", result.fun)

Following is the output of the scipy.optimize.minimize() function with bounds −

Optimal solution: [0.]
Function value at optimum: 2.0

Minimization with Constraints

This example minimizes f(x, y) = x^2 + y^2) with the constraint (x + y = 1) −

from scipy.optimize import minimize

# Objective function
def objective(x):
    return x[0]**2 + x[1]**2

# Constraint: x + y = 1
def constraint(x):
    return x[0] + x[1] - 1

# Constraints dictionary
constraints = {'type': 'eq', 'fun': constraint}

# Initial guess
x0 = [0.5, 0.5]

# Minimize the function with constraints
result = minimize(objective, x0, constraints=constraints)

# Display results
print("Optimal solution:", result.x)
print("Function value at optimum:", result.fun)

Here is the output of the scipy.optimize.minimize() function with constraints −

Optimal solution: [0.5 0.5]
Function value at optimum: 0.5
scipy_optimize.htm
Advertisements