WPILibC++ 2024.3.2
SimulatedAnnealing.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <cmath>
8#include <functional>
9#include <limits>
10#include <random>
11
12namespace frc {
13
14/**
15 * An implementation of the Simulated Annealing stochastic nonlinear
16 * optimization method.
17 *
18 * Solving optimization problems involves tweaking decision variables to try to
19 * minimize some cost function. Simulated annealing is good for solving
20 * optimization problems with many local minima and a very large search space
21 * (it’s a heuristic solver rather than an exact solver like, say, SQP or
22 * interior-point method). Simulated annealing is a popular choice for solving
23 * the traveling salesman problem (see TravelingSalesman).
24 *
25 * @see <a
26 * href="https://en.wikipedia.org/wiki/Simulated_annealing">https://en.wikipedia.org/wiki/Simulated_annealing</a>
27 * @tparam State The type of the state to optimize.
28 */
29template <typename State>
31 public:
32 /**
33 * Constructor for Simulated Annealing that can be used for the same functions
34 * but with different initial states.
35 *
36 * @param initialTemperature The initial temperature. Higher temperatures make
37 * it more likely a worse state will be accepted during iteration, helping
38 * to avoid local minima. The temperature is decreased over time.
39 * @param neighbor Function that generates a random neighbor of the current
40 * state.
41 * @param cost Function that returns the scalar cost of a state.
42 */
43 constexpr SimulatedAnnealing(double initialTemperature,
44 std::function<State(const State&)> neighbor,
45 std::function<double(const State&)> cost)
46 : m_initialTemperature{initialTemperature},
47 m_neighbor{neighbor},
48 m_cost{cost} {}
49
50 /**
51 * Runs the Simulated Annealing algorithm.
52 *
53 * @param initialGuess The initial state.
54 * @param iterations Number of iterations to run the solver.
55 * @return The optimized state.
56 */
57 State Solve(const State& initialGuess, int iterations) {
58 State minState = initialGuess;
59 double minCost = std::numeric_limits<double>::infinity();
60
61 std::random_device rd;
62 std::mt19937 gen{rd()};
63 std::uniform_real_distribution<> distr{0.0, 1.0};
64
65 State state = initialGuess;
66 double cost = m_cost(state);
67
68 for (int i = 0; i < iterations; ++i) {
69 double temperature = m_initialTemperature / i;
70
71 State proposedState = m_neighbor(state);
72 double proposedCost = m_cost(proposedState);
73 double deltaCost = proposedCost - cost;
74
75 double acceptanceProbability = std::exp(-deltaCost / temperature);
76
77 // If cost went down or random number exceeded acceptance probability,
78 // accept the proposed state
79 if (deltaCost < 0 || acceptanceProbability >= distr(gen)) {
80 state = proposedState;
81 cost = proposedCost;
82 }
83
84 // If proposed cost is less than minimum, the proposed state becomes the
85 // new minimum
86 if (proposedCost < minCost) {
87 minState = proposedState;
88 minCost = proposedCost;
89 }
90 }
91
92 return minState;
93 }
94
95 private:
96 double m_initialTemperature;
97 std::function<State(const State&)> m_neighbor;
98 std::function<double(const State&)> m_cost;
99};
100
101} // namespace frc
An implementation of the Simulated Annealing stochastic nonlinear optimization method.
Definition: SimulatedAnnealing.h:30
State Solve(const State &initialGuess, int iterations)
Runs the Simulated Annealing algorithm.
Definition: SimulatedAnnealing.h:57
constexpr SimulatedAnnealing(double initialTemperature, std::function< State(const State &)> neighbor, std::function< double(const State &)> cost)
Constructor for Simulated Annealing that can be used for the same functions but with different initia...
Definition: SimulatedAnnealing.h:43
state
Definition: core.h:2271
State
Possible state of a SysId routine.
Definition: SysIdRoutineLog.h:25
Definition: AprilTagPoseEstimator.h:15
compound_unit< energy::joules, inverse< mass::kilogram > > rd
Definition: radiation.h:61