-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrom.h
executable file
·228 lines (185 loc) · 5.43 KB
/
chrom.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
/*****************************************************************
** SGAL Simple Genetic Algorithm Library
**
** Copyright (c) 1995, Neal A. Sanche
*****************************************************************/
#ifndef CHROM_H_
#define CHROM_H_
#include <stdlib.h>
#include <iostream>
// Chromosome base class, for polymorphism
//////////////////////////////////////////////////////////////////
// CChromosomeBase class declaration
class CChromosomeBase {
public:
CChromosomeBase(): dFitness(0.0), dObjective(0.0), nLength(0), nBits(0) {}
virtual ~CChromosomeBase() {}
// Query the length and the number of bits
virtual int GetLength() { return nLength; }
virtual int GetNumBits() { return nBits; }
// Get and set the value of a specific bit
virtual int GetBit(int) = 0;
virtual void SetBit(int, int) = 0;
// Get and set fitness value (possibly scaled)
virtual const double GetFitness() const { return dFitness; }
virtual void SetFitness(const double fitness) { dFitness = fitness; }
// Get and set raw objective funtion values
virtual const double GetObjective() const { return dObjective; }
virtual void SetObjective(const double obj) { dObjective = obj; }
// Randomize the chromosome
virtual void Randomize() = 0;
// Make a chromosome equal to another
virtual CChromosomeBase& operator=(const CChromosomeBase&) = 0;
protected:
double dFitness;
double dObjective;
int nLength;
int nBits;
};
//////////////////////////////////////////////////////////////////
// CChromosome<TGENE> class declaration
template<class TGENE>
class CChromosome: public CChromosomeBase {
public:
CChromosome();
CChromosome(int length);
CChromosome(const CChromosome&);
CChromosomeBase& operator=(const CChromosomeBase&);
virtual ~CChromosome();
TGENE &operator[](int index); // Array operator
virtual int GetBit(int index);
virtual void SetBit(int index, int value);
virtual void Randomize();
protected:
TGENE *geneData;
};
//////////////////////////////////////////////////////////////////
// CChromosome<TGENE> class implementation
template<class TGENE>
CChromosome<TGENE>::CChromosome(): CChromosomeBase()
{
nLength = 0;
nBits = 8*sizeof(TGENE);
geneData = 0;
}
template<class TGENE>
CChromosome<TGENE>::CChromosome(int length)
{
nLength = length;
nBits = 8*sizeof(TGENE) * nLength;
geneData = new TGENE[nLength];
for(int i = 0; i < nLength; i++) {
geneData[i] = (TGENE)0;
}
}
template<class TGENE>
CChromosome<TGENE>::CChromosome(const CChromosome& that)
{
nLength = that.nLength;
geneData = 0;
*this = that;
}
template<class TGENE>
CChromosomeBase& CChromosome<TGENE>::operator=(const CChromosomeBase& thatbase)
{
CChromosome<TGENE> &that = (CChromosome<TGENE> &)thatbase;
if ((geneData != 0) && (nLength > 0)) {
delete[] geneData;
}
nLength = that.nLength;
nBits = 8*sizeof(TGENE) * nLength;
if (nBits != that.nBits) {
std::cerr << "Bad data type in copy." << std::endl;
return *this;
}
geneData = new TGENE[nLength];
for (int i = 0; i < nLength; i++) {
geneData[i] = that.geneData[i];
}
return *this;
}
template<class TGENE>
CChromosome<TGENE>::~CChromosome()
{
if ((geneData != 0) && (nLength > 0)) {
delete[] geneData;
}
}
template<class TGENE>
TGENE &CChromosome<TGENE>::operator[](int index) // Array operator
{
return geneData[index];
}
template<class TGENE>
int CChromosome<TGENE>::GetBit(int index)
{
// Do range checking
if (index < 0 || index >= nBits) return -1;
// Figure out which gene we're mangling
int iGene = index/8/sizeof(TGENE);
int iBit = index % (sizeof(TGENE)*8);
char *acGene = (char *)&geneData[iGene];
if (acGene[iBit/8] & ((char)1 << (iBit%8))) return 1;
else return 0;
}
template<class TGENE>
void CChromosome<TGENE>::SetBit(int index, int value)
{
// Do range checking
if ((index < 0) || (index >= nBits)) return;
// Figure out which gene we're mangling
int iGene = index/8/sizeof(TGENE);
int iBit = index % (sizeof(TGENE)*8);
char val = 0;
if (value > 0) val = 1;
char *acGene = (char *)&geneData[iGene];
acGene[iBit/8] = acGene[iBit/8] | ((char)val << (iBit%8));
}
template<class TGENE>
void CChromosome<TGENE>::Randomize()
{
for (int i = 0; i < nBits; i++) {
float test = (float)rand() / (float)RAND_MAX;
if (test >= 0.5) {
SetBit(i,1);
}
else {
SetBit(i,0);
}
}
}
/////////////////////////////////////////////////////////////////
// CChromosomeFactoryBase class definition
class CChromosomeFactoryBase
{
public:
CChromosomeFactoryBase() {}
virtual CChromosomeBase *Create() = 0;
virtual CChromosomeBase *Create(int) = 0;
};
/////////////////////////////////////////////////////////////////
// CChromosomeFactory<TGENE> class definition
template<class TGENE>
class CChromosomeFactory : public CChromosomeFactoryBase {
public:
CChromosomeFactory() {}
virtual CChromosomeBase *Create();
virtual CChromosomeBase *Create(int length);
};
/////////////////////////////////////////////////////////////////
// CChromosomeFactory<TGENE> class implementation
template<class TGENE>
CChromosomeBase *CChromosomeFactory<TGENE>::Create()
{
// Create a chromosome for use in the other
// parts of the program
return new CChromosome<TGENE>;
}
template<class TGENE>
CChromosomeBase *CChromosomeFactory<TGENE>::Create(int length)
{
// Create a chromosome for use in the other
// parts of the program
return new CChromosome<TGENE>(length);
}
#endif // CHROM_H_