-
Notifications
You must be signed in to change notification settings - Fork 1
/
gene.h
249 lines (199 loc) · 5.84 KB
/
gene.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
//------------------------------------------------------------------------------
//
// gene.h
//
//------------------------------------------------------------------------------
#ifndef __GENE_H_
#define __GENE_H_
#include <iostream>
#include <iomanip>
#include <vector>
#include <cassert>
using namespace std;
#define HYPERBOLIC_PENALTY (10.0)
#define WEIGHT_NUMBER (10.0)
#define WEIGHT_AREA (1.0)
//#define WEIGHT_AREA (0.1)
//#define WEIGHT_RUN (0.5)
#define WEIGHT_RUN (1.0)
//#define WEIGHT_STAR (1.0)
#define WEIGHT_STAR (0.1)
//#define WEIGHT_DUAL (1.0)
#define WEIGHT_DUAL (0.1)
// Number of individuals
//#define POPULATION_COUNT 5000
// #define POPULATION_COUNT (16)
// #define POPULATION_COUNT (32)
#define POPULATION_COUNT (64) //<-
// #define POPULATION_COUNT (128)
// #define POPULATION_COUNT (256)
// Probability for mutation
// #define pMUTATION (0.05)
#define pMUTATION (0.1) //<-
// #define pMUTATION (0.2)
// #define pMUTATION (0.5)
// #define pMUTATION (0.8)
// Probability for crossover
// #define pCROSSOVER (0.95)
#define pCROSSOVER (0.9) //<-
// #define pCROSSOVER (0.8)
// #define pCROSSOVER (0.3)
// #define pCROSSOVER (0.0)
// Probability of replacement crossover
#define pREPLACEMENT (0.5)
// Penalty value
#define PENALTY (1.0e+10)
#define SWAPINT(a,b) {int tmp=a; a=b; b=tmp;}
#define SWAPUINT(a,b) {unsigned int tmp=a; a=b; b=tmp;}
class Genome {
private:
bool _dirty;
double _score; // fitting function value
vector< unsigned int > _chromosome; // flag for cutting constraints
unsigned int _separator;
static double (*_fitness)( Genome & );
protected:
public:
Genome() {
_dirty = true;
_score = 1.0e+10;
_chromosome.clear();
_separator = 0;
}
Genome( const Genome & obj ) {
_dirty = obj._dirty;
_score = obj._score;
_chromosome = obj._chromosome;
_separator = obj._separator;
}
~Genome() {
_chromosome.clear();
}
static void setFitness( double (*__fitness)( Genome & ) ) { Genome::_fitness = __fitness; }
Genome & resize( unsigned int count ) {
_chromosome.resize( count );
_dirty = true;
_score = 0.0;
_separator = 0;
return *this;
}
void clear( void ) {
_chromosome.clear();
_dirty = true;
_score = 0.0;
_separator = 0;
return;
}
void setDirty( void ) { _dirty = true; }
void clearDirty( void ) { _dirty = false; }
const bool & dirty( void ) const { return _dirty; }
const vector< unsigned int > & chromosome( void ) const {
return _chromosome;
}
vector< unsigned int > & chromosome( void ) {
return _chromosome;
}
const double & score( void ) const {
return _score;
}
const unsigned int separator( void ) const {
return _separator;
}
void setSeparator( unsigned int __separator ) {
_separator = __separator;
}
Genome & copy( const Genome & obj );
void init( void );
void eval( void );
friend int commonHead( Genome & p1, Genome & p2,
vector< unsigned int > & corres1, vector< unsigned int > & corres2 );
friend int commonTail( Genome & p1, Genome & p2,
vector< unsigned int > & corres1, vector< unsigned int > & corres2 );
friend void crossover( const Genome & p1, const Genome & p2, Genome * c1, Genome * c2 );
friend void newcrossover( const Genome & p1, const Genome & p2, Genome * c1, Genome * c2 );
bool mutate( float ratio );
void newmutate( void );
Genome & operator = ( const Genome & obj ) {
if ( this != &obj ) {
_dirty = obj._dirty;
_score = obj._score;
_chromosome = obj._chromosome;
_separator = obj._separator;
}
return *this;
}
friend ostream & operator << ( ostream & stream, const Genome & obj );
};
class Population {
private:
unsigned int _nChromosomes;
vector< Genome * > _pool;
bool _ready;
vector< float > _psum;
protected:
void _update( void );
void _prep( void );
Genome & _rouletteWheel( void );
public:
Population() {
_pool.resize( POPULATION_COUNT );
_psum.resize( POPULATION_COUNT );
_ready = false;
}
Population( const Population & obj ) {
cerr << "Illegal call" << endl;
assert( false );
unsigned int n = MIN2( _pool.size(), obj._pool.size() );
for ( unsigned int k = 0; k < n; ++k )
_pool[ k ] = obj._pool[ k ];
}
Population & init( int __nChromosomes ) {
_nChromosomes = __nChromosomes;
for ( unsigned int k = 0; k < _pool.size(); ++k ) {
_pool[ k ] = new Genome;
_pool[ k ]->resize( _nChromosomes );
_pool[ k ]->init();
_pool[ k ]->eval();
}
sort();
return *this;
}
Population & clear( void ) {
_nChromosomes = 0;
for ( unsigned int k = 0; k < _pool.size(); ++k ) {
_pool[ k ]->clear();
delete _pool[ k ];
}
return *this;
}
const unsigned int & nChromosomes( void ) const {
return _nChromosomes;
}
const Genome & bestIndividual( void ) const {
return (*_pool[ 0 ]);
}
void insert( Genome & obj );
Genome & select( void ) {
if ( ! _ready ) _prep();
return _rouletteWheel();
}
void sort( void );
void step( void );
void newstep( void );
friend ostream & operator << ( ostream & stream, const Population & obj );
};
extern double number_only ( Genome & genome );
extern double number_area ( Genome & genome );
extern double number_run ( Genome & genome );
extern double number_star ( Genome & genome );
extern double number_weight ( Genome & genome );
extern double number_area_run ( Genome & genome );
extern double number_area_star ( Genome & genome );
extern double number_area_bias ( Genome & genome );
extern double number_area_length ( Genome & genome );
extern double number_max ( Genome & genome );
extern double number_area_max ( Genome & genome );
extern double number_ave ( Genome & genome );
extern double number_area_ave ( Genome & genome );
extern double test_measure ( Genome & genome );
#endif // __GENE_H_