class TabuSearch::Context
Attributes
best_fitness[RW]
best_genome[RW]
tabu_list[RW]
tabu_set[RW]
tabu_size[RW]
Public Class Methods
new(tabu_size = 10)
click to toggle source
# File lib/tabu_search/context.rb, line 8 def initialize(tabu_size = 10) @tabu_list = [] @tabu_set = Set.new @tabu_size = tabu_size @best_genome = nil @best_fitness = nil end
Public Instance Methods
search(unit, times)
click to toggle source
# File lib/tabu_search/context.rb, line 17 def search(unit, times) @best_genome = unit.genome.dup @best_fitness = unit.fitness times.times do data = search_best_neighbour(unit) unit.step(self, data) end unit.genome = best_genome unit end
search_best_neighbour(unit)
click to toggle source
# File lib/tabu_search/context.rb, line 42 def search_best_neighbour(unit) actions = unit.search_neighbour(self).sort_by! {|data| -data[-1] } return actions[0] if actions[0][-1] > best_fitness actions.each do |data| return data unless tabu_set.include?(data[0]) end return actions.first end
update(id, genome, fitness)
click to toggle source
# File lib/tabu_search/context.rb, line 29 def update(id, genome, fitness) if fitness > best_fitness self.best_genome = genome.dup self.best_fitness = fitness end unless tabu_set.include?(id) tabu_set << id tabu_list << id tabu_set.delete(tabu_list.shift) if tabu_list.length > tabu_size end end