Python ML Pages - 1 2 3 4 5 6
Download Python Project : Py-PSO-Circles.zip - (35.5 KB zip file) Download
Source Code of Python Project - Py-PSO-Circles :
Download this Source Code at python file: Py_PSO_Circles.py - (1.29 KB Python file) download
#1-Gradient Particle Swarm Optimization
#2-Hybrid Particle Swarm Optimization
#Implementing PSO with PySwarm,
#PySwarms is a python based tool that helps with swarm optimization.
#PySwarms is the best tool to integrate swarm optimization with basic optimization.
#pip install pyswarms
import numpy as np
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
import matplotlib.pyplot as plt
from pyswarms.utils.plotters.formatters import Designer
from pyswarms.utils.plotters.formatters import Mesher
from pyswarms.utils.plotters import plot_contour, plot_surface
# Set-up all the hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
# Call an instance of PSO
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)
# Perform the optimization
cost, pos = optimizer.optimize(fx.sphere, iters=50)
print("")
print ("optimizer.optimize(fx.sphere, iters=50) = ", cost, pos)
# Visualizing the function, Code for 2D plot - circles
m = Mesher(func=fx.sphere)
print ("Mesher(func=fx.sphere) = ", m)
animation = plot_contour(pos_history=optimizer.pos_history, mesher=m, mark=(0,0))
plt.figure(1)
plt.title('PSO-Particle Swarm Optimization, 2d Plot', fontsize = 10)
plt.show()
Output of Py-PSO-Circles project :
Download Python Project : Py-PSO-3D.zip - (14.7 KB zip file) Download
Source Code of Python Project - Py-PSO-3D :
Download this Source Code at python file: Py_PSO_3D.py - (2.95 KB Python file) download
import
from
# Define the Rastrigin function
def
n =
# Define the PSO algorithm
particles = np.random.uniform(-5.12, 5.12, (num_particles, dim))
velocities = np.zeros((num_particles, dim))
best_positions = np.copy(particles)
best_fitness = np.array([cost_func(p)
swarm_best_position = best_positions[np.argmin(best_fitness)]
swarm_best_fitness = np.min(best_fitness)
r1 = np.random.uniform(0, 1, (num_particles, dim))
r2 = np.random.uniform(0, 1, (num_particles, dim))
velocities = w * velocities + c1 * r1 * (best_positions - particles) + c2 * r2 * (swarm_best_position - particles)
particles += velocities
fitness_values = np.array([cost_func(p)
improved_indices = np.where(fitness_values < best_fitness)
best_positions[improved_indices] = particles[improved_indices]
best_fitness[improved_indices] = fitness_values[improved_indices]
swarm_best_position = particles[np.argmin(fitness_values)]
swarm_best_fitness = np.min(fitness_values)
# Define the dimensions of the problem
dim = 3
# Run the PSO algorithm on the Rastrigin function
solution, fitness = pso(rastrigin, dim=dim)
print
# Print the solution and fitness value
# Create a meshgrid for visualization
x = np.linspace(-5.12, 5.12, 100)
y = np.linspace(-5.12, 5.12, 100)
X, Y = np.meshgrid(x, y)
Z = rastrigin([X, Y])
# Create a 3D plot of the Rastrigin function
fig = plt.figure(1)
ax = fig.add_subplot(111, projection=
ax.plot_surface(X, Y, Z, cmap=
ax.set_xlabel(
ax.set_ylabel(
ax.set_zlabel(
plt.title(
# Plot the solution found by the PSO algorithm
ax.scatter(solution[0], solution[1], fitness, color=
Output of Py-PSO-3D project, Particle Swarm Optimization (PSO) to optimize the Rastrigin function: