SciPy optimize.fmin() Function



scipy.optimize.fmin() is a function in SciPy's optimization module used for unconstrained optimization of a scalar function. It employs the Nelder-Mead simplex algorithm which is a heuristic search method suitable for optimizing non-smooth or noisy functions.

This function is particularly useful when derivative information is not available or the function is non-differentiable. It minimizes the objective function by iteratively improving a simplex structure in the parameter space.

Syntax

The syntax for the scipy.optimize.fmin() function is as follows −

scipy.optimize.fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None, disp=True, retall=False, callback=None)

Parameters

Here are the parameters of the scipy.optimize.fmin() function −

  • func: The objective function to be minimized.
  • x0: Initial guess for the parameters.
  • args (optional): Additional arguments to pass to the objective function.
  • xtol (optional): Relative error in the solution for termination.
  • ftol (optional): Relative error in the function value for termination.
  • maxiter (optional): Maximum number of iterations to perform.
  • maxfun (optional): Maximum number of function evaluations to perform.
  • disp (optional): Set to True to print convergence messages (default is True).
  • retall (optional): Set to True to return a list of solutions at each iteration.
  • callback (optional): A function called after each iteration.

Return Value

The scipy.optimize.fmin() function returns the following −

  • xmin: The parameters that minimize the objective function.
  • fopt: The minimum value of the objective function.
  • iter: The number of iterations performed.
  • funcalls: The number of function calls made.
  • warnflag: Indicates if the algorithm exited without convergence.

Minimizing a Quadratic Function

Minimizing a quadratic function is a common optimization task that demonstrates the use of optimization techniques effectively. Following is an example of using scipy.optimize.fmin() to minimize a simple quadratic function −

from scipy.optimize import fmin

# Define the objective function
def objective_function(x):
    return (x - 3)**2

# Initial guess
x0 = 0

# Perform the optimization
result = fmin(objective_function, x0)

# Display the result
print("Optimal value of x:", result)

Here is the output of the scipy.optimize.fmin() function which is used for minimizing the quadratic function −

Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 28
         Function evaluations: 56
Optimal value of x: [3.]

Optimizing a Nonlinear Function

Optimizing a nonlinear function can demonstrate the versatility of scipy.optimize.fmin() for solving problems where the objective function has non-linear behavior. Here is the example of optimizing a Nonlinear Function with the help of scipy.optimize.fmin() function −

from scipy.optimize import fmin
import numpy as np

# Define the objective function
def objective_function(x):
    return np.sin(x) + (x - 2)**2

# Initial guess
x0 = 0

# Perform the optimization
result = fmin(objective_function, x0)

# Display the result
print("Optimal value of x:", result)

Below is the output of the optimization process applied on a Nonlinear Function −

Optimization terminated successfully.
         Current function value: 0.833970
         Iterations: 27
         Function evaluations: 54
Optimal value of x: [2.352125]

Optimizing a Nonlinear Multi-Modal Function

Optimizing a nonlinear multi-modal function is a great way to test the effectiveness of optimization algorithms such as scipy.optimize.fmin() especially when the function has multiple local minima and the goal is to find the global minimum. Here's an example of optimizing such a function using fmin() function −

from scipy.optimize import fmin
import numpy as np

# Define the nonlinear multi-modal objective function
def objective_function(x):
    return np.sin(3 * np.pi * x) + x**2

# Initial guess
x0 = 0.5

# Perform the optimization
result = fmin(objective_function, x0)

# Display the result
print("Optimal value of x:", result)
print("Minimum value of the function:", objective_function(result))

Here is the output function scipy.optimize.fmin() which is used to optimize the nonlinear Multi-Modal Function −

Optimization terminated successfully.
         Current function value: -0.755510
         Iterations: 9
         Function evaluations: 18
Optimal value of x: [0.48896484]
Minimum value of the function: [-0.75550985]
scipy_optimize.htm
Advertisements