diff --git a/sa.cpp b/sa.cpp index 3e5f762..371ae41 100644 --- a/sa.cpp +++ b/sa.cpp @@ -19,8 +19,8 @@ auto sa::solution::simulated_annealing(int capacity, const std::vector rand(eng)) { + long long diff = neighbor.fitness - prev.fitness; + if (diff < 0 || rand(eng) / temp < 0.8) { swap(prev, neighbor); } diff --git a/sa.hpp b/sa.hpp index bec18ac..44a7f19 100644 --- a/sa.hpp +++ b/sa.hpp @@ -95,8 +95,8 @@ class box { class solution { std::vector boxes; std::default_random_engine gen; + long long fitness; int capacity; - int fitness; int iterations; int iteration; @@ -140,7 +140,7 @@ class solution { solution() = default; solution(const solution &other, int itr): boxes(other.boxes), gen(other.gen), - capacity(other.capacity), fitness(other.fitness), + fitness(other.fitness), capacity(other.capacity), iteration(itr) {} solution(const std::vector &items, int capacity): // Gera a solução inicial