Description Active bindings Methods Examples
Class for the Particles used in the Particle Swarm Optimisation, It is call by the Particle Swarm object to make the population.
values_ranges
(list) max and min for each value of the particle
values
(numeric) values of the particle (his position in space)
fitness
(numeric) fitness of the particle (his score)
fitness_function
(function) function used to find the fitness
personal_best_values
(numeric) Best values of the particle
personal_best_fitness
(numeric) Fitness of the best values
velocity
(numeric) Velocity of the particle (one velocity for each values)
acceleration_coefficient
(numeric) coefficient c1 and c2 (for personal and global best)
inertia
(numeric) inertia of the particle
new()
Create a new Particle object.
Particle$new( values_ranges, values, fitness_function, acceleration_coefficient, inertia )
values_ranges
range for each value of the particle (min and max), his size need to be the same as values. (List)
values,
values of the particles. (numeric)
fitness_function
function used to test the Particle and find his fitness. (function)
acceleration_coefficient
a vector of two values, one for c1 (the personal coefficient), and one for c2 (the global coefficient). (numeric)
inertia
The inertia of the particle (the influence of the previous velocity on the next velocity). (numeric)
A new Particle
object.
get_fitness()
Calculate the fitness of the particle with the fitness function and save it in self$fitness
Particle$get_fitness()
self
update()
Update Particle's position and velocity.
Particle$update(swarm_best)
swarm_best
the best values of the swarm used to update the velocity
self
update_personal_best_fitness()
Update the Particle's best values and fitness.
Particle$update_personal_best_fitness()
self
print()
print the current values of the particle and his fitness
Particle$print()
clone()
The objects of this class are cloneable with this method.
Particle$clone(deep = FALSE)
deep
Whether 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 | # If you use the Particle Swarm Object there is no need to manually create the Particle
# But if you want to use the Particle for another project:
# 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))
particle_example <- Particle$new(values_ranges = values_ranges,
values = c(0,0),
fitness_function = fitness_function,
acceleration_coefficient = c(0.5,0.5),
inertia = 0.4)
print(particle_example)
particle_example$get_fitness()
print(particle_example)
particle_example$update(c(10,25))
print(particle_example)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.