diff --git a/Benchmark1.py b/Benchmark1.py new file mode 100644 index 0000000..6c03008 --- /dev/null +++ b/Benchmark1.py @@ -0,0 +1,65 @@ +# ============================================================================= +# Mahmoud and Igor HW #1 - EX. #1 - SP4E Course +# ============================================================================= + +# Import Packages +import numpy as np +import scipy +import scipy.optimize +import matplotlib +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +# Defined Functions +def objective(x): + return (2*x[0]**2+1.5*x[1]**2+x[0]*x[1]-x[0]-2*x[1]+6) +def objective1(x,y): + return 2*x**2+1.5*y**2+x*y-x-2*y+6 + + +global xmatrix +global counter +global zz +global xx +global yy + +initial=[2,1] # Initial Guesses +counter=0 +xmatrix=[[initial[0],initial[1]]] # The found parameters x,y, .. etc. per iteration + +xx=[initial[0]] +yy=[initial[1]] +zz=[objective1(initial[0],initial[1])] + +def getIterationSteps(x): + global counter + global xx + global yy + global zz + xmatrix.append([x[0],x[1]]) + zz.append(objective1(x[0],x[1])) + + counter+=1 + xx.append(x[0]) + yy.append(x[1]) + +ret=scipy.optimize.minimize(objective, initial, tol=1e-9, method='CG', callback=getIterationSteps) +print (ret) + +# Plot Results and Objective Function +X, Y = np.meshgrid(np.linspace(-3.0, 3.0, 30), np.linspace(-3.0, 3.0, 30)) +Z = objective1(X,Y) +fig = plt.figure() +ax = plt.axes(projection='3d') + +#Surface plot or wireframe plot ! +#ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none') +ax.plot_wireframe(X, Y, Z, color=[0.7,0.7,0.7]) +ax.set_title('Objective Function'); +ax.set_xlabel('X') +ax.set_ylabel('Y') +ax.set_zlabel('Z') +#Plotting iterations +np.transpose(zz) +ax.plot3D(xx,yy,zz, c='red', marker='o', markersize=15, linewidth=5) +plt.show() diff --git a/Exercise1.py b/Exercise1.py new file mode 100644 index 0000000..89037f4 --- /dev/null +++ b/Exercise1.py @@ -0,0 +1,65 @@ +# ============================================================================= +# Mahmoud and Igor HW #1 - EX. #1 - SP4E Course +# ============================================================================= + +# Import Packages +import numpy as np +import scipy +import scipy.optimize +import matplotlib +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +# Defined Functions +def objective(x): + return (2*x[0]**2+1.5*x[1]**2+x[0]*x[1]-x[0]-2*x[1]+6) +def objective1(x,y): + return 2*x**2+1.5*y**2+x*y-x-2*y+6 + + +global xmatrix +global counter +global zz +global xx +global yy + +initial=[3,3] # Initial Guesses +counter=0 +xmatrix=[[initial[0],initial[1]]] # The found parameters x,y, .. etc. per iteration + +xx=[initial[0]] +yy=[initial[1]] +zz=[objective1(initial[0],initial[1])] + +def getIterationSteps(x): + global counter + global xx + global yy + global zz + xmatrix.append([x[0],x[1]]) + zz.append(objective1(x[0],x[1])) + + counter+=1 + xx.append(x[0]) + yy.append(x[1]) + +ret=scipy.optimize.minimize(objective, initial, tol=1e-9, callback=getIterationSteps) +#print (ret) + +# Plot Results and Objective Function +X, Y = np.meshgrid(np.linspace(-3.0, 3.0, 30), np.linspace(-3.0, 3.0, 30)) +Z = objective1(X,Y) +fig = plt.figure() +ax = plt.axes(projection='3d') + +#Surface plot or wireframe plot ! +#ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none') +ax.plot_wireframe(X, Y, Z, color=[0.7,0.7,0.7]) +ax.set_title('Objective Function'); +ax.set_xlabel('X') +ax.set_ylabel('Y') +ax.set_zlabel('Z') +#Plotting iterations +np.transpose(zz) +ax.plot3D(xx,yy,zz, c='red', marker='o', markersize=15, linewidth=5) +plt.show()