Esempio n. 1
0
def plot3D(optimizer, xValues, yValues, zValues):
    '''

    :param optimizer: optimizer object returned in the application/definition of PSO
    :param xValues: numpy array : 2d (minValue, maxValue) axis
    :param yValues: numpy array : 2d (minValue, maxValue) axis
    :param zValues: numpy array : 2d (minValue, maxValue) axis
    :return: matplotlib object --> position particles 3d plot
    '''

    try:

        #Obtain a position-fitness matrix using the Mesher.compute_history_3d() method.
        positionHistory_3d = Mesher.compute_history_3d(optimizer.pos_history)

        d = Designer(limits=[xValues, yValues, zValues],
                     label=['x-axis', 'y-axis', 'z-axis'])

        plot3d = plot_surface(
            pos_history=positionHistory_3d,
            mesher=Mesher,
            designer=d,
            mark=(1, 1,
                  zValues[0]))  #BEST POSSIBLE POSITION MARK (* --> IN GRAPHIC)

        return plot3d

    except:
        raise
Esempio n. 2
0
def plot_show(func):

    if func == "Sphere":

        m = Mesher(func=fx.sphere, limits=[(-1, 1), (-1, 1)])
    elif func == "Ackley's":

        m = Mesher(func=fx.ackley, limits=[(-1.5, 1.5), (-1.5, 1.5)])

    d = Designer(limits=[(-1, 1), (-1, 1), (-0.1, 1)],
                 label=['x-axis', 'y-axis', 'z-axis'])

    pos_history_3d = m.compute_history_3d(
        optimizer.pos_history)  # preprocessing
    animation3d = plot_surface(pos_history=pos_history_3d,
                               mesher=m,
                               designer=d,
                               mark=(0, 0, 0))
    plt.show()
Esempio n. 3
0
def test_plot_surface_return_type(pos_history):
    """Tests if the animation function returns the expected type"""
    assert isinstance(plot_surface(pos_history), FuncAnimation)
Esempio n. 4
0
optimizer.optimize(fx.schaffer2, iters=100)
best_cost, best_pos = optimizer.optimize(fx.schaffer2, iters=100)

#history of particle positions for animation
hist = optimizer.pos_history

#This can plot the cost history, not needed for the assignment:
#plot_cost_history(optimizer.cost_history)

#Mesh for the plot:
m = Mesher(func=fx.schaffer2, limits=[(-1, 1), (-1, 1)])

#This can be used for designing the animation
#but the defaults are enough for this task
#d = Designer(limits=[(-1,1),(-1,1),(-0.1,1)],
#           label=["x-axis","y-axis","z-azis"])

#animation = plot_contour(pos_history=hist, mesher=m,mark=(0,0))
#animation.save("Plot.mp4", writer="imagemagick", fps=10)

#3D version for fun:
pos_his_3D = m.compute_history_3d(hist)
anim3D = plot_surface(pos_history=pos_his_3D, mesher=m, mark=(0, 0, 0))

#Makes the animation a HTML5 video for playing in Jupyter Notebooks!
#HTML(anim3D.to_html5_video())
#plt.rcParams['animation.html'] = 'html5'
#anim3D

plt.show()
Esempio n. 5
0
plt.show()

# Initialize mesher with sphere function
from pyswarms.utils.plotters.formatters import Mesher
m = Mesher(func=fx.sphere)

# Make animation
animation2d = plot_contour(pos_history=optimizer.pos_history,  # Use the cost_history we computed
                           mesher=m,                           # Customizations
                           mark=(0,0))                         # Mark minima

# Enables us to view it in a Jupyter notebook
animation2d.save('plot0.gif', writer='imagemagick', fps=10)
Image(url='plot0.gif')

# Obtain a position-fitness matrix using the Mesher.compute_history_3d()
# method. It requires a cost history obtainable from the optimizer class
pos_history_3d = m.compute_history_3d(optimizer.pos_history)

# Make a designer and set the x,y,z limits to (-1,1), (-1,1) and (-0.1,1) respectively
from pyswarms.utils.plotters.formatters import Designer
d = Designer(limits=[(-1,1), (-1,1), (-0.1,1)], label=['x-axis', 'y-axis', 'z-axis'])

# Make animation
animation3d = plot_surface(pos_history=pos_history_3d, # Use the cost_history we computed
                           mesher=m, designer=d,       # Customizations
                           mark=(0,0,0))               # Mark minima

# Enables us to view it in a Jupyter notebook
animation3d.save('plot1.gif', writer='imagemagick', fps=10)
Image(url='plot1.gif')
Esempio n. 6
0
##########################
# Animate
##########################

# Constants
m = Mesher(
    func=function,
    limits=limits[0:2])  # get the sphere function's mesh (for better plots)
d = Designer(limits=limits, label=['x-axis', 'y-axis',
                                   'z-axis'])  # Adjust figure limits

# Animate in 3D
pos_history_3d = m.compute_history_3d(optimizer.pos_history)  # preprocessing
animation3d = plot_surface(pos_history=pos_history_3d,
                           mesher=m,
                           designer=d,
                           mark=(0, 0, 0))
plt.show()

# # Animate swarm in 2D (contour plot)
# plot_contour(pos_history=optimizer.pos_history, mesher=m, designer=d, mark=(0,0))
# plt.show()

# # Plot the cost
# plot_cost_history(optimizer.cost_history)
# plt.show()

# def cosCos(inputs):
#     x,y = inputs
#     return np.cos(x)*np.cos(y)
# function = lambda x : returnFitness(x, cosCos)
Esempio n. 7
0
 def plot3d(self):
     # Prepare position history
     m = Mesher(func=self.model.getCumReturnsError)
     pos_history_3d = m.compute_history_3d(self.optimizer.pos_history)
     plot_surface(pos_history_3d)