knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(rgl)
set.seed(42) library(particle.swarm.optimisation)
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){ return(values[[1]]+values[[2]]+values[[3]]) }
This class is used to create a Particle for the Particle Swarm Optimisation.
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) print(exemple)
Used to calculate the fitness of the Particle (with the fitness function)
exemple$get_fitness() # 50+120+187 print(exemple)
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(exemple) print(paste('best fitness after :',exemple$personal_best_fitness,sep = ' '))
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.
This method print the current values of the particle and the fitness.
print(exemple) # or : exemple$print()
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.
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)
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) print(swarm_exemple)
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)
Where:
swarm_exemple$move_the_swarm(verbose = FALSE) print(swarm_exemple)
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.
swarm_exemple$save_pop()
this method is used to plot the swarm if the problem used two values (if there is 3 values you can use plot3D).
swarm_exemple$plot_the_swarm_2D(nb_it=0,save_file=FALSE)
this method is used to plot the swarm if the problem used three values. it take one param:
swarm_exemple$plot_the_swarm_3D(nb_it=0,save_file=FALSE)
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) print(swarm_exemple)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.