class Algorithm::LocalSearch::HillClimbing

Public Class Methods

new(params) click to toggle source

Initialize passing a instantiated class of a problem and tweak operator

# File lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb, line 7
def initialize(params)
  @tweak_operator = params[:tweak_operator]
  @problem = params[:problem]
end

Public Instance Methods

encapsulate_solution(solution) click to toggle source

Solution is a hash, with the keys :solution and :fitness

# File lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb, line 28
def encapsulate_solution(solution)
  hash = Hash.new
  hash[:solution] = solution
  hash[:fitness] = @problem.fitness(solution)
  hash
end
start() click to toggle source

solution_b = best solution solution_n = neighbor solution

# File lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb, line 14
def start
  solution_b = encapsulate_solution(@problem.default_solution.shuffle)
  while true do
    solution_n = encapsulate_solution(@tweak_operator.tweak(solution_b[:solution]))
    if solution_n[:fitness] < solution_b[:fitness]
      solution_b = solution_n.dup
    else
      break
    end
  end
  solution_b
end