Finish the prototype
This commit is contained in:
parent
ea8be549a2
commit
b791a80128
2
main.cpp
2
main.cpp
@ -17,5 +17,5 @@ int main()
|
||||
sa::solution act = sa::solution::simulated_annealing(capacity, items,
|
||||
1000, 0.99);
|
||||
|
||||
|
||||
act.print_sol();
|
||||
}
|
||||
|
||||
4
sa.cpp
4
sa.cpp
@ -1,10 +1,10 @@
|
||||
#include "sa.hpp"
|
||||
#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
|
||||
{
|
||||
sa::solution best(initial, cap);
|
||||
sa::solution best(items, capacity);
|
||||
best.randomize();
|
||||
|
||||
sa::solution prev = best;
|
||||
|
||||
34
sa.hpp
34
sa.hpp
@ -3,6 +3,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
@ -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<long long> &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<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;
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user