source: branches/blt4/packages/optimizer/examples/simple.tcl @ 2742

Last change on this file since 2742 was 1201, checked in by liveletlive, 15 years ago

changes to simple.tcl for new features added in pgapack
added a script (unfinished) to show per generation changes in population

File size: 4.2 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
23set auto_path [linsert $auto_path 0 /home/ganesh/workspace/optim_post_dir_changes/src/lib]
24package require -exact RapptureOptimizer 1.1
25
26
27# ----------------------------------------------------------------------
28#  Create a Tool object based on the tool.xml file...
29# ----------------------------------------------------------------------
30Rappture::getopts argv params {
31    value -tool tool.xml
32}
33
34# open the XML file containing the tool parameters
35if {![file exists $params(-tool)]} {
36    puts stderr "can't find tool \"$params(-tool)\""
37    exit 1
38}
39set xmlobj [Rappture::library $params(-tool)]
40
41set installdir [file dirname $params(-tool)]
42if {"." == $installdir} {
43    set installdir [pwd]
44}
45
46set 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# ----------------------------------------------------------------------
52blt::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
56pack .space -side left -expand yes -fill both
57
58blt::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
62pack .value -side left -expand yes -fill both
63
64button .quit -text Quit -command "optim abort yes"
65pack .quit
66button .restart -text Restart -command "optim restart yes"
67pack .restart
68
69set colors {magenta purple blue DeepSkyBlue cyan green yellow Gold orange tomato red FireBrick black}
70
71for {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
84set jobnumber 0
85proc 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# ----------------------------------------------------------------------
100Rappture::optimizer optim -tool $tool -using pgapack
101
102optim add number input.number(x1) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes
103optim add number input.number(x2) -min -2 -max 2 -randdist uniform -strictmin yes -strictmax yes
104optim 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
106optim get configure
107
108set popsize 200  ;# size of each population for genetic algorithm
109
110set status [optim perform \
111    -fitness output.number(f).current \
112    -updatecommand add_to_plot]
113optim samples 10
114puts "done: $status"
Note: See TracBrowser for help on using the repository browser.