Class: MHL::Particle

Inherits:
GenericParticle show all
Defined in:
lib/mhl/particle.rb

Instance Attribute Summary

Attributes inherited from GenericParticle

#attractor

Instance Method Summary collapse

Methods inherited from GenericParticle

#evaluate

Constructor Details

#initialize(initial_position, initial_velocity) ⇒ Particle

Returns a new instance of Particle.



7
8
9
10
# File 'lib/mhl/particle.rb', line 7

def initialize(initial_position, initial_velocity)
  super(initial_position)
  @velocity = initial_velocity
end

Instance Method Details

#move(chi, c1, c2, swarm_attractor) ⇒ Object

move particle and update attractor



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
# File 'lib/mhl/particle.rb', line 13

def move(chi, c1, c2, swarm_attractor)
  raise 'Particle attractor is nil!' if @attractor.nil?

  # update particle velocity and position according to the Constrained PSO
  # variant of the Particle Swarm Optimization algorithm:
  #
  # V_{i,j}(t+1) = \chi [ V_{i,j}(t) + \\
  #                       C_1 * r_{i,j}(t) * (P_{i,j}(t) - X_{i,j}(t)) + \\
  #                       C_2 * R_{i,j}(t) * (G_j(t) - X_{i,j}(t)) ] \\
  # X_{i,j}(t+1) = X_{i,j}(t) + V_{i,j}(t+1)
  #
  # see equation 4.30 of [SUN11].

  # update velocity
  @velocity = @velocity.zip(@position, @attractor[:position], swarm_attractor[:position]).map do |v_j,x_j,p_j,g_j|
    # everything is damped by inertia weight chi
    chi *
      #previous velocity
      (v_j +
      # "memory" component (linear attraction towards the best position
      # that this particle encountered so far)
      c1 * SecureRandom.random_number * (p_j - x_j) +
      # "social" component (linear attraction towards the best position
      # that the entire swarm encountered so far)
      c2 * SecureRandom.random_number * (g_j - x_j))
  end

  # update position
  @position = @position.zip(@velocity).map do |x_j,v_j|
    x_j + v_j
  end
end

#remain_within(constraints) ⇒ Object

implement confinement à la SPSO 2011. for more information, see equations 3.14 and 3.15 of [CLERC12].



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mhl/particle.rb', line 48

def remain_within(constraints)
  @position = @position.map.with_index do |x_j,j|
    d_max = constraints[:max][j]
    d_min = constraints[:min][j]
    if x_j > d_max
      # puts "resetting #{j}-th position component #{x_j} to #{d_max}"
      x_j = d_max
      # puts "resetting #{j}-th velocity component #{@velocity[j]} to #{-0.5 * @velocity[j]}"
      @velocity[j] = -0.5 * @velocity[j]
    elsif x_j < d_min
      # puts "resetting #{j}-th position component #{x_j} to #{d_min}"
      x_j = d_min
      # puts "resetting #{j}-th velocity component #{@velocity[j]} to #{-0.5 * @velocity[j]}"
      @velocity[j] = -0.5 * @velocity[j]
    end
    x_j
  end
end