You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/num_methods/simulated_annealing.md
+62-34Lines changed: 62 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ tags:
7
7
8
8
**Simulated Annealing (SA)** is a randomized algorithm, which approximates the global optimum of a function. It's called a randomized algorithm, because it employs a certain amount of randomness in its search and thus its output can vary for the same input.
9
9
10
-
## The Problem
10
+
## The problem
11
11
12
12
We are given a function $E(s)$, which calculates the potential of the state $s$. We are tasked with finding the state $s_{best}$ at which $E(s)$ is minimized. **SA** is suited for problems where the states are discrete and $E(s)$ has multiple local minima. We'll take the example of the [Travelling Salesman Problem (TSP)](https://en.wikipedia.org/wiki/Travelling_salesman_problem).
13
13
@@ -18,17 +18,17 @@ You are given a set of nodes in 2 dimensional space. Each node is characterised
18
18
### State
19
19
20
20
State space is the collection of all possible values that can be taken by the independent variables.
21
-
A State is a unique point in the state space of the problem. In the case of TSP, all possible paths that we can take to visit all the nodes is the state space, and any single one of these paths can be considered as a State.
21
+
A state is a unique point in the state space of the problem. In the case of TSP, all possible paths that we can take to visit all the nodes is the state space, and any single one of these paths can be considered as a state.
22
22
23
23
### Neighbouring state
24
24
25
25
It is a state in the state space which is close to the previous state. This usually means that we can obtain the neighbouring state from the original state using a simple transform. In the case of the Travelling Salesman Problem, a neighbouring state is obtained by randomly choosing 2 nodes, and swapping their positions in the current state.
26
26
27
-
### The Energy Function E(s)
27
+
### The energy function E(s)
28
28
29
29
$E(s)$ is the function which needs to be minimised (or maximised). It maps every state to a real number. In the case of TSP, $E(s)$ returns the distance of travelling one full circle in the order of nodes in the state.
30
30
31
-
## The Approach
31
+
## The approach
32
32
33
33
We start of with a random state $s$. In every step, we choose a neighbouring state $s_{next}$ of the current state $s$. If $E(s_{next}) < E(s)$, then we update $s = s_{next}$. Otherwise, we use a probability acceptance function $P(E(s),E(s_{next}),T)$ which decides whether we should move to $s_{next}$ or stay at $s$. T here is the temperature, which is initially set to a high value and decays slowly with every step. The higher the temperature, the more likely it is to move to $s_{next}$.
34
34
At the same time we also keep a track of the best state $s_{best}$ across all iterations. Proceeding till convergence or time runs out.
@@ -48,7 +48,7 @@ Simulated annealing, literally simulates this process. We start off with some ra
48
48
<i>This <ahref="https://upload.wikimedia.org/wikipedia/commons/d/d5/Hill_Climbing_with_Simulated_Annealing.gif">gif</a> by [Kingpin13](https://commons.wikimedia.org/wiki/User:Kingpin13) is distributed under <ahref="https://creativecommons.org/publicdomain/zero/1.0/deed.en">CC0 1.0</a></i> license.
49
49
</center>
50
50
51
-
## Probability Acceptance Function
51
+
## Probability Acceptance Function(PAF)
52
52
53
53
$P(E,E_{next},T) =
54
54
\begin{cases}
@@ -60,9 +60,8 @@ This function takes in the current state, the next state and the Temperature , r
60
60
61
61
```cpp
62
62
boolP(double E,double E_next,double T){
63
-
double e = 2.71828;
64
63
double prob = (double)rand()/RAND_MAX; // Generate a random number between 0 and 1
65
-
if(pow(e,-(E_next-E)/T) > prob) return true;
64
+
if(exp(-(E_next-E)/T) > prob) return true;
66
65
else return false;
67
66
}
68
67
```
@@ -74,23 +73,28 @@ class state{
74
73
state(){
75
74
// Generate the initial state
76
75
}
76
+
state(state& s){
77
+
// Implement the copy constructor
78
+
}
77
79
state next(){
78
-
state next;
79
-
next = s;
80
-
// Make changes to the state "next" and then return it
Fill in the state class functions as appropriate. If you are trying to find a global maxima and not a minima, ensure that the $E()$ function returns negative of the function you are maximising and finally print out $-E_{best}$. Set the below parameters as per your need.
114
118
115
119
### Parameters
116
-
- $T$ : Temperature. Set it to a higher value if you want the search to run for a longer time
117
-
- $u$ : Decay. Decides the rate of cooling. A slower cooling rate (larger value of u) usually gives better results. Ensure $u < 1$.
120
+
- $T$ : Temperature. Set it to a higher value if you want the search to run for a longer time.
121
+
- $u$ : Decay. Decides the rate of cooling. A slower cooling rate (larger value of u) usually gives better results, at the cost of running for a longer time. Ensure $u < 1$.
118
122
119
123
The number of iterations the loop will run for is given by the expression
120
124
121
125
$N = \lceil -\log_{u}{T} \rceil$
122
126
123
-
To see the effect of decay rate on solution results, run simulated annealing for decay rates 0.95 , 0.97 and 0.99 and see the difference.
127
+
Tips for choosing $T$ and $u$ : If there are many local minimas and a wide state space, set $u = 0.999$, for a slow cooling rate, which will allow the algorithm to explore more possibilities. On the other hand, if the state space is narrower, $u = 0.99$ should suffice. If you are not sure, play it safe by setting $u = 0.998$ or higher. Calculate the time complexity of a single iteration of the algorithm, and use this to approximate a value of $N$ which will prevent TLE, then use the below formula to obtain $T$.
128
+
129
+
$T = u^{-N}$
124
130
125
-
### Example State class for TSP
131
+
### Example implementation for TSP
126
132
```cpp
133
+
127
134
classstate{
128
135
public:
129
136
vector<pair<int,int>> points;
130
137
131
-
state(){ // Initial random order of points
132
-
points = {}; // Fill in some points to start with, or generate them randomly
double E(){ // calculates the round cost of travelling one full circle.
155
+
double E(){
149
156
double dist = 0;
150
157
bool first = true;
151
158
int n = points.size();
@@ -155,13 +162,34 @@ class state{
155
162
return dist;
156
163
};
157
164
};
165
+
166
+
167
+
intmain(){
168
+
pair<int,state> res;
169
+
res = simAnneal();
170
+
double E_best = res.first;
171
+
state best = res.second;
172
+
cout << "Lenght of shortest path found : " << E_best << "\n";
173
+
cout << "Order of points in shortest path : \n";
174
+
for(auto x: best.points){
175
+
cout << x.first << " " << x.second << "\n";
176
+
}
177
+
}
158
178
```
159
179
160
-
## Extra Modifications to the Algorithm:
180
+
## Further modifications to the algorithm:
161
181
162
182
- Add a time based exit condition to the while loop to prevent TLE
163
-
- You can replace the e value in the Probability Acceptance function to any real number > 1. For a given $E_{next} - E > 0$, a higher e value reduces the chance of accepting that state and a smaller e value, increases it.
164
-
183
+
- The Probability acceptance function given above, prefers accepting states which are lower in energy because of the $E_{next} - E$ factor in the numerator of the exponent. You can simply remove this factor, to make the PAF independent of the difference in energies.
184
+
- The effect of the difference in energies, $E_{next} - E$ on the PAF can be increased/decreased by increasing/decreasing the base of the exponent as shown below:
185
+
```cpp
186
+
boolP(double E,double E_next,double T){
187
+
double e = 2; // set e to any real number greater than 1
188
+
double prob = (double)rand()/RAND_MAX; // Generate a random number between 0 and 1
0 commit comments