Published by Dimitris Kougioumtzis · Updated
Explore the quarter-unit grid of a Rastrigin landscape. Accept improvements and probabilistically accept uphill proposals using exp(-delta/temperature).
Propose a neighboring quarter-grid point, accept downhill or equal moves, and accept uphill moves only when a seeded uniform draw falls below exp(-delta/temperature). Cool geometrically and retain the best state encountered.
When to use Seeded simulated annealing on a Rastrigin grid
Teaching stochastic exploration, uphill acceptance and reproducible cooling on a nonconvex Rastrigin landscape.
Choosing an alternative
Gradient descent follows a local derivative and can remain in a basin. Annealing can escape through uphill moves but a short fixed schedule does not guarantee the global optimum. This example is a bounded grid search, not a continuous optimizer.
Worked example: input and state changes
Example input
6,6,1,40,4,0.95
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Seed 1; initial objective 44.5.
State at this checkpoint
{ "values": [ 0, 1, 2 ], "active": [ 2 ], "kind": "graph", "points": [ [ -2, -2 ], [ 2, 2 ], [ 1.5, 1.5 ] ], "nodeLabels": [ "", "", "(1.5, 1.5); f=44.5" ], "pointMarkerIndices": [ 2, 2 ], "pointLabelIndices": [ 2 ], "directed": true, "chosen": [], "visited": [ 2 ], "result": 712, "output": [], "surface": { "xs": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "ys": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "heights": [ 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 4, 13.0625, 22.25, 11.5625, 1, 10.5625, 20.25, 10.0625, 0, 10.0625, 20.25, 10.5625, 1, 11.5625, 22.25, 13.0625, 4, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8 ], "label": "Rastrigin: 20+x²+y²−10cos(2πx)−10cos(2πy)", "pathIndices": [ 2 ], "pathHeights": [ 44.5 ] } }Step 10: Step 10: proposal (1, 1), delta -10.5625, T=2.5209976388984363; accepted.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6 ], "active": [ 6 ], "kind": "graph", "points": [ [ -2, -2 ], [ 2, 2 ], [ 1.5, 1.5 ], [ 1.5, 1.25 ], [ 1.25, 1.25 ], [ 1, 1.25 ], [ 1, 1 ] ], "nodeLabels": [ "", "", "(1.5, 1.5); f=44.5", "(1.5, 1.25); f=33.8125", "(1.25, 1.25); f=23.125", "(1, 1.25); f=12.5625", "(1, 1); f=2" ], "pointMarkerIndices": [ 2, 6 ], "pointLabelIndices": [ 6 ], "directed": true, "chosen": [ 0, 1, 2, 3 ], "visited": [ 2, 3, 4, 5, 6 ], "result": 32, "output": [], "surface": { "xs": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "ys": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "heights": [ 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 4, 13.0625, 22.25, 11.5625, 1, 10.5625, 20.25, 10.0625, 0, 10.0625, 20.25, 10.5625, 1, 11.5625, 22.25, 13.0625, 4, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8 ], "label": "Rastrigin: 20+x²+y²−10cos(2πx)−10cos(2πy)", "pathIndices": [ 2, 3, 4, 5, 6 ], "pathHeights": [ 44.5, 33.8125, 23.125, 12.5625, 2 ] } }Step 21: Step 21: proposal (0.75, 1), delta 9.5625, T=1.433943689634167; u=0.6326385678875439, acceptance probability 0.0012700844209907601; rejected.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "active": [ 8 ], "kind": "graph", "points": [ [ -2, -2 ], [ 2, 2 ], [ 1.5, 1.5 ], [ 1.5, 1.25 ], [ 1.25, 1.25 ], [ 1, 1.25 ], [ 1, 1 ], [ 1, 0.75 ], [ 1, 1 ] ], "nodeLabels": [ "", "", "(1.5, 1.5); f=44.5", "(1.5, 1.25); f=33.8125", "(1.25, 1.25); f=23.125", "(1, 1.25); f=12.5625", "(1, 1); f=2", "(1, 0.75); f=11.5625", "(1, 1); f=2" ], "pointMarkerIndices": [ 2, 8 ], "pointLabelIndices": [ 8 ], "directed": true, "chosen": [ 0, 1, 2, 3, 4, 5 ], "visited": [ 2, 3, 4, 5, 6, 7, 8 ], "result": 32, "output": [], "surface": { "xs": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "ys": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "heights": [ 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 4, 13.0625, 22.25, 11.5625, 1, 10.5625, 20.25, 10.0625, 0, 10.0625, 20.25, 10.5625, 1, 11.5625, 22.25, 13.0625, 4, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8 ], "label": "Rastrigin: 20+x²+y²−10cos(2πx)−10cos(2πy)", "pathIndices": [ 2, 3, 4, 5, 6, 7, 8 ], "pathHeights": [ 44.5, 33.8125, 23.125, 12.5625, 2, 11.5625, 2 ] } }Step 31: Step 31: proposal (1.25, 1), delta 10.5625, T=0.8585550557717488; u=0.48651738301223024, acceptance probability 0.000004539710770777862; rejected.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "active": [ 8 ], "kind": "graph", "points": [ [ -2, -2 ], [ 2, 2 ], [ 1.5, 1.5 ], [ 1.5, 1.25 ], [ 1.25, 1.25 ], [ 1, 1.25 ], [ 1, 1 ], [ 1, 0.75 ], [ 1, 1 ] ], "nodeLabels": [ "", "", "(1.5, 1.5); f=44.5", "(1.5, 1.25); f=33.8125", "(1.25, 1.25); f=23.125", "(1, 1.25); f=12.5625", "(1, 1); f=2", "(1, 0.75); f=11.5625", "(1, 1); f=2" ], "pointMarkerIndices": [ 2, 8 ], "pointLabelIndices": [ 8 ], "directed": true, "chosen": [ 0, 1, 2, 3, 4, 5 ], "visited": [ 2, 3, 4, 5, 6, 7, 8 ], "result": 32, "output": [], "surface": { "xs": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "ys": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "heights": [ 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 4, 13.0625, 22.25, 11.5625, 1, 10.5625, 20.25, 10.0625, 0, 10.0625, 20.25, 10.5625, 1, 11.5625, 22.25, 13.0625, 4, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8 ], "label": "Rastrigin: 20+x²+y²−10cos(2πx)−10cos(2πy)", "pathIndices": [ 2, 3, 4, 5, 6, 7, 8 ], "pathHeights": [ 44.5, 33.8125, 23.125, 12.5625, 2, 11.5625, 2 ] } }Step 41: Budget complete: best f=2 at (1, 1); 6 proposals accepted. No global-optimum guarantee.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "active": [ 8 ], "kind": "graph", "points": [ [ -2, -2 ], [ 2, 2 ], [ 1.5, 1.5 ], [ 1.5, 1.25 ], [ 1.25, 1.25 ], [ 1, 1.25 ], [ 1, 1 ], [ 1, 0.75 ], [ 1, 1 ] ], "nodeLabels": [ "", "", "(1.5, 1.5); f=44.5", "(1.5, 1.25); f=33.8125", "(1.25, 1.25); f=23.125", "(1, 1.25); f=12.5625", "(1, 1); f=2", "(1, 0.75); f=11.5625", "(1, 1); f=2" ], "pointMarkerIndices": [ 2, 8 ], "pointLabelIndices": [ 8 ], "directed": true, "chosen": [ 0, 1, 2, 3, 4, 5 ], "visited": [ 2, 3, 4, 5, 6, 7, 8 ], "result": 32, "output": [ 1, 1, 2, 1, 1, 2, 0.5140486262604124, 6, 1137623865 ], "surface": { "xs": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "ys": [ -2, -1.75, -1.5, -1.25, -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2 ], "heights": [ 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 4, 13.0625, 22.25, 11.5625, 1, 10.5625, 20.25, 10.0625, 0, 10.0625, 20.25, 10.5625, 1, 11.5625, 22.25, 13.0625, 4, 14.0625, 23.125, 32.3125, 21.625, 11.0625, 20.625, 30.3125, 20.125, 10.0625, 20.125, 30.3125, 20.625, 11.0625, 21.625, 32.3125, 23.125, 14.0625, 24.25, 33.3125, 42.5, 31.8125, 21.25, 30.8125, 40.5, 30.3125, 20.25, 30.3125, 40.5, 30.8125, 21.25, 31.8125, 42.5, 33.3125, 24.25, 14.5625, 23.625, 32.8125, 22.125, 11.5625, 21.125, 30.8125, 20.625, 10.5625, 20.625, 30.8125, 21.125, 11.5625, 22.125, 32.8125, 23.625, 14.5625, 5, 14.0625, 23.25, 12.5625, 2, 11.5625, 21.25, 11.0625, 1, 11.0625, 21.25, 11.5625, 2, 12.5625, 23.25, 14.0625, 5, 15.5625, 24.625, 33.8125, 23.125, 12.5625, 22.125, 31.8125, 21.625, 11.5625, 21.625, 31.8125, 22.125, 12.5625, 23.125, 33.8125, 24.625, 15.5625, 26.25, 35.3125, 44.5, 33.8125, 23.25, 32.8125, 42.5, 32.3125, 22.25, 32.3125, 42.5, 32.8125, 23.25, 33.8125, 44.5, 35.3125, 26.25, 17.0625, 26.125, 35.3125, 24.625, 14.0625, 23.625, 33.3125, 23.125, 13.0625, 23.125, 33.3125, 23.625, 14.0625, 24.625, 35.3125, 26.125, 17.0625, 8, 17.0625, 26.25, 15.5625, 5, 14.5625, 24.25, 14.0625, 4, 14.0625, 24.25, 14.5625, 5, 15.5625, 26.25, 17.0625, 8 ], "label": "Rastrigin: 20+x²+y²−10cos(2πx)−10cos(2πy)", "pathIndices": [ 2, 3, 4, 5, 6, 7, 8 ], "pathHeights": [ 44.5, 33.8125, 23.125, 12.5625, 2, 11.5625, 2 ] } }
Expected final state
Budget complete: best f=2 at (1, 1); 6 proposals accepted. No global-optimum guarantee.
Complexity and input bounds
Family: Optimization. Time complexity: O(I).
Implementation detail: Grid coordinates -8..8 represent physical coordinates divided by four. The objective is 20+X^2+Y^2-10cos(2*pi*X)-10cos(2*pi*Y), evaluated exactly on quarter-grid points as integer E=16*f. Seed 1..2147483646, steps 1..64, temperature .1..20, cooling .5..0.99. Four-direction proposals are rejection-sampled and clamped; only uphill moves draw for acceptance. Downloads return best E and output [bestX,bestY,bestF,lastX,lastY,lastF,finalTemperature,acceptedCount,finalState]. Invalid parameters return -1 without writes. Independent BigInt PRNG fixtures cover rejected draws and both acceptance outcomes across all nine languages.
Space complexity: O(1) optimizer state excluding recorded trajectory and terrain samples.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
gridX,gridY,seed,steps,temperature,cooling. Grid coordinates -8..8 represent x/4,y/4. Seed 1..2147483646, steps 1..64, temperature .1..20, cooling .5..0.99. No optimality/convergence guarantee.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Use physical objective differences when computing acceptance probability. Keep best-so-far distinct from the last accepted state. Specify whether rejected proposals consume random draws, how boundaries work, and how equal objective values break ties.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 | // Seeded Metropolis annealing on quarter-unit Rastrigin grid: physical point (x/4,y/4), integer x/y -8..8. Objective f=20+X^2+Y^2-10cos(2*pi*X)-10cos(2*pi*Y); exact integer energy E=16*f uses quarter-grid cosine values. Seed 1..2147483646, steps 1..64, temperature .1..20, cooling .5..0.99. Park-Miller rejection-sampled four-direction neighbor, clamped boundaries; uphill acceptance u<exp(-(Enew-E)/(16*T)), with u=state/2147483647. Downhill/equal proposals consume no acceptance draw; strict best-energy improvements retain first ties. Return best E (divide by 16 for f), or -1 invalid parameters without writes. Output nine doubles [bestX,bestY,bestF,lastX,lastY,lastF,finalTemperature,acceptedCount,finalState]. Grid-coordinate proposals and energies are exact integers; temperature/probability use doubles. O(I) work, O(1) state. Fixed budget/cooling is not an optimum or convergence guarantee. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(x, y, seed, steps, temperature, cooling, output) { if (x < -8 || x > 8 || y < -8 || y > 8 || seed < 1 || seed > 2147483646 || steps < 1 || steps > 64 || temperature < 0.1 || temperature > 20.0 || cooling < 0.5 || cooling > 0.99) { return -1; } let state = seed; let high = 0; let low = 0; let draw = 0; let direction = 0; let candidateX = 0; let candidateY = 0; let current = 0; let candidate = 0; let best = 0; let bestX = x; let bestY = y; let accepted = 0; let difference = 0; let accept = 0; let absX = 0; let absY = 0; let cosX = 0; let cosY = 0; let probability = 0.0; let uniform = 0.0; absX = x; if (absX < 0) { absX = -absX; } absY = y; if (absY < 0) { absY = -absY; } cosX = 0; cosY = 0; if (absX%4 == 0) { cosX = 1; } if (absX%4 == 2) { cosX = -1; } if (absY%4 == 0) { cosY = 1; } if (absY%4 == 2) { cosY = -1; } current = 320+x*x+y*y-160*cosX-160*cosY; best = current; for (let step = 0; step < steps; step++) { draw = 2147483644; while (draw >= 2147483644) { high = Math.floor(state / 127773); low = state-high*127773; state = 16807*low-2836*high; if (state <= 0) { state += 2147483647; } draw = state-1; } direction = draw%4; candidateX = x; candidateY = y; if (direction == 0) { candidateX--; } if (direction == 1) { candidateX++; } if (direction == 2) { candidateY--; } if (direction == 3) { candidateY++; } if (candidateX < -8) { candidateX = -8; } if (candidateX > 8) { candidateX = 8; } if (candidateY < -8) { candidateY = -8; } if (candidateY > 8) { candidateY = 8; } absX = candidateX; if (absX < 0) { absX = -absX; } absY = candidateY; if (absY < 0) { absY = -absY; } cosX = 0; cosY = 0; if (absX%4 == 0) { cosX = 1; } if (absX%4 == 2) { cosX = -1; } if (absY%4 == 0) { cosY = 1; } if (absY%4 == 2) { cosY = -1; } candidate = 320+candidateX*candidateX+candidateY*candidateY-160*cosX-160*cosY; difference = candidate-current; accept = 0; if (difference <= 0) { accept = 1; } else { high = Math.floor(state / 127773); low = state-high*127773; state = 16807*low-2836*high; if (state <= 0) { state += 2147483647; } uniform = state/2147483647.0; probability = Math.exp(-difference/(16.0*temperature)); if (uniform < probability) { accept = 1; } } if (accept != 0) { x = candidateX; y = candidateY; current = candidate; accepted++; if (current < best) { best = current; bestX = x; bestY = y; } } temperature *= cooling; } output[0] = bestX/4.0; output[1] = bestY/4.0; output[2] = best/16.0; output[3] = x/4.0; output[4] = y/4.0; output[5] = current/16.0; output[6] = temperature; output[7] = accepted; output[8] = state; return best; } |
Source
Primary sources for simulated annealing and the Metropolis acceptance rule, with Park-Miller for reproducibility. The bounded quarter-unit Rastrigin example, clamped proposal, fixed geometric cooling and tie conventions are playground choices, not an exact reproduction of either paper experiment.
- Optimization by Simulated Annealing
S. Kirkpatrick; C. D. Gelatt; M. P. Vecchi (1983). Research paper. - Equation of State Calculations by Fast Computing Machines
Nicholas Metropolis; Arianna W. Rosenbluth; Marshall N. Rosenbluth; Augusta H. Teller; Edward Teller (1953). Research paper. - Random Number Generators: Good Ones Are Hard to Find
Stephen K. Park; Keith W. Miller (1988). Primary research paper.