source: trunk/packages/optimizer/src/test.c @ 1272

Last change on this file since 1272 was 809, checked in by mmc, 17 years ago

Created the initial API for the optimizer. Type "make" in this directory
and then run the "rptest" program to see how it works. The guts of
rp_optimizer.c are missing, but the rest of it works.

File size: 2.5 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  TEST PROGRAM for rp_optimizer
4 *
5 *  This is a simple test program used to exercise the rp_optimizer
6 *  library in Rappture.
7 *
8 * ======================================================================
9 *  AUTHOR:  Michael McLennan, Purdue University
10 *  Copyright (c) 2004-2007  Purdue Research Foundation
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16#include <stdio.h>
17#include <math.h>
18#include "rp_optimizer.h"
19
20RpOptimStatus
21GetValue(values, numValues, fitnessPtr)
22    RpOptimParam **values;         /* array of input values */
23    int numValues;                 /* number of values on the list */
24    double *fitnessPtr;            /* returns: value of fitness */
25{
26    double a = 0.0;
27    double b = 0.0;
28    char* func = "unknown";
29
30    int n;
31    double fval;
32
33    for (n=0; n < numValues; n++) {
34        if (strcmp(values[n]->name,"a") == 0) {
35            a = values[n]->value.num;
36        }
37        else if (strcmp(values[n]->name,"b") == 0) {
38            b = values[n]->value.num;
39        }
40        else if (strcmp(values[n]->name,"func") == 0) {
41            func = values[n]->value.str;
42        }
43        else {
44            printf("ERROR: unrecognized parameter \"%s\"\n", values[n]->name);
45            return RP_OPTIM_FAILURE;
46        }
47    }
48
49    if (strcmp(func,"f1") == 0) {
50        fval = (a == 0.0) ? 1.0 : b*sin(a)/a;
51    }
52    else if (strcmp(func,"f2") == 0) {
53        fval = (b-a == 0.0) ? 1.0 : sin(b-a)/(b-a);
54    }
55    else if (strcmp(func,"another") == 0) {
56        fval = -a*a + b;
57    }
58    else {
59        printf("ERROR: unrecognized func value \"%s\"\n", func);
60        return RP_OPTIM_FAILURE;
61    }
62    printf("a=%g, b=%g func=%s  =>  f=%g\n", a, b, func, fval);
63
64    *fitnessPtr = fval;
65    return RP_OPTIM_SUCCESS;
66}
67
68int
69main(int argc, char** argv)
70{
71    RpOptimEnv *envPtr;
72    RpOptimStatus status;
73    char *funcValues[] = { "f1", "f2", "another", NULL };
74
75    envPtr = RpOptimCreate();
76    RpOptimAddParamNumber(envPtr, "a", 0.0, 1.0);
77    RpOptimAddParamNumber(envPtr, "b", 1.0, 10.0);
78    RpOptimAddParamString(envPtr, "func", funcValues);
79
80    status = RpOptimPerform(envPtr, GetValue, 10);
81
82    if (status == RP_OPTIM_SUCCESS) {
83        printf ("success!\n");
84    } else {
85        printf ("failed :(\n");
86    }
87
88    RpOptimDelete(envPtr);
89    return 0;
90}
Note: See TracBrowser for help on using the repository browser.