forked from jibsen/galib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.C
180 lines (149 loc) · 4.28 KB
/
seed.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* ----------------------------------------------------------------------------
seed.C
mbwall 16nov98
---------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <ga/ga.h>
#include <ga/std_stream.h>
#define cout STD_COUT
#define cerr STD_CERR
#define ifstream STD_IFSTREAM
float objective(GAGenome &);
int
main(int argc, char *argv[]) {
cout << "Random Seed Test\n\n";
cout << "This program does three runs of a genetic algorithm, with the \n";
cout << "random seed resetting between each run. Each of the three runs \n";
cout << "should be identical\n\n";
cout.flush();
GAParameterList params;
GASteadyStateGA::registerDefaultParameters(params);
params.set(gaNnGenerations, 100);
params.set(gaNflushFrequency, 5);
params.set(gaNpMutation, 0.001);
params.set(gaNpCrossover, 0.8);
params.parse(argc, argv, gaFalse);
int i,j;
char filename[128] = "smiley.txt";
unsigned int seed=0;
for(i=1; i<argc; i++){
if(strcmp("file", argv[i]) == 0 || strcmp("f", argv[i]) == 0){
if(++i >= argc){
cerr << argv[0] << ": the file option needs a filename.\n";
exit(1);
}
else{
sprintf(filename, argv[i]);
continue;
}
}
else if(strcmp("seed", argv[i]) == 0){
if(++i >= argc){
cerr << argv[0] << ": the seed option needs a filename.\n";
exit(1);
}
else {
seed = atoi(argv[i]);
continue;
}
}
else {
cerr << argv[0] << ": unrecognized arguement: " << argv[i] << "\n\n";
cerr << "valid arguments include standard GAlib arguments plus:\n";
cerr << " f\tfilename from which to read (" << filename << ")\n";
cerr << "\n";
exit(1);
}
}
const int n=5;
cout << n << " random numbers\n";
GAResetRNG(seed);
for(i=0; i<n; i++)
cout << " " << GARandomFloat();
cout << "\n";
cout << n << " random numbers\n";
GAResetRNG(seed);
for(i=0; i<n; i++)
cout << " " << GARandomFloat();
cout << "\n";
cout << n << " random numbers\n";
GAResetRNG(seed);
for(i=0; i<n; i++)
cout << " " << GARandomFloat();
cout << "\n";
cout.flush();
ifstream inStream(filename);
if(!inStream){
cerr << "Cannot open " << filename << " for input.\n";
exit(1);
}
int height, width;
inStream >> height >> width;
short **target = new short*[width];
for(i=0; i<width; i++)
target[i] = new short[height];
for(j=0; j<height; j++)
for(i=0; i<width; i++)
inStream >> target[i][j];
inStream.close();
GA2DBinaryStringGenome genome(width, height, objective, (void *)target);
GASimpleGA ga(genome);
ga.parameters(params);
// first run
GAResetRNG(seed);
genome.initialize();
cout << genome << "\n";
ga.set(gaNscoreFilename, "bog1.dat");
ga.evolve();
genome = ga.statistics().bestIndividual();
cout << "run 1: the random seed is: " << GAGetRandomSeed() << "\n";
for(j=0; j<height; j++){
for(i=0; i<width; i++)
cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
cout << "\n";
}
cout << "\n"; cout.flush();
// second run
GAResetRNG(seed);
genome.initialize();
cout << genome << "\n";
ga.set(gaNscoreFilename, "bog2.dat");
ga.evolve();
genome = ga.statistics().bestIndividual();
cout << "run 2: the random seed is: " << GAGetRandomSeed() << "\n";
for(j=0; j<height; j++){
for(i=0; i<width; i++)
cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
cout << "\n";
}
cout << "\n"; cout.flush();
// third run
GAResetRNG(seed);
genome.initialize();
cout << genome << "\n";
ga.set(gaNscoreFilename, "bog3.dat");
ga.evolve();
genome = ga.statistics().bestIndividual();
cout << "run 3: the random seed is: " << GAGetRandomSeed() << "\n";
for(j=0; j<height; j++){
for(i=0; i<width; i++)
cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
cout << "\n";
}
cout << "\n"; cout.flush();
for(i=0; i<width; i++)
delete target[i];
delete [] target;
return 0;
}
float
objective(GAGenome & c) {
GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)c;
short **pattern = (short **)c.userData();
float value=0.0;
for(int i=0; i<genome.width(); i++)
for(int j=0; j<genome.height(); j++)
value += (float)(genome.gene(i,j) == pattern[i][j]);
return(value);
}