#ifndef SIMULATED_ANNEALING_HEADER_12647_H #define SIMULATED_ANNEALING_HEADER_12647_H #include #include #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 = 1; long long now = 0; for (auto i : items) { if (now + i > capacity) { count++; now = i; continue; } now += i; } return count; } static void print_box(int box, std::queue &stored) { std::cout << "Caixa " << box << ":"; while (!stored.empty()) { std::cout << ' ' << stored.front(); stored.pop(); } std::cout << '\n'; } public: solution() = default; solution(const std::vector &items, int capacity): // Gera a solução inicial items(items), gen(std::random_device()()), capacity(capacity), fitness(calculate_boxes()) {} void setneighbor() { // 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); } void print_sol() const { std::cout << "Número de caixas: " << fitness << '\n'; int box_now = 1; long long now = 0; std::queue 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 capacity, const std::vector &items, double alpha, double temp)->solution; }; } // namespace sa #endif