In scientific computing, optimization is important for finding optimal solutions to challenging problems. The “SciPy” library in Python provides powerful optimization functions, including the “minimize()” function. This “SciPy” function is a powerful tool for finding the minimum of a scalar function of one or more variables.
This article presents a complete guide on Python SciPy “minimize()” function of the “scipy.optimize” module using numerous examples.
What is “SciPy Minimize” in Python?
The “minimize()” function of the “SciPy” library is a widely-used tool for minimizing scalar functions with multiple variables and it provides a convenient interface for applying both constrained and unconstrained minimization algorithms. Its main purpose is to find the minimum values of one or more variables in a scalar function.
Syntax
This function takes the following vital parameters:
-
- The “fun” parameter corresponds to the objective function that has to be minimized.
- The “x0” parameter specifies the initial guess for the optimization variables.
- The “method” specifies the algorithm or method for optimization.
- The “bounds” parameter indicates the bounds for the optimization variables.
- The “constraints” parameter indicates the additional constraints for the optimization problem (optional).
Example 1: Minimizing a Scalar Function
In the below code, the “scipy.optimize.minimize()” function is utilized to minimize a scalar function:
def scalar_function(x):
return x**2 + 5*x + 6
result = scipy.optimize.minimize(scalar_function, x0=0)
print(result.x)
In the above code:
-
- The “optimize” module is imported.
- The function named “scalar_function()” is defined to take a single argument “x” and return the value of the quadratic function “x^2 + 5x + 6”.
- The “optimize.minimize()” function takes the user-defined function named “scalar_function()” as its first argument and “x0=0” (as a second argument) which specifies the initial guess for the value of “x” when performing the optimization.
- The retrieved value of this function is an object containing the optimization result.
- The optimized value of “x” is shown by accessing the “x” attribute of the result object.
Output
The given scalar function has been minimized successfully.
Example 2: Minimizing a Function Having More Than One Variable
The following code block utilizes the discussed function to minimize a function with multiple variables:
import scipy
def func(x):
return x[0]**2 + x[1]**2
x0 = numpy.array([1, 2])
res = scipy.optimize.minimize(func, x0, method='Nelder-Mead')
print(res.x)
In the above code snippet:
-
- Minimize the function named “func()”, which is a function with two variables.
- The “optimize.minimize()” function takes the defined function, x0, and method as its arguments, respectively.
- The “x0” array is an initial guess for the minimum, and the “method” parameter specifies the optimization algorithm to use. Here, the “Nelder-Mead” algorithm is utilized.
- The “minimize()” function retrieves an instance of the “OptimizeResult” class and the “x” attribute is used to calculate the minimum function value.
Output
The input function has been minimized with multiple variables appropriately.
Conclusion
The SciPy “minimize()” is a function in the SciPy library that can be used to minimize a function of one or more variables. The function takes the function to be minimized and the initial guess for the minimum as its arguments and minimizes the function accordingly. It uses a variety of optimization algorithms to find the minimum of the function. A comprehensive guide was presented on Python’s “scipy.optimize.minimize()” function using numerous examples.