Class: MHL::QuantumParticle

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

Instance Attribute Summary collapse

Attributes inherited from GenericParticle

#attractor

Instance Method Summary collapse

Methods inherited from GenericParticle

#evaluate, #initialize

Constructor Details

This class inherits a constructor from MHL::GenericParticle

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

Instance Method Details

#move(alpha, mean_best, swarm_attractor) ⇒ Object

move particle using QPSO - Type II algorithm



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

def move(alpha, mean_best, swarm_attractor)
  raise 'Particle attractor is nil!' if @attractor.nil?

  dimension = @position.size

  # phi represents the \phi_{i,n} parameter in [SUN11], formula 4.83
  phi = Array.new(dimension) { SecureRandom.random_number }

  # p_i represents the p_{i,n} parameter in [SUN11], formulae 4.82 and 4.83
  p_i = phi.zip(@attractor[:position], swarm_attractor[:position]).map do |phi_j,p_j,g_j|
    phi_j * p_j + (1.0 - phi_j) * g_j
  end

  # delta represents the displacement for the current position.
  # See [SUN11], formula 4.82
  delta = @position.zip(mean_best).map do |x_n,c_n| 
    # \alpha * | X_{i,n} - C_n | * log(\frac{1}{u_{i,n+1}})
    alpha * (x_n - c_n).abs * Math.log(1.0 / SecureRandom.random_number)
  end

  # update position
  if SecureRandom.random_number < 0.5
    @position = p_i.zip(delta).map {|p_in,delta_n| p_in + delta_n }
  else
    @position = p_i.zip(delta).map {|p_in,delta_n| p_in - delta_n }
  end

  @position
end

#remain_within(constraints) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mhl/quantum_particle.rb', line 43

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
    elsif x_j < d_min
      # puts "resetting #{j}-th position component #{x_j} to #{d_min}"
      x_j = d_min
    end
    x_j
  end
end