source: trunk/packages/optimizer/src/pgapack/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.8 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 *******************************************************************/
24void 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
56
57double GetTerm(PGAContext *ctx, int p, int pop, int t, int problem) {
58    double    x;
59    int       len;
60    double    l, u;
61
62    len = BinLen[problem];
63    l   = Lower[problem];
64    u   = Upper[problem];
65
66    if (gray_on)
67        x = PGAGetRealFromGrayCode(ctx, p, pop, t*len, (t+1)*len-1, l, u);
68    else
69        x = PGAGetRealFromBinary(ctx, p, pop, t*len, (t+1)*len-1, l, u);
70    return(x);
71}
72
73
74double dejong1(PGAContext *ctx, int p, int pop) {
75    int i;
76    double term, sum = 0;
77   
78    for(i = 0; i < NumCoords[0]; i++) {
79        term = GetTerm(ctx, p, pop, i, 0);
80        sum += (term * term);
81    }
82   
83    return (sum);
84}
85
86
87double dejong2(PGAContext *ctx, int p, int pop) {
88    double x1, x2, p1, p2;
89   
90    x1 = GetTerm(ctx, p, pop, 0, 1);
91    x2 = GetTerm(ctx, p, pop, 1, 1);
92
93    p1 = x1 * x1 - x2;
94    p2 = 1 - x1;
95   
96    return (100 * p1 * p1 + p2 * p2);
97}
98
99double dejong3(PGAContext *ctx, int p, int pop) {
100    int i;
101    double sum = 0;
102   
103    for(i = 0; i < NumCoords[2]; i++)
104        sum += floor(GetTerm(ctx, p, pop, i, 2));
105
106    return (sum);
107}
108
109double dejong4(PGAContext *ctx, int p, int pop) {
110    int i;
111    double term, sum = 0;
112   
113    for(i = 0; i < NumCoords[3]; i++) {
114        term = GetTerm(ctx, p, pop, i, 3);
115        sum += ((i + 1) * term * term * term * term);
116    }
117   
118    return (sum + PGARandomGaussian(ctx, 0, 1));
119}
120
121double dejong5(PGAContext *ctx, int p, int pop) {
122    int    a[2][25];
123    int    i, j;
124    double sum_over_i = 0, sum_over_j = 0;
125
126    for (i=0; i<5; i++) {
127        a[0][5*i]   = -32;
128        a[1][i]     = -32;
129
130        a[0][5*i+1] = -16;
131        a[1][i+5]   = -16;
132
133        a[0][5*i+2] = 0;
134        a[1][i+10]  = 0;
135
136        a[0][5*i+3] = 16;
137        a[1][i+15]  = 16;
138
139        a[0][5*i+4] = 32;
140        a[1][i+20]  = 32;
141    }
142
143    for (j = 0; j < 25; j++) {
144        sum_over_i =
145            pow(GetTerm(ctx, p, pop, 0, 4) - a[0][j], 6) +
146            pow(GetTerm(ctx, p, pop, 1, 4) - a[1][j], 6);
147        sum_over_j += (1.0 / (j + sum_over_i));
148    }
149   
150    return (1.0 / (0.002 + sum_over_j));
151}
152
153
154void printResultInterpretation(PGAContext *ctx, int problem) {
155    int      best, i;
156    double   value;
157
158    if (PGAGetRank(ctx, MPI_COMM_WORLD) == 0) {
159        best = PGAGetBestIndex(ctx, PGA_OLDPOP);
160       
161        printf("The real interpretation:\n");
162        for (i = 0; i < NumCoords[problem]; i++) {
163            value = GetTerm(ctx, best, PGA_OLDPOP, i, problem);
164           
165            switch ( i % 5 ) {
166            case 0:
167                printf ("#%4d: [%11.7g]", i, value);
168                break;
169            case 1:
170            case 2:
171            case 3:
172                printf (", [%11.7g]", value);
173                break;
174            case 4:
175                printf (", [%11.7g]", value);
176                if (i+1 < NumCoords[problem])
177                    printf ("\n");
178                break;
179            }
180        }
181        printf("\n");
182    }
183}
184
185
186/*  Get an integer parameter from the user.  Since this is
187 *  typically a parallel program, we must only do I/O on the
188 *  "master" process -- process 0.  Once we read the parameter,
189 *  we broadcast it to all the other processes, then every
190 *  process returns the correct value.
191 */
192int GetIntegerParameter(char *query) {
193    int  rank, tmp;
194
195    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
196    if (rank == 0) {
197        printf(query);
198        scanf("%d", &tmp);
199    }
200    MPI_Bcast(&tmp, 1, MPI_INT, 0, MPI_COMM_WORLD);
201    return(tmp);
202}
Note: See TracBrowser for help on using the repository browser.