Finding the Global Minimum of a Function 
using Simulated Annealing
Author's Data: Housam Binous
Department of Chemical Engineering
National Institute of Applied Sciences and Technology
Tunis, TUNISIA
Email: binoushousam@yahoo.com

Acknowledgement :

This example was inspired from the Matlab program available at the following MIT OpenCourseWare web page:

http://ocw.mit.edu/OcwWeb/Chemical-Engineering/10-34Fall-2005/CourseHome/index.htm

Off[FindMinimum::"lstol"]

Off[General::"spell1"]

Plotting f(t) shows that this function has many local minima.

f[x_] := 
      0.5 x^2 + Cos[Pi x] - 2  Sin[2 Pi x] + 
        Cos[3 Pi x] Sin[Pi x]

plt1 = 
      Plot[f[x], {x, -4, 4}, PlotStyle ->  RGBColor[1, 0, 0], 
        Frame ->  True]

Using built-in functions of Mathematica, one finds several solutions and is not sure if he has found the global minimum which is obtained for x=-0.711232 and f(x)=-3.0233.

Minimize[f[x], x]

NMinimize[f[x], {x, -4, 4}]

NMinimize[f[x], {x, -2, 2}]

FindMinimum[f[x], {x, -2, 2}]

Simulated annealing gives the global minimum xbest and fbest=f(xbest).

X = 0

fbest = f[X]

M = 0.5;

kT0 = 100;

For[k = 1, 
      k < 11, {x = (Random[] - 0.5)*8, 
        F = f[x], 
For[j = 1, 
          j < 10^4, {kT = 
              kT0*(10^4 + 1 - j)/10^4, 
newx = 
              x + (Random[] - 0.5)*M, 
            newf = f[newx], 
p = 
              Min[{1, Exp[-(newf - F)/kT]}], 
            u = Random[], 
If[
              p ≥ u, {x = newx, F = newf}], j++}], 
        sol = FindMinimum[f[t], {t, x}], F = sol[[1]], 
        x = t /. sol[[2]], 
        If[F < fbest, {xbest = x, fbest = F}], k++}]

fbest

xbest

Mathematica built-in functions have found several local minima.
The global minimum found using simulated annealing is the dark blue dot.

Show[plt1, 
      Graphics[{RGBColor[0, 0, 1], PointSize[0.025], Point[{xbest, fbest}], 
          RGBColor[0, 1, 1], PointSize[0.025], 
          Point[{0.31758853903658163, -2.0607196324911374}], 
          Point[{1.2738706370162356, -2.4604469522330414}], 
          Point[{-1.6688381950990252, -0.7095400263875662}]}]]