class MetaheuristicAlgorithms::SimplifiedParticleSwarmOptimization

Public Class Methods

new(function_wrapper, number_of_variables: 1, objective: :maximization) click to toggle source
# 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

Public Instance Methods

Private Instance Methods

initialize_particles(number_of_particiles) click to toggle source
# File lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb, line 51
def initialize_particles(number_of_particiles)

  @particle_locations = []

  number_of_particiles.times do |individual_index|
    decision_variable_values = (0...@number_of_variables).map do |variable_index|
      get_decision_variable_value_by_randomization(variable_index)
    end
    @particle_locations << decision_variable_values
  end

end
move_particles(global_best_position, social_coefficient, random_variable_coefficient) click to toggle source
# File lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb, line 64
def move_particles(global_best_position, social_coefficient, random_variable_coefficient)
 
  # 0 to @particle_locations-1
  @particle_locations = @particle_locations.map do |particle_location|

    (0...@number_of_variables).map do |variable_index|

      # The value out-of-range in order to enter while loop
      # new_particle_location_coordinate = @function_wrapper.minimum_decision_variable_values[variable_index] - BigDecimal('1')
      new_particle_location_coordinate = @function_wrapper.minimum_decision_variable_values[variable_index].to_f - 1

      while new_particle_location_coordinate < @function_wrapper.minimum_decision_variable_values[variable_index].to_f || new_particle_location_coordinate > @function_wrapper.maximum_decision_variable_values[variable_index].to_f
        # new_particle_location_coordinate = (BigDecimal('1') - social_coefficient) * particle_location[variable_index] + social_coefficient * global_best_position[variable_index] + random_variable_coefficient * (bigdecimal_rand - BigDecimal('0.5'))
        new_particle_location_coordinate = (1 - social_coefficient) * particle_location[variable_index] + social_coefficient * global_best_position[variable_index] + random_variable_coefficient * (rand - 0.5)
      end

      new_particle_location_coordinate

    end

  end          

end