collapse = TRUE,
  comment = "#>"

Call the library


To use the Particle or the swarm we need to define a fitness function and a range of value.

range_of_value <- list(c(1,300),c(1,300),c(1,300))
fitness_function <- function(values){

The Particle Class

This class is used to create a Particle for the Particle Swarm Optimisation.

method new() (initialize):

Used to create the Particle, take different parameters:

exemple <- Particle$new(values_ranges = range_of_value,
                           values = c(50,120,187),
                           fitness_function = fitness_function,
                           acceleration_coefficient = c(0.3,0.4),
                           inertia = 0.4)

method get_fitness()

Used to calculate the fitness of the Particle (with the fitness function)

exemple$get_fitness() # 50+120+187

method update()

Used to change the position of the Particle based on his personal best and Swarm best, it also changes the personal best if needed. It takes one param:

print(paste('best fitness before :',exemple$personal_best_fitness,sep = ' '))
exemple$update(swarm_best = c(200,300,300)) # the swarm best is just a random value here
print(paste('best fitness after :',exemple$personal_best_fitness,sep = ' '))

method update_personal_best_fitness()

This method is used to change the personal best values and fitness with the current values and fitness of the Particles.

exemple$update_personal_best_fitness() # do nothing because the update method of the previous chunck also call this method.

method print()

This method print the current values of the particle and the fitness.

# or :

The ParticleSwarm Class

This class is used to create the swarm and launch the PSO. As a user you just need to init the ParticleSwarm object with the $new method and launch the PSO with the $run method.

method new()

Used to create the ParticleSwarm object, take different parameters:

swarm_exemple <- ParticleSwarm$new(pop_size = 10,
                                    ranges_of_values = range_of_value,
                                    fitness_function = fitness_function,
                                    max_it = 10,
                                    values_names = list('a','b','c'),
                                    acceleration_coefficient_range = list(c(0,1),c(0,1)),
                                    inertia = 0.4)

method generate_pop()

The generate pop is the method used to create the population of particles. The values of the particles are randomly selected in the range of values, the same goes for the acceleration coefficient. There is no need to call this method because the run does it for us. It takes one parameter:

swarm_exemple$generate_pop(verbose = FALSE)

method move_the_swarm()

This method is used to move the particle in the swarm. it takes one argument:

The swarm's move are based on the following equation: V(t+1) = V(t) * i + c1 * r1 * (pb - x(t)) + c2 * r2 * (gb - x(t)) x(t+1) = x(t) + V(t+1)


swarm_exemple$move_the_swarm(verbose = FALSE)

method save_pop()

This method is used to save the current population in a csv file, the result is a data frame with the particle in row and the values in col, the last col is the fitness of the particle.


method plot_the_swarm_2D()

this method is used to plot the swarm if the problem used two values (if there is 3 values you can use plot3D).


method plot_the_swarm_3D()

this method is used to plot the swarm if the problem used three values. it take one param:


method run()

This is the main method of the call, it call all the other method to do the PSO (you just need to call this method) It takes two params:

swarm_exemple$run(verbose = FALSE,plot = FALSE,save_file = FALSE)

Try the particle.swarm.optimisation package in your browser

Any scripts or data that you put into this service are public.

particle.swarm.optimisation documentation built on May 21, 2021, 9:06 a.m.