File: probopt_sce.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (378 lines) | stat: -rw-r--r-- 10,956 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# probopt_sce.tcl --
#     Implementation of the "shuffled complexes evolution" optimisation method in Tcl
#
#     Note:
#     This implementation is based on:
#     - Qingyuan Duan, Soroosh Sorooshian and Vijai Gupta:
#       Optimal use of the SCE-UA global optimisation method for calibrating
#       watershed models, Journal of Hydrology, volume 158, 1994, pp. 265-284
#
#     - Q.Y. Duan, V.K. Gupta and S. Sorooshian:
#       Shuffled Comples Evolution Approach for Effective and Efficient
#       Global Minimization, Journal of Optimization Theory and Applications,
#       volume 76, no. 3, 1993, pp. 501-521
#
#     TODO:
#     - Limit the number of iterations
#     - Provide a dictionary of calculation results
#

# namespace --
#
namespace eval ::math::probopt {
    variable sceNevals 0
}

# sce --
#     Front-end procedure for the SCE algorithm
#
# Arguments:
#     func            Function for which the global minimum is to be found
#     bounds          Boundaries for all independent variables of the function,
#                     as a list of pairs of minimum and maximum
#     args            Set of options - key-value pairs
#
# Result:
#     Estimate of the global minimum as found via the procedure
#
proc ::math::probopt::sce {func bounds args} {
    variable sceNevals
    #
    # Set the default options
    #
    set dims [llength $bounds]
    set options [dict create -complexes 2 -mincomplexes 2 -newpoints 1 -shuffle 0 -pointspercomplex 0 -pointspersubcomplex 0 \
                             -iterations 100 -maxevaluations 1.0e9 -abstolerance 0.0 -reltolerance 0.001]
    #
    # Handle the options
    #

    foreach {key value} $args {
        if { [dict exists $options $key] } {
                dict set options $key $value
        } else {
            return -code error "Unknown option: $key"
        }
    }

    dict with options {}

    #
    # Recalculate a few options
    #
    foreach v {-shuffle -pointspercomplex} {
        if { [set $v] == 0 } {
            set $v [expr {2*$dims + 1}]
            dict set options $v [set $v]
        }
    }
    set v "-pointspersubcomplex"
    if { [set $v] == 0 } {
        set $v [expr {$dims + 1}]
        dict set options $v [set $v]
    }

    #
    # Ready to call the actual procedure
    #
    set sceNEvals 0

    return [SceCompute $func $bounds $options]

    #SceCompute $func $bounds $options
}

# SceCompute --
#     Actually compute the global optimum using the SCE algorithm
#
# Arguments:
#     func            Function for which the global minimum is to be found
#     bounds          Boundaries for all independent variables of the function,
#                     as a list of pairs of minimum and maximum
#     options         Dictionary of options
#
# Result:
#     Dictionary containing among other things the estimate of the
#     global minimum as found via the procedure
#
proc ::math::probopt::SceCompute {func bounds options} {
    variable sceNEvals

    set dims         [llength $bounds]
    set npcomplex    [dict get $options -pointspercomplex]
    set p            [dict get $options -complexes]
    set pmin         [dict get $options -mincomplexes]
    set npsubcomplex [dict get $options -pointspersubcomplex]
    set nnewpoints   [dict get $options -newpoints]
    set nshuffle     [dict get $options -shuffle]
    set niterations  [dict get $options -iterations]
    set abstol       [dict get $options -abstolerance]
    set reltol       [dict get $options -reltolerance]

    set npoints [expr {$npcomplex * $p}]

    #
    # Generate the initial set of points
    #
    set points {}
    for {set i 0} {$i < $p} {incr i} {
        for {set k 0} {$k < $npcomplex} {incr k} {
            set coords [GeneratePoint $bounds]
            lappend points [list $coords [$func $coords]]
            incr sceNEvals
        }
    }

    for {set iteration 0} {$iteration < $niterations} {incr iteration} {
        #
        # Sort the points and create subcomplexes
        #
        set points [lsort -index 1 -increasing $points]

        array unset complex
        for {set i 0} {$i < $p} {incr i} {
            for {set k 0} {$k < $npcomplex} {incr k} {
                lappend complex($i) [lindex $points [expr {$k*$p + $i}]]
            }
        }

        #
        # Optimise the subcomplexes
        #
        for {set i 0} {$i < $p} {incr i} {
            for {set shuffle 0} {$shuffle < $nshuffle} {incr shuffle} {
                set complex($i) [OptimiseComplex $complex($i) $npsubcomplex $nnewpoints $func $bounds]
            }
        }

        #
        # Join the subcomplexes into a list of points and sort the points
        #
        set points {}

        for {set i 0} {$i < $p} {incr i} {
            set points [concat $points $complex($i)]
        }

        if { $iteration == 0 } {
            set oldminimum [lindex [lsort -index 1 -increasing $points] 0 1]
        } else {
            set newminimum [lindex [lsort -index 1 -increasing $points] 0 1]
            if { abs($oldminimum-$newminimum) != 0.0 &&
                 ( abs($oldminimum-$newminimum) < $abstol ||
                   abs($oldminimum-$newminimum) < 0.5 * $reltol * (abs($oldminimum)+abs($newminimum)) ) } {
                break
            } else {
                set oldminimum $newminimum
            }
        }
    }

    #
    # Sort the points and return the best one
    #
    set result [lsort -index 1 -increasing $points]
    set optimum_coords [lindex $result 0 0]
    set optimum_value  [lindex $result 0 1]
    set best_values    {}
    foreach r $result {
        set best_values [concat [lindex $r 1] $best_values]
    }
    return [dict create optimum-coordinates $optimum_coords optimum-value $optimum_value evaluations $sceNEvals best-values $best_values]
}

# OptimiseComplex --
#     Optimise the complex, using subcomplexes
#
# Arguments:
#     complex           The points (and the function values) making up the full complex
#     nsubcomplex       The number of points to be selected for the subcomplex
#     nnewpoints        The number of new points to be generated
#     bounds            The bounds on the coordinates defining the feasible region
#
# Result:
#     A new set of points
#
proc ::math::probopt::OptimiseComplex {complex nsubcomplex nnewpoints func bounds} {
    variable sceNEvals
    #
    # Construct the subcomplex
    #
    set subcomplex [lsort -index 1 -increasing [TriangularSelect $complex $nsubcomplex]]

    #
    # Construct new points:
    # - Determine the centroid, excluding the worst point
    # - Reflect the worst point
    # - Make sure the result is within the feasible region, otherwise
    #   select a new point
    # - Keep the new point if it has a lower function value
    # - Otherwise try a contraction step
    #
    for {set new 0} {$new < $nnewpoints} {incr new} {
        set centroid [Centroid [lrange $complex 0 end-1]]
        set newPoint [ReflectPointInPoint $centroid [lindex $complex end 0]]

        if { ! [WithinBounds $bounds $newPoint] } {
            set newPoint [GeneratePoint $bounds]
            set fvalue [$func $newPoint]
            incr sceNEvals
        } else {
            set fvalue [$func $newPoint]
            incr sceNEvals

            #
            # If the point is better, keep it, otherwise attempt a contraction step
            #
            if { $fvalue > [lindex $complex end 1] } {
                set newPoint [Centroid [list [list $centroid 0.0] [list $newPoint $fvalue]]]
                set fvalue [$func $newPoint]
                incr sceNEvals

                if { $fvalue > [lindex $complex end 1] } {
                    set newPoint [GeneratePoint $bounds]
                    set fvalue [$func $newPoint]
                    incr sceNEvals
                }
            }
        }

        lset complex end [list $newPoint $fvalue]

        set complex [lsort -index 1 -increasing $complex]
    }

    return $complex
}

# GeneratePoint --
#     Generate the coordinates of a random point within the given bounds
#
# Arguments:
#     bounds         Bounds on all coordinates
#
# Result:
#     List of coordinates
#
proc ::math::probopt::GeneratePoint {bounds} {

    set coords {}
    foreach bound $bounds {
        lassign $bound cmin cmax
        lappend coords [expr {$cmin + ($cmax - $cmin) * rand()}]
    }

    return $coords
}

# WithinBounds --
#     Determine if the coordinates of a point are within the given bounds
#
# Arguments:
#     bounds         Bounds on all coordinates
#     point          Coordinates of the point
#
# Result:
#     1 if the point is within the hyperrectangle, 0 otherwise
#
proc ::math::probopt::WithinBounds {bounds point} {

    set within 1

    foreach c $point bound $bounds {
        lassign $bound cmin cmax
        if { $c < $cmin || $c > $cmax } {
            set within 0
            break
        }
    }

    return $within
}

# Centroid --
#     Calculate the centroid of a set of points in N dimensions
#
# Arguments:
#     points         List of point coordinates
#
# Result:
#     Coordinates of the centroid
#
# Note:
#     As this is to be used in the SCE algorithm, the list of
#     point coordinates is slightly more complicated than
#     just the coordinates.
#
proc ::math::probopt::Centroid {points} {
    set dims   [llength [lindex $points 0 0]]
    set number [llength $points]

    set centroid [lrepeat $dims 0]

    foreach point $points {
        set coords [lindex $point 0]

        set idx 0
        foreach c $coords sum $centroid {
            set sum [expr {$sum + $c}]
            lset centroid $idx $sum
            incr idx
        }
    }

    set idx 0
    foreach c $centroid {
        lset centroid $idx [expr {$c / double($number)}]
        incr idx
    }

    return $centroid
}

# ReflectPointInPoint --
#     Reflect a point in another point and return the result
#
# Arguments:
#     centre         Point that serves as the reflection center
#     point          Point to be reflected (list of coordinates)
#
# Result:
#     Coordinates of the new point
#
proc ::math::probopt::ReflectPointInPoint {centre point} {

    set newPoint {}

    foreach c $centre p $point {
        lappend newPoint [expr {2.0 * $c - $p}]
    }

    return $newPoint
}

# TriangularSelect --
#     Select "number" values from a list of values
#     - the probability is triangular
#
# Arguments:
#     values       List of values to choose from
#     number       Number of values to choose (must be smaller than length of the list)
#
# Result:
#     Selected values
#
proc ::math::probopt::TriangularSelect {values number} {
    set selected {}

    for {set i 0} {$i < $number} {incr i} {
        set n [llength $values]

        set r   [expr {1.0 - sqrt(1.0 - rand())}]
        set idx [expr {int($r * $n)}]
        lappend selected [lindex $values $idx]
        set values [lreplace $values $idx $idx]
    }

    return $selected
}