-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindividual.cpp
More file actions
169 lines (134 loc) · 5.55 KB
/
Copy pathindividual.cpp
File metadata and controls
169 lines (134 loc) · 5.55 KB
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
#include "individual.h"
/******************************************************************************/
/******************************************************************************/
CIndividual::CIndividual(double* pf_chromosome, unsigned int un_chromosome_length) :
m_fFitness(FITNESS_NOT_SET),
m_bBinaryEncoding(false),
m_unChromosomeLength(un_chromosome_length)
{
m_pfChromosome = new double[m_unChromosomeLength];
memcpy(m_pfChromosome, pf_chromosome, sizeof(double) * m_unChromosomeLength);
m_ptBinaryChromosome = NULL;
}
/******************************************************************************/
/******************************************************************************/
CIndividual::CIndividual(unsigned int* pt_chromosome, unsigned int un_chromosome_length) :
m_fFitness(FITNESS_NOT_SET),
m_bBinaryEncoding(true),
m_unChromosomeLength(un_chromosome_length)
{
m_pfChromosome = new double[m_unChromosomeLength];
m_ptBinaryChromosome = new unsigned int[m_unChromosomeLength*BIT_X_GENE];
for( int i = 0; i < m_unChromosomeLength; i++ ) {
double gene_value = 0;
int exp_factor = 1;
for( int j = 0; j < BIT_X_GENE; j++ ) {
int index = j + i*BIT_X_GENE;
m_ptBinaryChromosome[index] = pt_chromosome[index];
gene_value += pt_chromosome[index]*exp_factor;
exp_factor *= 2;
}
m_pfChromosome[i] = gene_value/(exp_factor-1)*20.0 - 10.0;
}
}
/******************************************************************************/
/******************************************************************************/
CIndividual::CIndividual(const CIndividual& original)
{
// Copy all the fields and make a deep copy of the chromosome
this->m_fFitness = original.m_fFitness;
this->m_unChromosomeLength = original.m_unChromosomeLength;
this->m_pfChromosome = new double[m_unChromosomeLength];
this->m_bBinaryEncoding = original.m_bBinaryEncoding;
memcpy(this->m_pfChromosome, original.m_pfChromosome, m_unChromosomeLength * sizeof(double));
if( this->m_bBinaryEncoding )
{
this->m_ptBinaryChromosome = new unsigned int[m_unChromosomeLength*BIT_X_GENE];
memcpy(this->m_ptBinaryChromosome, original.m_ptBinaryChromosome, m_unChromosomeLength * BIT_X_GENE * sizeof(unsigned int));
} else {
m_ptBinaryChromosome = NULL;
}
}
/******************************************************************************/
/******************************************************************************/
CIndividual::~CIndividual()
{
delete[] m_pfChromosome;
if( m_ptBinaryChromosome != NULL )
delete[] m_ptBinaryChromosome;
}
/******************************************************************************/
/******************************************************************************/
bool CIndividual::IsBinaryEncoded()
{
return m_bBinaryEncoding;
}
/******************************************************************************/
/******************************************************************************/
double* CIndividual::GetChromosome()
{
return m_pfChromosome;
}
/******************************************************************************/
/******************************************************************************/
unsigned int* CIndividual::GetBinaryChromosome()
{
if( m_ptBinaryChromosome == NULL ) {
printf("the chromosome in not bynary!!");
fflush(stdout);
}
return m_ptBinaryChromosome;
}
/******************************************************************************/
/******************************************************************************/
unsigned int CIndividual::GetChromosomeLength()
{
return m_unChromosomeLength;
}
/******************************************************************************/
/******************************************************************************/
unsigned int CIndividual::GetBinaryChromosomeLength()
{
return m_unChromosomeLength * BIT_X_GENE;
}
/******************************************************************************/
/******************************************************************************/
void CIndividual::SetFitness(double f_fitness)
{
if (m_fFitness == FITNESS_NOT_SET)
{
m_fFitness = f_fitness;
} else {
printf("Fitness has already been set for this individual! You should\n"
"only set the fitness for an individual once!");
fflush(stdout);
}
}
/******************************************************************************/
/******************************************************************************/
double CIndividual::GetFitness()
{
//printf("CIndividual::GetFitness %2f\n",m_fFitness);
return m_fFitness;
}
/******************************************************************************/
/******************************************************************************/
bool CIndividual::operator< (const CIndividual& rhs)
{
return (m_fFitness > rhs.m_fFitness);
}
/******************************************************************************/
/******************************************************************************/
bool CIndividual::FitnessLessThan(CIndividual* lhs, CIndividual* rhs)
{
//printf("CIndividual:: KK ");
return lhs->GetFitness() < rhs->GetFitness();
}
/******************************************************************************/
/******************************************************************************/
void CIndividual::Print(){
for(int y=0;y<m_unChromosomeLength;y++){
printf(" // %f // ",m_pfChromosome[y]);
}
printf("\n");
}