Class: MetaheuristicAlgorithms::SimplifiedParticleSwarmOptimization

Inherits:
Object
  • Object
show all
Includes:
BaseAlgorithmModule, Helper
Defined in:
lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb

Instance Method Summary collapse

Methods included from Helper

#bigdecimal_rand

Methods included from BaseAlgorithmModule

#gaussian, #get_decision_variable_value_by_randomization

Constructor Details

#initialize(function_wrapper, number_of_variables: 1, objective: :maximization) ⇒ SimplifiedParticleSwarmOptimization

Returns a new instance of SimplifiedParticleSwarmOptimization.



7
8
9
10
11
12
13
14
15
16
# File 'lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb', line 7

def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
  @function_wrapper = function_wrapper
  @number_of_variables = number_of_variables
  @objective_method_name = case objective
                             when :maximization
                               :max
                             when :minimization
                               :min
                           end        
end

Instance Method Details

#search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: 0.5, random_variable_coefficient: 0.2) ⇒ Object

def search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: BigDecimal(‘0.5’), random_variable_coefficient: BigDecimal(‘0.2’))



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
45
46
47
48
# File 'lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb', line 19

def search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: 0.5, random_variable_coefficient: 0.2)

  number_of_particiles = number_of_particiles.to_i unless number_of_particiles.kind_of?(Integer)
  number_of_iterations = number_of_iterations.to_i unless number_of_iterations.kind_of?(Integer)
  # social_coefficient = BigDecimal(social_coefficient.to_s) unless social_coefficient.kind_of?(BigDecimal)
  # random_variable_coefficient = BigDecimal(random_variable_coefficient.to_s) unless random_variable_coefficient.kind_of?(BigDecimal)        
  social_coefficient = social_coefficient.to_f unless social_coefficient.kind_of?(Float)
  random_variable_coefficient = random_variable_coefficient.to_f unless random_variable_coefficient.kind_of?(Float)        

  initialize_particles(number_of_particiles)

  global_best_position = nil
  best_function_value = nil

  number_of_iterations.times do |iteration|

    function_values = @particle_locations.map do |particle_location|
      @function_wrapper.objective_function_value(particle_location).to_f
    end 

    best_function_value = function_values.send(@objective_method_name)
    global_best_position = @particle_locations[function_values.index(best_function_value)]

    move_particles(global_best_position, social_coefficient, random_variable_coefficient)

  end

  { best_decision_variable_values: global_best_position, best_objective_function_value: best_function_value }

end