Changeset 897


Ignore:
Timestamp:
Feb 19, 2008 2:48:42 PM (15 years ago)
Author:
mmc
Message:

Improved the Rappture optimization API to include Tcl bindings.
Added standard build/test stuff for Tcl libraries. Created a
plugin hook for optimization libraries like pgapack. The plugin
support still needs some work.

Location:
trunk/optimizer
Files:
19 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/optimizer/src/rp_optimizer.c

    r809 r897  
    99 * ======================================================================
    1010 *  AUTHOR:  Michael McLennan, Purdue University
    11  *  Copyright (c) 2004-2007  Purdue Research Foundation
     11 *  Copyright (c) 2008  Purdue Research Foundation
    1212 *
    1313 *  See the file "license.terms" for information on usage and
     
    2020#include "rp_optimizer.h"
    2121
     22static void RpOptimCleanupParam _ANSI_ARGS_((RpOptimParam *paramPtr));
     23
    2224/*
    2325 * ----------------------------------------------------------------------
     
    3133 */
    3234RpOptimEnv*
    33 RpOptimCreate()
     35RpOptimCreate(pluginData, cleanupPtr)
     36    ClientData pluginData;        /* special data created for this env */
     37    RpOptimCleanup *cleanupPtr;   /* routine to clean up pluginData */
    3438{
    3539    RpOptimEnv *envPtr;
    3640    envPtr = (RpOptimEnv*)malloc(sizeof(RpOptimEnv));
     41    envPtr->pluginData = pluginData;
     42    envPtr->cleanupPtr = cleanupPtr;
     43
    3744    envPtr->numParams = 0;
    38     envPtr->maxParams = 2;
     45    envPtr->maxParams = 5;
    3946    envPtr->paramList = (RpOptimParam**)malloc(
    4047        (size_t)(envPtr->maxParams*sizeof(RpOptimParam*))
     
    9097 * ----------------------------------------------------------------------
    9198 */
    92 void
    93 RpOptimAddParamNumber(envPtr, name, min, max)
     99RpOptimParam*
     100RpOptimAddParamNumber(envPtr, name)
    94101    RpOptimEnv *envPtr;   /* context for this optimization */
    95102    char *name;           /* name of this parameter */
    96     double min;           /* minimum value for this parameter */
    97     double max;           /* minimum value for this parameter */
    98103{
    99104    RpOptimParamNumber *numPtr;
     
    102107    numPtr->base.type = RP_OPTIMPARAM_NUMBER;
    103108    numPtr->base.value.num = 0.0;
    104     numPtr->min = min;
    105     numPtr->max = max;
     109    numPtr->min = -DBL_MAX;
     110    numPtr->max = DBL_MAX;
    106111
    107112    RpOptimAddParam(envPtr, (RpOptimParam*)numPtr);
     113
     114    return (RpOptimParam*)numPtr;
    108115}
    109116
     
    118125 * ----------------------------------------------------------------------
    119126 */
    120 void
    121 RpOptimAddParamString(envPtr, name, allowedValues)
     127RpOptimParam*
     128RpOptimAddParamString(envPtr, name)
    122129    RpOptimEnv *envPtr;   /* context for this optimization */
    123130    char *name;           /* name of this parameter */
    124     char **allowedValues; /* null-term list of allowed values */
    125 {
    126     int n;
    127     RpOptimParam **endPtrPtr;
     131{
    128132    RpOptimParamString *strPtr;
    129133    strPtr = (RpOptimParamString*)malloc(sizeof(RpOptimParamString));
     
    131135    strPtr->base.type = RP_OPTIMPARAM_STRING;
    132136    strPtr->base.value.str = NULL;
    133 
    134     /* count the number of allowed values */
    135     if (allowedValues) {
    136         for (n=0; allowedValues[n] != NULL; n++)
    137             ;
    138     } else {
    139         n = 0;
    140     }
    141     strPtr->numValues = n;
    142     strPtr->values = (char**)malloc(n*sizeof(char*));
    143 
    144     /* build a null-terminated list of copies of allowed values */
    145     for (n=0; n < strPtr->numValues; n++) {
    146         strPtr->values[n] = strdup(allowedValues[n]);
    147     }
     137    strPtr->values = NULL;
    148138
    149139    RpOptimAddParam(envPtr, (RpOptimParam*)strPtr);
     140
     141    return (RpOptimParam*)strPtr;
     142}
     143
     144/*
     145 * ----------------------------------------------------------------------
     146 * RpOptimFindParam()
     147 *
     148 * Used to look for an existing parameter with the specified name.
     149 * Returns a pointer to the parameter, or NULL if not found.
     150 * ----------------------------------------------------------------------
     151 */
     152RpOptimParam*
     153RpOptimFindParam(envPtr, name)
     154    RpOptimEnv *envPtr;   /* context for this optimization */
     155    char *name;           /* name of this parameter */
     156{
     157    int n;
     158    for (n=0; envPtr->numParams; n++) {
     159        if (strcmp(name, envPtr->paramList[n]->name) == 0) {
     160            return envPtr->paramList[n];
     161        }
     162    }
     163    return NULL;
     164}
     165
     166/*
     167 * ----------------------------------------------------------------------
     168 * RpOptimDeleteParam()
     169 *
     170 * Used to delete a parameter from the environment.  This is especially
     171 * useful when an error is found while configuring the parameter, just
     172 * after it was created.  If the name is not recognized, this function
     173 * does nothing.
     174 * ----------------------------------------------------------------------
     175 */
     176void
     177RpOptimDeleteParam(envPtr, name)
     178    RpOptimEnv *envPtr;   /* context for this optimization */
     179    char *name;           /* name of this parameter */
     180{
     181    int n, j;
     182    for (n=0; envPtr->numParams; n++) {
     183        if (strcmp(name, envPtr->paramList[n]->name) == 0) {
     184            RpOptimCleanupParam(envPtr->paramList[n]);
     185            for (j=n+1; j < envPtr->numParams; j++) {
     186                envPtr->paramList[j-1] = envPtr->paramList[j];
     187            }
     188            envPtr->numParams--;
     189            break;
     190        }
     191    }
    150192}
    151193
     
    172214    RpOptimStatus status = RP_OPTIM_UNKNOWN;
    173215
    174     int n, nruns, ival;
     216    int n, nruns, ival, nvals;
    175217    double dval, fitness;
    176218    RpOptimParamNumber *numPtr;
     
    201243                case RP_OPTIMPARAM_STRING:
    202244                    strPtr = (RpOptimParamString*)envPtr->paramList[n];
    203                     ival = (int)floor(drand48() * strPtr->numValues);
     245                    for (nvals=0; strPtr->values[nvals]; nvals++)
     246                        ;  /* count values */
     247                    ival = (int)floor(drand48() * nvals);
    204248                    envPtr->paramList[n]->value.str = strPtr->values[ival];
    205249                    break;
     
    244288{
    245289    int n;
    246     RpOptimParam *paramPtr;
     290
     291    for (n=0; n < envPtr->numParams; n++) {
     292        RpOptimCleanupParam(envPtr->paramList[n]);
     293    }
     294    if (envPtr->cleanupPtr) {
     295        (*envPtr->cleanupPtr)(envPtr->pluginData);
     296    }
     297    free(envPtr->paramList);
     298    free(envPtr);
     299}
     300
     301/*
     302 * ----------------------------------------------------------------------
     303 * RpOptimCleanupParam()
     304 *
     305 * Used internally to free up the data associated with an optimization
     306 * parameter.  Looks at the parameter type and frees up the appropriate
     307 * data.
     308 * ----------------------------------------------------------------------
     309 */
     310static void
     311RpOptimCleanupParam(paramPtr)
     312    RpOptimParam *paramPtr;  /* data to be freed */
     313{
     314    int n;
    247315    RpOptimParamNumber *numPtr;
    248316    RpOptimParamString *strPtr;
    249317
    250     for (n=0; n < envPtr->numParams; n++) {
    251         paramPtr = envPtr->paramList[n];
    252         free(paramPtr->name);
    253         switch (paramPtr->type) {
    254             case RP_OPTIMPARAM_NUMBER:
    255                 numPtr = (RpOptimParamNumber*)paramPtr;
    256                 /* nothing special to free here */
    257                 break;
    258             case RP_OPTIMPARAM_STRING:
    259                 strPtr = (RpOptimParamString*)paramPtr;
    260                 for (n=0; n < strPtr->numValues; n++) {
     318    free(paramPtr->name);
     319    switch (paramPtr->type) {
     320        case RP_OPTIMPARAM_NUMBER:
     321            numPtr = (RpOptimParamNumber*)paramPtr;
     322            /* nothing special to free here */
     323            break;
     324        case RP_OPTIMPARAM_STRING:
     325            strPtr = (RpOptimParamString*)paramPtr;
     326            if (strPtr->values) {
     327                for (n=0; strPtr->values[n]; n++) {
    261328                    free(strPtr->values[n]);
    262329                }
    263                 break;
    264         }
    265         free(paramPtr);
    266     }
    267     free(envPtr->paramList);
    268     free(envPtr);
    269 }
     330            }
     331            break;
     332    }
     333    free(paramPtr);
     334}
  • trunk/optimizer/src/rp_optimizer.h

    r809 r897  
    99 * ======================================================================
    1010 *  AUTHOR:  Michael McLennan, Purdue University
    11  *  Copyright (c) 2004-2007  Purdue Research Foundation
     11 *  Copyright (c) 2008  Purdue Research Foundation
    1212 *
    1313 *  See the file "license.terms" for information on usage and
     
    1515 * ======================================================================
    1616 */
    17 #undef _ANSI_ARGS_
    18 #undef CONST
     17#ifndef RP_OPTIMIZER
     18#define RP_OPTIMIZER
    1919
    20 #ifndef NO_CONST
    21 #  define CONST const
    22 #else
    23 #  define CONST
    24 #endif
     20#include <tcl.h>
     21#include <math.h>
     22#include <float.h>
     23#include <string.h>
     24#include <malloc.h>
    2525
    26 #ifndef NO_PROTOTYPES
    27 #  define _ANSI_ARGS_(x)  x
    28 #else
    29 #  define _ANSI_ARGS_(x)  ()
    30 #endif
    31 
    32 #ifdef EXTERN
    33 #  undef EXTERN
    34 #endif
    35 #ifdef __cplusplus
    36 #  define EXTERN extern "C"
    37 #else
    38 #  define EXTERN extern
    39 #endif
     26/*
     27 * General-purpose allocation/cleanup routines:
     28 * These are used, for example, for the plug-in architecture.
     29 */
     30typedef ClientData (RpOptimInit)_ANSI_ARGS_(());
     31typedef void (RpOptimCleanup)_ANSI_ARGS_((ClientData cdata));
    4032
    4133/*
     
    7365    RpOptimParam base;              /* basic parameter info */
    7466    char **values;                  /* array of allowed values */
    75     int numValues;                  /* number of allowed values */
    7667} RpOptimParamString;
    7768
     
    8172 */
    8273typedef struct RpOptimEnv {
     74    ClientData pluginData;          /* data created by plugin init routine */
     75    RpOptimCleanup *cleanupPtr;     /* cleanup routine for pluginData */
    8376    RpOptimParam **paramList;       /* list of input parameters to vary */
    8477    int numParams;                  /* current number of parameters */
     
    9891    int numValues, double *fitnessPtr));
    9992
    100 
    10193/*
    10294 *  Here are the functions in the API:
    10395 */
    104 EXTERN RpOptimEnv* RpOptimCreate _ANSI_ARGS_(());
     96EXTERN RpOptimEnv* RpOptimCreate _ANSI_ARGS_((ClientData pluginData,
     97    RpOptimCleanup *cleanupPtr));
    10598
    106 EXTERN void RpOptimAddParamNumber _ANSI_ARGS_((RpOptimEnv *envPtr,
    107     char *name, double min, double max));
     99EXTERN RpOptimParam* RpOptimAddParamNumber _ANSI_ARGS_((RpOptimEnv *envPtr,
     100    char *name));
    108101
    109 EXTERN void RpOptimAddParamString _ANSI_ARGS_((RpOptimEnv *envPtr,
    110     char *name, char **allowedValues));
     102EXTERN RpOptimParam* RpOptimAddParamString _ANSI_ARGS_((RpOptimEnv *envPtr,
     103    char *name));
     104
     105EXTERN RpOptimParam* RpOptimFindParam _ANSI_ARGS_((RpOptimEnv *envPtr,
     106    char *name));
     107
     108EXTERN void RpOptimDeleteParam _ANSI_ARGS_((RpOptimEnv *envPtr, char *name));
    111109
    112110EXTERN RpOptimStatus RpOptimPerform _ANSI_ARGS_((RpOptimEnv *envPtr,
     
    114112
    115113EXTERN void RpOptimDelete _ANSI_ARGS_((RpOptimEnv *envPtr));
     114
     115#endif
Note: See TracChangeset for help on using the changeset viewer.