Page MenuHomec4science

optimizer.py
No OneTemporary

File Metadata

Created
Fri, May 3, 05:26

optimizer.py

# Import libraries
import numpy as np
import scipy.optimize
### Global variables (only necessary for plotting of steps taken by optimizer)
# Dimension of problem, default value = 2 will be overwritten by main_minimize
DIM = 2
# Initial guess to minimizer, default value = (4,4) will be overwritten by main_minimize
STEPS = np.array([4, 4])
# This array will contain the steps of the minimizer on its way to the minimum
STEPS = STEPS.reshape(-1, DIM)
### This function fills the STEPS array, and is used as input to scipy.optimize.minimize
def my_callback(xk):
global STEPS
STEPS = np.vstack((STEPS, np.array(xk)))
#### This function returns the minimum value of the function func
# x: initial guess of the minimum
# tolerance: termination criteria for algortihm
# max_iterations: maximum number of iterations as termination criteria for algortihm
# extra_args: tuple containing matrix A and b to be used if not default A and b used
def find_minimum( func, x, tolerance, max_iterations, extra_args=() ):
result = scipy.optimize.minimize(func, x0 = x, args=extra_args, method='CG', tol=tolerance, options={ "maxiter" : max_iterations}, callback=my_callback)
return result.x

Event Timeline