Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def anneal(start_temp=1.0, n=10000, advent=None, scurve_k=10.0, scurve_bias=0.3):
- import math
- temp = start_temp
- cooling_rate = (start_temp / float(n))
- if advent is None:
- advent = randcal()
- else:
- advent = advent.copy()
- bestscore = current_score = score(advent)
- best = advent.copy()
- count = 0
- step = 1.0 / n
- try:
- while count < n:
- i = np.random.randint(0,24)
- j = np.random.randint(0,24)
- advent[i/6,i%6], advent[j/6,j%6] = advent[j/6,j%6], advent[i/6,i%6]
- newscore = score(advent)
- if newscore < current_score or np.random.rand() < temp:
- current_score = newscore
- else:
- advent[i/6,i%6], advent[j/6,j%6] = advent[j/6,j%6], advent[i/6,i%6]
- # Cool temperature along s-curve (needs a more robust sigmoid func!)
- # Seems to perform better than a linear decrease.
- sigmoid = 1 - 1.0 / (
- 1 + math.e ** (
- -scurve_k * (step * count - scurve_bias)
- )
- )
- temp = start_temp * sigmoid
- if current_score < bestscore:
- best = advent.copy()
- bestscore = current_score
- count += 1
- if count % 100 == 0:
- print count, temp, bestscore, current_score
- except:
- print 'Error. Best so far was:', (best, bestscore)
- raise
- return (best, bestscore)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement