-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopulation.cc
411 lines (373 loc) · 9.5 KB
/
population.cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# include <population.h>
# include <math.h>
# include <iostream>
//# define MAX_RULE 65536
# define MAX_RULE 256
/* Population constructor */
/* Input: genome count , genome size, pointer to Program instance */
Population::Population(int gcount,int gsize,Program *p)
{
elitism=1;
selection_rate = 0.1;
mutation_rate = 0.05;
genome_count = gcount;
genome_size = gsize;
generation = 0;
program = p;
/* Create the population and based on genome count and size */
/* Initialize the genomes to random */
genome=new int*[genome_count];
children=new int*[genome_count];
for(int i=0;i<genome_count;i++)
{
genome[i]=new int[genome_size];
children[i]=new int[genome_size];
for(int j=0;j<genome_size;j++)
genome[i][j]=rand()%MAX_RULE;
}
fitness_array=new double[genome_count];
}
/* Reinitialize the population to random */
void Population::reset()
{
generation = 0;
for(int i=0;i<genome_count;i++)
for(int j=0;j<genome_size;j++)
genome[i][j]=rand()%MAX_RULE;
for(int i=0;i<genome_count;i++)
fitness_array[i]=-1e+8;
}
/* Return the fitness of a genome */
double Population::fitness(vector<int> &g)
{
double f=program->fitness(g);
return f;
}
/* The selection of the chromosomes according to the fitness value is performed */
void Population::select()
{
int itemp[genome_size];
for(int i=0;i<genome_count;i++)
{
for(int j=0;j<genome_count-1;j++)
{
if(fitness_array[j+1]>fitness_array[j])
{
double dtemp;
dtemp=fitness_array[j];
fitness_array[j]=fitness_array[j+1];
fitness_array[j+1]=dtemp;
memcpy(itemp,genome[j],genome_size*sizeof(int));
memcpy(genome[j],genome[j+1],genome_size*sizeof(int));
memcpy(genome[j+1],itemp,genome_size*sizeof(int));
}
}
}
}
/* Crossover operation: based on tournament selection */
/* Select the tournament_size based on the genome count : */
/* (if genome_count > 100 ) tournament_size = 10 else tournament_size = 4 */
/* Select 2 chromosomes based on the tournament size and cross them over based on the crossover probability */
/* There is 1 crossover point and it is random */
void Population::crossover()
{
int parent[2];
int nchildren=(int)((1.0 - selection_rate) * genome_count);
if(!(nchildren%2==0)) nchildren++;
const int tournament_size =(genome_count<=100)?4:10;
int count_children=0;
while(1)
{
// The two parents are selected here according to the tournament selection procedure
for(int i=0;i<2;i++)
{
double max_fitness=-1e+10;
int max_index=-1;
int r;
// Select the best parents of the candidates
for(int j=0;j<tournament_size;j++)
{
r=rand() % (genome_count);
if(j==0 || fitness_array[r]>max_fitness)
{
max_index=r;
max_fitness=fitness_array[r];
}
}
parent[i]=max_index;
}
int pt1;
// The one-point crossover is performed here (the point is pt1)
pt1=rand() % genome_size;
memcpy(children[count_children],
genome[parent[0]],pt1 * sizeof(int));
memcpy(&children[count_children][pt1],
&genome[parent[1]][pt1],(genome_size-pt1)*sizeof(int));
memcpy(children[count_children+1],
genome[parent[1]],pt1 * sizeof(int));
memcpy(&children[count_children+1][pt1],
&genome[parent[0]][pt1],(genome_size-pt1)*sizeof(int));
count_children+=2;
if(count_children>=nchildren) break;
}
for(int i=0;i<nchildren;i++)
{
memcpy(genome[genome_count-i-1],
children[i],genome_size * sizeof(int));
}
}
void Population::setElitism(int s)
{
elitism = s;
}
/* Mutate the current population */
/* Standard mutation algorithm: mutate all chromosomes in the population based on the mutation probability */
void Population::mutate()
{
int start = elitism * (int)(genome_count*selection_rate);
start = elitism;
start = 1;
for(int i=start;i<genome_count;i++)
{
for(int j=0;j<genome_size;j++)
{
double r=rand()*1.0/RAND_MAX;
if(r<mutation_rate)
{
genome[i][j]=rand() % MAX_RULE;
}
}
}
}
/* Evaluate the fitness for all chromosomes in the current population */
void Population::calcFitnessArray()
{
vector<int> g;
g.resize(genome_size);
double dmin = 1e+100;
for(int i=0;i<genome_count;i++)
{
for(int j=0;j<genome_size;j++) g[j]=genome[i][j];
fitness_array[i]=fitness(g);
if(fabs(fitness_array[i])<dmin) dmin=fabs(fitness_array[i]);
if(i%10==0)
{
printf("%d:%lf ",i,dmin);
fflush(stdout);
}
}
printf("\n");
}
/* Return the current generation */
int Population::getGeneration() const
{
return generation;
}
/* Return the genome count */
int Population::getCount() const
{
return genome_count;
}
/* Return the size of the population */
int Population::getSize() const
{
return genome_size;
}
/* Evolve the next generation */
void Population::nextGeneration()
{
if(generation==0)
calcFitnessArray();
select();
crossover();
mutate();
calcFitnessArray();
extern int localSearchGenerations,localSearchChromosomes;
if((generation+1)%100==0)
for(int i=0;i<20;i++)
localSearch(rand() % genome_count);
++generation;
}
void Population::localSearch(int gpos)
{
vector<int> g;
g.resize(genome_size);
int pos=gpos;
for(int iters=1;iters<=100;iters++)
{
int randgenome=rand() % genome_count;
int randpos=rand() % genome_size;
for(int i=0;i<randpos;i++) g[i]=genome[pos][i];
for(int i=randpos;i<genome_size;i++) g[i]=genome[randgenome][i];
double f=fitness(g);
if(fabs(f)<fabs(fitness_array[pos]))
{
for(int i=0;i<genome_size;i++) genome[pos][i]=g[i];
fitness_array[pos]=f;
}
else
{
for(int i=0;i<randpos;i++) g[i]=genome[randgenome][i];
for(int i=randpos;i<genome_size;i++) g[i]=genome[pos][i];
f=fitness(g);
if(fabs(f)<fabs(fitness_array[pos]))
{
for(int i=0;i<genome_size;i++) genome[pos][i]=g[i];
fitness_array[pos]=f;
}
}
}
return;
}
/* Set the mutation rate */
void Population::setMutationRate(double r)
{
if(r>=0 && r<=1) mutation_rate = r;
}
/* Set the selection rate */
void Population::setSelectionRate(double r)
{
if(r>=0 && r<=1) selection_rate = r;
}
/* Return the selection rate */
double Population::getSelectionRate() const
{
return selection_rate;
}
/* Return the mutation rate */
double Population::getMutationRate() const
{
return mutation_rate;
}
/* Return the best fitness for this population */
double Population::getBestFitness() const
{
return fitness_array[0];
}
/* Return the best chromosome */
vector<int> Population::getBestGenome() const
{
vector<int> g;g.resize(genome_size);
for(int i=0;i<genome_size;i++) g[i]=genome[0][i];
return g;
}
/* Evaluate and return the best fitness for all chromosomes in the population */
double Population::evaluateBestFitness()
{
vector<int> g;g.resize(genome_size);
for(int i=0;i<genome_size;i++) g[i]=genome[0][i];
return fitness(g);
}
void Population::setBest(vector<int> &g,double f)
{
double tf=fitness(g);
if(tf<fitness_array[0] && fabs(tf-f)>1e-4)
{
return;
}
if(g.size()>genome_size)
{
int *old=new int[genome_size];
for(int i=0;i<genome_count;i++)
{
for(int j=0;j<genome_size;j++) old[j]=genome[i][j];
delete[] genome[i];
genome[i]=new int[g.size()];
for(int j=0;j<g.size();j++) genome[i][j]=0;
for(int j=0;j<genome_size;j++) genome[i][j]=old[j];
delete[] children[i];
children[i]=new int[g.size()];
}
genome_size=g.size();
extern int genome_length;
genome_length=genome_size;
delete[] old;
}
int pos=0;
for(int i=0;i<genome_size;i++)
{
genome[pos][i]=g[i];
}
fitness_array[pos]=f;
}
void Population::setGenome(int pos,vector<int> &g,double f,int k)
{
double tf=fitness(g);
if(tf<fitness_array[pos] && fabs(tf-f)>1e-4) return;
if(g.size()>genome_size)
{
int *old=new int[genome_size];
for(int i=0;i<genome_count;i++)
{
for(int j=0;j<genome_size;j++) old[j]=genome[i][j];
delete[] genome[i];
genome[i]=new int[g.size()];
for(int j=0;j<g.size();j++) genome[i][j]=0;
int ipos=0;
for(int l=0;l<k;l++)
{
for(int j=0;j<genome_size/k;j++)
{
genome[i][l*g.size()/k+j]=old[ipos];
ipos++;
}
}
delete[] children[i];
children[i]=new int[g.size()];
}
genome_size=g.size();
extern int genome_length;
genome_length=genome_size;
delete[] old;
}
for(int i=0;i<genome_size;i++)
{
genome[pos][i]=g[i];
}
fitness_array[pos]=f;
}
void Population::setGenome(int pos,vector<int> &g,double f)
{
double tf=fitness(g);
if(tf<fitness_array[pos] && fabs(tf-f)>1e-4) return;
if(g.size()>genome_size)
{
int *old=new int[genome_size];
for(int i=0;i<genome_count;i++)
{
for(int j=0;j<genome_size;j++) old[j]=genome[i][j];
delete[] genome[i];
genome[i]=new int[g.size()];
for(int j=0;j<g.size();j++) genome[i][j]=0;
for(int j=0;j<genome_size;j++) genome[i][j]=old[j];
delete[] children[i];
children[i]=new int[g.size()];
}
genome_size=g.size();
extern int genome_length;
genome_length=genome_size;
delete[] old;
}
for(int i=0;i<genome_size;i++)
{
genome[pos][i]=g[i];
}
fitness_array[pos]=f;
}
void Population::getGenome(int pos,vector<int> &g)
{
g.resize(genome_size);
for(int i=0;i<g.size();i++) g[i]=genome[pos][i];
}
/* Destructor */
Population::~Population()
{
for(int i=0;i<genome_count;i++)
{
delete[] children[i];
delete[] genome[i];
}
delete[] genome;
delete[] children;
delete[] fitness_array;
}