#include "sa.hpp" #include auto sa::solution::simulated_annealing(int capacity, const std::vector &items, const double alpha, double temp, const double temp_min)->sa::solution { sa::solution best(items, capacity); best.randomize(); sa::solution prev = best; std::random_device rdevice; std::default_random_engine eng(rdevice()); std::uniform_real_distribution<> rand(0, 1); while (temp > temp_min) { sa::solution neighbor(prev); neighbor.setneighbor(); int diff = prev.fitness - neighbor.fitness; if (diff <= 0 || std::exp(-diff / temp) > rand(eng)) { prev.swap(neighbor); } temp *= alpha; if (prev.fitness < best.fitness) { best = prev; } } return best; }