Skip to content

Instantly share code, notes, and snippets.

@jasonmc
Created August 19, 2011 19:23
Show Gist options
  • Save jasonmc/1157751 to your computer and use it in GitHub Desktop.
Save jasonmc/1157751 to your computer and use it in GitHub Desktop.
Simulated annealing calculator
# Licensed under CRAPL - see http://matt.might.net/articles/crapl/CRAPL-LICENSE.txt
from math import exp
import matplotlib.pyplot as plt
def doplot(r,filename=None,lw=0.1,fancy=False):
fig = plt.figure()
ax = fig.add_subplot(111)
l = ax.plot(r)
plt.setp(l, color='k', linewidth=lw,aa=True)
plt.xlabel('Number of iterations')
plt.ylabel('Execution time (cost)')
plt.show()
badDelta = -0.06
def getSchedule(cooling,limit):
tempt = 1
for _ in range(limit):
tempt = tempt*cooling
yield tempt
merit = lambda delta,k,temp: exp(delta /(k*temp))
def old():
#print [merit(-0.06,1,x) for x in getSchedule(0.5,15)][6]
delta = -0.06
delta = -0.01
# k = 1
# cf = 0.6
# k = 0.1
# cf = 0.8
k = 1
cf = 0.6
k = 0.3
cf = 0.7
# cooling_factor = cf
# merits = [merit(delta,k,x) for x in getSchedule(cooling_factor,15)]
#print merits[8]
# for x in merits:
# print x
# from plot_progress import doplot
# doplot(merits,lw=0.5)
if __name__ == "__main__":
import sys
delta = float(sys.argv[1])
steps = int(sys.argv[2])
k = float(sys.argv[3])
cooling_factor = float(sys.argv[4])
merits = [merit(delta,k,x) for x in getSchedule(cooling_factor,steps)]
from plot_progress import doplot
doplot(merits,lw=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment