source: trunk/optimizer/examples/simple.tcl @ 903

Last change on this file since 903 was 903, checked in by mmc, 15 years ago

Final tweaks on the optimization package. The demo now works properly.
Just run "wish simple.tcl" to see it work.

Fixed the Tool class to work better with the optimizer. The "run"
method now returns the result directly as a Rappture::Library object,
and the Analyzer merely loads the object.

File size: 3.6 KB
Line 
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# ======================================================================
18package require BLT
19package require Itcl
20package require Rappture
21package require RapptureGUI
22package require RapptureOptimizer
23
24set popsize 100  ;# size of each population for genetic algorithm
25
26# ----------------------------------------------------------------------
27#  Create a Tool object based on the tool.xml file...
28# ----------------------------------------------------------------------
29Rappture::getopts argv params {
30    value -tool tool.xml
31}
32
33# open the XML file containing the tool parameters
34if {![file exists $params(-tool)]} {
35    puts stderr "can't find tool \"$params(-tool)\""
36    exit 1
37}
38set xmlobj [Rappture::library $params(-tool)]
39
40set installdir [file dirname $params(-tool)]
41if {"." == $installdir} {
42    set installdir [pwd]
43}
44
45set tool [Rappture::Tool ::#auto $xmlobj $installdir]
46
47# ----------------------------------------------------------------------
48#  Create some plotting stuff so we can watch the progress of the
49#  optimization as it runs.
50# ----------------------------------------------------------------------
51blt::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
55pack .space -side left -expand yes -fill both
56
57blt::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
61pack .value -side left -expand yes -fill both
62
63set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black}
64
65for {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
77set jobnumber 0
78proc 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# ----------------------------------------------------------------------
89#  Create an optimization context and configure the parameters used
90#  for optimization...
91# ----------------------------------------------------------------------
92Rappture::optimizer optim -tool $tool -using pgapack
93
94optim add number input.number(x1) -min -2 -max 2
95optim add number input.number(x2) -min -2 -max 2
96optim configure -operation minimize -popsize $popsize -maxruns 1000
97
98set status [optim perform \
99    -fitness output.number(f).current \
100    -updatecommand add_to_plot]
101
102puts "done: $status"
Note: See TracBrowser for help on using the repository browser.