diff --git a/Homework1/__pycache__/post_processing.cpython-37.pyc b/Homework1/__pycache__/post_processing.cpython-37.pyc new file mode 100644 index 0000000..bcf0011 Binary files /dev/null and b/Homework1/__pycache__/post_processing.cpython-37.pyc differ diff --git a/Homework1/optimizer.py b/Homework1/optimizer.py index 9316ea2..ec701b8 100644 --- a/Homework1/optimizer.py +++ b/Homework1/optimizer.py @@ -1,76 +1,53 @@ #!/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 -import matplotlib.pyplot as plt -from mpl_toolkits.mplot3d import Axes3D - -def objective(x, *args): #objective function to be minimized +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) + 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]) +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 +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) +print(X_int) # Post-processing -X_domain = np.linspace(-1.2*X0[0], 1.2*X0[0], 100) -Y_domain = np.linspace(-1.2*X0[0], 1.2*X0[0], 100) -xx, yy = np.meshgrid(X_domain, Y_domain) -S_post = np.array([objective(np.array([x , y]), A, B) - for x, y in zip(np.ravel(xx), np.ravel(yy))]) # Value of S for 3D plot (whole X-Y domain) -S_post = S_post.reshape(xx.shape) - -S_int = np.array([objective(np.array([x , y]), A, B) - for x, y in zip(x_int[:,0], x_int[:,1])]) # Value of S at intermediate minimization steps - - -fig = plt.figure() -ax = plt.axes(projection = '3d') -ax.plot3D(x_int[:,0], x_int[:,1], S_int, 'r--o', alpha = 1.0, zorder = 10) -surface = ax.plot_surface(xx, yy, S_post, cmap = 'Greys', edgecolor = 'none', alpha = 1.0, zorder = 0) -ax.set_xlabel('$x$', fontsize = 14) -ax.set_ylabel('$y$', fontsize = 14) -ax.set_zlabel('$S$', fontsize = 14) -ax.view_init(elev = 60.0, azim = 130.0) -ax.set_title('Minimization method: %s' % method, fontsize = 14, pad = 32) -#fig.colorbar(surface, shrink = 0.5, aspect = 5) -plt.show() +post_processing.plot_results(X_int, A, B, method) diff --git a/Homework1/post_processing.py b/Homework1/post_processing.py new file mode 100644 index 0000000..07695fc --- /dev/null +++ b/Homework1/post_processing.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Mon Oct 14 15:19:11 2019 +Authors: Omid Ashtari and Armand Sieber +Description: Scripts intended to display minimization proplems results +""" + +import numpy as np + +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +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 plot_results(X_int, A, B, method): + + # Pre-processing the data to be dispalyed + ########################################## + + # Set domain size and coordinates according to intermediate solution of minimization problem + x1 = np.max(np.abs(X_int[:, 0])) + x2 = np.max(np.abs(X_int[:, 1])) + X_domain = np.linspace(-1.5*x1, 1.5*x1, 100) + Y_domain = np.linspace(-1.5*x2, 1.5*x2, 100) + xx, yy = np.meshgrid(X_domain, Y_domain) + + # Compute S for the computational domain (S_post) and intermediate minimization solution (S_int) + S_post = np.array([objective(np.array([x , y]), A, B) + for x, y in zip(np.ravel(xx), np.ravel(yy))]) # Value of S for 3D plot (whole X-Y domain) + S_post = S_post.reshape(xx.shape) + + S_int = np.array([objective(np.array([x , y]), A, B) + for x, y in zip(X_int[:,0], X_int[:,1])]) # Value of S at intermediate minimization steps + + # Dispalying the results + ######################## + + fig = plt.figure() + ax = plt.axes(projection = '3d') + ax.plot3D(X_int[:,0], X_int[:,1], S_int, 'r--o', alpha = 1.0, zorder = 10) + surface = ax.plot_surface(xx, yy, S_post, cmap = 'Greys', edgecolor = 'none', alpha = 1.0, zorder = 0) # zorder 'best' solution found to plot S_int over S_post + # --> alternative using mayavi + ax.set_xlabel('$x$', fontsize = 14) + ax.set_ylabel('$y$', fontsize = 14) + ax.set_zlabel('$S$', fontsize = 14) + ax.view_init(elev = 60.0, azim = 130.0) + ax.set_title('Minimization method: %s' % method, fontsize = 14, pad = 32) + plt.show() + + return 0