diff --git a/main.cpp b/main.cpp index e1270a1..e96472a 100644 --- a/main.cpp +++ b/main.cpp @@ -17,5 +17,5 @@ int main() sa::solution act = sa::solution::simulated_annealing(capacity, items, 1000, 0.99); - + act.print_sol(); } diff --git a/sa.cpp b/sa.cpp index e500f05..a2f7d47 100644 --- a/sa.cpp +++ b/sa.cpp @@ -1,10 +1,10 @@ #include "sa.hpp" #include -auto sa::solution::simulated_annealing(int cap, const std::vector &initial, +auto sa::solution::simulated_annealing(int capacity, const std::vector &items, const double alpha, double temp)->sa::solution { - sa::solution best(initial, cap); + sa::solution best(items, capacity); best.randomize(); sa::solution prev = best; diff --git a/sa.hpp b/sa.hpp index 289a58b..6189ee1 100644 --- a/sa.hpp +++ b/sa.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -15,7 +16,7 @@ class solution { int fitness; auto calculate_boxes()->int { - int count = 0; + int count = 1; long long now = 0; for (auto i : items) { @@ -30,6 +31,15 @@ class solution { return count; } + static void print_box(int box, std::queue &stored) { + std::cout << "Caixa " << box << ":"; + while (!stored.empty()) { + std::cout << ' ' << stored.front(); + stored.pop(); + } + std::cout << '\n'; + } + public: solution() = default; @@ -60,11 +70,29 @@ class solution { std::swap(gen, other.gen); } - void print_sol() { + void print_sol() const { std::cout << "NĂºmero de caixas: " << fitness << '\n'; + + int box_now = 1; + long long now = 0; + std::queue items_stored; + + for (auto i : items) { + if (now + i > capacity) { + print_box(box_now, items_stored); + std::cout << '\n'; + box_now++; + now = i; + } else { + now += i; + } + items_stored.push(i); + } + + print_box(box_now, items_stored); } - static auto simulated_annealing(int cap, const std::vector &initial, + static auto simulated_annealing(int capacity, const std::vector &items, double alpha, double temp)->solution; };