-
Notifications
You must be signed in to change notification settings - Fork 6
/
testprogdertr.c
137 lines (129 loc) · 3.67 KB
/
testprogdertr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <assert.h>
#include "pgapack.h"
/* The fortran subroutine */
extern void objfcn_ (int *, double *, double *, int *);
double x [100];
int nprob = 0;
static const int popsizes [] =
/* 1/10 2/11 3/12 4/13 5/14 6/15 7/16 8/17 9/18 */
{ 6, 50, 250, 60, 60, 6, 4, 40, 8
, 4, 4, 4, 10, 16, 4, 4, 4, 10
};
/* Initialization ranges: Many badly scaled functions don't work at all
* if we do not provide a suitable initialization range. The default for
* is the interval [0, 1].
*/
static const double * init_lower [] =
{ NULL
, (double []){ 0, 0, 0, 0, 0, 0 }
, NULL
, (double []){ 0, 0 }
, (double []){ -10, -10, -10 }
, NULL
, (double []){ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
, -1
}
, NULL
, NULL
, (double []){ 0, 0 }
, (double []){ 0, 0, 0, 0 }
, (double []){ 0, 0, 0 }
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
static const double * init_upper [] =
{ NULL
, (double []){ 20, 20, 20, 20, 20, 20 }
, NULL
, (double []){ 20, 20 }
, (double []){ 10, 10, 10 }
, NULL
, (double []){ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1
}
, NULL
, NULL
, (double []){ 1e8, 1 }
, (double []){ 10, 10, 10, 10 }
, (double []){ 100, 100, 100 }
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
double evaluate (PGAContext *ctx, int p, int pop, double *aux)
{
int i;
int dimension = PGAGetStringLength (ctx);
double e;
for (i=0; i<dimension; i++) {
x [i] = PGAGetRealAllele (ctx, p, pop, i);
}
objfcn_ (&dimension, x, &e, &nprob);
return e;
}
/*
* We get 3 arguments:
* - The problem number
* - The dimension of the problem
* - The number of iterations
*/
int main (int argc, char **argv)
{
int dimension = 0;
int maxiter = 0;
PGAContext *ctx;
if (argc != 4) {
fprintf ( stderr
, "Usage: %s <problem> <dimension> <iterations>\n"
, argv [0]
);
exit (1);
}
nprob = atoi (argv [1]);
dimension = atoi (argv [2]);
maxiter = atoi (argv [3]);
if (nprob < 1 || nprob > 18) {
fprintf (stderr, "Problem number 1 <= p <= 18\n");
exit (1);
}
if (dimension < 2 || dimension > 100) {
fprintf (stderr, "Dimension 2 <= D <= 100\n");
exit (1);
}
if (maxiter < 10) {
fprintf (stderr, "Iterations >= 10\n");
exit (1);
}
ctx = PGACreate (&argc, argv, PGA_DATATYPE_REAL, dimension, PGA_MINIMIZE);
PGASetMaxGAIterValue (ctx, maxiter);
PGASetPopSize (ctx, popsizes [nprob - 1]);
PGASetNumReplaceValue (ctx, popsizes [nprob - 1]);
if (init_lower [nprob - 1] != NULL) {
assert (init_upper [nprob - 1] != NULL);
PGASetRealInitRange (ctx, init_lower [nprob-1], init_upper [nprob-1]);
}
PGASetRandomSeed (ctx, 1);
PGASetSelectType (ctx, PGA_SELECT_LINEAR);
PGASetPopReplaceType (ctx, PGA_POPREPL_RTR);
PGASetMutationOnlyFlag (ctx, PGA_TRUE);
PGASetMutationType (ctx, PGA_MUTATION_DE);
PGASetDECrossoverProb (ctx, 0.2);
PGASetDECrossoverType (ctx, PGA_DE_CROSSOVER_BIN);
PGASetDEVariant (ctx, PGA_DE_VARIANT_RAND);
PGASetDEScaleFactor (ctx, 0.85);
PGASetUp (ctx);
PGARun (ctx, evaluate);
PGADestroy (ctx);
}