57 State
Solve(
const State& initialGuess,
int iterations) {
58 State minState = initialGuess;
59 double minCost = std::numeric_limits<double>::infinity();
61 std::random_device rd;
62 std::mt19937 gen{rd()};
63 std::uniform_real_distribution<> distr{0.0, 1.0};
65 State state = initialGuess;
66 double cost = m_cost(state);
68 for (
int i = 0; i < iterations; ++i) {
69 double temperature = m_initialTemperature / i;
71 State proposedState = m_neighbor(state);
72 double proposedCost = m_cost(proposedState);
73 double deltaCost = proposedCost - cost;
75 double acceptanceProbability = std::exp(-deltaCost / temperature);
79 if (deltaCost < 0 || acceptanceProbability >= distr(gen)) {
80 state = proposedState;
86 if (proposedCost < minCost) {
87 minState = proposedState;
88 minCost = proposedCost;