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 |
---|
11 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
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 | |
---|
22 | static void RpOptimCleanupParam _ANSI_ARGS_((RpOptimParam *paramPtr)); |
---|
23 | |
---|
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 | */ |
---|
34 | RpOptimEnv* |
---|
35 | RpOptimCreate(pluginDefn) |
---|
36 | RpOptimPlugin *pluginDefn; /* plug-in handling this optimization */ |
---|
37 | { |
---|
38 | RpOptimEnv *envPtr; |
---|
39 | envPtr = (RpOptimEnv*)malloc(sizeof(RpOptimEnv)); |
---|
40 | |
---|
41 | envPtr->pluginDefn = pluginDefn; |
---|
42 | envPtr->pluginData = NULL; |
---|
43 | envPtr->toolData = NULL; |
---|
44 | |
---|
45 | if (pluginDefn->initProc) { |
---|
46 | envPtr->pluginData = (*pluginDefn->initProc)(); |
---|
47 | } |
---|
48 | |
---|
49 | envPtr->numParams = 0; |
---|
50 | envPtr->maxParams = 5; |
---|
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 | */ |
---|
67 | void |
---|
68 | RpOptimAddParam(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 | */ |
---|
104 | RpOptimParam* |
---|
105 | RpOptimAddParamNumber(envPtr, name) |
---|
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; |
---|
113 | numPtr->base.value.dval = 0.0; |
---|
114 | numPtr->min = -DBL_MAX; |
---|
115 | numPtr->max = DBL_MAX; |
---|
116 | numPtr->mutnrate = PARAM_NUM_UNSPEC_MUTN_RATE; /*Unspecified by default, should result in global mutn rate being applied*/ |
---|
117 | numPtr->mutnValue = 0.01; /* value of this number will be changed by plus/minus hundredth of its current value*/ |
---|
118 | numPtr->randdist = RAND_NUMBER_DIST_GAUSSIAN ;/*Gaussian by default*/ |
---|
119 | numPtr->stddev = 1; |
---|
120 | numPtr->mean = 0; |
---|
121 | numPtr->strictmax = 0; /*no strictmax truncation by default*/ |
---|
122 | numPtr->strictmin = 0; /*no strict minimum truncation by default*/ |
---|
123 | numPtr->units = ""; /* Blank Units by default*/ |
---|
124 | RpOptimAddParam(envPtr, (RpOptimParam*)numPtr); |
---|
125 | |
---|
126 | return (RpOptimParam*)numPtr; |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * ---------------------------------------------------------------------- |
---|
131 | * RpOptimAddParamString() |
---|
132 | * |
---|
133 | * Used to add a string parameter as an input to an optimization. |
---|
134 | * Each string has a name and a list of allowed values terminated |
---|
135 | * by a NULL. Adds this string to the end of the parameter list |
---|
136 | * in the given optimization context. |
---|
137 | * ---------------------------------------------------------------------- |
---|
138 | */ |
---|
139 | RpOptimParam* |
---|
140 | RpOptimAddParamString(envPtr, name) |
---|
141 | RpOptimEnv *envPtr; /* context for this optimization */ |
---|
142 | char *name; /* name of this parameter */ |
---|
143 | { |
---|
144 | RpOptimParamString *strPtr; |
---|
145 | strPtr = (RpOptimParamString*)malloc(sizeof(RpOptimParamString)); |
---|
146 | strPtr->base.name = strdup(name); |
---|
147 | strPtr->base.type = RP_OPTIMPARAM_STRING; |
---|
148 | strPtr->base.value.sval.num = -1; |
---|
149 | strPtr->base.value.sval.str = NULL; |
---|
150 | strPtr->values = NULL; |
---|
151 | strPtr->numValues = 0; |
---|
152 | |
---|
153 | RpOptimAddParam(envPtr, (RpOptimParam*)strPtr); |
---|
154 | |
---|
155 | return (RpOptimParam*)strPtr; |
---|
156 | } |
---|
157 | |
---|
158 | /* |
---|
159 | * ---------------------------------------------------------------------- |
---|
160 | * RpOptimFindParam() |
---|
161 | * |
---|
162 | * Used to look for an existing parameter with the specified name. |
---|
163 | * Returns a pointer to the parameter, or NULL if not found. |
---|
164 | * ---------------------------------------------------------------------- |
---|
165 | */ |
---|
166 | RpOptimParam* |
---|
167 | RpOptimFindParam(envPtr, name) |
---|
168 | RpOptimEnv *envPtr; /* context for this optimization */ |
---|
169 | char *name; /* name of this parameter */ |
---|
170 | { |
---|
171 | int n; |
---|
172 | for (n=0; envPtr->numParams; n++) { |
---|
173 | if (strcmp(name, envPtr->paramList[n]->name) == 0) { |
---|
174 | return envPtr->paramList[n]; |
---|
175 | } |
---|
176 | } |
---|
177 | return NULL; |
---|
178 | } |
---|
179 | |
---|
180 | /* |
---|
181 | * ---------------------------------------------------------------------- |
---|
182 | * RpOptimDeleteParam() |
---|
183 | * |
---|
184 | * Used to delete a parameter from the environment. This is especially |
---|
185 | * useful when an error is found while configuring the parameter, just |
---|
186 | * after it was created. If the name is not recognized, this function |
---|
187 | * does nothing. |
---|
188 | * ---------------------------------------------------------------------- |
---|
189 | */ |
---|
190 | void |
---|
191 | RpOptimDeleteParam(envPtr, name) |
---|
192 | RpOptimEnv *envPtr; /* context for this optimization */ |
---|
193 | char *name; /* name of this parameter */ |
---|
194 | { |
---|
195 | int n, j; |
---|
196 | for (n=0; envPtr->numParams; n++) { |
---|
197 | if (strcmp(name, envPtr->paramList[n]->name) == 0) { |
---|
198 | RpOptimCleanupParam(envPtr->paramList[n]); |
---|
199 | for (j=n+1; j < envPtr->numParams; j++) { |
---|
200 | envPtr->paramList[j-1] = envPtr->paramList[j]; |
---|
201 | } |
---|
202 | envPtr->numParams--; |
---|
203 | break; |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | /* |
---|
209 | * ---------------------------------------------------------------------- |
---|
210 | * RpOptimDelete() |
---|
211 | * |
---|
212 | * Used to delete the context for an optimization once it is finished |
---|
213 | * or no longer needed. Frees up the memory needed to store the |
---|
214 | * context and all input parameters. After this call, the context |
---|
215 | * should not be used again. |
---|
216 | * ---------------------------------------------------------------------- |
---|
217 | */ |
---|
218 | void |
---|
219 | RpOptimDelete(envPtr) |
---|
220 | RpOptimEnv *envPtr; /* context for this optimization */ |
---|
221 | { |
---|
222 | int n; |
---|
223 | |
---|
224 | if (envPtr->pluginDefn && envPtr->pluginDefn->cleanupProc) { |
---|
225 | (*envPtr->pluginDefn->cleanupProc)(envPtr->pluginData); |
---|
226 | } |
---|
227 | for (n=0; n < envPtr->numParams; n++) { |
---|
228 | RpOptimCleanupParam(envPtr->paramList[n]); |
---|
229 | } |
---|
230 | free(envPtr->paramList); |
---|
231 | free(envPtr); |
---|
232 | } |
---|
233 | |
---|
234 | /* |
---|
235 | * ---------------------------------------------------------------------- |
---|
236 | * RpOptimCleanupParam() |
---|
237 | * |
---|
238 | * Used internally to free up the data associated with an optimization |
---|
239 | * parameter. Looks at the parameter type and frees up the appropriate |
---|
240 | * data. |
---|
241 | * ---------------------------------------------------------------------- |
---|
242 | */ |
---|
243 | static void |
---|
244 | RpOptimCleanupParam(paramPtr) |
---|
245 | RpOptimParam *paramPtr; /* data to be freed */ |
---|
246 | { |
---|
247 | int n; |
---|
248 | RpOptimParamNumber *numPtr; |
---|
249 | RpOptimParamString *strPtr; |
---|
250 | |
---|
251 | free(paramPtr->name); |
---|
252 | switch (paramPtr->type) { |
---|
253 | case RP_OPTIMPARAM_NUMBER: |
---|
254 | numPtr = (RpOptimParamNumber*)paramPtr; |
---|
255 | /* nothing special to free here */ |
---|
256 | break; |
---|
257 | case RP_OPTIMPARAM_STRING: |
---|
258 | strPtr = (RpOptimParamString*)paramPtr; |
---|
259 | if (strPtr->values) { |
---|
260 | for (n=0; strPtr->values[n]; n++) { |
---|
261 | free(strPtr->values[n]); |
---|
262 | } |
---|
263 | } |
---|
264 | break; |
---|
265 | } |
---|
266 | free(paramPtr); |
---|
267 | } |
---|