GAChromosome.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "GA/GAChromosome.H"
00039
00040 #include "Util/Assert.H"
00041 #include "Util/Types.H"
00042 #include <cmath>
00043 #include <cstdlib>
00044 #include <cstring>
00045 #include <istream>
00046 #include <ostream>
00047
00048 GAChromosome::GAChromosome() :
00049 fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00050 { }
00051
00052 GAChromosome::GAChromosome(const int N) :
00053 fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00054 {
00055 init(N);
00056 }
00057
00058 GAChromosome::GAChromosome(const int N, const int *a) :
00059 fitness(0.0f), linear_fitness(0.0f), breedings(0), size(0), genes(NULL)
00060 {
00061 init(N, a);
00062 }
00063
00064 GAChromosome::GAChromosome(const GAChromosome& c) :
00065 fitness(c.fitness), linear_fitness(c.linear_fitness),
00066 breedings(c.breedings), size(0), genes(NULL)
00067 {
00068 init(c.size, c.genes);
00069 }
00070
00071 void GAChromosome::resize(const int N)
00072 {
00073 if (size) delete [] genes;
00074 genes = new int[N];
00075 size = N;
00076 }
00077
00078 void GAChromosome::init(const int N, const int *a)
00079 {
00080 resize(N);
00081 memcpy(genes, a, N * sizeof(int));
00082 }
00083
00084 void GAChromosome::init(const int N)
00085 {
00086 resize(N);
00087 for (int i = 0; i < N; i++)
00088 genes[i] = (rand() % 3) - 1;
00089 }
00090
00091 GAChromosome::~GAChromosome()
00092 {
00093 if (size) delete [] genes;
00094 }
00095
00096 int GAChromosome::get_size() const
00097 {
00098 return size;
00099 }
00100
00101 void GAChromosome::set_gene(const int i, const int a)
00102 {
00103 ASSERT(i >= 0 && i < size && abs(a) < 2);
00104 genes[i] = a;
00105 }
00106
00107 int GAChromosome::get_gene(const int i) const
00108 {
00109 ASSERT(i >= 0 && i < size);
00110 return genes[i];
00111 }
00112
00113 void GAChromosome::set_fitness(const float a)
00114 {
00115 ASSERT(a >= 0);
00116 fitness = a;
00117 }
00118
00119 float GAChromosome::get_fitness() const
00120 {
00121 return fitness;
00122 }
00123
00124 void GAChromosome::set_linear_fitness(const float a)
00125 {
00126 ASSERT(a >= 0);
00127 linear_fitness = a;
00128 }
00129
00130 float GAChromosome::get_linear_fitness() const
00131 {
00132 return linear_fitness;
00133 }
00134
00135 void GAChromosome::set_breedings(const int a)
00136 {
00137 ASSERT(a >= 0);
00138 breedings = a;
00139 }
00140
00141 int GAChromosome::get_breedings() const
00142 {
00143 return breedings;
00144 }
00145
00146 GAChromosome& GAChromosome::operator=(const GAChromosome& c)
00147 {
00148 init(c.size, c.genes);
00149 fitness = c.fitness;
00150 linear_fitness = c.linear_fitness;
00151 breedings = c.breedings;
00152 return *this;
00153 }
00154
00155 bool GAChromosome::operator<(const GAChromosome& c) const
00156 {
00157 return breedings < c.breedings;
00158 }
00159
00160 void GAChromosome::mutation()
00161 {
00162 int i = rand() % size;
00163 genes[i] = ((genes[i] + 2) % 3) - 1;
00164 }
00165
00166 void GAChromosome::add_breeding()
00167 {
00168 breedings++;
00169 }
00170
00171 void GAChromosome::use_breeding()
00172 {
00173 ASSERT(breedings > 0);
00174 breedings--;
00175 }
00176
00177 std::istream& operator>> (std::istream& in, GAChromosome& c)
00178 {
00179 int s, f, l, b;
00180 in >> s >> f >> l >> b;
00181 ASSERT(f >=0 && l >= 0 && b >= 0);
00182 c.resize(s);
00183 c.fitness = f;
00184 c.linear_fitness = l;
00185 c.breedings = b;
00186 for (int i = 0; i < c.size; i++)
00187 {
00188 int g;
00189 in >> g;
00190 ASSERT(abs(g) < 2);
00191 c.genes[i] = g;
00192 }
00193 return in;
00194 }
00195
00196 std::ostream& operator<< (std::ostream& out, GAChromosome& c)
00197 {
00198 out << c.size << '\n';
00199 out << c.fitness << ' ' << c.linear_fitness << ' ';
00200 out << c.breedings << '\n';
00201 for (int i = 0; i < c.size; i++)
00202 {
00203 out << c.genes[i] << '\n';
00204 }
00205 return out;
00206 }
00207
00208
00209
00210
00211
00212