Class: MHL::QPSOSwarm

Inherits:
GenericSwarmBehavior show all
Defined in:
lib/mhl/qpso_swarm.rb

Constant Summary

Constants inherited from GenericSwarmBehavior

GenericSwarmBehavior::DEFAULT_ALPHA, GenericSwarmBehavior::DEFAULT_C1, GenericSwarmBehavior::DEFAULT_C2, GenericSwarmBehavior::DEFAULT_CHI, GenericSwarmBehavior::PHI

Instance Method Summary collapse

Methods inherited from GenericSwarmBehavior

#update_attractor

Constructor Details

#initialize(size, initial_positions, params = {}) ⇒ QPSOSwarm

Returns a new instance of QPSOSwarm.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mhl/qpso_swarm.rb', line 8

def initialize(size, initial_positions, params={})
  @size      = size
  @particles = Array.new(@size) do |index|
    QuantumParticle.new(initial_positions[index])
  end

  # find problem dimension
  @dimension  = initial_positions[0].size

  @iteration = 1

  # define procedure to get dynamic value for alpha
  @get_alpha = if params.has_key? :alpha and params[:alpha].respond_to? :call
    params[:alpha]
  else
    ->(it) { (params[:alpha] || DEFAULT_ALPHA).to_f }
  end

  if params.has_key? :constraints
    puts "QPSOSwarm called w/ constraints: #{params[:constraints]}"
  end

  @constraints = params[:constraints]
end

Instance Method Details

#mutateObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mhl/qpso_swarm.rb', line 33

def mutate
  # get alpha parameter
  alpha = @get_alpha.call(@iteration)

  # this calculates the C_n parameter (the centroid of the set of all the
  # particle attractors) as defined in equations 4.81 and 4.82 of [SUN11].
  attractors = @particles.map {|p| p.attractor[:position] }
  c_n = 0.upto(@dimension-1).map do |j|
    attractors.inject(0.0) {|s,attr| s += attr[j] } / @size.to_f
  end

  # move particles
  @particles.each do |p|
    p.move(alpha, c_n, @swarm_attractor)
    if @constraints
      p.remain_within(@constraints)
    end
  end

  @iteration += 1
end