source: trunk/packages/optimizer/examples/simple.tcl @ 1062

Last change on this file since 1062 was 1052, checked in by gah, 16 years ago

fixes to optimizer build

File size: 3.8 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# ======================================================================
18set auto_path [linsert $auto_path 0 /tmp/opt/lib]
19package require BLT
20package require Itcl
21package require Rappture
22package require RapptureGUI
23package require -exact RapptureOptimizer 1.1
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
63button .quit -text Quit -command "optim abort yes"
64pack .quit
65set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black}
66
67for {set pop [expr [llength $colors]-1]} {$pop >= 0} {incr pop -1} {
68    blt::vector x1vec$pop
69    blt::vector x2vec$pop
70    .space element create spots$pop -xdata x1vec$pop -ydata x2vec$pop \
71        -color [lindex $colors $pop] -linewidth 0 -label "Population #$pop"
72
73    blt::vector jobvec$pop
74    blt::vector fvec$pop
75    .value element create line$pop -xdata jobvec$pop -ydata fvec$pop \
76        -color [lindex $colors $pop] -symbol none
77}
78
79set jobnumber 0
80proc add_to_plot {xmlobj} {
81    global jobnumber popsize
82    set pop [expr {$jobnumber/$popsize}]
83    x1vec$pop append [$xmlobj get input.number(x1).current]
84    x2vec$pop append [$xmlobj get input.number(x2).current]
85    jobvec$pop append $jobnumber
86    fvec$pop append [$xmlobj get output.number(f).current]
87    incr jobnumber
88}
89
90# ----------------------------------------------------------------------
91#  Create an optimization context and configure the parameters used
92#  for optimization...
93# ----------------------------------------------------------------------
94Rappture::optimizer optim -tool $tool -using pgapack
95
96optim add number input.number(x1) -min -2 -max 2
97optim add number input.number(x2) -min -2 -max 2
98optim configure -operation minimize -popsize $popsize -maxruns 1000
99
100set status [optim perform \
101    -fitness output.number(f).current \
102    -updatecommand add_to_plot]
103
104puts "done: $status"
Note: See TracBrowser for help on using the repository browser.