Listing 1: The simulated annealing algorithm

1.  Generate an initial trial solution: trial(x)
2.  best(x) = curr(x) = trial(x)
3.  For i=1 to MaxIterations
4.  Begin
5.     Generate a new trial(x) solution from curr(x)
6.     If  trialCost < bestCost
7.        best(x) = curr(x) = trial(x)
8.     Else If  trialCost < currCost
9.        curr(x) = trial(x)
10.    Else Begin
11.       anneal = exp((currCost-trialCost)/t(i)) 
12.       Generate a random number r between 0 and 1
13.       If r < anneal
14.          curr(x) = trial(x) // keep it
16.    End
15. End
— End of Listing —