Page MenuHomec4science

optimizer.py
No OneTemporary

File Metadata

Created
Sat, May 11, 13:01

optimizer.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 10 10:19:11 2019
Authors: Omid Ashtari and Armand Sieber
Description: Scripts intended to minimize a quadratic function using scipy.optimize built-in function.
The function to minimize is defined as S(X)= X'AX - X'B, with X = [x, y], X' the transform of X
A is a 2x2 matrix and B 2X1 vector
"""
import numpy as np
import sys
from scipy.optimize import minimize
import post_processing
def objective(X, *args): #objective function to be minimized
A = args[0]
B = args[1].T
S = np.dot(X.T, np.dot(A, X)) - np.dot(X.T, B)
return S
def reporter(X_r):
global X_int
X_int = np.vstack([X_int, X_r])
return 0
# Import minimization method
if len(sys.argv) < 2:
print('Solver method missing. Default method implemented --> CG')
method = 'CG'
else:
method = sys.argv[1]
available_methods = ('Nelder-Mead', 'Powell', 'CG', 'BFGS', 'L-BFGS-B', 'TNC', 'SLSQP')
if method in available_methods: # test availibility of user inputs
print('Minimzation performed with the %s solver' % method)
else:
sys.exit('Minimization method missing or not available!')
# Parameters of the function to minimize
A = np.array([[4, 0], [1, 3]])
B = np.array([0, 1])
# Minimization process
X0 = np.array([3, 2]) #initial guess
X_int = X0 #stores solution at each step
solution = minimize(objective, X0, args = (A, B), method = method, callback = reporter,options = {'maxiter':100})
print(solution)
print(X_int)
# Post-processing
post_processing.plot_results(X_int, A, B, method)

Event Timeline