Class: Algorithm::LocalSearch::HillClimbing

Inherits:
Object
  • Object
show all
Defined in:
lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ HillClimbing

Initialize passing a instantiated class of a problem and tweak operator



5
6
7
8
# File 'lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb', line 5

def initialize(params)
  @tweak_operator = params[:tweak_operator]
  @problem = params[:problem]
end

Instance Method Details

#encapsulate_solution(solution) ⇒ Object

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



25
26
27
28
29
30
# File 'lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb', line 25

def encapsulate_solution(solution)
  hash = Hash.new
  hash[:solution] = solution
  hash[:fitness] = @problem.fitness(tasks_sequence: solution)
  hash
end

#startObject

Main method



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb', line 11

def start
  best = encapsulate_solution(@problem.default_solution)
  while true do
    r = encapsulate_solution(@tweak_operator.tweak(best[:solution]))
    if r[:fitness] < best[:fitness]
      best = r.dup
    else
      break
    end
  end
  best
end