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) 2008 Purdue Research Foundation |
---|
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 | set pop 0 |
---|
27 | set popsize 100 ;# size of each population for genetic algorithm |
---|
28 | |
---|
29 | |
---|
30 | # ---------------------------------------------------------------------- |
---|
31 | # Create a Tool object based on the tool.xml file... |
---|
32 | # ---------------------------------------------------------------------- |
---|
33 | Rappture::getopts argv params { |
---|
34 | value -tool tool.xml |
---|
35 | } |
---|
36 | |
---|
37 | # open the XML file containing the tool parameters |
---|
38 | if {![file exists $params(-tool)]} { |
---|
39 | puts stderr "can't find tool \"$params(-tool)\"" |
---|
40 | exit 1 |
---|
41 | } |
---|
42 | set xmlobj [Rappture::library $params(-tool)] |
---|
43 | |
---|
44 | set installdir [file dirname $params(-tool)] |
---|
45 | if {"." == $installdir} { |
---|
46 | set installdir [pwd] |
---|
47 | } |
---|
48 | |
---|
49 | set tool [Rappture::Tool ::#auto $xmlobj $installdir] |
---|
50 | |
---|
51 | # ---------------------------------------------------------------------- |
---|
52 | # Create some plotting stuff so we can watch the progress of the |
---|
53 | # optimization as it runs. |
---|
54 | # ---------------------------------------------------------------------- |
---|
55 | blt::graph .space |
---|
56 | .space xaxis configure -title "x1" -min -2 -max 2 |
---|
57 | .space yaxis configure -title "x2" -min -2 -max 2 |
---|
58 | .space legend configure -hide yes |
---|
59 | pack .space -side left -expand yes -fill both |
---|
60 | |
---|
61 | blt::graph .value |
---|
62 | .value xaxis configure -title "job number" -min 0 |
---|
63 | .value yaxis configure -title "f(x1,x2)" -logscale yes -max 1 |
---|
64 | .value legend configure -hide yes |
---|
65 | pack .value -side left -expand yes -fill both |
---|
66 | |
---|
67 | button .quit -text Quit -command "optim abort yes" |
---|
68 | pack .quit |
---|
69 | button .restart -text Restart -command "optim restart yes" |
---|
70 | pack .restart |
---|
71 | |
---|
72 | set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black brown grey lavender wheat navy} |
---|
73 | |
---|
74 | |
---|
75 | #for {set pop [expr [llength $colors]-1]} {$pop >= 0} {incr pop -1} { |
---|
76 | # blt::vector x1vec$pop |
---|
77 | # blt::vector x2vec$pop |
---|
78 | # .space element create dot -xdata x1vec$pop -ydata x2vec$pop -color black -linewidth 0 -symbol scross |
---|
79 | # -color [lindex $colors $pop] -linewidth 0 -label "Population #$pop" |
---|
80 | blt::vector jobvec |
---|
81 | blt::vector fvec |
---|
82 | .value element create line -xdata jobvec -ydata fvec -color black -symbol none |
---|
83 | |
---|
84 | # -color [lindex $colors $pop] -symbol none |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | set jobnumber 0 |
---|
89 | set pop -1 |
---|
90 | proc add_to_plot {xmlobj} { |
---|
91 | |
---|
92 | global jobnumber popsize pop |
---|
93 | if {$jobnumber%$popsize == 0} { |
---|
94 | incr pop |
---|
95 | blt::vector x1vec$pop |
---|
96 | blt::vector x2vec$pop |
---|
97 | |
---|
98 | blt::graph .space$pop |
---|
99 | .space$pop xaxis configure -title "x1" -min -2 -max 2 |
---|
100 | .space$pop yaxis configure -title "x2" -min -2 -max 2 |
---|
101 | .space$pop legend configure -hide yes |
---|
102 | pack .space$pop -side left -expand yes -fill both |
---|
103 | .space$pop element create dot -xdata x1vec$pop -ydata x2vec$pop -color black -linewidth 0 -symbol scross |
---|
104 | |
---|
105 | } |
---|
106 | x1vec$pop append [$xmlobj get input.number(x1).current] |
---|
107 | x2vec$pop append [$xmlobj get input.number(x2).current] |
---|
108 | jobvec append $jobnumber |
---|
109 | fvec append [$xmlobj get output.number(f).current] |
---|
110 | incr jobnumber |
---|
111 | # optim samples $jobnumber |
---|
112 | } |
---|
113 | |
---|
114 | # ---------------------------------------------------------------------- |
---|
115 | # Create an optimization context and configure the parameters used |
---|
116 | # for optimization... |
---|
117 | # ---------------------------------------------------------------------- |
---|
118 | Rappture::optimizer optim -tool $tool -using pgapack |
---|
119 | |
---|
120 | optim add number input.number(x1) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes |
---|
121 | optim add number input.number(x2) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes |
---|
122 | optim configure -operation minimize -popsize 100 -maxruns 100 -mutnrate 0.1 -crossovrate 0.8 -randnumseed 20 -stpcriteria maxiter -mutnandcrossover no -allowdup yes -numReplPerPop 40 -mutnValue 0.1 -crossovtype sbx |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | optim get configure |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | set status [optim perform \ |
---|
131 | -fitness output.number(f).current \ |
---|
132 | -updatecommand add_to_plot] |
---|
133 | optim samples 10 |
---|
134 | puts "done: $status" |
---|