Page MenuHomec4science

optimizer.py
No OneTemporary

File Metadata

Created
Sat, Jun 8, 21:05

optimizer.py

import scipy.optimize
import numpy as np
def optimizer(f, method, x0, toll):
"""
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.
toll : 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, 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': 10^-9})
minimizer = resultINFO.x
return minimizer, resultINFO, np.asarray(points)

Event Timeline