source: trunk/packages/optimizer/src/rp_optimizer.c @ 1154

Last change on this file since 1154 was 1154, checked in by liveletlive, 16 years ago

tweaked the configuration code for number parameters.
added code required to include units of a number parameter.

File size: 8.6 KB
RevLine 
[809]1/*
2 * ----------------------------------------------------------------------
3 *  rp_optimizer
4 *
5 *  This is the C language API for the optimization package in
6 *  Rappture.  It lets you set up an optimization of some fitness
7 *  function with respect to a set of inputs.
8 *
9 * ======================================================================
10 *  AUTHOR:  Michael McLennan, Purdue University
[897]11 *  Copyright (c) 2008  Purdue Research Foundation
[809]12 *
13 *  See the file "license.terms" for information on usage and
14 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 * ======================================================================
16 */
17#include <math.h>
18#include <stdlib.h>
19#include <string.h>
20#include "rp_optimizer.h"
21
[897]22static void RpOptimCleanupParam _ANSI_ARGS_((RpOptimParam *paramPtr));
23
[809]24/*
25 * ----------------------------------------------------------------------
26 * RpOptimCreate()
27 *
28 * Used to create the context for an optimization.  Creates an empty
29 * context and returns a pointer to it.  The context can be updated
30 * by calling functions like RpOptimAddParamNumber to define various
31 * input parameters.
32 * ----------------------------------------------------------------------
33 */
34RpOptimEnv*
[899]35RpOptimCreate(pluginDefn)
36    RpOptimPlugin *pluginDefn;    /* plug-in handling this optimization */
[809]37{
38    RpOptimEnv *envPtr;
39    envPtr = (RpOptimEnv*)malloc(sizeof(RpOptimEnv));
[897]40
[899]41    envPtr->pluginDefn = pluginDefn;
42    envPtr->pluginData = NULL;
43    envPtr->toolData   = NULL;
44
45    if (pluginDefn->initProc) {
46        envPtr->pluginData = (*pluginDefn->initProc)();
47    }
48
[809]49    envPtr->numParams = 0;
[897]50    envPtr->maxParams = 5;
[809]51    envPtr->paramList = (RpOptimParam**)malloc(
52        (size_t)(envPtr->maxParams*sizeof(RpOptimParam*))
53    );
54
55    return envPtr;
56}
57
58/*
59 * ----------------------------------------------------------------------
60 * RpOptimAddParam()
61 *
62 * Used internally to add a new parameter into the given optimization
63 * context.  The internal list of parameters is resized, if necessary,
64 * to accommodate the new parameter.
65 * ----------------------------------------------------------------------
66 */
67void
68RpOptimAddParam(envPtr, paramPtr)
69    RpOptimEnv *envPtr;      /* context for this optimization */
70    RpOptimParam *paramPtr;  /* parameter being added */
71{
72    RpOptimParam **newParamList;
73
74    /*
75     * Add the new parameter at the end of the list.
76     */
77    envPtr->paramList[envPtr->numParams++] = paramPtr;
78
79    /*
80     * Out of space?  Then double the space available for params.
81     */
82    if (envPtr->numParams >= envPtr->maxParams) {
83        envPtr->maxParams *= 2;
84        newParamList = (RpOptimParam**)malloc(
85            (size_t)(envPtr->maxParams*sizeof(RpOptimParam*))
86        );
87        memcpy(newParamList, envPtr->paramList,
88            (size_t)(envPtr->numParams*sizeof(RpOptimParam*)));
89        free(envPtr->paramList);
90        envPtr->paramList = newParamList;
91    }
92}
93
94/*
95 * ----------------------------------------------------------------------
96 * RpOptimAddParamNumber()
97 *
98 * Used to add a number parameter as an input to an optimization.
99 * Each number has a name and a double precision value that can be
100 * constrained between min/max values.  Adds this number to the end
101 * of the parameter list in the given optimization context.
102 * ----------------------------------------------------------------------
103 */
[897]104RpOptimParam*
105RpOptimAddParamNumber(envPtr, name)
[809]106    RpOptimEnv *envPtr;   /* context for this optimization */
107    char *name;           /* name of this parameter */
108{
109    RpOptimParamNumber *numPtr;
110    numPtr = (RpOptimParamNumber*)malloc(sizeof(RpOptimParamNumber));
111    numPtr->base.name = strdup(name);
112    numPtr->base.type = RP_OPTIMPARAM_NUMBER;
[898]113    numPtr->base.value.dval = 0.0;
[897]114    numPtr->min = -DBL_MAX;
115    numPtr->max = DBL_MAX;
[1070]116    numPtr->mutnrate = PARAM_NUM_UNSPEC_MUTN_RATE; /*Unspecified by default, should result in global mutn rate being applied*/
117    numPtr->randdist = RAND_NUMBER_DIST_GAUSSIAN ;/*Gaussian by default*/
118    numPtr->stddev = 1;
119    numPtr->mean = 0;
[1154]120    numPtr->strictmax = 0; /*no strictmax truncation by default*/
121    numPtr->strictmin = 0; /*no strict minimum truncation by default*/
122        numPtr->units = ""; /* Blank Units by default*/
[809]123    RpOptimAddParam(envPtr, (RpOptimParam*)numPtr);
[897]124
125    return (RpOptimParam*)numPtr;
[809]126}
127
128/*
129 * ----------------------------------------------------------------------
130 * RpOptimAddParamString()
131 *
132 * Used to add a string parameter as an input to an optimization.
133 * Each string has a name and a list of allowed values terminated
134 * by a NULL.  Adds this string to the end of the parameter list
135 * in the given optimization context.
136 * ----------------------------------------------------------------------
137 */
[897]138RpOptimParam*
139RpOptimAddParamString(envPtr, name)
[809]140    RpOptimEnv *envPtr;   /* context for this optimization */
141    char *name;           /* name of this parameter */
142{
143    RpOptimParamString *strPtr;
144    strPtr = (RpOptimParamString*)malloc(sizeof(RpOptimParamString));
145    strPtr->base.name = strdup(name);
146    strPtr->base.type = RP_OPTIMPARAM_STRING;
[898]147    strPtr->base.value.sval.num = -1;
148    strPtr->base.value.sval.str = NULL;
[897]149    strPtr->values = NULL;
[898]150    strPtr->numValues = 0;
[809]151
[897]152    RpOptimAddParam(envPtr, (RpOptimParam*)strPtr);
153
154    return (RpOptimParam*)strPtr;
155}
156
157/*
158 * ----------------------------------------------------------------------
159 * RpOptimFindParam()
160 *
161 * Used to look for an existing parameter with the specified name.
162 * Returns a pointer to the parameter, or NULL if not found.
163 * ----------------------------------------------------------------------
164 */
165RpOptimParam*
166RpOptimFindParam(envPtr, name)
167    RpOptimEnv *envPtr;   /* context for this optimization */
168    char *name;           /* name of this parameter */
169{
170    int n;
171    for (n=0; envPtr->numParams; n++) {
172        if (strcmp(name, envPtr->paramList[n]->name) == 0) {
173            return envPtr->paramList[n];
174        }
[809]175    }
[897]176    return NULL;
177}
[809]178
[897]179/*
180 * ----------------------------------------------------------------------
181 * RpOptimDeleteParam()
182 *
183 * Used to delete a parameter from the environment.  This is especially
184 * useful when an error is found while configuring the parameter, just
185 * after it was created.  If the name is not recognized, this function
186 * does nothing.
187 * ----------------------------------------------------------------------
188 */
189void
190RpOptimDeleteParam(envPtr, name)
191    RpOptimEnv *envPtr;   /* context for this optimization */
192    char *name;           /* name of this parameter */
193{
194    int n, j;
195    for (n=0; envPtr->numParams; n++) {
196        if (strcmp(name, envPtr->paramList[n]->name) == 0) {
197            RpOptimCleanupParam(envPtr->paramList[n]);
198            for (j=n+1; j < envPtr->numParams; j++) {
199                envPtr->paramList[j-1] = envPtr->paramList[j];
200            }
201            envPtr->numParams--;
202            break;
203        }
[809]204    }
205}
206
207/*
208 * ----------------------------------------------------------------------
209 * RpOptimDelete()
210 *
211 * Used to delete the context for an optimization once it is finished
212 * or no longer needed.  Frees up the memory needed to store the
213 * context and all input parameters.  After this call, the context
214 * should not be used again.
215 * ----------------------------------------------------------------------
216 */
217void
218RpOptimDelete(envPtr)
219    RpOptimEnv *envPtr;   /* context for this optimization */
220{
221    int n;
[897]222
[899]223    if (envPtr->pluginDefn && envPtr->pluginDefn->cleanupProc) {
224        (*envPtr->pluginDefn->cleanupProc)(envPtr->pluginData);
225    }
[897]226    for (n=0; n < envPtr->numParams; n++) {
227        RpOptimCleanupParam(envPtr->paramList[n]);
228    }
229    free(envPtr->paramList);
230    free(envPtr);
231}
232
233/*
234 * ----------------------------------------------------------------------
235 * RpOptimCleanupParam()
236 *
237 * Used internally to free up the data associated with an optimization
238 * parameter.  Looks at the parameter type and frees up the appropriate
239 * data.
240 * ----------------------------------------------------------------------
241 */
242static void
243RpOptimCleanupParam(paramPtr)
244    RpOptimParam *paramPtr;  /* data to be freed */
245{
246    int n;
[809]247    RpOptimParamNumber *numPtr;
248    RpOptimParamString *strPtr;
249
[897]250    free(paramPtr->name);
251    switch (paramPtr->type) {
252        case RP_OPTIMPARAM_NUMBER:
253            numPtr = (RpOptimParamNumber*)paramPtr;
254            /* nothing special to free here */
255            break;
256        case RP_OPTIMPARAM_STRING:
257            strPtr = (RpOptimParamString*)paramPtr;
258            if (strPtr->values) {
259                for (n=0; strPtr->values[n]; n++) {
[809]260                    free(strPtr->values[n]);
261                }
[897]262            }
263            break;
[809]264    }
[897]265    free(paramPtr);
[809]266}
Note: See TracBrowser for help on using the repository browser.