#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); sa::solution prev = best; std::random_device rdevice; std::default_random_engine eng(rdevice()); std::uniform_real_distribution<> rand(0, 1); int iteration = 0; while (temp > temp_min) { iteration++; sa::solution neighbor(prev, iteration); neighbor.setneighbor(); long long diff = neighbor.fitness - prev.fitness; if (diff < 0 || rand(eng) / temp < 0.8) { swap(prev, neighbor); } temp *= alpha; if (prev.fitness < best.fitness) { best = prev; } } best.setiterations(iteration); return best; }