Skip to content

Instantly share code, notes, and snippets.

@ThomRoman
Last active July 10, 2019 05:46
Show Gist options
  • Save ThomRoman/d0cf46f63ed015d1d04c7acf898ce62b to your computer and use it in GitHub Desktop.
Save ThomRoman/d0cf46f63ed015d1d04c7acf898ce62b to your computer and use it in GitHub Desktop.
Implementing the Particle Swarm Optimization
#!/usr/bin/python3
"""
* AUTHOR : https://gist.github.com/IranNeto/21542660d740ac02dfce2f6aeb11ebeb
https://github.com/IranNeto
* WEB : https://medium.com/analytics-vidhya/implementing-particle-swarm-optimization-pso-algorithm-in-python-9efc2eb179a6
"""
# https://code.sololearn.com/cvIleZW1gfC6/#py
import random
import numpy as np
W = 0.5
c1 = 0.8
c2 = 0.9
n_iterations = int(50)
target_error = float(1e-6)
n_particles = int(30)
class Particle():
def __init__(self):
self.position = np.array([(-1) ** (bool(random.getrandbits(1))) * random.random()*50, (-1)**(bool(random.getrandbits(1))) * random.random()*50])
self.pbest_position = self.position
self.pbest_value = float('inf')
self.velocity = np.array([0,0])
def __str__(self):
print("Estoy en ", self.position, " y el mejor es ", self.pbest_position)
def move(self):
self.position = self.position + self.velocity
class Space():
def __init__(self, target, target_error, n_particles):
self.target = target
self.target_error = target_error
self.n_particles = n_particles
self.particles = []
self.gbest_value = float('inf')
self.gbest_position = np.array([random.random()*50, random.random()*50])
def print_particles(self):
for particle in self.particles:
particle.__str__()
def fitness(self, particle):
return particle.position[0] ** 2 + particle.position[1] ** 2 + 1
def set_pbest(self):
for particle in self.particles:
fitness_cadidate = self.fitness(particle)
if(particle.pbest_value > fitness_cadidate):
particle.pbest_value = fitness_cadidate
particle.pbest_position = particle.position
def set_gbest(self):
for particle in self.particles:
best_fitness_cadidate = self.fitness(particle)
if(self.gbest_value > best_fitness_cadidate):
self.gbest_value = best_fitness_cadidate
self.gbest_position = particle.position
def move_particles(self):
for particle in self.particles:
global W
new_velocity = (W*particle.velocity) + (c1*random.random()) * (particle.pbest_position - particle.position) + \
(random.random()*c2) * (self.gbest_position - particle.position)
particle.velocity = new_velocity
particle.move()
search_space = Space(1, target_error, n_particles)
particles_vector = [Particle() for _ in range(search_space.n_particles)]
search_space.particles = particles_vector
search_space.print_particles()
iteration = 0
while(iteration < n_iterations):
search_space.set_pbest()
search_space.set_gbest()
if(abs(search_space.gbest_value - search_space.target) <= search_space.target_error):
break
search_space.move_particles()
iteration += 1
print("La mejor posicion es : ", search_space.gbest_position, " en n_iteraciones: ", iteration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment