Finish the class for the solution
This commit is contained in:
parent
041cfcadb2
commit
55008b3e40
60
sa.hpp
Normal file
60
sa.hpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#ifndef SIMULATED_ANNEALING_HEADER_12647_H
|
||||||
|
#define SIMULATED_ANNEALING_HEADER_12647_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
namespace sa {
|
||||||
|
|
||||||
|
class solution {
|
||||||
|
std::vector<long long> items;
|
||||||
|
std::default_random_engine gen;
|
||||||
|
int capacity;
|
||||||
|
int fitness;
|
||||||
|
|
||||||
|
auto calculate_boxes()->int {
|
||||||
|
int count = 0;
|
||||||
|
long long now = 0;
|
||||||
|
|
||||||
|
for (auto i : items) {
|
||||||
|
if (now + i > capacity) {
|
||||||
|
count++;
|
||||||
|
now = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
now += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
solution(const std::vector<long long> &items, int capacity): // Gera a solução inicial
|
||||||
|
items(items), capacity(capacity), fitness(calculate_boxes()) {
|
||||||
|
std::random_device rdevice;
|
||||||
|
gen.seed(rdevice());
|
||||||
|
}
|
||||||
|
|
||||||
|
solution(const solution &sol):
|
||||||
|
items(sol.items), gen(sol.gen), capacity(sol.capacity) { // Gera um vizinho da solução
|
||||||
|
|
||||||
|
std::uniform_int_distribution<> dist(0, items.size() - 1);
|
||||||
|
int first = dist(gen);
|
||||||
|
int second = 0;
|
||||||
|
while ((second = dist(gen)) == first) {}
|
||||||
|
|
||||||
|
std::swap(items[first], items[second]);
|
||||||
|
fitness = calculate_boxes();
|
||||||
|
}
|
||||||
|
|
||||||
|
void randomize() {
|
||||||
|
shuffle(items.begin(), items.end(), gen);
|
||||||
|
|
||||||
|
fitness = calculate_boxes();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace sa
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user