Funny error message in Exercise 5: Warm-up Temperature (advanced)
Hi
I try to solve the advanced level of excercise 5. It says “Modify the accept_prob function so that it returns the probability of accepting the new state using simulated annealing.”
But when submitting the code, it seems that the solution should be a 0/1 answer:
“Test Failed: AssertionError: 2.7182818284590455 != 1.0 : The algorithm should greedily select S_new if it is greater that S_old.”
But you where supposed to alter the code from greedy to simulated annealing, right?
This is my code:
import random
import math
def accept_prob(S_old, S_new, T):
# this is the acceptance "probability" in the greedy hill-climbing method
# where new solutions are accepted if and only if they are better
# than the old one.
# change it to be the acceptance probability in simulated annealing
"""if S_new > S_old:
return 1.0
else:
return 0.0"""
return math.exp(-(S_old - S_new)/T)
# the above function will be used as follows. this is shown just for
# your information; you don't have to change anything here
def accept(S_old, S_new, T):
n = accept_prob(S_old, S_new, T)
if random.random() < n:
print(True)
else:
print(False)
return n
print(accept(150, 140, 10))And this is the output when run:
True
0.36787944117144233
Quite OK, if you ask me.