ParticleSwarm: Swarm

Description Active bindings Methods Examples

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_size

number of individu in the swarm. (numeric)

values_names

list of names for each value (character)

fitness_function

function used to test the Particle and find his fitness. (function)

max_it

Maximum number of iteration for the PSO. (numeric)

acceleration_coefficient_range

a vector of four values (min and max for c1 and c2) (numeric)

inertia

The inertia for the particle (the influence of the previous velocity on the next velocity). (numeric)

ranges_of_values

range 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
verbose

print the different step (iteration and individu)

plot

plot the result of each iteration (only for 2D or 3D problem)

save_file

save the population of each Iteration in a file and save the plot if plot=TRUE

dir_name

name 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
verbose

print 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
verbose

print 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_it

number of the iteration, used to create the name of the csv file

dir_name

Name 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_it

number of the iteration used to save the plot as a png

save_file

save 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_it

number 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_file

save 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
deep

Whether to make a deep clone.

Examples

 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)

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