| ParticleSwarm {particle.swarm.optimisation} | R Documentation |
Swarm
Description
Particle Swarm, used to launch the Particle Swarm Optimisation, The PSO is used to maximise the fitness.
Active bindings
pop_size(numeric) number of particles in the swarm
ranges_of_values(list) range for each value for the particle
values_names(list) list of names for each value (optionnal)
pop(list) list of particle in the swarm
fitness_function(function) fitness function used to find the fitness of the particle
list_fitness(list) list of fitness of the particles
max_it(numeric) maximum number of iteration
acceleration_coefficient_range(list) coefficient c1 and c2 for the particles
swarm_best_fitness(numeric) best fitness of the swarm
swarm_best_values(numeric) values of the particle with the best fitness
inertia(numeric) inertia of the particles
Methods
Public methods
Method new()
Create a new ParticleSwarm object.
Usage
ParticleSwarm$new( pop_size, values_names, fitness_function, max_it, acceleration_coefficient_range, inertia, ranges_of_values )
Arguments
pop_sizenumber of individu in the swarm. (numeric)
values_nameslist of names for each value (character)
fitness_functionfunction used to test the Particle and find his fitness. (function)
max_itMaximum number of iteration for the PSO. (numeric)
acceleration_coefficient_rangea vector of four values (min and max for c1 and c2) (numeric)
inertiaThe inertia for the particle (the influence of the previous velocity on the next velocity). (numeric)
ranges_of_valuesrange for each value of the particle (min and max). (List)
Returns
A new ParticleSwarm object.
Examples
# Create a ParticleSwarm object
swarm <- ParticleSwarm$new(pop_size=20,
values_names=c('a','b'),
max_it=20,
fitness_function = function(values){return(values[1]+values[2])},
acceleration_coefficient=list(c(0.5,1),c(0.5,1)),
inertia=0.5,
ranges_of_values=list(c(-100,100),c(-100,100)))
Method run()
Make the Particle Swarm Optimisation
Usage
ParticleSwarm$run( verbose = TRUE, plot = TRUE, save_file = FALSE, dir_name = "PSO_pop" )
Arguments
verboseprint the different step (iteration and individu)
plotplot the result of each iteration (only for 2D or 3D problem)
save_filesave the population of each Iteration in a file and save the plot if plot=TRUE
dir_namename of the directory, default value is PSO_pop
Returns
self
Examples
# Create a ParticleSwarm object
swarm <- ParticleSwarm$new(pop_size=20,
values_names=c('a','b'),
max_it=20,
fitness_function = function(values){return(values[1]+values[2])},
acceleration_coefficient=list(c(0.5,1),c(0.5,1)),
inertia=0.5,
ranges_of_values=list(c(-100,100),c(-100,100)))
# run the PSO
swarm$run(verbose = FALSE,
plot = FALSE,
save_file = FALSE)
# return the best result:
print(swarm$swarm_best_values)
Method generate_pop()
create the population of the swarm (this method is automatically called by the run method)
Usage
ParticleSwarm$generate_pop(verbose = TRUE)
Arguments
verboseprint the advancement or not
Returns
self
Method move_the_swarm()
The method used to change the location of each particle (this method is automatically called by the run method)
Usage
ParticleSwarm$move_the_swarm(verbose)
Arguments
verboseprint or not the advancement
Returns
self
Method save_pop()
The method used to save the values and fitness of the population in a CSV file (this method is automatically called by the run method if you have chosen to save the result)
Usage
ParticleSwarm$save_pop(nb_it, dir_name)
Arguments
nb_itnumber of the iteration, used to create the name of the csv file
dir_nameName of the directory
Returns
self
Method plot_the_swarm_2D()
method used to plot a 2D plot (this method is automatically called by the run method if you have chosen to plot the swarm)
Usage
ParticleSwarm$plot_the_swarm_2D(nb_it, save_file)
Arguments
nb_itnumber of the iteration used to save the plot as a png
save_filesave the plot as a file
Returns
self
Method plot_the_swarm_3D()
method used to plot a 3D plot
Usage
ParticleSwarm$plot_the_swarm_3D(nb_it, save_file)
Arguments
nb_itnumber of the iteration used to save the plot as a png (this method is automatically called by the run method if you have chosen to plot the swarm)
save_filesave the plot as a file
Returns
self
Method print()
Print the current result of the population
Usage
ParticleSwarm$print()
Method clone()
The objects of this class are cloneable with this method.
Usage
ParticleSwarm$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# In this example we use the PSO to solve the following equation:
# a * 5 + b * 25 + 10 = 15
fitness_function <- function(values){
a <- values[1]
b <- values[2]
particule_result <- a*5 + b*25 + 10
difference <- 15 - particule_result
fitness <- 1 - abs(difference)
return(fitness)
}
values_ranges <- list(c(-10^3,10^3),c(-10^3,10^3))
swarm <- ParticleSwarm$new(pop_size = 200,
values_names = list("a","b"),
fitness_function = fitness_function,
max_it = 75,
acceleration_coefficient_range = list(c(0,1),c(0,1)),
inertia = 0.5,
ranges_of_values = values_ranges)
swarm$run(plot = FALSE,verbose = FALSE,save_file = FALSE)
# the solution is :
swarm$swarm_best_values
swarm$swarm_best_values[[1]]*5 + swarm$swarm_best_values[[2]] *25 + 10
## ------------------------------------------------
## Method `ParticleSwarm$new`
## ------------------------------------------------
# Create a ParticleSwarm object
swarm <- ParticleSwarm$new(pop_size=20,
values_names=c('a','b'),
max_it=20,
fitness_function = function(values){return(values[1]+values[2])},
acceleration_coefficient=list(c(0.5,1),c(0.5,1)),
inertia=0.5,
ranges_of_values=list(c(-100,100),c(-100,100)))
## ------------------------------------------------
## Method `ParticleSwarm$run`
## ------------------------------------------------
# Create a ParticleSwarm object
swarm <- ParticleSwarm$new(pop_size=20,
values_names=c('a','b'),
max_it=20,
fitness_function = function(values){return(values[1]+values[2])},
acceleration_coefficient=list(c(0.5,1),c(0.5,1)),
inertia=0.5,
ranges_of_values=list(c(-100,100),c(-100,100)))
# run the PSO
swarm$run(verbose = FALSE,
plot = FALSE,
save_file = FALSE)
# return the best result:
print(swarm$swarm_best_values)