Description Active bindings Methods Examples
Particle Swarm, used to launch the Particle Swarm Optimisation, The PSO is used to maximise the fitness.
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
new()Create a new ParticleSwarm object.
ParticleSwarm$new( pop_size, values_names, fitness_function, max_it, acceleration_coefficient_range, inertia, ranges_of_values )
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)
A new ParticleSwarm object.
# 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()Make the Particle Swarm Optimisation
ParticleSwarm$run( verbose = TRUE, plot = TRUE, save_file = FALSE, dir_name = "PSO_pop" )
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
self
# 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)
generate_pop()create the population of the swarm (this method is automatically called by the run method)
ParticleSwarm$generate_pop(verbose = TRUE)
verboseprint the advancement or not
self
move_the_swarm()The method used to change the location of each particle (this method is automatically called by the run method)
ParticleSwarm$move_the_swarm(verbose)
verboseprint or not the advancement
self
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)
ParticleSwarm$save_pop(nb_it, dir_name)
nb_itnumber of the iteration, used to create the name of the csv file
dir_nameName of the directory
self
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)
ParticleSwarm$plot_the_swarm_2D(nb_it, save_file)
nb_itnumber of the iteration used to save the plot as a png
save_filesave the plot as a file
self
plot_the_swarm_3D()method used to plot a 3D plot
ParticleSwarm$plot_the_swarm_3D(nb_it, save_file)
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
self
print()Print the current result of the population
ParticleSwarm$print()
clone()The objects of this class are cloneable with this method.
ParticleSwarm$clone(deep = FALSE)
deepWhether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.