Changeset 903 for trunk


Ignore:
Timestamp:
Feb 23, 2008 8:16:38 PM (16 years ago)
Author:
mmc
Message:

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.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/scripts/analyzer.tcl

    r822 r903  
    5252    public method simulate {args}
    5353    public method reset {{when -eventually}}
    54     public method load {file}
     54    public method load {xmlobj}
    5555    public method clear {}
    5656    public method download {option args}
     
    7777
    7878    private common job                 ;# array var used for blt::bgexec jobs
    79 
    80     # resources file tells us the results directory
    81     public common _resultdir ""
    82     public proc setResultDir {path} { set _resultdir $path }
    83 }
    84 
    85 # must use this name -- plugs into Rappture::resources::load
    86 proc analyzer_init_resources {} {
    87     Rappture::resources::register \
    88         results_directory Rappture::Analyzer::setResultDir
    89 }
    90                                                                                
     79}
     80
    9181itk::usual Analyzer {
    9282    keep -background -cursor foreground -font
     
    354344    }
    355345
    356     # read back the results from run.xml
     346    # load results from run.xml into analyzer
    357347    if {$status == 0 && $result != "ABORT"} {
    358         if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} {
    359             set status [catch {load $file} msg]
    360             if {$status != 0} {
    361                 global errorInfo
    362                 set result "$msg\n$errorInfo"
    363             }
    364 
    365             # if there's a results_directory defined in the resources
    366             # file, then move the run.xml file there for storage
    367             if {"" != $_resultdir} {
    368                 catch {
    369                     if {![file exists $_resultdir]} {
    370                         _mkdir $_resultdir
    371                     }
    372                     file rename -force -- $file $_resultdir
    373                 }
    374             }
    375         } else {
    376             set status 1
    377             set result "Can't find result file in output.\nDid you call Rappture::result in your simulator?"
    378         }
     348        set status [catch {load $result} result]
    379349    }
    380350
     
    441411
    442412# ----------------------------------------------------------------------
    443 # USAGE: load <file>
    444 #
    445 # Loads the data from the given <file> into the appropriate results
     413# USAGE: load <xmlobj>
     414#
     415# Loads the data from the given <xmlobj> into the appropriate results
    446416# sets.  If necessary, new results sets are created to store the data.
    447417# ----------------------------------------------------------------------
    448 itcl::body Rappture::Analyzer::load {file} {
     418itcl::body Rappture::Analyzer::load {xmlobj} {
    449419    # only show the last result? then clear first
    450420    if {[$_tool xml get tool.analyzer] == "last"} {
     
    452422    }
    453423
    454     # try to load new results from the given file
    455     set xmlobj [Rappture::library $file]
    456424    lappend _runs $xmlobj
    457425
  • trunk/gui/scripts/tool.tcl

    r775 r903  
    4040
    4141    public common _resources
    42     public proc setAppName {name} { set _resources(-appname) $name }
    43     public proc setHubName {name} { set _resources(-hubname) $name }
    44     public proc setHubURL {name}  { set _resources(-huburl) $name }
    45     public proc setSession {name} { set _resources(-session) $name }
    46     public proc setJobPrt {name}  { set _resources(-jobprotocol) $name }
     42    public proc setAppName {name}   { set _resources(-appname) $name }
     43    public proc setHubName {name}   { set _resources(-hubname) $name }
     44    public proc setHubURL {name}    { set _resources(-huburl) $name }
     45    public proc setSession {name}   { set _resources(-session) $name }
     46    public proc setJobPrt {name}    { set _resources(-jobprotocol) $name }
     47    public proc setResultDir {name} { set _resources(-resultdir) $name }
    4748}
    4849
     
    5051proc tool_init_resources {} {
    5152    Rappture::resources::register \
    52         application_name Rappture::Tool::setAppName \
    53         application_id   Rappture::Tool::setAppId \
    54         hub_name         Rappture::Tool::setHubName \
    55         hub_url          Rappture::Tool::setHubURL \
    56         session_token    Rappture::Tool::setSession \
    57         job_protocol     Rappture::Tool::setJobPrt
     53        application_name  Rappture::Tool::setAppName \
     54        application_id    Rappture::Tool::setAppId \
     55        hub_name          Rappture::Tool::setHubName \
     56        hub_url           Rappture::Tool::setHubURL \
     57        session_token     Rappture::Tool::setSession \
     58        job_protocol      Rappture::Tool::setJobPrt \
     59        results_directory Rappture::Tool::setResultDir
    5860}
    5961                                                                               
     
    263265    #
    264266    if {$status == 0} {
    265         set file [string trim $job(output)]
    266         return [list $status $file]
     267        set result [string trim $job(output)]
     268        if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} {
     269            set status [catch {Rappture::library $file} result]
     270            if {$status != 0} {
     271                global errorInfo
     272                set result "$result\n$errorInfo"
     273            }
     274
     275            # if there's a results_directory defined in the resources
     276            # file, then move the run.xml file there for storage
     277            if {[info exists _resources(-resultdir)]
     278                  && "" != $_resources(-resultdir)} {
     279                catch {
     280                    if {![file exists $_resources(-resultdir)]} {
     281                        _mkdir $_resources(-resultdir)
     282                    }
     283                    file rename -force -- $file $_resources(-resultdir)
     284                }
     285            }
     286        } else {
     287            set status 1
     288            set result "Can't find result file in output.\nDid you call Rappture
     289::result in your simulator?"
     290        }
     291        return [list $status $result]
    267292    } elseif {"" != $job(output) || "" != $job(error)} {
    268293        return [list $status [string trim "$job(output)\n$job(error)"]]
  • trunk/optimizer/examples/simple.tcl

    r899 r903  
    77#  to drive development and testing.
    88#
    9 #  Run this as:  tclsh simple.tcl ?-tool path/to/tool.xml?
     9#  Run this as:  wish simple.tcl ?-tool path/to/tool.xml?
    1010#
    1111# ======================================================================
     
    1616#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1717# ======================================================================
     18package require BLT
    1819package require Itcl
    1920package require Rappture
    2021package require RapptureGUI
    2122package require RapptureOptimizer
     23
     24set popsize 100  ;# size of each population for genetic algorithm
    2225
    2326# ----------------------------------------------------------------------
     
    4346
    4447# ----------------------------------------------------------------------
     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# ----------------------------------------------------------------------
    4589#  Create an optimization context and configure the parameters used
    4690#  for optimization...
     
    5094optim add number input.number(x1) -min -2 -max 2
    5195optim add number input.number(x2) -min -2 -max 2
    52 optim configure -operation minimize -popsize 100 -maxruns 200
     96optim configure -operation minimize -popsize $popsize -maxruns 1000
    5397
    5498set status [optim perform \
    5599    -fitness output.number(f).current \
    56     -updatecommand {puts "checking"}]
     100    -updatecommand add_to_plot]
    57101
    58102puts "done: $status"
  • trunk/optimizer/src/plugin_pgapack.c

    r899 r903  
    111111    PGASetPopReplaceType(ctx, dataPtr->popRepl);
    112112    PGASetCrossoverType(ctx, PGA_CROSSOVER_UNIFORM);
     113
     114    /* stop if any of these are true */
     115    PGASetStoppingRuleType(ctx, PGA_STOP_MAXITER);
     116    PGASetStoppingRuleType(ctx, PGA_STOP_NOCHANGE);
     117    PGASetStoppingRuleType(ctx, PGA_STOP_TOOSIMILAR);
    113118
    114119    PGASetUserFunction(ctx, PGA_USERFUNCTION_CREATESTRING, PgapCreateString);
  • trunk/optimizer/src/rp_optimizer_tcl.c

    r899 r903  
    416416                case RP_OPTIMPARAM_NUMBER:
    417417                    optSpecPtr = rpOptimNumberOpts;
     418                    if (option == NULL) {
     419                        /* no particular option value? then include type */
     420                        if (Tcl_ListObjAppendElement(interp, rrval,
     421                              Tcl_NewStringObj("number",-1)) != TCL_OK) {
     422                            Tcl_DecrRefCount(rrval);
     423                            Tcl_DecrRefCount(rval);
     424                            return TCL_ERROR;
     425                        }
     426                    }
    418427                    break;
    419428                case RP_OPTIMPARAM_STRING:
    420429                    optSpecPtr = rpOptimStringOpts;
     430                    if (option == NULL) {
     431                        /* no particular option value? then include type */
     432                        if (Tcl_ListObjAppendElement(interp, rrval,
     433                              Tcl_NewStringObj("string",-1)) != TCL_OK) {
     434                            Tcl_DecrRefCount(rrval);
     435                            Tcl_DecrRefCount(rval);
     436                            return TCL_ERROR;
     437                        }
     438                    }
    421439                    break;
    422440                default:
     
    638656        }
    639657        Tcl_SetResult(interp, envPtr->pluginDefn->name, TCL_STATIC);
     658
     659        /* if the -tool was specified, then add it as a second element */
     660        toolDataPtr = (RpOptimToolData*)envPtr->toolData;
     661        if (toolDataPtr->toolPtr) {
     662            Tcl_AppendElement(interp,
     663                Tcl_GetStringFromObj(toolDataPtr->toolPtr, (int*)NULL));
     664        }
    640665        return TCL_OK;
    641666    }
     
    671696{
    672697    RpOptimStatus result = RP_OPTIM_SUCCESS;
     698    Tcl_Obj *xmlObj = NULL;
    673699    RpOptimToolData *toolDataPtr = (RpOptimToolData*)envPtr->toolData;
    674700    Tcl_Interp *interp = toolDataPtr->interp;
     701
    675702    int n, status;
    676703#define MAXBUILTIN 10
     
    678705    int rc; Tcl_Obj **rv;
    679706    Tcl_Obj *dataPtr;
    680     char *out;
     707    Tcl_DString buffer;
    681708
    682709    /*
     
    730757            fprintf(stderr, "== JOB FAILED: malformed result: expected {status output}\n");
    731758        } else {
    732             out = Tcl_GetStringFromObj(rv[1], (int*)NULL);
    733759            if (status != 0) {
    734760                result = RP_OPTIM_FAILURE;
    735761                fprintf(stderr, "== JOB FAILED with status code %d:\n%s\n",
    736                     status, out);
     762                    status, Tcl_GetStringFromObj(rv[1], (int*)NULL));
    737763            } else {
    738764                /*
     
    745771                 *    xmlobj get fitnessExpr
    746772                 */
    747                 getcmd[0] = rv[1];
     773                xmlObj = rv[1];
     774                /* hang onto this for -updatecommand below */
     775                Tcl_IncrRefCount(xmlObj);
     776
     777                getcmd[0] = xmlObj;
    748778                getcmd[1] = Tcl_NewStringObj("get",-1);
    749779                getcmd[2] = Tcl_NewStringObj(envPtr->fitnessExpr,-1);
     
    786816     */
    787817    if (toolDataPtr->updateCmdPtr) {
    788         status = Tcl_GlobalEvalObj(toolDataPtr->interp,
    789             toolDataPtr->updateCmdPtr);
     818        Tcl_DStringInit(&buffer);
     819        Tcl_DStringAppend(&buffer,
     820            Tcl_GetStringFromObj(toolDataPtr->updateCmdPtr, (int*)NULL), -1);
     821        Tcl_DStringAppendElement(&buffer,
     822            (xmlObj != NULL) ? Tcl_GetStringFromObj(xmlObj, (int*)NULL): "");
     823
     824        status = Tcl_GlobalEval(toolDataPtr->interp,
     825            Tcl_DStringValue(&buffer));
    790826
    791827        if (status == TCL_ERROR) {
     
    793829        }
    794830        else if (status == TCL_BREAK || status == TCL_RETURN) {
    795             return RP_OPTIM_ABORTED;
    796         }
    797     }
    798     return RP_OPTIM_SUCCESS;
     831            result = RP_OPTIM_ABORTED;
     832        }
     833        Tcl_DStringFree(&buffer);
     834    }
     835
     836    if (xmlObj) {
     837        Tcl_DecrRefCount(xmlObj);  /* done with this now */
     838    }
     839    return result;
    799840}
  • trunk/optimizer/tests/aaa.test

    r898 r903  
    6767  ctxt add number input.temperature -min 0 -max 1
    6868  ctxt get
    69 } {input.temperature -min 0.0 -max 1.0}
     69} {input.temperature number -min 0.0 -max 1.0}
    7070
    7171test aaa-2.2 {add a parameter with a bad type} {
     
    8787test aaa-2.6 {add doesn't add parameter when there's an error} {
    8888  ctxt get
    89 } {input.temperature -min 0.0 -max 1.0}
     89} {input.temperature number -min 0.0 -max 1.0}
    9090
    9191test aaa-2.7 {add a string parameter} {
    9292  ctxt add string input.choice -values {a b c}
    9393  ctxt get input.choice
    94 } {-values {a b c}}
     94} {string -values {a b c}}
    9595
    9696test aaa-2.8 {add a string parameter with a missing option} {
     
    104104test aaa-2.10 {add doesn't add parameter when there's an error} {
    105105  ctxt get
    106 } {{input.temperature -min 0.0 -max 1.0} {input.choice -values {a b c}}}
     106} {{input.temperature number -min 0.0 -max 1.0} {input.choice string -values {a b c}}}
     107
     108test aaa-2.11 {add another number and return } {
     109  ctxt add number input.temporary -min -5 -max 5
     110  lsort [ctxt get input.temp*]
     111} {{input.temperature number -min 0.0 -max 1.0} {input.temporary number -min -5.0 -max 5.0}}
     112
     113test aaa-2.12 {query -min values for multiple parameters} {
     114  lsort [ctxt get input.temp* -min]
     115} {{input.temperature 0.0} {input.temporary -5.0}}
    107116
    108117# cleanup
Note: See TracChangeset for help on using the changeset viewer.