<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python

#-----------------------------------------------------------------------------
# Jonathan R. Senning &lt;jonathan.senning@gordon.edu&gt;
# Gordon College
# April 20, 2005
# Revised April 27, 2006 to update graphing commands
# Revised November 26, 2008 to update commands to save PostScript file
# Converted to Python November 26, 2008
#
# $Id: simanneal-demo,v 1.5 2007/03/26 20:02:12 senning Exp senning $
#
# Demonstrate how simulated annealing is used to find the global minimum of a
# function with multiple local minima.  The function simanneal() was written
# sometime in the past; unfortunately I don't remember where I got the
# algorithm from :(.
#-----------------------------------------------------------------------------

from pylab import *
from simanneal import simanneal

# Define interval and function

a = 0
b = 5
cooling_factor = 0.8

def f(x):
    return x * exp(-0.5*x) * sin(55*x) * cos(12*x)

# Perform simulated annealing.  The last parameter may be changed to a
# nonzero value to print out intermediate results while searching for the
# minimum.

x_min, fx_min = simanneal( f, a, b, cooling_factor, verbose=True )
print "f(%f) = %f" % ( x_min, fx_min )

# Display graph of results.  The graph of the function f(x) is shown on the
# interval [a,b] and the minimum point is plotted as a single point.

x = linspace( a, b, 2001 )
y = f( x )

plot( x, y, 'b-', [x_min], [fx_min], 'ro' )
xlabel( 'x' )
ylabel( 'y' )
title( 'Minimization of Continuous Function by Simulated Annealing' )
legend( ( 'Function', 'Minimum' ), loc='upper right' )
show()

# End of File
</pre></body></html>