00001
00039
00040
00041
00042 #ifndef __INDIVIDUAL_H
00043 #define __INDIVIDUAL_H
00044
00045 #include <EALib/ChromosomeT.h>
00046 #include <EALib/PVMinterface.h>
00047
00048
00049
00053 class Individual : protected std::vector< Chromosome * >
00054 {
00055 public:
00056 explicit Individual(unsigned noChromosomes = 0);
00057 Individual(unsigned, const Chromosome&);
00058 Individual(const Chromosome&);
00059 Individual(const Chromosome&,
00060 const Chromosome&);
00061 Individual(const Chromosome&,
00062 const Chromosome&,
00063 const Chromosome&);
00064 Individual(const Chromosome&,
00065 const Chromosome&,
00066 const Chromosome&,
00067 const Chromosome&);
00068 Individual(const Chromosome&,
00069 const Chromosome&,
00070 const Chromosome&,
00071 const Chromosome&,
00072 const Chromosome&);
00073 Individual(const Chromosome&,
00074 const Chromosome&,
00075 const Chromosome&,
00076 const Chromosome&,
00077 const Chromosome&,
00078 const Chromosome&);
00079 Individual(const Chromosome&,
00080 const Chromosome&,
00081 const Chromosome&,
00082 const Chromosome&,
00083 const Chromosome&,
00084 const Chromosome&,
00085 const Chromosome&);
00086 Individual(const Chromosome&,
00087 const Chromosome&,
00088 const Chromosome&,
00089 const Chromosome&,
00090 const Chromosome&,
00091 const Chromosome&,
00092 const Chromosome&,
00093 const Chromosome&);
00094 Individual(const std::vector< Chromosome* >&);
00095 Individual(const Individual&);
00096 virtual ~Individual();
00097
00098 unsigned size() const
00099 {
00100 return static_cast< const std::vector< Chromosome * > * >(this)->size();
00101 }
00102
00103 unsigned totalSize() const;
00104 double fitnessValue() const
00105 {
00106 return fitness;
00107 }
00108 double selectionProbability() const
00109 {
00110 return selProb;
00111 }
00112 bool isFeasible() const
00113 {
00114 return feasible;
00115 }
00116 unsigned numberOfCopies() const
00117 {
00118 return numCopies;
00119 }
00120 bool isElitist() const
00121 {
00122 return elitist;
00123 }
00124
00125 void setFitness(double fit)
00126 {
00127 fitness = scaledFitness = fit;
00128 }
00129 void setFeasible(bool f)
00130 {
00131 feasible = f;
00132 }
00133 void setSelectionProbability(double ps)
00134 {
00135 selProb = ps;
00136 }
00137
00138 Chromosome& operator [ ](unsigned i)
00139 {
00140 RANGE_CHECK(i < size())
00141 return *(*(begin() + i));
00142 }
00143
00144 const Chromosome& operator [ ](unsigned i) const
00145 {
00146 RANGE_CHECK(i < size())
00147 return *(*(begin() + i));
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00160 Individual& operator = (const Individual&);
00161
00162
00163
00164 void replace(unsigned i, const Chromosome& chrom);
00165 void insert(unsigned i, const Chromosome& chrom);
00166 void append(const Chromosome& chrom);
00167 void remove (unsigned i);
00168 void remove (unsigned from, unsigned to);
00169
00170 void setAge(unsigned a = 0)
00171 {
00172 age = a;
00173 }
00174 void incAge()
00175 {
00176 ++age;
00177 }
00178 unsigned getAge() const
00179 {
00180 return age;
00181 }
00182
00183
00184
00185
00188 template<class ChromosomeTemplate> void append(const ChromosomeTemplate& chrom) {
00189 ChromosomeTemplate* newChrom = new ChromosomeTemplate(chrom);
00190 std::vector< Chromosome * >::push_back(newChrom);
00191 newChrom->registerIndividual(*this, size() - 1);
00192 }
00193
00194
00195
00199 void setLearnTime(unsigned lt = 0)
00200 {
00201 learnTime = lt;
00202 }
00203 unsigned getLearnTime() const
00204 {
00205 return learnTime;
00206 }
00207
00208
00209
00212 void setEvaluationFlag()
00213 {
00214 evalFlg = true;
00215 }
00216 void clearEvaluationFlag()
00217 {
00218 evalFlg = false;
00219 }
00220 bool needEvaluation() const {
00221 return evalFlg;
00222 }
00223
00224 bool operator == (const Individual&) const;
00225 bool operator < (const Individual&) const;
00226
00227 double getFitness() const
00228 {
00229 return fitness;
00230 }
00231
00232 double getScaledFitness() const
00233 {
00234 return scaledFitness;
00235 }
00236
00237 void setScaledFitness(double sf)
00238 {
00239 scaledFitness = sf;
00240 }
00241
00242 void setSelProb(double sp)
00243 {
00244 selProb = sp;
00245 }
00246
00247 double getSelProb() const
00248 {
00249 return selProb;
00250 }
00251
00252 void setNumCopies(unsigned snc)
00253 {
00254 numCopies = snc;
00255 }
00256
00257 unsigned getNumCopies() const
00258 {
00259 return numCopies;
00260 }
00261
00262 void setEvalFlg(bool ef)
00263 {
00264 evalFlg = ef;
00265 }
00266
00267 bool getEvalFlg() const
00268 {
00269 return evalFlg;
00270 }
00271
00272 bool getFeasible() const
00273 {
00274 return feasible;
00275 }
00276
00277 void setElitist(bool e)
00278 {
00279 elitist = e;
00280 }
00281
00282 bool getElitist() const
00283 {
00284 return elitist;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00295 int pvm_pkind();
00296
00298 int pvm_upkind();
00299
00300
00301
00302
00303 protected:
00304 double fitness;
00305 double scaledFitness;
00306 bool evalFlg;
00307 bool feasible;
00308 double selProb;
00309 unsigned numCopies;
00310 bool elitist;
00311 unsigned age;
00312
00313
00314 unsigned learnTime;
00315
00316 #ifndef __NO_GENERIC_IOSTREAM
00317 friend std::ostream& operator << (std::ostream& os,
00318 const Individual& ind)
00319 {
00320 os << "Individual(" << ind.size() << ")\n"
00321 << ind.fitness << '\n'
00322 << ind.scaledFitness << '\n'
00323 << ind.evalFlg << '\n'
00324 << ind.feasible << '\n'
00325 << ind.selProb << '\n'
00326 << ind.numCopies << '\n'
00327 << ind.elitist << '\n'
00328 << ind.age << '\n'
00329 << ind.learnTime << '\n';
00330
00331 for (unsigned i = 0; i < ind.size(); ++i)
00332 os << '\n' << ind[ i ];
00333 os << std::endl;
00334 return os;
00335 }
00336
00337 friend std::istream& operator >> (std::istream& is, Individual& ind)
00338 {
00339 unsigned i, indSize(0);
00340 std::string s, t;
00341
00342 is >> s;
00343 is.get();
00344
00345 if (is.good() &&
00346 s.substr(0, 11) == "Individual(" &&
00347 s.find(')') != std::string::npos) {
00348
00349
00350 t = s.substr(s.find('(') + 1, s.find(')') - s.find('(') - 1);
00351 indSize = atoi(t.c_str());
00352
00353
00354 ind.resize(indSize);
00355
00356 is >> ind.fitness
00357 >> ind.scaledFitness
00358 >> ind.evalFlg
00359 >> ind.feasible
00360 >> ind.selProb
00361 >> ind.numCopies
00362 >> ind.elitist
00363 >> ind.age
00364 >> ind.learnTime;
00365 for (i = 0; i < ind.size(); ++i)
00366 is >> ind[ i ];
00367 }
00368 return is;
00369 }
00370 #endif // !__NO_GENERIC_IOSTREAM
00371
00372 friend class Population;
00373 friend class PopulationMOO;
00374 friend class IndividualMOO;
00375 };
00376
00377
00378
00379 #endif
00380