Created
September 24, 2017 14:36
-
-
Save YazzyYaz/ef7f15528047dda233400970d04de7a5 to your computer and use it in GitHub Desktop.
PSO - No Gym
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
import copy | |
class Particle(object): | |
def __init__(self, position, velocity): | |
self.position = copy.copy(position) | |
self.velocity = copy.copy(velocity) | |
self.best_position = copy.copy(position) | |
def update_time_step(self, time_unit): | |
self.position += copy.copy(self.velocity) * time_unit | |
def update_velocity(self, velocity): | |
self.velocity = copy.copy(velocity) | |
def update_best_position(self, position): | |
self.best_position = copy.copy(position) | |
def get_position(self): | |
return copy.copy(self.position) | |
def get_best_position(self): | |
return copy.copy(self.best_position) | |
def get_velocity(self): | |
return copy.copy(self.velocity) | |
class Swarm(object): | |
def __init__(self, **swarm_args): | |
self.swarm_best_position = 0 | |
self.swarm_best_value = 0 | |
self.particles = [] | |
self.animation_position = [] | |
self.particle_count = swarm_args['particle_count'] | |
self.alpha = swarm_args['alpha'] | |
self.inertia = swarm_args['inertia'] | |
self.cognitive = swarm_args['cognitive'] | |
self.social = swarm_args['social'] | |
self.objective_function = swarm_args['objective_function'] | |
random_position_generator = swarm_args['random_position_generator'] | |
random_velocity_generator = swarm_args['random_velocity_generator'] | |
for i in range(particle_count): | |
particle = Particle(random_position_generator(), random_velocity_generator()) | |
self.particles.append(particle) | |
particle.best_value = self.objective_function(particle.get_position()) | |
if ((self.swarm_best_value != None) and (self.swarm_best_value < self.objective_function(particle.get_position()))): | |
self.swarm_best_position = particle.get_position() | |
def epoch(self, time_unit): | |
swarm_position = np.zeros(shape = (0, 2)) | |
for particle in self.particles: | |
particle.update_time_step(time_unit) | |
particle.update_velocity((self.inertia * particle.get_velocity()) | |
+ (self.cognitive * np.random.uniform(0, self.alpha) * (self.swarm_best_position - particle.get_position())) | |
+ (self.social * np.random.uniform(0, self.alpha) * (particle.get_best_position() - particle.get_position()))) | |
if ( self.objective_function(particle.get_position()) > self.objective_function(particle.get_best_position()) ): | |
particle.update_best_position(particle.get_position()) | |
if ( self.objective_function(particle.get_best_position()) > self.swarm_best_value ): | |
self.swarm_best_position = particle.get_best_position() | |
self.swarm_best_value = self.objective_function(particle.get_best_position()) | |
swarm_position = np.vstack([swarm_position, np.array(particle.get_position())]) | |
self.animation_position.append(swarm_position) | |
def update_plot(self, i): | |
self.scat.set_offsets([np.array(self.animation_position[i])[:,0], np.array(self.animation_position[i])[:,1]]) | |
return | |
def finish(self): | |
self.fig = plt.figure() | |
plt.axis([-2, 2, -2, 2]) | |
self.scat = plt.scatter(np.array(self.animation_position[0])[:,0], np.array(self.animation_position[0])[:,1]) | |
ani = animation.FuncAnimation(self.fig, self.update_plot, frames=range(len(self.animation_position)), interval=100) | |
plt.show() | |
particle_count = 5 | |
inertia = 0.79 | |
cognitive = 1.49445 | |
social = 1.49445 | |
time_unit = 1 | |
alpha = 0.2 | |
def random_position_generator(): | |
return np.random.uniform(-2, 2, 2) | |
def random_velocity_generator(): | |
return np.random.uniform(-0.02, 0.02, 2) | |
def objective_function(position): | |
return -np.sqrt(np.mean(position**2)) | |
swarm_args = { | |
'particle_count': particle_count, | |
'inertia': inertia, | |
'cognitive': cognitive, | |
'social': social, | |
'alpha': alpha, | |
'objective_function': objective_function, | |
'random_position_generator': random_position_generator, | |
'random_velocity_generator': random_velocity_generator | |
} | |
MySwarm = Swarm(**swarm_args) | |
for index in range(100): | |
MySwarm.epoch(time_unit) | |
MySwarm.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment