-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAchromosome.h
264 lines (213 loc) · 11.4 KB
/
GAchromosome.h
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
/***************************************************************************
GAchromosome.h - description
-------------------
begin : Fri Sep 27 2002
copyright : (C) 2002 by Craig Nicol
email : craig.nicol@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/***************************************************************************
* CURRENT STATUS *
* # chromosome is fully templated, but the sep string for printing a *
* chromosome can lead to a segfault. *
* # population_bitfield is templated against chromosome and *
* its children. *
* # population_bitfield has a decent stochastic selector. *
* # population_bitfield needs a decent wander selector. *
***************************************************************************/
#ifndef __GACHROMOSOME_H__
#define __GACHROMOSOME_H__
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <strstream>
#include <string>
#include <algorithm>
#include <type_traits>
#include <limits.h>
#include <typeinfo>
template<typename T> std::string is_true(std::string truestr, std::string falsestr, std::__false_type) { return falsestr; };
template<typename T> std::string is_true(std::string truestr, std::string falsestr, std::__true_type) { return truestr; };
template<typename T>
std::string get_type() {
return typeid(T).name(); // Only returns one character
}
namespace mg_GA {
/***********************************************************************
* SAMPLE FITNESS FUNCTIONS *
***********************************************************************/
template<class _Ctype, int _Csize>
struct fitness_base {
virtual double operator()(_Ctype chrom[_Csize]) = 0;
} ;
template<class _Ctype, int _Csize>
struct maxones : public fitness_base<_Ctype,_Csize>{
double operator()(_Ctype chrom[_Csize]) {
return std::count(chrom, chrom+_Csize, 1);
};
} ;
template<class _Ctype, int _Csize>
struct crossover_base {
virtual _Ctype * operator()(_Ctype chrom_one[_Csize], _Ctype chrom_two[_Csize], _Ctype result[_Csize]) = 0;
} ;
template<class _Ctype, int _Csize>
struct allpointx: public crossover_base<_Ctype,_Csize> {
_Ctype * operator()(_Ctype chrom_one[_Csize], _Ctype chrom_two[_Csize], _Ctype result[_Csize]) {
for(int i=0; i<_Csize; i++)
result[i] = rand()%2?chrom_one[i]:chrom_two[i];
return result;
}
} ;
template<class _Ctype, int _Csize>
struct onepointx: public crossover_base<_Ctype,_Csize> {
_Ctype * operator()(_Ctype chrom_one[_Csize], _Ctype chrom_two[_Csize], _Ctype result[_Csize]) {
_Ctype* first;
_Ctype* second;
int point = rand()%_Csize;
int start_chrom = rand()%2;
if(start_chrom == 1) {
first = chrom_one;
second = chrom_two;
} else {
first = chrom_two;
second = chrom_one;
}
// would memcpy be more efficient here?
for(int i=0; i<point; ++i)
result[i] = first[i];
for(int i=point; i<_Csize; ++i)
result[i] = second[i];
return result;
}
} ;
template<class _Ctype, int _Csize>
struct twopointx: public crossover_base<_Ctype,_Csize> {
_Ctype * operator()(_Ctype chrom_one[_Csize], _Ctype chrom_two[_Csize], _Ctype result[_Csize]) {
_Ctype* first;
_Ctype* second;
int point1 = rand()%_Csize;
int point2 = rand()%_Csize;
if(point1>point2) {
int temp = point1;
point1 = point2;
point2 = temp;
}
//assert(point1 < _Csize);
//assert(point2 < _Csize);
int start_chrom = rand()%2;
if(start_chrom == 1) {
first = chrom_one;
second = chrom_two;
} else {
first = chrom_two;
second = chrom_one;
}
// would memcpy be more efficient here?
for(int i=0; i<point1; ++i)
result[i] = first[i];
for(int i=point1; i<point2; ++i)
result[i] = second[i];
for(int i=point2; i<_Csize; ++i)
result[i] = first[i];
return result;
}
} ;
template<class _Ctype, int _Csize>
struct mutate_base {
// mutate_base() { std::cout << "<" << typeid(_Ctype) << ", " << _Csize << ">" << std::endl; };
virtual _Ctype * operator()(_Ctype chrom[_Csize], float mr) = 0;
} ;
template<class _Ctype, int _Csize>
struct random_mutate: public mutate_base<_Ctype, _Csize> {
// random_mutate() { std::cout << "r<" << typeid(_Ctype) << ", " << _Csize << ">" << std::endl; };
_Ctype * operator()(_Ctype chrom[_Csize], float mr) {
for(int i=0; i<_Csize; i++)
chrom[i] = ((rand() % INT_MAX)<(mr*INT_MAX))?(rand() % _Csize):chrom[i];
return chrom;
};
};
template<typename T>
void arg_func(double ff(T * i) ) {} ;
/***********************************************************************
* *
* CHROMOSOME TEMPLATE DEFINITION *
* *
***********************************************************************/
template<class _CTYPE = bool, int _Csize = 32, int _Numvals = 2>
class chromosome {
/***********************************************************************
* VARIABLE DEFINITIONS *
***********************************************************************/
protected:
typedef _CTYPE _chrom_t[_Csize];
_chrom_t _chromosome;
double _mrate;
char sep[2];
fitness_base<_CTYPE,_Csize> * _fitfunc;
mutate_base<_CTYPE, _Csize> * _mutatefunc;
crossover_base<_CTYPE, _Csize> * _crossfunc;
// Defaults
// static allpointx<_CTYPE, _Csize> defaultcross;
/***********************************************************************
* FUNCTION DEFINITIONS *
***********************************************************************/
private:
void set_sep() {sep[1] = '\0'; if ((_Numvals < 10) && (_Numvals > 0)) sep[0] = '\0'; else sep[0] = '-'; /* assert(_crossfunc!=NULL); */ } ;
protected:
chromosome() {} ; // Disable default constructor
// Function below SEGFAULTs if result is returned directly when called
// from chromosome < chromosome
double calcfitness() { double result = (*_fitfunc)(_chromosome); return result; /* assert(_crossfunc!=NULL); */ };
void init_chrom() {for(int i=0; i<_Csize; i++) _chromosome[i] = rand() % _Numvals; /* assert(_crossfunc!=NULL); */ set_sep();};
// merge_chrom returns the array as its third argument as c++ cannot have an
// array as a return type
void merge_chrom(_chrom_t first, _chrom_t second, _chrom_t result) { /* assert(_crossfunc!=NULL); */ (*_crossfunc)(first, second, result); /* assert(_crossfunc!=NULL); */ };
chromosome(_chrom_t& chrom, fitness_base<_CTYPE,_Csize> * ff, crossover_base<_CTYPE,_Csize> * xf, mutate_base<_CTYPE,_Csize> * mf, double mrate = 0.01) : _mrate(mrate), _fitfunc(ff), _mutatefunc(mf), _crossfunc(xf) { /* assert(_crossfunc!=NULL); assert(_mutatefunc!=NULL); */ std::copy(chrom, chrom+_Csize, _chromosome); set_sep(); };
void mutate() { (*_mutatefunc)(_chromosome, _mrate); };
public:
chromosome(fitness_base<_CTYPE,_Csize>* ff, crossover_base<_CTYPE,_Csize> * xf, mutate_base<_CTYPE,_Csize> * mf, double mrate = 0.01) : _mrate(mrate), _fitfunc(ff), _mutatefunc(mf), _crossfunc(xf) { /* assert(_crossfunc!=NULL); assert(_mutatefunc!=NULL); */ init_chrom(); };
~chromosome() {};
void set_fitfunc(fitness_base<_CTYPE,_Csize>* ff) {_fitfunc = ff; /* assert(_crossfunc!=NULL); */ } ;
chromosome operator+(chromosome second) { _chrom_t result; /* assert(_crossfunc!=NULL); */ merge_chrom(_chromosome,second._chromosome,result); return chromosome(result,_fitfunc,_crossfunc,_mutatefunc); };
chromosome operator~() { mutate(); return *this; };
bool operator[](size_t __pos) {return _chromosome[__pos]; };
std::string tostring() { std::strstream out; out << '<' << get_type<_CTYPE>() << ", " << _Csize << ">" << " chromosome" << '\0'; /* assert(_crossfunc!=NULL); */ return out.str(); };
std::string showchrom() { std::strstream out; for(int i=0; i<_Csize; i++) { out << _chromosome[i] << sep; }; out << '\0'; /* assert(_crossfunc!=NULL); */ return out.str(); };
_CTYPE* getchrom() { return _chromosome; };
double f() {return calcfitness(); } ;
};
/***********************************************************************
* 'FRIENDLY' FUNCTIONS *
***********************************************************************/
template <class _CTYPE, int _Csize, int _Numvals>
static bool operator>(chromosome<_CTYPE,_Csize,_Numvals> first, chromosome<_CTYPE,_Csize,_Numvals> second) { return first.f() > second.f(); };
template <class _CTYPE, int _Csize, int _Numvals>
static bool operator>=(chromosome<_CTYPE,_Csize,_Numvals> first, chromosome<_CTYPE,_Csize,_Numvals> second) { return first.f() >= second.f(); };
template <class _CTYPE, int _Csize, int _Numvals>
static bool operator<(chromosome<_CTYPE,_Csize,_Numvals> first, chromosome<_CTYPE,_Csize,_Numvals> second) { return first.f() < second.f(); };
template <class _CTYPE, int _Csize, int _Numvals>
static bool operator<=(chromosome<_CTYPE,_Csize,_Numvals> first, chromosome<_CTYPE,_Csize,_Numvals> second) { return first.f() <= second.f(); };
/* The cached chromosome here prevents the calculation of the fitness
* function when we know the value hasn't changed.
*/
template <class _Ctype, int _Csize, int _Maxval=2>
class chrom_cached_lazy : public chromosome<_Ctype, _Csize, _Maxval>
{
private:
bool _isvalid;
double _fitness;
public:
chrom_cached_lazy(fitness_base<_Ctype,_Csize>* ff, crossover_base<_Ctype,_Csize> * xf, mutate_base<_Ctype,_Csize> * mf, double mr = 0.01) : _isvalid(false) {this->_mrate = mr; this->_fitfunc = ff; this->_crossfunc = xf; this->_mutatefunc = mf; /* assert(_crossfunc!=NULL); assert(_mutatefunc!=NULL); */ this->init_chrom(); };
double f() {if (!_isvalid) {_fitness = this->calcfitness(); _isvalid = true;} return _fitness; };
chrom_cached_lazy operator+(chrom_cached_lazy second) {/* assert(_crossfunc!=NULL); */ chrom_cached_lazy newc(this->_fitfunc, this->_crossfunc, this->_mutatefunc, this->_mrate); this->merge_chrom(this->_chromosome, second._chromosome, newc._chromosome); return newc; };
chrom_cached_lazy operator~() {this->mutate(); _isvalid = false; return *this;}
};
};
#endif