diff --git a/sa.hpp b/sa.hpp new file mode 100644 index 0000000..bbddade --- /dev/null +++ b/sa.hpp @@ -0,0 +1,60 @@ +#ifndef SIMULATED_ANNEALING_HEADER_12647_H +#define SIMULATED_ANNEALING_HEADER_12647_H + +#include +#include +#include + +namespace sa { + +class solution { + std::vector 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 &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