class MetaheuristicAlgorithms::FireflyAlgorithm

Public Class Methods

new(function_wrapper, number_of_variables: 1, objective: :maximization) click to toggle source
# File lib/metaheuristic_algorithms/firefly_algorithm.rb, line 34
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_by
                             when :minimization
                               :min_by
                           end
end

Public Instance Methods

Private Instance Methods

constrain_within_range(location_coordinate, variable_index) click to toggle source
# File lib/metaheuristic_algorithms/firefly_algorithm.rb, line 128
def constrain_within_range(location_coordinate, variable_index)
  if location_coordinate < (minimum_decision_variable_values = @function_wrapper.minimum_decision_variable_values[variable_index].to_f)
    minimum_decision_variable_values
  elsif location_coordinate > (maximum_decision_variable_values = @function_wrapper.maximum_decision_variable_values[variable_index].to_f)
    maximum_decision_variable_values
  else
    location_coordinate
  end
end
distance_of_two_fireflies(firefly_1, firefly_2) click to toggle source
# File lib/metaheuristic_algorithms/firefly_algorithm.rb, line 138
def distance_of_two_fireflies(firefly_1, firefly_2)

  # sum_of_squares = (0...@number_of_variables).inject(BigDecimal('0')) do |sum, variable_index|
  sum_of_squares = (0...@number_of_variables).inject(0) do |sum, variable_index|
    sum + (firefly_1.location_coordinates[variable_index] - firefly_2.location_coordinates[variable_index])**2
  end

  Math.sqrt(sum_of_squares)

end
initialize_fireflies(number_of_fireflies) click to toggle source
# File lib/metaheuristic_algorithms/firefly_algorithm.rb, line 72
def initialize_fireflies(number_of_fireflies)

  @fireflies = []

  for i in (0...number_of_fireflies)
    decision_variable_values = (0...@number_of_variables).map do |variable_index|
      get_decision_variable_value_by_randomization(variable_index)
    end

    # firefly = Firefly.new(@function_wrapper, decision_variable_values, BigDecimal('0'))
    firefly = Firefly.new(@function_wrapper, decision_variable_values, 0)
    @fireflies << firefly
  end

end
move_fireflies(randomization_parameter_alpha, absorption_coefficient_gamma) click to toggle source
# File lib/metaheuristic_algorithms/firefly_algorithm.rb, line 88
def move_fireflies(randomization_parameter_alpha, absorption_coefficient_gamma)

  # attractiveness_beta_at_distance_0 = BigDecimal('1')
  attractiveness_beta_at_distance_0 = 1

  fireflies_copy = @fireflies.map(&:deep_clone)

  @fireflies.each do |firefly_i|

    fireflies_copy.delete(firefly_i)

    fireflies_copy.each do |firefly_j|

      if firefly_i.light_intensity < firefly_j.light_intensity

        distance_of_two_fireflies = distance_of_two_fireflies(firefly_i, firefly_j)

        # attractiveness_beta = attractiveness_beta_at_distance_0 * BigMath.exp(-absorption_coefficient_gamma * distance_of_two_fireflies.power(2), 10)
        attractiveness_beta = attractiveness_beta_at_distance_0 * Math.exp(-absorption_coefficient_gamma * distance_of_two_fireflies**2)

        @number_of_variables.times do |variable_index|
          # new_location_coordinate = firefly_i.location_coordinates[variable_index] * (BigDecimal('1') - attractiveness_beta)
          #                           + firefly_j.location_coordinates[variable_index] * attractiveness_beta
          #                           + randomization_parameter_alpha * (bigdecimal_rand - BigDecimal('0.5'))
          new_location_coordinate = firefly_i.location_coordinates[variable_index] * (1 - attractiveness_beta) 
                                    + firefly_j.location_coordinates[variable_index] * attractiveness_beta 
                                    + randomization_parameter_alpha * (rand - 0.5)                
          new_location_coordinate = constrain_within_range(new_location_coordinate, variable_index)

          firefly_i.location_coordinates[variable_index] = new_location_coordinate
        end

      end

    end

  end

end