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) 2008 Purdue Research Foundation |
---|
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 | #ifndef RP_OPTIMIZER |
---|
18 | #define RP_OPTIMIZER |
---|
19 | |
---|
20 | #include <tcl.h> |
---|
21 | #include <math.h> |
---|
22 | #include <float.h> |
---|
23 | #include <string.h> |
---|
24 | #include <malloc.h> |
---|
25 | #include "rp_tcloptions.h" |
---|
26 | |
---|
27 | #define PGAPACK_RUNTIME_TABLE_DEFAULT_SIZE 5000 /*Approx Number of Samples in a run*/ |
---|
28 | #define SINGLE_SAMPLE_DATA_BUFFER_DEFAULT_SIZE 5000 |
---|
29 | |
---|
30 | /*User defined Random Number Distributions*/ |
---|
31 | #define RAND_NUMBER_DIST_GAUSSIAN 1 |
---|
32 | #define RAND_NUMBER_DIST_UNIFORM 2 |
---|
33 | |
---|
34 | /* Used to indicate unspecified mutation rate for a param, will result in global mutn rate being applied*/ |
---|
35 | #define PARAM_NUM_UNSPEC_MUTN_RATE -1.0 |
---|
36 | |
---|
37 | /* |
---|
38 | * General-purpose allocation/cleanup routines: |
---|
39 | * These are used, for example, for the plug-in architecture. |
---|
40 | */ |
---|
41 | typedef ClientData (RpOptimInit)_ANSI_ARGS_(()); |
---|
42 | typedef void (RpOptimCleanup)_ANSI_ARGS_((ClientData cdata)); |
---|
43 | |
---|
44 | /* |
---|
45 | * During each optimization, a function of the following type is |
---|
46 | * called again and again to evaluate input parameters and compute |
---|
47 | * the fitness function. |
---|
48 | */ |
---|
49 | typedef enum { |
---|
50 | RP_OPTIM_SUCCESS=0, RP_OPTIM_UNKNOWN, RP_OPTIM_FAILURE, RP_OPTIM_ABORTED, RP_OPTIM_RESTARTED |
---|
51 | } RpOptimStatus; |
---|
52 | |
---|
53 | struct RpOptimEnv; /* defined below */ |
---|
54 | struct RpOptimParam; |
---|
55 | |
---|
56 | typedef RpOptimStatus (RpOptimEvaluator) _ANSI_ARGS_(( |
---|
57 | struct RpOptimEnv *envPtr, struct RpOptimParam *values, int numValues, |
---|
58 | double *fitnessPtr)); |
---|
59 | |
---|
60 | typedef RpOptimStatus (RpOptimHandler) _ANSI_ARGS_(( |
---|
61 | struct RpOptimEnv *envPtr, RpOptimEvaluator *evalProc, |
---|
62 | char *fitnessExpr)); |
---|
63 | |
---|
64 | /* |
---|
65 | * Each optimization package is plugged in to this infrastructure |
---|
66 | * by defining the following data at the top of rp_optimizer_tcl.c |
---|
67 | */ |
---|
68 | typedef struct RpOptimPlugin { |
---|
69 | char *name; /* name of this package for -using */ |
---|
70 | RpOptimInit *initProc; /* initialization routine */ |
---|
71 | RpOptimHandler *runProc; /* handles the core optimization */ |
---|
72 | RpOptimCleanup *cleanupProc; /* cleanup routine */ |
---|
73 | RpTclOption *optionSpec; /* specs for config options */ |
---|
74 | } RpOptimPlugin; |
---|
75 | |
---|
76 | /* |
---|
77 | * This is the basic definition for each parameter within an optimization. |
---|
78 | * Each parameter has a name, type, and value. The value is important |
---|
79 | * at the end of an optimization. It is either the final value, or the |
---|
80 | * value where we left off when the optimization was terminated. |
---|
81 | */ |
---|
82 | typedef enum { |
---|
83 | RP_OPTIMPARAM_NUMBER, RP_OPTIMPARAM_STRING |
---|
84 | } RpOptimParamType; |
---|
85 | |
---|
86 | typedef struct RpDiscreteString { |
---|
87 | int num; /* integer representing this string */ |
---|
88 | char *str; /* actual string selected */ |
---|
89 | } RpDiscreteString; |
---|
90 | |
---|
91 | typedef struct RpOptimParam { |
---|
92 | char *name; /* name of this parameter */ |
---|
93 | RpOptimParamType type; /* paramter type -- number, string, etc */ |
---|
94 | union { |
---|
95 | double dval; /* slot for number value */ |
---|
96 | int ival; /* slot for integer value */ |
---|
97 | RpDiscreteString sval; /* slot for string value */ |
---|
98 | } value; |
---|
99 | } RpOptimParam; |
---|
100 | |
---|
101 | /* |
---|
102 | * NUMBER PARAMETERS have additional constraints. |
---|
103 | */ |
---|
104 | typedef struct RpOptimParamNumber { |
---|
105 | RpOptimParam base; /* basic parameter info */ |
---|
106 | double min; /* optimization constraint: min value */ |
---|
107 | double max; /* optimization constraint: max value */ |
---|
108 | double mutnrate; /* independently sets mutation rate for each parameter*/ |
---|
109 | double mutnValue; /* on mutation bumps up/down the value by val (+-) mutnValue*val */ |
---|
110 | int randdist; /* gaussian or uniform distribution*/ |
---|
111 | int strictmin; /* whether a strict min is to be applied for gauss. rand. numbers*/ |
---|
112 | int strictmax; /* whether a strict max is to be applied for gauss. rand. numbers*/ |
---|
113 | double stddev; /* std deviaton for gaussian profile*/ |
---|
114 | double mean; /* mean for gaussian profile*/ |
---|
115 | char *units; |
---|
116 | } RpOptimParamNumber; |
---|
117 | |
---|
118 | /* |
---|
119 | * STRING PARAMETERS have a set of allowed values. |
---|
120 | */ |
---|
121 | typedef struct RpOptimParamString { |
---|
122 | RpOptimParam base; /* basic parameter info */ |
---|
123 | char **values; /* array of allowed values */ |
---|
124 | int numValues; /* number of allowed values */ |
---|
125 | } RpOptimParamString; |
---|
126 | |
---|
127 | /* |
---|
128 | * Each optimization problem is represented by a context that includes |
---|
129 | * the parameters that will be varied. |
---|
130 | */ |
---|
131 | typedef struct RpOptimEnv { |
---|
132 | RpOptimPlugin *pluginDefn; /* plug-in handling this optimization */ |
---|
133 | ClientData pluginData; /* data created by plugin init routine */ |
---|
134 | RpOptimEvaluator *evalProc; /* called during optimization to do run */ |
---|
135 | char *fitnessExpr; /* fitness function in string form */ |
---|
136 | ClientData toolData; /* data used during tool execution */ |
---|
137 | RpOptimParam **paramList; /* list of input parameters to vary */ |
---|
138 | int numParams; /* current number of parameters */ |
---|
139 | int maxParams; /* storage for this many paramters */ |
---|
140 | } RpOptimEnv; |
---|
141 | |
---|
142 | /* |
---|
143 | * Here are the functions in the API: |
---|
144 | */ |
---|
145 | EXTERN RpOptimEnv* RpOptimCreate _ANSI_ARGS_((RpOptimPlugin *pluginDefn)); |
---|
146 | |
---|
147 | EXTERN RpOptimParam* RpOptimAddParamNumber _ANSI_ARGS_((RpOptimEnv *envPtr, |
---|
148 | char *name)); |
---|
149 | |
---|
150 | EXTERN RpOptimParam* RpOptimAddParamString _ANSI_ARGS_((RpOptimEnv *envPtr, |
---|
151 | char *name)); |
---|
152 | |
---|
153 | EXTERN RpOptimParam* RpOptimFindParam _ANSI_ARGS_((RpOptimEnv *envPtr, |
---|
154 | char *name)); |
---|
155 | |
---|
156 | EXTERN void RpOptimDeleteParam _ANSI_ARGS_((RpOptimEnv *envPtr, char *name)); |
---|
157 | |
---|
158 | EXTERN void RpOptimDelete _ANSI_ARGS_((RpOptimEnv *envPtr)); |
---|
159 | |
---|
160 | |
---|
161 | #endif |
---|