Excercise 6 - Intermediate
This solution should perfectly correspond to the excercise.
- The probability is only calculated for T > 0. Thus T = 0 is handled.
- We climb whenever the new random position is higher than the current one and
- we consider simulated annealing (elif-branch).
The results are quite good. However, this code fails.
Any idea why?
if T > 0:
prob = math.exp(-(h[x] - h[x_new]) / T)
else:
prob = 0
rand = random.random()
if h[x_new] > h[x]:
x = x_new # the new position is higher, go there
elif (rand <= prob):
x = x_new
else:
pass