1simulation::annealing(n) Tcl Simulation Tools simulation::annealing(n)
2
3
4
5______________________________________________________________________________
6
8 simulation::annealing - Simulated annealing
9
11 package require Tcl ?8.4?
12
13 package require simulation::annealing 0.2
14
15 ::simulation::annealing::getOption keyword
16
17 ::simulation::annealing::hasOption keyword
18
19 ::simulation::annealing::setOption keyword value
20
21 ::simulation::annealing::findMinimum args
22
23 ::simulation::annealing::findCombinatorialMinimum args
24
25_________________________________________________________________
26
28 The technique of simulated annealing provides methods to estimate the
29 global optimum of a function. It is described in some detail on the
30 Wiki http://wiki.tcl.tk/.... The idea is simple:
31
32 · randomly select points within a given search space
33
34 · evaluate the function to be optimised for each of these points
35 and select the point that has the lowest (or highest) function
36 value or - sometimes - accept a point that has a less optimal
37 value. The chance by which such a non-optimal point is accepted
38 diminishes over time.
39
40 · Accepting less optimal points means the method does not neces‐
41 sarily get stuck in a local optimum and theoretically it is
42 capable of finding the global optimum within the search space.
43
44 The method resembles the cooling of material, hence the name.
45
46 The package simulation::annealing offers the command findMinimum:
47
48 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)}]
49
50 prints the estimated minimum value of the function f(x,y) =
51 x**2+y**2+sin(10*x)+4*cos(20*y) and the values of x and y where the
52 minimum was attained:
53
54 result -4.9112922923 x -0.181647676593 y 0.155743646974
55
56
58 The package defines the following auxiliary procedures:
59
60 ::simulation::annealing::getOption keyword
61 Get the value of an option given as part of the findMinimum com‐
62 mand.
63
64 string keyword
65 Given keyword (without leading minus)
66
67
68 ::simulation::annealing::hasOption keyword
69 Returns 1 if the option is available, 0 if not.
70
71 string keyword
72 Given keyword (without leading minus)
73
74
75 ::simulation::annealing::setOption keyword value
76 Set the value of the given option.
77
78 string keyword
79 Given keyword (without leading minus)
80
81 string value
82 (New) value for the option
83
84 The main procedures are findMinimum and findCombinatorialMinimum:
85
86 ::simulation::annealing::findMinimum args
87 Find the minimum of a function using simulated annealing. The
88 function and the method's parameters is given via a list of key‐
89 word-value pairs.
90
91 int n List of keyword-value pairs, all of which are available
92 during the execution via the getOption command.
93
94 ::simulation::annealing::findCombinatorialMinimum args
95 Find the minimum of a function of discrete variables using simu‐
96 lated annealing. The function and the method's parameters is
97 given via a list of keyword-value pairs.
98
99 int n List of keyword-value pairs, all of which are available
100 during the execution via the getOption command.
101
102 The findMinimum command predefines the following options:
103
104 · -parameters list: triples defining parameters and ranges
105
106 · -function expr: expression defining the function
107
108 · -code body: body of code to define the function (takes prece‐
109 dence over -function). The code should set the variable "result"
110
111 · -init code: code to be run at start up -final code: code to be
112 run at the end -trials n: number of trials before reducing the
113 temperature -reduce factor: reduce the temperature by this fac‐
114 tor (between 0 and 1) -initial-temp t: initial temperature
115 -scale s: scale of the function (order of magnitude of the val‐
116 ues) -estimate-scale y/n: estimate the scale (only if -scale is
117 not present) -verbose y/n: print detailed information on
118 progress to the report file (1) or not (0) -reportfile file:
119 opened file to print to (defaults to stdout)
120
121 Any other options can be used via the getOption procedure in the body.
122 The findCombinatorialMinimum command predefines the following options:
123
124 · -number-params n: number of binary parameters (the solution
125 space consists of lists of 1s and 0s). This is a required
126 option.
127
128 · -initial-values: list of 1s and 0s constituting the start of the
129 search.
130
131 The other predefined options are identical to those of findMinimum.
132
134 The procedure findMinimum works by constructing a temporary procedure
135 that does the actual work. It loops until the point representing the
136 estimated optimum does not change anymore within the given number of
137 trials. As the temperature gets lower and lower the chance of accepting
138 a point with a higher value becomes lower too, so the procedure will in
139 practice terminate.
140
141 It is possible to optimise over a non-rectangular region, but some care
142 must be taken:
143
144 · If the point is outside the region of interest, you can specify
145 a very high value.
146
147 · This does mean that the automatic determination of a scale fac‐
148 tor is out of the question - the high function values that force
149 the point inside the region would distort the estimation.
150
151 Here is an example of finding an optimum inside a circle:
152
153 puts [::simulation::annealing::findMinimum -trials 3000 -reduce 0.98 -parameters {x -5.0 5.0 y -5.0 5.0} -code {
154 if { hypot($x-5.0,$y-5.0) < 4.0 } {
155 set result [expr {$x*$x+$y*$y+sin(10.0*$x)+4.0*cos(20.0*$y)}]
156 } else {
157 set result 1.0e100
158 }
159 }]
160
161 The method is theoretically capable of determining the global optimum,
162 but often you need to use a large number of trials and a slow reduction
163 of temperature to get reliable and repeatable estimates.
164
165 You can use the -final option to use a deterministic optimization
166 method, once you are sure you are near the required optimum.
167
168 The findCombinatorialMinimum procedure is suited for situations where
169 the parameters have the values 0 or 1 (and there can be many of them).
170 Here is an example:
171
172 · We have a function that attains an absolute minimum if the first
173 ten numbers are 1 and the rest is 0:
174
175 proc cost {params} {
176 set cost 0
177 foreach p [lrange $params 0 9] {
178 if { $p == 0 } {
179 incr cost
180 }
181 }
182 foreach p [lrange $params 10 end] {
183 if { $p == 1 } {
184 incr cost
185 }
186 }
187 return $cost
188 }
189
190
191 · We want to find the solution that gives this minimum for various
192 lengths of the solution vector params:
193
194 foreach n {100 1000 10000} {
195 break
196 puts "Problem size: $n"
197 puts [::simulation::annealing::findCombinatorialMinimum -trials 300 -verbose 0 -number-params $n -code {set result [cost $params]}]
198 }
199
200
201 · As the vector grows, the computation time increases, but the
202 procedure will stop if some kind of equilibrium is reached. To
203 achieve a useful solution you may want to try different values
204 of the trials parameter for instance. Also ensure that the func‐
205 tion to be minimized depends on all or most parameters - see the
206 source code for a counter example and run that.
207
209 math, optimization, simulated annealing
210
212 Copyright (c) 2008 Arjen Markus <arjenmarkus@users.sourceforge.net>
213
214
215
216
217simulation 0.2 simulation::annealing(n)