1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Rappture optimization |
---|
3 | # |
---|
4 | # This script shows the core of the Rappture optimization loop. |
---|
5 | # It represents a simple skeleton of the much more complicated |
---|
6 | # Rappture GUI, but it drives enough of the optimization process |
---|
7 | # to drive development and testing. |
---|
8 | # |
---|
9 | # Run this as: wish simple.tcl ?-tool path/to/tool.xml? |
---|
10 | # |
---|
11 | # ====================================================================== |
---|
12 | # AUTHOR: Michael McLennan, Purdue University |
---|
13 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
14 | # |
---|
15 | # See the file "license.terms" for information on usage and |
---|
16 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
17 | # ====================================================================== |
---|
18 | set auto_path [linsert $auto_path 0 /tmp/opt/lib] |
---|
19 | package require BLT |
---|
20 | package require Itcl |
---|
21 | package require Rappture |
---|
22 | package require RapptureGUI |
---|
23 | set auto_path [linsert $auto_path 0 /home/ganesh/workspace/optim_post_dir_changes/src/lib] |
---|
24 | package require -exact RapptureOptimizer 1.1 |
---|
25 | |
---|
26 | |
---|
27 | # ---------------------------------------------------------------------- |
---|
28 | # Create a Tool object based on the tool.xml file... |
---|
29 | # ---------------------------------------------------------------------- |
---|
30 | Rappture::getopts argv params { |
---|
31 | value -tool tool.xml |
---|
32 | } |
---|
33 | |
---|
34 | # open the XML file containing the tool parameters |
---|
35 | if {![file exists $params(-tool)]} { |
---|
36 | puts stderr "can't find tool \"$params(-tool)\"" |
---|
37 | exit 1 |
---|
38 | } |
---|
39 | set xmlobj [Rappture::library $params(-tool)] |
---|
40 | |
---|
41 | set installdir [file dirname $params(-tool)] |
---|
42 | if {"." == $installdir} { |
---|
43 | set installdir [pwd] |
---|
44 | } |
---|
45 | |
---|
46 | set tool [Rappture::Tool ::#auto $xmlobj $installdir] |
---|
47 | |
---|
48 | # ---------------------------------------------------------------------- |
---|
49 | # Create some plotting stuff so we can watch the progress of the |
---|
50 | # optimization as it runs. |
---|
51 | # ---------------------------------------------------------------------- |
---|
52 | blt::graph .space |
---|
53 | .space xaxis configure -title "x1" -min -2 -max 2 |
---|
54 | .space yaxis configure -title "x2" -min -2 -max 2 |
---|
55 | .space legend configure -hide no |
---|
56 | pack .space -side left -expand yes -fill both |
---|
57 | |
---|
58 | blt::graph .value |
---|
59 | .value xaxis configure -title "job number" -min 0 |
---|
60 | .value yaxis configure -title "f(x1,x2)" -logscale yes -max 1 |
---|
61 | .value legend configure -hide yes |
---|
62 | pack .value -side left -expand yes -fill both |
---|
63 | |
---|
64 | button .quit -text Quit -command "optim abort yes" |
---|
65 | pack .quit |
---|
66 | button .restart -text Restart -command "optim restart yes" |
---|
67 | pack .restart |
---|
68 | |
---|
69 | set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black} |
---|
70 | |
---|
71 | for {set pop [expr [llength $colors]-1]} {$pop >= 0} {incr pop -1} { |
---|
72 | blt::vector x1vec$pop |
---|
73 | blt::vector x2vec$pop |
---|
74 | .space element create spots$pop -xdata x1vec$pop -ydata x2vec$pop \ |
---|
75 | -color [lindex $colors $pop] -linewidth 0 -label "Population #$pop" |
---|
76 | |
---|
77 | blt::vector jobvec$pop |
---|
78 | blt::vector fvec$pop |
---|
79 | .value element create line$pop -xdata jobvec$pop -ydata fvec$pop \ |
---|
80 | -color [lindex $colors $pop] -symbol none |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | set jobnumber 0 |
---|
85 | proc add_to_plot {xmlobj} { |
---|
86 | global jobnumber popsize |
---|
87 | set pop [expr {$jobnumber/$popsize}] |
---|
88 | x1vec$pop append [$xmlobj get input.number(x1).current] |
---|
89 | x2vec$pop append [$xmlobj get input.number(x2).current] |
---|
90 | jobvec$pop append $jobnumber |
---|
91 | fvec$pop append [$xmlobj get output.number(f).current] |
---|
92 | incr jobnumber |
---|
93 | # optim samples $jobnumber |
---|
94 | } |
---|
95 | |
---|
96 | # ---------------------------------------------------------------------- |
---|
97 | # Create an optimization context and configure the parameters used |
---|
98 | # for optimization... |
---|
99 | # ---------------------------------------------------------------------- |
---|
100 | Rappture::optimizer optim -tool $tool -using pgapack |
---|
101 | |
---|
102 | optim add number input.number(x1) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes |
---|
103 | optim add number input.number(x2) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes |
---|
104 | optim configure -operation minimize -popsize 200 -maxruns 100 -mutnrate 0.5 -crossovrate 0.8 -randnumseed 20 -stpcriteria toosimilar -mutnandcrossover no -allowdup yes -numReplPerPop 50 -mutnValue 0.1 -crossovtype sbx -randReplProp 0.05 |
---|
105 | |
---|
106 | optim get configure |
---|
107 | |
---|
108 | set popsize 200 ;# size of each population for genetic algorithm |
---|
109 | |
---|
110 | set status [optim perform \ |
---|
111 | -fitness output.number(f).current \ |
---|
112 | -updatecommand add_to_plot] |
---|
113 | optim samples 10 |
---|
114 | puts "done: $status" |
---|