simulation::annealing(n)
π NAME π
simulation::annealing β simulated annealing, bestie. yes, the cooling-metal one. it's giving β¨global optimizationβ¨.
π¦ SYNOPSIS π¦
package require Tcl ?8.5 9?
package require simulation::annealing 0.3
::simulation::annealing::getOption keyword
::simulation::annealing::hasOption keyword
::simulation::annealing::setOption keyword value
::simulation::annealing::findMinimum args
::simulation::annealing::findCombinatorialMinimum args
β¨ DESCRIPTION β¨
okay so simulated annealing is the technique that helps you estimate the global optimum of a function. it's described in some detail over on the Wiki at http://wiki.tcl.tk/.... if you wanna go deep. but the idea? lowkey simple, fr fr:
- randomly select points within a given search space
- evaluate the function you're optimising at each of those points and grab the one with the lowest (or highest) function value β or, sometimes, just accept a point that has a less optimal value. the chance that a non-optimal point gets accepted diminishes over time. π«Ά
- and the way accepting less optimal points saves you? it means the method doesn't necessarily get stuck in a local optimum, and theoretically it is capable of finding the global optimum within the search space. π
the method resembles the cooling of material, hence the name. that's the whole vibe check. π‘οΈ
the package simulation::annealing offers the command findMinimum:
puts [::simulation::annealing::findMinimum -trials 300 -parameters {x -5.0 5.0 y -5.0 5.0} -function {$x*$x+$y*$y+sin(10.0*$x)+4.0*cos(20.0*$y)}]
this prints the estimated minimum value of the function f(x,y) = x**2+y**2+sin(10*x)+4*cos(20*y), plus the values of x and y where that minimum was attained:
result -4.9112922923 x -0.181647676593 y 0.155743646974
ate. π
π PROCEDURES π
the package defines the following auxiliary procedures:
::simulation::annealing::getOption keywordβ get the value of an option given as part of thefindMinimumcommand. π- string
keywordβ given keyword (without leading minus)
- string
::simulation::annealing::hasOption keywordβ returns1if the option is available,0if not. no cap.- string
keywordβ given keyword (without leading minus)
- string
::simulation::annealing::setOption keyword valueβ set the value of the given option. slay.- string
keywordβ given keyword (without leading minus) - string
valueβ (new) value for the option
- string
the main procedures are findMinimum and findCombinatorialMinimum:
::simulation::annealing::findMinimum argsβ find the minimum of a function using simulated annealing. the function and the method's parameters is given via a list of keyword-value pairs.- int
nβ list of keyword-value pairs, all of which are available during the execution via thegetOptioncommand.
- int
::simulation::annealing::findCombinatorialMinimum argsβ find the minimum of a function of discrete variables using simulated annealing. the function and the method's parameters is given via a list of keyword-value pairs.- int
nβ list of keyword-value pairs, all of which are available during the execution via thegetOptioncommand.
- int
πΈ options that findMinimum predefines πΈ
-parameterslist β triples defining parameters and ranges-functionexpr β expression defining the function-codebody β body of code to define the function (takes precedence over-function). the code should set the variableresult-initcode β code to be run at start up-finalcode β code to be run at the end-trialsn β number of trials before reducing the temperature-reducefactor β reduce the temperature by this factor (between 0 and 1)-initial-tempt β initial temperature-scales β scale of the function (order of magnitude of the values)-estimate-scaley/n β estimate the scale (only if-scaleis not present)-verbosey/n β print detailed information on progress to the report file (1) or not (0)-reportfilefile β opened file to print to (defaults tostdout)
any other options can be used via the getOption procedure in the body. β¨
π options that findCombinatorialMinimum predefines π
-number-paramsn β number of binary parameters (the solution space consists of lists of1s and0s). this is a required option.-initial-valuesβ list of1s and0s constituting the start of the search.
the other predefined options are identical to those of findMinimum. π
π₯ TIPS π₯
the procedure findMinimum works by constructing a temporary procedure that does the actual work. it loops until the point representing the estimated optimum does not change anymore within the given number of trials. as the temperature gets lower and lower the chance of accepting a point with a higher value becomes lower too, so the procedure will in practice terminate. it's giving patience. π§
it is possible to optimise over a non-rectangular region, but some care must be taken:
- if the point is outside the region of interest, you can specify a very high value.
- this does mean that the automatic determination of a scale factor is out of the question β the high function values that force the point inside the region would distort the estimation. π
here is an example of finding an optimum inside a circle:
puts [::simulation::annealing::findMinimum -trials 3000 -reduce 0.98 -parameters {x -5.0 5.0 y -5.0 5.0} -code {
if { hypot($x-5.0,$y-5.0) < 4.0 } {
set result [expr {$x*$x+$y*$y+sin(10.0*$x)+4.0*cos(20.0*$y)}]
} else {
set result 1.0e100
}
}]
the method is theoretically capable of determining the global optimum, but often you need to use a large number of trials and a slow reduction of temperature to get reliable and repeatable estimates. bestie, be patient with it. π«Ά
you can use the -final option to use a deterministic optimization method, once you are sure you are near the required optimum. π
the findCombinatorialMinimum procedure is suited for situations where the parameters have the values 0 or 1 (and there can be many of them). here is an example:
- we have a function that attains an absolute minimum if the first ten numbers are
1and the rest is0:
proc cost {params} {
set cost 0
foreach p [lrange $params 0 9] {
if { $p == 0 } {
incr cost
}
}
foreach p [lrange $params 10 end] {
if { $p == 1 } {
incr cost
}
}
return $cost
}
- we want to find the solution that gives this minimum for various lengths of the solution vector
params:
foreach n {100 1000 10000} {
break
puts "Problem size: $n"
puts [::simulation::annealing::findCombinatorialMinimum -trials 300 -verbose 0 -number-params $n -code {set result [cost $params]}]
}
- as the vector grows, the computation time increases, but the procedure will stop if some kind of equilibrium is reached. to achieve a useful solution you may want to try different values of the trials parameter for instance. also ensure that the function to be minimized depends on all or most parameters β see the source code for a counter example and run that. π
β KEYWORDS β
math, optimization, simulated annealing
π CATEGORY π
Mathematics
π COPYRIGHT π
Copyright (c) 2008 Arjen Markus <arjenmarkus@users.sourceforge.net>
okay that's the whole page, bestie β brought to you by tcllib, version 0.3, filed under simulation::annealing(n). go optimise something iconic. π
β¨π¦
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2008 Arjen Markus <arjenmarkus@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms