#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() { std::shuffle(items.begin(), items.end(), gen); fitness = calculate_boxes(); } void swap(solution &other) { std::swap(fitness, other.fitness); std::swap(items, other.items); std::swap(capacity, other.capacity); std::swap(gen, other.gen); } }; } // namespace sa #endif