Skip to content

Commit 1eb4b14

Browse files
committed
Formatting
1 parent 1708451 commit 1eb4b14

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Compiled pages are published at [https://cp-algorithms.com/](https://cp-algorith
2929

3030
### New articles
3131

32+
- (19 October 2024) [Simulated Annealing](https://cp-algorithms.com/num_methods/simulated_annealing.html)
3233
- (12 July 2024) [Manhattan distance](https://cp-algorithms.com/geometry/manhattan-distance.html)
3334
- (8 June 2024) [Knapsack Problem](https://cp-algorithms.com/dynamic_programming/knapsack.html)
3435
- (28 January 2024) [Introduction to Dynamic Programming](https://cp-algorithms.com/dynamic_programming/intro-to-dp.html)

src/num_methods/simulated_annealing.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ tags:
99

1010
## The Problem
1111

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).
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).
1313

1414
### Travelling Salesman Problem (TSP)
1515

16-
You are given a set of nodes in 2 dimensional space. Each node is characterised by its $ x $ and $ y $ coordinates. Your task is to find the an ordering of the nodes, which will minimise the distance to be travelled when visiting these nodes in that order.
16+
You are given a set of nodes in 2 dimensional space. Each node is characterised by its $x$ and $y$ coordinates. Your task is to find the an ordering of the nodes, which will minimise the distance to be travelled when visiting these nodes in that order.
1717

1818
### State
1919

@@ -26,12 +26,12 @@ It is a State in the State space which is close to the previous State. This usua
2626

2727
### E(s)
2828

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.
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.
3030

3131
## Approach
3232

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-
At the same time we also keep a track of the best state $ s_{best} $ across all iterations. Proceed till convergence or time runs out.
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+
At the same time we also keep a track of the best state $s_{best}$ across all iterations. Proceed till convergence or time runs out.
3535

3636

3737
## Probability Acceptance Function
@@ -41,9 +41,8 @@ P(E,E_{next},T) =
4141
\text{True} &\quad\text{if } \exp{\frac{-(E_{next}-E)}{T}} \ge random(0,1) \\
4242
\text{False} &\quad\text{otherwise}\\
4343
\end{cases}
44-
4544
$$
46-
This function takes in the current state, the next state and the Temperature , returning a boolean value, which tells our search whether it should move to $ s_{next} $ or stay at s. Note that for $E_{next} < E$ , this function will always return True.
45+
This function takes in the current state, the next state and the Temperature , returning a boolean value, which tells our search whether it should move to $s_{next}$ or stay at s. Note that for $E_{next} < E$ , this function will always return True.
4746

4847
```cpp
4948
bool P(double E,double E_next,double T){
@@ -72,13 +71,12 @@ class state{
7271
};
7372
};
7473
75-
int main(){
76-
double T = 1000; // Initial temperature
77-
double u = 0.99; // decay rate
74+
pair<int,state> simAnneal(){
7875
state s = state();
7976
state best = s;
80-
double E = s.E();
81-
double E_next;
77+
double T = 1000; // Initial temperature
78+
double u = 0.99; // decay rate
79+
double E = s.E(),E_next;
8280
double E_best = E;
8381
while (T > 1){
8482
state next = s.next();
@@ -93,12 +91,12 @@ int main(){
9391
E = E_next;
9492
T *= u;
9593
}
96-
cout << E_best << "\n";
97-
return 0;
94+
return {E_best,best};
9895
}
96+
9997
```
10098
## How to use:
101-
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.
99+
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.
102100

103101
### Parameters
104102
- T : Temperature. Set it to a higher value if you want the search to run for a longer time
@@ -149,11 +147,11 @@ class state{
149147
## Extra Modifications to the Algorithm:
150148

151149
- Add a time based exit condition to the while loop to prevent TLE
152-
- 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.
150+
- 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.
153151

154152

155153
## Problems
156154

157-
- https://usaco.org/index.php?page=viewproblem2&cpid=698
158-
- https://codeforces.com/contest/1556/problem/H
159-
- https://atcoder.jp/contests/intro-heuristics/tasks/intro_heuristics_a
155+
- [USACO Jan 2017 - Subsequence Reversal](https://usaco.org/index.php?page=viewproblem2&cpid=698)
156+
- [Deltix Summer 2021 - DIY Tree](https://codeforces.com/contest/1556/problem/H)
157+
- [AtCoder Contest Scheduling](https://atcoder.jp/contests/intro-heuristics/tasks/intro_heuristics_a)

0 commit comments

Comments
 (0)