Spectrum is now read-only. Learn more about the decision in our official announcement.


Elements of AI is a two part online course about AI and this community is for the participants of the course to discuss. Please join our new community at


Wrong answer, even though it works? Exercise 6: Simulated Annealing

October 21, 2020 at 2:39pm

Wrong answer, even though it works? Exercise 6: Simulated Annealing

October 21, 2020 at 2:39pm
I got the solution for the advanced level. It seemed to work, it also passed the test (after 70 seconds). However when I submitted my answer I instantly got the feedback that my answer was wrong. Why's that? My code:
(Also I first used a global variable T, which somehow messed up testing. I always got AssertionErrors that 0.4 wasn't greater then 30. Using T only locally fixed that. I'm sure something can be done about that, because that cost me quite some time to figure that out and get my code to pass the test)

October 21, 2020 at 3:11pm
Sorry, meant to post this in #buildingAI

October 31, 2020 at 4:51pm
Hey. A bit late, but in case you haven't sorted this yet: I just had a look at your answer and sometimes the result falls below the minimum required 30. I used the suggested cooling schedule for T, i.e.: T = max(0, ((steps - step)/steps)**3-.005), and created a variable to compare the result of simulated annealing against (instead of random.random() ). This way I could tweak the comparison to the value that gave me the more successful results.

November 3, 2020 at 9:00am
Hi, yeah, I already got it to work, but still thank you! :)

July 7, 2021 at 4:08am
Hello Finn,
I am currently doing the course, and I am a little bit stuck in Ex. 6 Advanced.
I read your Github notebook about Ex.6 Ad., and my code is similar, except for this line that you have in this way:
if S_new > S_old or (T > 0 and random.random() < math.exp((S_new - S_old)/T)):
I am sure that if I do it like your code, it'll work, but I wanna learn and I can't understand why the probability of acceptance the new solution is compared to random.random().
I am doing it like this,
if S_new > S_old or (T > 0 and S_new < math.exp((S_new - S_old)/T)):
Best regards,

July 14, 2021 at 12:22pm
Exp function gives you the probability of accepting the solution. It does not give you a new point, so you shouldn't compare it to S_new.