source: trunk/packages/optimizer/src/pgapack/gekco/pgapack/examples/c/dejong.c @ 5673

Last change on this file since 5673 was 5673, checked in by ldelgass, 9 years ago

Fix line endings, set eol-style to native on all C/C++ sources.

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1/*  The DeJong test suite.
2 *
3 */
4#include <pgapack.h>
5
6double dejong1(PGAContext *, int, int);
7double dejong2(PGAContext *, int, int);
8double dejong3(PGAContext *, int, int);
9double dejong4(PGAContext *, int, int);
10double dejong5(PGAContext *, int, int);
11void   printResultInterpretation(PGAContext *, int);
12int    GetIntegerParameter(char *query);
13
14int    gray_on;
15
16int    BinLen[5]     = { 10, 12, 10, 8, 17 };
17int    NumCoords[5]  = { 3, 2, 5, 30, 2 };
18double Lower[5]      = { -5.12, -2.048, -5.12, -1.28, -65.536 };
19double Upper[5]      = { 5.11, 2.047, 5.11, 1.27, 65.535 };
20
21/*******************************************************************
22 *                   user main program                              *
23 *******************************************************************/
24int main( int argc, char **argv ) {
25    PGAContext *ctx;    /* the context variable */
26    int testnum;        /* the DeJong test to run */
27    int maxiter;        /* the maximum number of iterations */
28
29    MPI_Init(&argc, &argv);
30
31    testnum = GetIntegerParameter("Which test? (1 - 5)\n") - 1;
32    gray_on = GetIntegerParameter("Gray-coded? (0 = no)\n");
33    maxiter = GetIntegerParameter("How many iterations?\n");
34
35    ctx = PGACreate(&argc, argv, PGA_DATATYPE_BINARY,
36                    BinLen[testnum]*NumCoords[testnum], PGA_MINIMIZE);
37   
38    PGASetMaxGAIterValue(ctx, maxiter);
39    PGASetRandomSeed(ctx, 1);
40   
41    PGASetUp(ctx);
42
43    if (testnum == 0)    PGARun(ctx, dejong1);
44    if (testnum == 1)    PGARun(ctx, dejong2);
45    if (testnum == 2)    PGARun(ctx, dejong3);
46    if (testnum == 3)    PGARun(ctx, dejong4);
47    if (testnum == 4)    PGARun(ctx, dejong5);
48
49    printResultInterpretation(ctx, testnum);
50
51    PGADestroy(ctx);
52   
53    MPI_Finalize();
54
55    return 0;
56}
57
58double GetTerm(PGAContext *ctx, int p, int pop, int t, int problem);
59
60double GetTerm(PGAContext *ctx, int p, int pop, int t, int problem) {
61    double    x;
62    int       len;
63    double    l, u;
64
65    len = BinLen[problem];
66    l   = Lower[problem];
67    u   = Upper[problem];
68
69    if (gray_on)
70        x = PGAGetRealFromGrayCode(ctx, p, pop, t*len, (t+1)*len-1, l, u);
71    else
72        x = PGAGetRealFromBinary(ctx, p, pop, t*len, (t+1)*len-1, l, u);
73    return(x);
74}
75
76
77double dejong1(PGAContext *ctx, int p, int pop) {
78    int i;
79    double term, sum = 0;
80   
81    for(i = 0; i < NumCoords[0]; i++) {
82        term = GetTerm(ctx, p, pop, i, 0);
83        sum += (term * term);
84    }
85   
86    return (sum);
87}
88
89
90double dejong2(PGAContext *ctx, int p, int pop) {
91    double x1, x2, p1, p2;
92   
93    x1 = GetTerm(ctx, p, pop, 0, 1);
94    x2 = GetTerm(ctx, p, pop, 1, 1);
95
96    p1 = x1 * x1 - x2;
97    p2 = 1 - x1;
98   
99    return (100 * p1 * p1 + p2 * p2);
100}
101
102double dejong3(PGAContext *ctx, int p, int pop) {
103    int i;
104    double sum = 0;
105   
106    for(i = 0; i < NumCoords[2]; i++)
107        sum += floor(GetTerm(ctx, p, pop, i, 2));
108
109    return (sum);
110}
111
112double dejong4(PGAContext *ctx, int p, int pop) {
113    int i;
114    double term, sum = 0;
115   
116    for(i = 0; i < NumCoords[3]; i++) {
117        term = GetTerm(ctx, p, pop, i, 3);
118        sum += ((i + 1) * term * term * term * term);
119    }
120   
121    return (sum + PGARandomGaussian(ctx, 0, 1));
122}
123
124double dejong5(PGAContext *ctx, int p, int pop) {
125    int    a[2][25];
126    int    i, j;
127    double sum_over_i = 0, sum_over_j = 0;
128
129    for (i=0; i<5; i++) {
130        a[0][5*i]   = -32;
131        a[1][i]     = -32;
132
133        a[0][5*i+1] = -16;
134        a[1][i+5]   = -16;
135
136        a[0][5*i+2] = 0;
137        a[1][i+10]  = 0;
138
139        a[0][5*i+3] = 16;
140        a[1][i+15]  = 16;
141
142        a[0][5*i+4] = 32;
143        a[1][i+20]  = 32;
144    }
145
146    for (j = 0; j < 25; j++) {
147        sum_over_i =
148            pow(GetTerm(ctx, p, pop, 0, 4) - a[0][j], 6) +
149            pow(GetTerm(ctx, p, pop, 1, 4) - a[1][j], 6);
150        sum_over_j += (1.0 / (j + sum_over_i));
151    }
152   
153    return (1.0 / (0.002 + sum_over_j));
154}
155
156
157void printResultInterpretation(PGAContext *ctx, int problem) {
158    int      best, i;
159    double   value;
160
161    if (PGAGetRank(ctx, MPI_COMM_WORLD) == 0) {
162        best = PGAGetBestIndex(ctx, PGA_OLDPOP);
163       
164        printf("The real interpretation:\n");
165        for (i = 0; i < NumCoords[problem]; i++) {
166            value = GetTerm(ctx, best, PGA_OLDPOP, i, problem);
167           
168            switch ( i % 5 ) {
169            case 0:
170                printf ("#%4d: [%11.7g]", i, value);
171                break;
172            case 1:
173            case 2:
174            case 3:
175                printf (", [%11.7g]", value);
176                break;
177            case 4:
178                printf (", [%11.7g]", value);
179                if (i+1 < NumCoords[problem])
180                    printf ("\n");
181                break;
182            }
183        }
184        printf("\n");
185    }
186}
187
188
189/*  Get an integer parameter from the user.  Since this is
190 *  typically a parallel program, we must only do I/O on the
191 *  "master" process -- process 0.  Once we read the parameter,
192 *  we broadcast it to all the other processes, then every
193 *  process returns the correct value.
194 */
195int GetIntegerParameter(char *query) {
196    int  rank, tmp;
197
198    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
199    if (rank == 0) {
200        printf(query);
201        scanf("%d", &tmp);
202    }
203    MPI_Bcast(&tmp, 1, MPI_INT, 0, MPI_COMM_WORLD);
204    return(tmp);
205}
Note: See TracBrowser for help on using the repository browser.