# Particle swarm optimization for the `ackley` function


```python
# Import dependencies.
import sympy as sp
import numpy as np
import matplotlib as mpl
import ipywidgets as widgets
import modules.oiplib as oiplib
```


```python
# Configure plotting.
sp.interactive.init_printing(use_unicode=False, wrap_line=False, no_global=True)
%matplotlib notebook
```


```python
# Define symbolic variables of the ackley function.
#x, y = sp.symbols("x y")

# Define the function.
f, x, y = oiplib.ackley();
# display(f)
```


```python
size = 5
```


```python
# Define a plot range and plot the function with sympy: 
yMin, yMax, xMin, xMax = -size, size, -size, size
plot = sp.plotting.plot3d(f, (x, xMin, xMax), (y, yMin, yMax));
```


    <IPython.core.display.Javascript object>



<div id='b2dd0600-3d7b-4c09-8f10-386079f84f01'></div>



```python
swarm = oiplib.ParticleSwarm(oiplib.ackley_function, (xMin, yMin), (xMax, yMax))
```


```python
swarm.run()
```


```python
fig2 = mpl.pyplot.figure(2)
ax = fig2.add_subplot(111, projection='3d')

def plot_func(iteration):
    xp = []
    yp = []
    zp = []
    for particle in swarm.particles:
        xp.append(particle.coordinatesX[iteration])
        yp.append(particle.coordinatesY[iteration])
        zp.append(particle.coordinatesZ[iteration])
        
    span = 5
    stepSize = 0.1
        
    xa = np.arange(-span, span+stepSize, stepSize)
    ya = np.arange(-span, span+stepSize, stepSize)
    xa, ya = np.meshgrid(xa, ya)
    za = np.zeros(xa.shape)
    
    it = np.nditer(za, flags=['multi_index'], op_flags=['readwrite']) 
    while not it.finished:
        i, j = it.multi_index
        za[i, j] = swarm.func(xa[i, j], ya[i, j])
        it.iternext()
    
    ax.cla()
    
    ax.set_xbound(-size, size)
    ax.set_ybound(-size, size)
    ax.set_zbound(0, size*3)
    ax.set_autoscale_on(False)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    surf = ax.plot_surface(xa, ya, za, color=(0, 0, 0, 0.2), rcount=span*2+1, ccount=span*2+1)
    scatter = ax.scatter(xp, yp, zp, c=zp, cmap="RdYlGn_r")
    
widgets.interact(plot_func, iteration=widgets.IntSlider(value=0, min=0, max=len(swarm.particles[0].coordinatesZ)-1, step=1), continuous_update=False);
```


    <IPython.core.display.Javascript object>



<div id='9c190b71-1dd9-4439-8a3c-b484a6d06066'></div>



    interactive(children=(IntSlider(value=0, description='iteration', max=83), Output()), _dom_classes=('widget-in…



```python
fig3 = mpl.pyplot.figure(3, figsize=(9, 9/3))
ax1 = fig3.add_subplot(1, 3, 1, projection='3d')
ax2 = fig3.add_subplot(1, 3, 2, projection='3d')
ax3 = fig3.add_subplot(1, 3, 3, projection='3d')

def subplot_func(axes, iteration):
    xp = []
    yp = []
    zp = []
    for particle in swarm.particles:
        xp.append(particle.coordinatesX[iteration])
        yp.append(particle.coordinatesY[iteration])
        zp.append(particle.coordinatesZ[iteration])
        
    span = 5
    stepSize = 0.1
        
    xa = np.arange(-span, span+stepSize, stepSize)
    ya = np.arange(-span, span+stepSize, stepSize)
    xa, ya = np.meshgrid(xa, ya)
    za = np.zeros(xa.shape)
    
    it = np.nditer(za, flags=['multi_index'], op_flags=['readwrite']) 
    while not it.finished:
        i, j = it.multi_index
        za[i, j] = swarm.func(xa[i, j], ya[i, j])
        it.iternext()
    
    axes.cla()
    
    axes.set_xbound(-size, size)
    axes.set_ybound(-size, size)
    axes.set_zbound(0, size*3)
    axes.set_autoscale_on(False)
    axes.set_xlabel('X')
    axes.set_ylabel('Y')
    axes.set_zlabel('Z')
    
    surf = axes.plot_surface(xa, ya, za, color=(0, 0, 0, 0.2), rcount=span*2+1, ccount=span*2+1)
    scatter = axes.scatter(xp, yp, zp, c=zp, cmap="RdYlGn_r")
    
subplot_func(ax1, 0)
subplot_func(ax2, 10)
subplot_func(ax3, len(swarm.particles[0].coordinatesZ) - 1)

mpl.pyplot.tight_layout()
```


    <IPython.core.display.Javascript object>







```python

```

d5ed4b903059dccf2dce6605d9917903252d37f8
ipynb
Jupyter Notebook
src/ackley.ipynb
Clapfire/oip_image_processing_assignment_2
7370a659787a63c48df4923e5fccd0b0650d2199
MIT
src/ackley.ipynb
Clapfire/oip_image_processing_assignment_2
7370a659787a63c48df4923e5fccd0b0650d2199
MIT
2020-03-24T18:00:09.000Z
2021-08-23T20:35:48.000Z
src/ackley.ipynb
Clapfire/oip_image_processing_assignment_2
7370a659787a63c48df4923e5fccd0b0650d2199
MIT
Qwen/Qwen-72B
1. YES
2. YES