lib/tabu_search/context.rb in tabu_search-0.1.0 vs lib/tabu_search/context.rb in tabu_search-0.1.1
- old
+ new
@@ -13,11 +13,11 @@
@best_genome = nil
@best_fitness = nil
end
def search(unit, times)
- @best_genome = unit.genome
+ @best_genome = unit.genome.dup
@best_fitness = unit.fitness
times.times do
data = search_best_neighbour(unit)
unit.step(self, data)
@@ -37,21 +37,16 @@
tabu_list << id
tabu_set.delete(tabu_list.shift) if tabu_list.length > tabu_size
end
end
-
- private
-
def search_best_neighbour(unit)
- actions = unit.search_neighbour(self)
+ actions = unit.search_neighbour(self).sort_by! {|data| -data[-1] }
+ return actions[0] if actions[0][-1] > best_fitness
- sorted_actions = actions.sort_by {|data| -data[-1] }
- return sorted_actions[0] if sorted_actions[0][-1] > best_fitness
-
- sorted_actions.each do |data|
+ actions.each do |data|
return data unless tabu_set.include?(data[0])
end
- return sorted_actions.first
+ return actions.first
end
end
end