I’m hoping it’s to read about Swarm Intelligence! I’m also hoping you’re interested to read about the interactive dashboard side of things too so we can play with it at the end.
Finding the “just right” Goldilocks Zone using Swarm Intelligence#
Say you’re building a house and you want to maximise the number of rooms you can fit in your plot of land, maybe saying that all rooms have to be a certain size or bigger. That’s the kind of thing that optimisation algorithms are useful for.
Optimisation methods like Particle Swarm Optimisation are used when you want to find the best/optimum for some system / problem. You could just try every possible input but that might take a while so smarter people than me have invented better ways.
Let’s build a dashboard in which you can control parameters of Particle Swarm Optimisation, click a target and see the little dots flock towards it. Like an interactive, 2D version of this plot on Wikipedia.
Genetic algorithm is based on genetic evolution where each generation there is survival-of-the-fittest-style well… death. In the case of Particle Swarm Optimisation, there is the same population throughout because we want them to remember where they were when they were at their fittest. Like looking back at yourself on your wedding day or after a health kick. Each particles position is a potential solution to your problem so they’re all trying to find the best position together.
In the case of Genetic Algorithm each member of the population was just a few numbers (their X and Y position), the parameters that you’re trying to optimise. In this case each particle will not just have a X and Y position, they also have a velocity. We also need a way to know how to improve the particles in our swarm…
### Closer (smaller distance) is better
We’ll need to find the fittest member of the population using euclidean distance / mean squared error (which particle is closest to the target).
#collapse-hidedefmean_squared_error(y_true,y_pred):return((y_true-y_pred)**2).mean(axis=0)target_x,target_y=0,0defproblem(soln):globaltarget_x#using globals so we can link this to the click event laterglobaltarget_yreturnmean_squared_error(soln,[target_x,target_y])defassess_fitness(individual,problem):"Determines the fitness of an individual using the given problem"returnproblem(individual)
Each member is going to keep track of their fittest position, this can help them if they explore a worse direction, or want to tell other particles (but we’ll get to that later). They also keep an ID so that we can colour them across iterations.
Here’s that in code (before we add any of the update logic).
For each particle, we want their position and velocity. We also convert their velocity into angle and magnitude for the little arrows in the visualisation.
defto_angle(vector):x=vector[0]y=vector[1]mag=np.sqrt(x**2+y**2)angle=(np.pi/2.)-np.arctan2(x/mag,y/mag)returnmag,angledefget_vectorfield_data(swarm):'''Returns (xs, ys, angles, mags, ids)'''xs,ys,angles,mags,ids=[],[],[],[],[]forparticleinswarm:xs.append(particle.position[0])ys.append(particle.position[1])mag,angle=to_angle(particle.velocity)mags.append(mag)angles.append(angle)ids.append(particle.id)returnxs,ys,angles,mags,idsvect_data=get_vectorfield_data(swarm)vectorfield=hv.VectorField(vect_data,vdims=['Angle','Magnitude','Index'])# [x, y, id] for all particlesparticles=[np.array([vect_data[0],vect_data[1],vect_data[4]])fori,particleinenumerate(swarm)]points=hv.Points(particles,vdims=['Index'])layout=vectorfield*pointslayout.opts(opts.VectorField(color='Index',cmap='tab20c',magnitude=dim('Magnitude').norm()*10,pivot='tail'),opts.Points(color='Index',cmap='tab20c',size=5))
Note: Here we initialised the particles with a velocity for visualisationg, we’ll initialise them with zero velocity when it comes to actually optimising.
Okay so we have a population of particles, each with a position, velocity and fittest position but how can we update this population to find our optimum spot.
Each particle could just move in the direction that they think the optimum spot is. But if they overshoot it or get lost, thankfully they remember their best position so they can use that a little bit too.
Seems pretty inefficient for a bunch of these particles to all be trying the same thing without sharing any information with each other. In PSO, they can get “fittest position” from some other members of the population when they’re updating (called the social component).
They choose a few other particles and say “hey I’m looking for this red marker, any chance you’ve seen it? “ and the other particles reply “No but here is where I was when I was closest to it.“. Thrilling conversations.
Note: Intesting side note, PSO was introduced by James Kennedy and Russell Eberhart in 1995 after they discovered its optimisation properties while trying to build a social simulator.
A quick way to get stuck with a bad solution to a complex problem is to only listen to one suggestion and following that. This is what happens in particle swarm optimisation when all particles communicate to all of the particles during their update step (called the global component).
Here’s the code for the Particle to update itself each iteration.
defupdate(self,fittest_informant,global_fittest,follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step):""" Updates the velocity and position of the particle using the PSO update algorithm"""self.position+=self.velocity*scale_update_stepcognitive=random.uniform(0,follow_personal_best)social=random.uniform(0,follow_social_best)glob=random.uniform(0,follow_global_best)self.velocity=(follow_current*self.velocity+cognitive*(self.fittest_position-self.position)+social*(fittest_informant.fittest_position-self.position)+glob*(global_fittest.fittest_position-self.position))current_fitness=self.assess_fitness()if(current_fitness<self.previous_fitnessandself.previous_fitnessisnotNone):self.fittest_position=self.positionself.previous_fitness=current_fitness
Note: We are using a variant of the PSO algorithm introduced in 1995, with a social component as well as global. Also, we sample uniformly from 0 and our given update parameter before updating each part of the equation.
There are various values used to determine how to update the current velocity (as described above).
follow_current is how much to use the particles current velocity.
cognitive is how much to use the particles personal best fittest position.
social is how much to use it’s the fittest position of a smaller subset of the population.
glob (global) is how much to use the fittest position of the fittest particle in the population.
These are applied to the difference between the particles current position and a “fit” other position (either it’s own fittest position or another particle’s fittest position).
Here is the Particle class with the update and assess_fitness methods added in.
#collapse-hideclassParticle:""" An Particle used in PSO. Attributes ---------- problem : function to minimise velocity : nparray The current velocity of the particle position : nparray The current position of the particle, used as the solution for the problem given id : int The unique id of the particle Public Methods ------- assess_fitness() Determines the fitness of the particle using the given problem update(fittest_informant, global_fittest, follow_current, follow_personal_best, follow_social_best, follow_global_best, scale_update_step) Updates the velocity and position of the particle using the PSO update algorithm """def__init__(self,problem,velocity,position,index):self.velocity=velocityself.position=positionself.fittest_position=positionself.problem=problemself.id=indexself.previous_fitness=1e7defassess_fitness(self):"""Determines the fitness of the particle using the given problem"""returnassess_fitness(self.position,self.problem)defupdate(self,fittest_informant,global_fittest,follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step):""" Updates the velocity and position of the particle using the PSO update algorithm"""self.position+=self.velocity*scale_update_stepcognitive=random.uniform(0,follow_personal_best)social=random.uniform(0,follow_social_best)glob=random.uniform(0,follow_global_best)self.velocity=(follow_current*self.velocity+cognitive*(self.fittest_position-self.position)+social*(fittest_informant.fittest_position-self.position)+glob*(global_fittest.fittest_position-self.position))current_fitness=self.assess_fitness()if(current_fitness<self.previous_fitness):self.fittest_position=self.positionself.previous_fitness=current_fitness
We use this find_current_best method to keep track of our current fittest Particle, and to find the best among a selected few “informant” Particles for the social component.
#collapse-showdeffind_current_best(swarm,problem):"""Evaluates a given swarm and returns the fittest particle based on their best previous position This can be sped up to only loop over swarm once, but because this is a tutorial, 3 lines is nicer. """fitnesses=[assess_fitness(x.fittest_position,problem)forxinswarm]best_value=min(fitnesses)best_index=fitnesses.index(best_value)returnswarm[best_index]
This is just a wrapper which updates all the particles and keeps track of the current fittest.
Note: One thing to note is that we randomly sample the swarm to get the “informants” for the social update in each particle. There are many different topologies that can be chosen for this part of the algorithm, but we’re keeping it simple here.
classPSO:""" An implementation of Particle Swarm Optimisation, pioneered by Kennedy, Eberhart and Shi. The swarm consists of Particles with 2 fixed length vectors; velocity and position. Position is initialised with a uniform distribution between 0 and 1. Velocity is initialised with zeros. Each particle has a given number of informants which are randomly chosen at each iteration. Attributes ---------- swarm_size : int The size of the swarm vector_length : int The dimensions of the problem, should be the same used when creating the problem object num_informants: int The number of informants used for social component in particle velocity update Public Methods ------- improve(follow_current, follow_personal_best, follow_social_best, follow_global_best, scale_update_step) Update each particle in the swarm and updates the global fitness update_swarm(follow_current, follow_personal_best, follow_social_best, follow_global_best, scale_update_step) Updates each particle, randomly choosing informants for each particle's update. update_global_fittest() Updates the `globale_fittest` variable to be the current fittest Particle in the swarm. """def__init__(self,problem,swarm_size,vector_length,num_informants=2):self.swarm_size=swarm_sizeself.num_informants=num_informantsself.problem=problemself.swarm=[Particle(self.problem,np.zeros(vector_length),np.random.rand(vector_length),i)fori,xinenumerate(range(swarm_size))]self.global_fittest=np.random.choice(self.swarm,1)[0]defupdate_swarm(self,follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step):"""Update each particle in the swarm"""forparticleinself.swarm:informants=np.random.choice(self.swarm,self.num_informants)ifparticlenotininformants:np.append(informants,particle)fittest_informant=find_current_best(informants,self.problem)particle.update(fittest_informant,self.global_fittest,follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step)defupdate_global_fittest(self):fittest=find_current_best(self.swarm,self.problem)global_fittest_fitness=self.global_fittest.assess_fitness()if(fittest.assess_fitness()<global_fittest_fitness):self.global_fittest=fittestdefimprove(self,follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step):"""Improves the population for one iteration."""self.update_swarm(follow_current,follow_personal_best,follow_social_best,follow_global_best,scale_update_step)self.update_global_fittest()size=25vector_length=2num_informants=2pso=PSO(problem,size,vector_length)
# Interaction
We’re using Panel (a library from Anaconda) for the sliders and buttons. Because there are a lot of settings for PSO, we’ll leave a escape hatch for people in the form of a reset_button which will set the sliders to their default.
default_pop_size=25default_time=3default_num_informants=6population_size_slider=pn.widgets.IntSlider(name='Population Size',start=10,end=50,value=default_pop_size)time_slider=pn.widgets.IntSlider(name='Time Evolving (s)',start=0,end=15,value=default_time)num_informants_slider=pn.widgets.IntSlider(name='Number of Informants',start=0,end=20,value=default_num_informants)default_current=0.7default_personal_best=2.0default_social_best=0.9default_global_best=0.0default_scale_update_step=0.7follow_current_slider=pn.widgets.FloatSlider(name='Follow Current',start=0.0,end=5,value=default_current)follow_personal_best_slider=pn.widgets.FloatSlider(name='Follow Personal Best',start=0,end=5,value=default_personal_best)follow_social_best_slider=pn.widgets.FloatSlider(name='Follow Social Best',start=0.0,end=5,value=default_social_best)follow_global_best_slider=pn.widgets.FloatSlider(name='Follow Global Best',start=0.0,end=1,value=default_global_best)scale_update_step_slider=pn.widgets.FloatSlider(name='Scale Update Step',start=0.0,end=1,value=0.7)reset_params_button=pn.widgets.Button(name='Reset Parameters',width=50)defreset_event(event):globaldefault_currentglobaldefault_personal_bestglobaldefault_social_bestglobaldefault_global_bestglobaldefault_scale_update_stepglobaldefault_pop_sizeglobaldefault_timeglobaldefault_num_informantsfollow_current_slider.value,follow_personal_best_slider.value=default_current,default_personal_bestfollow_social_best_slider.value,follow_global_best_slider.value=default_social_best,default_global_bestscale_update_step_slider.value,population_size_slider.value=default_scale_update_step,default_pop_sizetime_slider.value,num_informants_slider.value=default_time,default_num_informantsreset_params_button.on_click(reset_event)
For the “click to set target” interaction, we’ll use a HoloviewsDynamicMap. It sounds complicated but put simply, it links a stream with a callback function. In this case the stream we’re using is a hv.stream.SingleTap, which will trigger the tap_event callback function with the x and y position of the tap when a tap happens. A hv.Points object is returned which can be displayed later.
Now for the best part, animating the Particles. This time our callback will return our swarm visualised using hv.Points for the particle points, hv.VectorField for the velocity arrows, and hv.Points to circle the fittest particle.
We’re going to use a HoloviewsDynamicMap again. This time, our stream that we link to the callback is one with no parameters so we can trigger it with our buttons. run_button creates a new population and uses DynamicMap’s periodic method to keep updating it for a given period of time (set with a slider from above).
defupdate_dm():pso.improve(follow_current_slider.value,follow_personal_best_slider.value,follow_social_best_slider.value,follow_global_best_slider.value,scale_update_step_slider.value)vect_data=get_vectorfield_data(pso.swarm)vectorfield=hv.VectorField(vect_data,vdims=['Angle','Magnitude','Index'])particles=[np.array([vect_data[0],vect_data[1],vect_data[4]])fori,particleinenumerate(swarm)]scatter=hv.Points(particles,vdims=['Index'],group='Particles')fittest=hv.Points((pso.global_fittest.fittest_position[0],pso.global_fittest.fittest_position[1],1),label='Current Fittest')layout=vectorfield*scatter*fittestlayout.opts(opts.Points(color='b',fill_alpha=0.1,line_width=1,size=10),opts.VectorField(color='Index',cmap='tab20c',magnitude=dim('Magnitude').norm()*10,pivot='tail'),opts.Points('Particles',color='Index',cmap='tab20c',size=5,xlim=(0,1),ylim=(0,1)))returnlayoutvector_field=hv.DynamicMap(update_dm,streams=[Stream.define('Next')()])run_button=pn.widgets.Button(name='\u25b6 Begin Improving',width=50)defb(event):globalpsosize=population_size_slider.valuevector_length=2num_informants=num_informants_slider.valuepso=PSO(problem,size,vector_length,num_informants)vector_field.periodic(0.005,timeout=time_slider.value)run_button.on_click(b)
Watcher(inst=Button(name='▶ Begin Improving', width=50), cls=<class 'panel.widgets.button.Button'>, fn=<function b at 0x7fd552b0d940>, mode='args', onlychanged=False, parameter_names=('clicks',), what='value', queued=False, precedence=0)
We’ll also add a button which can step through the update process or reset the population. We do this by hooking up other buttons to the vector_field.streams DynamicMap and passing it to hv.streams.Stream.trigger.
instructions=pn.pane.Markdown('''# Particle Swarm Optimisation Dashboard ## Instructions: 1. **Click on the plot to place the target.** 2. Click '\u25b6 Begin Improving' button to begin improving for the time on the Time Evolving slider. 3. Experiment with the sliders ''')dashboard=pn.Column(instructions,pn.Row((vector_field*target_tap).opts(width=600,height=600),pn.Column(pn.Row(run_button,pn.Spacer(width=50),new_pop_button),next_generation_button,time_slider,num_informants_slider,population_size_slider,follow_current_slider,follow_personal_best_slider,follow_social_best_slider,follow_global_best_slider,scale_update_step_slider,reset_params_button)))
dashboard.servable()
Particle Swarm Optimisation Dashboard
Instructions:
Click on the plot to place the target.
Click ‘▶ Begin Improving’ button to begin improving for the time on the Time Evolving slider.
Particle Swarm Optimisation is a really intesting algorithm which was built while trying to build a simiplified model of social interactions. The original aim was to create an algorithm in which the particles would behave like flocking birds.
We’ve seen how each particle has a velocity and position, and the position represents a potential solution to your problem. For updating the velocities, each particle uses its current position, its own fittest position and the fittest positions of other particles.
We’ve also looked the HoloViz tools (Holoviews, Panel and Bokeh). Using these we build an interactive dashboard which shows all the particles updating!
Thanks for reading!
This web page was generated from a Jupyter notebook and not all
interactivity will work on this website.