Class: Algorithm::LocalSearch::HillClimbing
- Inherits:
-
Object
- Object
- Algorithm::LocalSearch::HillClimbing
- Defined in:
- lib/opt_alg_framework/algorithm/local_search/hill_climbing.rb
Instance Method Summary collapse
-
#encapsulate_solution(solution) ⇒ Object
Solution is a hash, with the keys :solution and :fitness.
-
#initialize(params) ⇒ HillClimbing
constructor
Initialize passing a instantiated class of a problem and tweak operator.
-
#start ⇒ Object
solution_b = best solution solution_n = neighbor solution.
Constructor Details
#initialize(params) ⇒ HillClimbing
Initialize passing a instantiated class of a problem and tweak operator
7 8 9 10 |
# 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 |
Instance Method Details
#encapsulate_solution(solution) ⇒ Object
Solution is a hash, with the keys :solution and :fitness
28 29 30 31 32 33 |
# 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 ⇒ Object
solution_b = best solution solution_n = neighbor solution
14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |