Class: MetaheuristicAlgorithms::SimplifiedParticleSwarmOptimization
- Inherits:
-
Object
- Object
- MetaheuristicAlgorithms::SimplifiedParticleSwarmOptimization
- Includes:
- BaseAlgorithmModule, Helper
- Defined in:
- lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb
Instance Method Summary collapse
-
#initialize(function_wrapper, number_of_variables: 1, objective: :maximization) ⇒ SimplifiedParticleSwarmOptimization
constructor
A new instance of SimplifiedParticleSwarmOptimization.
- #search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: BigDecimal('0.5'), random_variable_coefficient: BigDecimal('0.2')) ⇒ Object
Methods included from Helper
Methods included from BaseAlgorithmModule
#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: BigDecimal('0.5'), random_variable_coefficient: BigDecimal('0.2')) ⇒ Object
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 45 46 |
# File 'lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb', line 18 def search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: BigDecimal('0.5'), random_variable_coefficient: BigDecimal('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) = BigDecimal(.to_s) unless .kind_of?(BigDecimal) random_variable_coefficient = BigDecimal(random_variable_coefficient.to_s) unless random_variable_coefficient.kind_of?(BigDecimal) initialize_particles(number_of_particiles) global_best_position = nil best_function_value = nil # 0 to number_of_iterations-1 (0...number_of_iterations).each do |iteration| function_values = @particle_locations.map do |particle_location| @function_wrapper.objective_function_value(particle_location) 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, , random_variable_coefficient) end { best_decision_variable_values: global_best_position, best_objective_function_value: best_function_value } end |