Page MenuHomec4science

optimizer.py
No OneTemporary

File Metadata

Created
Tue, Nov 12, 06:14

optimizer.py

import scipy.optimize
import numpy as np
def optimizer(f, method, x0, tol):
"""
Find the minimizer of a scalar function of 2 variables.
Parameters
----------
f : scalar function of a vector of 2 variables.
method : strig
e.g. "CG" for Conjigate gradient, "BFGS" for quasi-Newton method of Broyden,
Fletcher, Goldfarb, and Shanno
x0 : ndarray
array of 2 elements with the initial guess of the minimizer.
tol : float
exit tollerance for the method e.g. 10^-8
Returns
-------
minimizer : ndarray
The solution of the optimization
resultINFO: OptimizeResult object
Important attributes are:
x: ndarray - The solution of the optimization.
success: bool - Whether or not the optimizer exited successfully.
message: str - Description of the cause of the termination.
nit: int - Number of iterations performed by the optimizer.
points : numpy ndarray
2-D array of shape (number of iterations + 1, 2) containing the successive approximations
for the solution obtained with the iterations
"""
points = [[x0[0], x0[1]]]
# Define function to store iteration points when calling scipy.optimize.minimize
def store_iterations(x):
points.append([x[0], x[1]])
# Compute optimizer and store iterations by using callback
resultINFO = scipy.optimize.minimize(f, x0,
method=method,
callback=store_iterations,
options={'gtol': tol})
minimizer = resultINFO.x
return minimizer, resultINFO, np.asarray(points)

Event Timeline