diff --git a/hw1-conjugate-gradient/main.py b/hw1-conjugate-gradient/main.py deleted file mode 100644 index 818f5747..00000000 --- a/hw1-conjugate-gradient/main.py +++ /dev/null @@ -1,32 +0,0 @@ -### IMPORT LIBRARIES - -import numpy as np -import scipy as sp -import argparse - -#import quadratic_function -#import optimizer -#import conjugate_gradient - - -### DEFINE PARAMETERS / COEFFICIENTS - -A = np.array([[4.0, 0.0], [1.0, 3.0]]) -b = np.array([0.0, 1.0]) - - -### ARGPARSE EXAMPLE : TO DO - -parser = argparse.ArgumentParser(description='Process Minimization Schemes') -#parser.add_argument("A", help="Get the matrix A coefficients", -# type=string) - -parser.add_argument('A', metavar='N', type=float, nargs='+', - help='input matrix') -#parser.add_argument('b', metavar='--b', type=float, nargs='+', -# help='input vector') - -args = parser.parse_args() -print(args.A) -print(args.b) - diff --git a/hw1-conjugate-gradient/main_minimize.py b/hw1-conjugate-gradient/main_minimize.py index 1fd03c2f..9dda10c7 100644 --- a/hw1-conjugate-gradient/main_minimize.py +++ b/hw1-conjugate-gradient/main_minimize.py @@ -1,53 +1,49 @@ ### IMPORT LIBRARIES - import numpy as np import scipy as sp import argparse #import quadratic_function #import optimizer from conjugate_gradient import conjgrad ### DEFINE PARAMETERS / COEFFICIENTS - A = np.array([[4.0, 0.0], [1.0, 3.0]]) b = np.array([0.0, 1.0]).T x = np.array([4, 4]).T # initial guess max_iterations = 1000 tolerance = 0.001 -### ARGPARSE -### DOES : SET VIA INTERFACE the Coefficients and Optimization Method - +### DEFINE DICTIONARY TO SET THE OPTIMIZATION METHOD TO RUN FUNCTION_MAP = {'CG' : conjgrad, - 'minimizer' : min} + 'BFGS' : min} ## To Do : BFGS matching, CallBack, Plots Option Argspace + +### ARGPARSE +### DOES : SET(via interface) Matrix Coefficients + Optimization Method parser = argparse.ArgumentParser(description='Process Minimization Schemes') parser.add_argument('-i',action='append', nargs='+',help='Input both : -i matrix -i vector') -parser.add_argument('-method', choices=FUNCTION_MAP.keys()) -#parser.add_argument('--method', dest='minimize', action='store_const', -# const=min, default=conjgrad, -# help='minimize the system (default: use CG algorithm)') - +parser.add_argument('-method', choices=FUNCTION_MAP.keys(),help='Choose method of minimization (CG or BFGS)') args = parser.parse_args() A = np.array([float(k) for k in args.i[0]]) b = np.array([float(k) for k in args.i[1]]) -dim = int(np.sqrt(len(A))) # Get the problem dimension - +dim = len(b) # int(np.sqrt(len(A))) # Get dimension for reshape A = np.reshape(A,(dim,dim)) -b = np.reshape(b,(1,dim)) +b.reshape(dim, -1) -### PRINT SET UP - -print("Matrix A: ",A) +### PRINT INITIAL STATE +print("Matrix A: ",A.shape) print("Vector b: ",b) +print("x0: ",x) +print("tolerance: ",tolerance) +print("max_iterations: ",max_iterations) +### RUN THE OPTIMIZATION func = FUNCTION_MAP[args.method] -[results, k] = func(A, b, x, tolerance, max_iterations) - -#args.method(A, b, x, tolerance, max_iterations) +[result, k] = func(A, b, x, tolerance, max_iterations) -print("Result :",results," ", k) +### PRINT RESULTS +print("Results :",result," ", k)