```python
#Exercise 3:

! pip install sympy
! pip install numpy 
! pip install scipy
! pip install matplotlib
! pip install random
! pip install pyswarm
import matplotlib.pyplot as plt
from scipy.optimize import minimize
import numpy as np
import time
import random as r
import sympy as sp
from sympy import symbols
from pyswarm import pso
```

    Requirement already satisfied: sympy in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (1.6.2)
    Requirement already satisfied: mpmath>=0.19 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from sympy) (1.1.0)
    Requirement already satisfied: numpy in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (1.19.2)
    Requirement already satisfied: scipy in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (1.5.2)
    Requirement already satisfied: numpy>=1.14.5 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from scipy) (1.19.2)
    Requirement already satisfied: matplotlib in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (3.3.2)
    Requirement already satisfied: kiwisolver>=1.0.1 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (1.2.0)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (2.4.7)
    Requirement already satisfied: python-dateutil>=2.1 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (2.8.1)
    Requirement already satisfied: pillow>=6.2.0 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (8.0.0)
    Requirement already satisfied: numpy>=1.15 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (1.19.2)
    Requirement already satisfied: certifi>=2020.06.20 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (2020.6.20)
    Requirement already satisfied: cycler>=0.10 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from matplotlib) (0.10.0)
    Requirement already satisfied: six>=1.5 in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from python-dateutil>=2.1->matplotlib) (1.15.0)
    [31mERROR: Could not find a version that satisfies the requirement random (from versions: none)[0m
    [31mERROR: No matching distribution found for random[0m
    Collecting pyswarm
      Downloading pyswarm-0.6.tar.gz (4.3 kB)
    Requirement already satisfied: numpy in /opt/miniconda3/envs/conda_env/lib/python3.7/site-packages (from pyswarm) (1.19.2)
    Building wheels for collected packages: pyswarm
      Building wheel for pyswarm (setup.py) ... [?25ldone
    [?25h  Created wheel for pyswarm: filename=pyswarm-0.6-py3-none-any.whl size=4480 sha256=25566f7fc3138ead70b748b0615324e749f6b5cf493f08d389b01be651cfbf3f
      Stored in directory: /home/scorpion_178/.cache/pip/wheels/ac/69/65/926e9c51b9fa99757cb43e8f6b74c5e6bb8b41a038b35c2db1
    Successfully built pyswarm
    Installing collected packages: pyswarm
    Successfully installed pyswarm-0.6



```python
#Case of Rosenbrock

save_rosenbrock=[]
def rosenbrock(X):  #cost function
    global save_rosenbrock
    global new
    Rosen=100*(X[1]-X[0]**2)**2 + (1-X[0])**2
    if not save_rosenbrock :
        save_rosenbrock.append(Rosen)
    if Rosen< save_rosenbrock[-1]:
        save_rosenbrock.append(Rosen)
    return Rosen

#Rosenbrock function is not constrained in this case but has bounds.

rose_ub=5*np.ones(2)
rose_lb=-rose_ub


#calling pso  and plotting results 
#Setting parameters for plot

fig=plt.figure(figsize=(13,5))
ax1=fig.add_subplot(121)
ax1.set_title("Rosenbrock Optimization with PSO",fontweight="bold")
ax1.set_ylabel("values")
ax1.set_xlabel("Iterations")


for i in range(10):
    save_rosenbrock=[]
    new=[]
    start_time=time.time()
    xrosenbrock, frosenbrock = pso(rosenbrock, rose_lb, rose_ub, f_ieqcons=None,swarmsize=40,maxiter=100)
    print("Run",i +1)
    print(f"--- time used for function call in run {i+1} is %s seconds ---" % (time.time() - start_time))
    print("The optimal  point is:",xrosenbrock)
    print("The final Solution",frosenbrock)
    print("\n")
    
    #plot of Rosenbrock Optimisation  
    ax1.plot(save_rosenbrock,marker='', label=f"Run {i+1}")  
    ax1.plot(0,save_rosenbrock[0],marker='.', color=plt.gca().lines[-1].get_color())
    ax1.plot(len(save_rosenbrock)-1,save_rosenbrock[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax1.grid(color='lightgray', alpha=0.7)
    
    #plot of Rosenbrock Optimisation with log function 
    ax2=fig.add_subplot(122)
    ax2.plot(save_rosenbrock,marker='', label=f"Run {i+1}")
    ax2.plot(0,save_rosenbrock[0],marker='.', color=plt.gca().lines[-1].get_color())
    ax2.plot(len(save_rosenbrock)-1,save_rosenbrock[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax2.grid(color='lightgray', alpha=0.5)
    ax2.set_title("Rosenbrock Optimization with PSO(logged)",fontweight="bold")
    ax2.set_ylabel(" logged Function values")
    ax2.set_xlabel("iterations")
    ax2.set_yscale('log')
    ax2.legend(loc="lower right",bbox_to_anchor=(1.35,-0.0),borderaxespad=0., ncol=1, title="Runs")
    

```


```python

```


```python

```


```python
#Case of Eggcrate

save_eggcrate=[]
def eggcrate(X:float):
    global save_eggcrate
    egg=X[0]**2 + X[1]**2 + 25*(np.sin(X[0])**2 + np.sin(X[1])**2)   
    if not save_eggcrate :
        save_eggcrate.append(egg)
    if egg< save_eggcrate[-1]:
        save_eggcrate.append(egg)
    return egg
#Eggcrate function is not constrained in this case but has bounds.
egg_ub=2*np.pi*(np.ones(2))
egg_lb=-egg_ub

#calling pso  and plotting results 
#Setting parameters for plot
fig=plt.figure(figsize=(13,5))



for i in range(10):
    save_eggcrate=[]
    start_time=time.time()
    xeggcrate,feggcrate=pso(eggcrate, egg_lb, egg_ub, f_ieqcons=None,swarmsize=30,minfunc=1e-8)
    print("Run",i +1)
    print(f"--- time used for function call in run {i+1} is %s seconds ---" % (time.time() - start_time))
    print("The optimal  point is:",xeggcrate)
    print("The final solution",feggcrate)
    print("\n")
    ax1=fig.add_subplot(121)
    ax1.plot(save_eggcrate,marker='', label=f"Run {i+1}")
    ax1.plot(0,save_eggcrate[0],marker='', color=plt.gca().lines[-1].get_color())
    ax1.plot(len(save_eggcrate)-1,save_eggcrate[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax1.grid(color='lightgray', alpha=0.5)
    ax1.set_title("Eggcrate Optimization with PSO",fontweight="bold")
    ax1.set_ylabel("values")
    ax1.set_xlabel("iterations")
    
   #plot of Eggcrate Optimisation with log function  
    ax2=fig.add_subplot(122)
    ax2.plot(save_eggcrate,marker='', label=f"Run {i+1}")
    ax2.plot(0,save_eggcrate[0],marker='.', color=plt.gca().lines[-1].get_color())
    ax2.plot(len(save_eggcrate)-1,save_eggcrate[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax2.grid(color='lightgray', alpha=0.5)
    ax2.set_title("Eggcrate Optimization with PSO(log)",fontweight="bold")
    ax2.set_ylabel("values")
    ax2.set_xlabel("iterations")
    ax2.set_yscale('log')
    ax2.legend(loc="lower right",bbox_to_anchor=(1.40,-0.0),borderaxespad=0., ncol=1, title="Runs")
    

```


```python

```


```python

```


```python
#Case of Golinski

save_golinski=[]
def Golinski(X):
    Golin=0.7854*X[0]*X[1]**2*(3.3333*X[2]**2+14.9334*X[2]-43.0934)-1.5079*X[0]*(X[5]**2+X[6]**2)+7.477*(X[5]**3+X[6]**3)+0.7854*(X[3]*X[5]**2+X[4]*X[6]**2)
    global save_golinski
    if not save_golinski:
        save_golinski.append(Golin)
    if (Golin<save_golinski[-1] and np.all(np.array([cons1(X),cons2(X),cons3(X),cons4(X),cons5(X),cons6(X),cons7(X), cons8(X),cons9(X),cons10(X),cons11(X)]))>=0):
        save_golinski.append(Golin)
    if save_golinski[-1]<2994.355: #this will avoid recording values below optimal value found by PSO
        del save_golinski[-1]
    return Golin

#the following functions cons1 to cons11 are the constraints
def cons1(X):
    return 1-(27/(X[0]*X[1]**2*X[2]))
def cons2(X):
    return 1-(397.5/(X[0]*X[1]**2*X[2]**2))
def cons3(X):
    return 1-(1.93*X[3]**3/(X[1]*X[2]*X[5]**4))
def cons4(X):
    return 1-(1.93*X[4]**3/(X[1]*X[2]*X[6]**4))
def cons5(X):
    return 1-(np.sqrt((745*X[3]/(X[1]*X[2]))**2+16.9*10**6)/(110.0*X[5]**3))
def cons6(X):
    return 1-(np.sqrt((745*X[4]/(X[1]*X[2]))**2+157.5*10**6)/(85.0*X[6]**3))
def cons7(X):
    return 1-(X[1]*X[2]/40)
def cons8(X):
    return 1-(5*X[1]/X[0])
def cons9(X):
    return 1-(X[0]/(12*X[1]))
def cons10(X):
    return 1-((1.5*X[5]+1.9)/X[3])
def cons11(X):
    return 1-((1.1*X[6]+1.9)/X[4])
cons=({'type':'ineq',  'fun':  c1},
      {'type':'ineq',  'fun':  c2},
      {'type':'ineq',  'fun':  c3},
      {'type':'ineq',  'fun':  c4},
      {'type':'ineq',  'fun':  c5},
      {'type':'ineq',  'fun':  c6},
      {'type':'ineq',  'fun':  c7},
      {'type':'ineq',  'fun':  c8},
      {'type':'ineq',  'fun':  c9},
      {'type':'ineq',  'fun':  c10},
      {'type':'ineq',  'fun':  c11})

#the bounds for pso algorithm

golinski_ub=[3.6,0.8,28.0,8.3,8.3,3.9,5.9]
golinski_lb=[2.6,0.7,17.0,7.3,7.3,2.9,5.0]
fig=plt.figure(figsize=(16,7))
for i in range(10):
    save_golinski=[]
    print("Run",i +1)
    start_time=time.time()
    xgolinski,fgolinski=pso(Golinski,golinski_lb,golinski_ub,ieqcons=[c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11],swarmsize=450,omega=0.6, phip=1.52,phig=1.52,maxiter=200,debug=False)
    print(f"--- time used for function call in run {i+1} is %s seconds ---" % (time.time() - start_time))
    print("The optimal  point is:",xgolinski)
    print(fgolinski)
    ax1=fig.add_subplot(121)
    ax1.plot(save_golinski,marker='', label=f"Run No{i+1}")
    ax1.plot(0,save_golinski[0],marker='', color=plt.gca().lines[-1].get_color())
    ax1.plot(len(save_golinski)-1,save_golinski[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax1.grid(color='lightgray', alpha=0.7)
    ax1.set_xlabel("Iterations")
    ax1.set_ylabel("Function Values")
    ax1.set_title("Golinski Function Optimisation with PSO",fontweight="bold")
    ax1.legend(loc="lower right",bbox_to_anchor=(1.5,-0.0),borderaxespad=0., ncol=1)
    
    
    ax2=fig.add_subplot(122)
    ax2.plot(save_golinski,marker='', label=f"Run {i+1}")
    ax2.plot(0,save_golinski[0],marker='.', color=plt.gca().lines[-1].get_color())
    ax2.plot(len(save_golinski)-1,save_golinski[-1],marker='.',color=plt.gca().lines[-1].get_color())
    ax2.grid(color='lightgray', alpha=0.5)
    ax2.set_title("Golinski Optimization with PSO(log)",fontweight="bold")
    ax2.set_ylabel(" logged Function values")
    ax2.set_xlabel("swarm evolution")
    ax2.set_yscale('log')
    ax2.legend(loc="lower right",bbox_to_anchor=(1.40,-0.0),borderaxespad=0., ncol=1, title="Runs")


```


```python

```

6355f1cfd3607ec2bd2084388083427b42c422fa
ipynb
Jupyter Notebook
model/Exercise 3_Metaheuristics.ipynb
IsmaelMekene/Metaheuristics--Stochastic-Optimization
737be6c9421fbd3be6497153dbab3c63db958c49
MIT
model/Exercise 3_Metaheuristics.ipynb
IsmaelMekene/Metaheuristics--Stochastic-Optimization
737be6c9421fbd3be6497153dbab3c63db958c49
MIT
model/Exercise 3_Metaheuristics.ipynb
IsmaelMekene/Metaheuristics--Stochastic-Optimization
737be6c9421fbd3be6497153dbab3c63db958c49
MIT
Qwen/Qwen-72B
1. YES
2. YES