[898] | 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 | # |
---|
[903] | 9 | # Run this as: wish simple.tcl ?-tool path/to/tool.xml? |
---|
[898] | 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 | # ====================================================================== |
---|
[903] | 18 | package require BLT |
---|
[898] | 19 | package require Itcl |
---|
| 20 | package require Rappture |
---|
| 21 | package require RapptureGUI |
---|
| 22 | package require RapptureOptimizer |
---|
| 23 | |
---|
[903] | 24 | set popsize 100 ;# size of each population for genetic algorithm |
---|
| 25 | |
---|
[898] | 26 | # ---------------------------------------------------------------------- |
---|
| 27 | # Create a Tool object based on the tool.xml file... |
---|
| 28 | # ---------------------------------------------------------------------- |
---|
| 29 | Rappture::getopts argv params { |
---|
| 30 | value -tool tool.xml |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | # open the XML file containing the tool parameters |
---|
| 34 | if {![file exists $params(-tool)]} { |
---|
| 35 | puts stderr "can't find tool \"$params(-tool)\"" |
---|
| 36 | exit 1 |
---|
| 37 | } |
---|
| 38 | set xmlobj [Rappture::library $params(-tool)] |
---|
| 39 | |
---|
| 40 | set installdir [file dirname $params(-tool)] |
---|
| 41 | if {"." == $installdir} { |
---|
| 42 | set installdir [pwd] |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | set tool [Rappture::Tool ::#auto $xmlobj $installdir] |
---|
| 46 | |
---|
| 47 | # ---------------------------------------------------------------------- |
---|
[903] | 48 | # Create some plotting stuff so we can watch the progress of the |
---|
| 49 | # optimization as it runs. |
---|
| 50 | # ---------------------------------------------------------------------- |
---|
| 51 | blt::graph .space |
---|
| 52 | .space xaxis configure -title "x1" -min -2 -max 2 |
---|
| 53 | .space yaxis configure -title "x2" -min -2 -max 2 |
---|
| 54 | .space legend configure -hide no |
---|
| 55 | pack .space -side left -expand yes -fill both |
---|
| 56 | |
---|
| 57 | blt::graph .value |
---|
| 58 | .value xaxis configure -title "job number" -min 0 |
---|
| 59 | .value yaxis configure -title "f(x1,x2)" -logscale yes -max 1 |
---|
| 60 | .value legend configure -hide yes |
---|
| 61 | pack .value -side left -expand yes -fill both |
---|
| 62 | |
---|
| 63 | set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black} |
---|
| 64 | |
---|
| 65 | for {set pop [expr [llength $colors]-1]} {$pop >= 0} {incr pop -1} { |
---|
| 66 | blt::vector x1vec$pop |
---|
| 67 | blt::vector x2vec$pop |
---|
| 68 | .space element create spots$pop -xdata x1vec$pop -ydata x2vec$pop \ |
---|
| 69 | -color [lindex $colors $pop] -linewidth 0 -label "Population #$pop" |
---|
| 70 | |
---|
| 71 | blt::vector jobvec$pop |
---|
| 72 | blt::vector fvec$pop |
---|
| 73 | .value element create line$pop -xdata jobvec$pop -ydata fvec$pop \ |
---|
| 74 | -color [lindex $colors $pop] -symbol none |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | set jobnumber 0 |
---|
| 78 | proc add_to_plot {xmlobj} { |
---|
| 79 | global jobnumber popsize |
---|
| 80 | set pop [expr {$jobnumber/$popsize}] |
---|
| 81 | x1vec$pop append [$xmlobj get input.number(x1).current] |
---|
| 82 | x2vec$pop append [$xmlobj get input.number(x2).current] |
---|
| 83 | jobvec$pop append $jobnumber |
---|
| 84 | fvec$pop append [$xmlobj get output.number(f).current] |
---|
| 85 | incr jobnumber |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | # ---------------------------------------------------------------------- |
---|
[898] | 89 | # Create an optimization context and configure the parameters used |
---|
| 90 | # for optimization... |
---|
| 91 | # ---------------------------------------------------------------------- |
---|
| 92 | Rappture::optimizer optim -tool $tool -using pgapack |
---|
| 93 | |
---|
[899] | 94 | optim add number input.number(x1) -min -2 -max 2 |
---|
| 95 | optim add number input.number(x2) -min -2 -max 2 |
---|
[903] | 96 | optim configure -operation minimize -popsize $popsize -maxruns 1000 |
---|
[898] | 97 | |
---|
[899] | 98 | set status [optim perform \ |
---|
| 99 | -fitness output.number(f).current \ |
---|
[903] | 100 | -updatecommand add_to_plot] |
---|
[898] | 101 | |
---|
| 102 | puts "done: $status" |
---|