00001
00020 #include "individual.h"
00021 #include <algorithm>
00022 #include <cassert>
00023 #include <cstdio>
00024
00025 using namespace realea;
00026
00027 static bool m_criterion = false;
00028 static bool m_minimize;
00029
00030
00034 class SortIndMin {
00035 public:
00036 bool operator()(tIndividualReal *a, tIndividualReal *b) {
00037 if (a->isEval() && b->isEval())
00038 return (a->perf() < b->perf());
00039 else if (a->isEval())
00040 return true;
00041 else
00042 return false;
00043 }
00044 };
00045
00046 class SortIndMax {
00047 public:
00048 bool operator()(tIndividualReal *a, tIndividualReal *b) {
00049 if (a->isEval() && b->isEval())
00050 return (a->perf() > b->perf());
00051 else if (a->isEval())
00052 return true;
00053 else
00054 return false;
00055 }
00056 };
00057
00058
00059
00063 tIndividualReal::tIndividualReal(const tChromosomeReal &com) : m_sol(com) , m_evaluated(false), pcounters(),m_notid(true) {
00064
00065 }
00066
00067 tIndividualReal::tIndividualReal(const tChromosomeReal &com, tFitness fitness) : m_sol(com) , m_evaluated(true), pcounters(),m_notid(true) {
00068 m_perf = fitness;
00069 }
00070
00071
00072 tIndividualReal::~tIndividualReal(void) {
00073 pcounters.clear();
00074 }
00075
00076 void tIndividualReal::setMinimize(void) {
00077 m_minimize = true;
00078 m_criterion = true;
00079 }
00080
00081
00082 void tIndividualReal::setMaximize(void) {
00083 m_minimize = false;
00084 m_criterion = true;
00085 }
00086
00087 bool tIndividualReal::isMinimize(void) {
00088 return m_minimize;
00089 }
00090
00091
00092 void tIndividualReal::change(const tChromosomeReal &sol, tFitness fitness) {
00093 m_evaluated = true;
00094 m_sol = sol;
00095 m_perf = fitness;
00096 }
00097
00098
00099 tFitness tIndividualReal::perf(void) {
00100 if (!m_evaluated)
00101 throw new IndException("Performance measure has not been obtained");
00102
00103 return m_perf;
00104 }
00105
00106 struct FindKey : public unary_function<tCounter,bool> {
00107 string m_key;
00108
00109 bool operator()(const tCounter &value) {
00110 return (value.first == m_key);
00111 }
00112
00113 };
00114
00115 void tIndividualReal::incremCount(string ident) {
00116 tCounterList::iterator posi;
00117 FindKey find_key;
00118 find_key.m_key = ident;
00119 unsigned value;
00120
00121 posi = find_if(pcounters.begin(), pcounters.end(), find_key);
00122
00123 if (posi==pcounters.end()) {
00124 value = 1;
00125 tCounter count;
00126 count.first = ident;
00127 count.second = 1;
00128
00129
00130 pcounters.push_back(count);
00131 }
00132 else {
00133 posi->second ++;
00134 }
00135
00136 }
00137
00138
00139 unsigned int tIndividualReal::getCount(string ident) {
00140 tCounterList::iterator posi;
00141 FindKey find_key;
00142 find_key.m_key = ident;
00143
00144 posi = find_if(pcounters.begin(), pcounters.end(), find_key);
00145
00146 if (posi == pcounters.end())
00147 return 0;
00148 else
00149 return posi->second;
00150 }
00151
00152 void tIndividualReal::setPerf(tFitness perf) {
00153 if (m_evaluated) {
00154 throw new string("individual has been evaluated previously");
00155 }
00156 m_perf = perf;
00157 m_evaluated = true;
00158 }
00159
00160 bool tIndividualReal::isEval(void) {
00161 return m_evaluated;
00162 }
00163
00164 bool tIndividualReal::isBetter(tIndividualReal *theother) {
00165 if (!m_criterion)
00166 throw new IndException("Criterion (Maximize/Maximize) has not been set");
00167
00168 if (m_minimize) {
00169 return (this->perf() < theother->perf());
00170 }
00171 else
00172 return (this->perf() > theother->perf());
00173 }
00174
00175 bool tIndividualReal::isWorse(tIndividualReal *theother) {
00176 if (!m_criterion)
00177 throw new IndException("Criterion (Maximize/Maximize) has not been set");
00178
00179 if (m_minimize) {
00180 return (this->perf() > theother->perf());
00181 }
00182 else
00183 return (this->perf() < theother->perf());
00184
00185 }
00186
00190 void setMinimize(void);
00194 void setMaximize(void);
00195
00196
00197 tGen tIndividualReal::gen(unsigned n) {
00198 if (!(n < m_sol.size())) {
00199 printf("Size: %u\tn: %u\n", m_sol.size(), n);
00200 }
00201
00202 assert(n < m_sol.size());
00203 return m_sol[n];
00204 }
00205
00206 tFitness tIndividualReal::eval(IEval *funeval) {
00207 if (m_evaluated) {
00208 return m_perf;
00209 }
00210
00211 tFitness fitness = funeval->eval(this->sol());
00212 setPerf(fitness);
00213 return fitness;
00214 }
00215
00216 void tIndividualReal::setId(unsigned id) {
00217 m_id = id;
00218 m_notid = false;
00219 }
00220
00221 unsigned tIndividualReal::getId(void) {
00222 if (m_notid) {
00223 throw string("IndividualReal::Id");
00224 }
00225 return m_id;
00226 }
00227
00228 void tIndividualReal::sort(vector<tIndividualReal*> &individuals) {
00229 if (tIndividualReal::isMinimize())
00230 std::sort(individuals.begin(), individuals.end(), SortIndMin());
00231 else
00232 std::sort(individuals.begin(), individuals.end(), SortIndMax());
00233 }