Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbee2358

Browse files
committed
final fixes
1 parent9b8c5b6 commitbee2358

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +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)
32+
- (1 May 2025)[Simulated Annealing](https://cp-algorithms.com/num_methods/simulated_annealing.html)
3333
- (12 July 2024)[Manhattan distance](https://cp-algorithms.com/geometry/manhattan-distance.html)
3434
- (8 June 2024)[Knapsack Problem](https://cp-algorithms.com/dynamic_programming/knapsack.html)
3535
- (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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ We are given a function $E(s)$, which calculates the energy of the state $s$. We
1616
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 an ordering of the nodes, which will minimise the distance to be travelled when visiting these nodes in that order.
1717

1818
##Motivation
19-
Annealing is a metallurgical process, wherein a material is heated up and allowed to cool, in order to allow the atoms inside to rearrange themselves in an arrangement with minimal internal energy, which in turn causes the material to have different properties. The state is the arrangement of atoms and the internal energy is the function being minimised. We can think of the original state of the atoms, as a local minima for its internal energy. To make the material rearrange its atoms, we need to motivate it to go across a region where its internal energy is not minimised in order to reach the global minima. This motivation is given by heating the material to a higher temperature.
19+
Annealing is a metallurgical process, wherein a material is heated up and allowed to cool, in order to allow the atoms inside to rearrange themselves in an arrangement with minimal internal energy, which in turn causes the material to have different properties. The state is the arrangement of atoms and the internal energy is the function being minimised. We can think of the original state of the atoms, as a local minima for its internal energy. To make the material rearrange its atoms, we need to motivate it to go across a region where its internal energy is not minimised in order to reach the global minima. This motivation is given by heating the material to a higher temperature.
2020

2121
Simulated annealing, literally, simulates this process. We start off with some random state (material) and set a high temperature (heat it up). Now, the algorithm is ready to accept states which have a higher energy than the current state, as it is motivated by the high temperature. This prevents the algorithm from getting stuck inside local minimas and move towards the global minima. As time progresses, the algorithm cools down and refuses the states with higher energy and moves into the closest minima it has found.
2222

@@ -26,7 +26,7 @@ $E(s)$ is the function which needs to be minimised (or maximised). It maps every
2626

2727
###State
2828

29-
The state space is the domain of the energy function, E(s), and a state is any element which belongs to the state space. 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.
29+
The state space is the domain of the energy function,$E(s)$, and a state is any element which belongs to the state space. 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.
3030

3131
###Neighbouring state
3232

@@ -41,12 +41,11 @@ At the same time we also keep a track of the best state $s_{best}$ across all it
4141
<center>
4242
<imgsrc="https://upload.wikimedia.org/wikipedia/commons/d/d5/Hill_Climbing_with_Simulated_Annealing.gif"width="800px">
4343
<br>
44-
<i>A visual representation of simulated annealing, searching for the maxima of this function with multiple local maxima.</i>.
44+
<i>A visual representation of simulated annealing, searching for the maxima of this function with multiple local maxima.</i>
4545
<br>
46-
<i>This <ahref="https://commons.wikimedia.org/wiki/File:Hill_Climbing_with_Simulated_Annealing.gif">animation</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.
4746
</center>
4847

49-
###Temperature($T$) and decay($u$)
48+
###Temperature(T) and decay(u)
5049

5150
The temperature of the system quantifies the willingness of the algorithm to accept a state with a higher energy. The decay is a constant which quantifies the "cooling rate" of the algorithm. A slow cooling rate (larger $u$) is known to give better results.
5251

@@ -107,8 +106,8 @@ pair<double, state> simAnneal() {
107106
best = s;
108107
E_best = E_next;
109108
}
109+
E = E_next;
110110
}
111-
E = E_next;
112111
T *= u;
113112
}
114113
return {E_best, best};
@@ -158,7 +157,6 @@ class state {
158157

159158
doubleE() {
160159
double dist = 0;
161-
bool first = true;
162160
int n = points.size();
163161
for (int i = 0;i < n; i++)
164162
dist += euclidean(points[i], points[(i+1)%n]);
@@ -187,8 +185,8 @@ int main() {
187185
- 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:
188186
```cpp
189187
boolP(double E, double E_next, double T, mt19937 rng) {
190-
e = 2 // set e to any real number greater than 1
191-
double prob =exp(-(E_next-E)/T);
188+
doublee = 2 // set e to any real number greater than 1
189+
double prob =pow(e,-(E_next-E)/T);
192190
if (prob > 1)
193191
return true;
194192
else {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp