Finish the prototype

This commit is contained in:
Segcolt 2024-10-12 16:29:02 -03:00
parent ea8be549a2
commit b791a80128
3 changed files with 34 additions and 6 deletions

View File

@ -17,5 +17,5 @@ int main()
sa::solution act = sa::solution::simulated_annealing(capacity, items, sa::solution act = sa::solution::simulated_annealing(capacity, items,
1000, 0.99); 1000, 0.99);
act.print_sol();
} }

4
sa.cpp
View File

@ -1,10 +1,10 @@
#include "sa.hpp" #include "sa.hpp"
#include <cmath> #include <cmath>
auto sa::solution::simulated_annealing(int cap, const std::vector<long long> &initial, auto sa::solution::simulated_annealing(int capacity, const std::vector<long long> &items,
const double alpha, double temp)->sa::solution const double alpha, double temp)->sa::solution
{ {
sa::solution best(initial, cap); sa::solution best(items, capacity);
best.randomize(); best.randomize();
sa::solution prev = best; sa::solution prev = best;

34
sa.hpp
View File

@ -3,6 +3,7 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <queue>
#include <random> #include <random>
#include <vector> #include <vector>
@ -15,7 +16,7 @@ class solution {
int fitness; int fitness;
auto calculate_boxes()->int { auto calculate_boxes()->int {
int count = 0; int count = 1;
long long now = 0; long long now = 0;
for (auto i : items) { for (auto i : items) {
@ -30,6 +31,15 @@ class solution {
return count; return count;
} }
static void print_box(int box, std::queue<long long> &stored) {
std::cout << "Caixa " << box << ":";
while (!stored.empty()) {
std::cout << ' ' << stored.front();
stored.pop();
}
std::cout << '\n';
}
public: public:
solution() = default; solution() = default;
@ -60,11 +70,29 @@ class solution {
std::swap(gen, other.gen); std::swap(gen, other.gen);
} }
void print_sol() { void print_sol() const {
std::cout << "Número de caixas: " << fitness << '\n'; std::cout << "Número de caixas: " << fitness << '\n';
int box_now = 1;
long long now = 0;
std::queue<long long> 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<long long> &initial, static auto simulated_annealing(int capacity, const std::vector<long long> &items,
double alpha, double temp)->solution; double alpha, double temp)->solution;
}; };