Learn R Programming

particle.swarm.optimisation (version 1.0)

Particle: Particle

Description

Class for the Particles used in the Particle Swarm Optimisation, It is call by the Particle Swarm object to make the population.

Arguments

Active bindings

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

Methods

Public methods

Method new()

Create a new Particle object.

Usage

Particle$new(
  values_ranges,
  values,
  fitness_function,
  acceleration_coefficient,
  inertia
)

Arguments

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)

Returns

A new Particle object.

Method get_fitness()

Calculate the fitness of the particle with the fitness function and save it in self$fitness

Usage

Particle$get_fitness()

Returns

self

Method update()

Update Particle's position and velocity.

Usage

Particle$update(swarm_best)

Arguments

swarm_best

the best values of the swarm used to update the velocity

Returns

self

Method update_personal_best_fitness()

Update the Particle's best values and fitness.

Usage

Particle$update_personal_best_fitness()

Returns

self

Method print()

print the current values of the particle and his fitness

Usage

Particle$print()

Method clone()

The objects of this class are cloneable with this method.

Usage

Particle$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

Run this code
# NOT RUN {
# 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)
# }

Run the code above in your browser using DataLab