00001 #include <cmath>
00002 #include <ecf/ECF.h>
00003 #include "EvalOp.h"
00004 #include "atributi.h"
00005
00006 #include <fstream>
00007 #include <sstream>
00008
00009
00010
00011 Unimodal unimodal;
00012
00013
00014 EvalOp::EvalOp()
00015 {}
00016
00017
00018 void EvalOp::readClasses(std::string fileName, uint nProblems, uint nOperators)
00019 {
00020 ifstream file(fileName.c_str());
00021
00022 operators.resize(nOperators);
00023 for(uint i = 0; i < nOperators; i++)
00024 operators[i].resize(nProblems);
00025
00026 string line;
00027 uint funId;
00028
00029 for(funId = 1; funId <= nProblems; funId++) {
00030 stringstream sline;
00031 uint id;
00032
00033 do {
00034 sline.str("");sline.clear();
00035 getline(file, line);
00036 sline << line;
00037 sline >> id;
00038 } while(funId != id);
00039
00040 std::vector<uint> good, bad, ugly;
00041
00042
00043 sline.str("");sline.clear();
00044 getline(file, line);
00045 sline << line;
00046 while(sline >> id) {
00047 operators[id - 1][funId - 1] = 0;
00048 good.push_back(id);
00049 }
00050
00051 sline.str("");sline.clear();
00052 getline(file, line);
00053 sline << line;
00054 while(sline >> id) {
00055 operators[id - 1][funId - 1] = 1;
00056 ugly.push_back(id);
00057 }
00058
00059 sline.str("");sline.clear();
00060 getline(file, line);
00061 sline << line;
00062 while(sline >> id) {
00063 operators[id - 1][funId - 1] = 2;
00064 bad.push_back(id);
00065 }
00066
00067
00068 vector< vector<uint> > fun;
00069 fun.push_back(good);
00070 fun.push_back(ugly);
00071 fun.push_back(bad);
00072 classes.push_back(fun);
00073
00074 }
00075
00076 }
00077
00078
00079 void EvalOp::readLandscape(std::string fileName, uint problems)
00080 {
00081 ifstream file(fileName.c_str());
00082
00083 string line;
00084 uint funId;
00085
00086 for(funId = 0; funId < problems; funId++) {
00087 stringstream sLine;
00088 double f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14;
00089
00090 getline(file, line);
00091 sLine << line;
00092 sLine >> f1 >> f2 >> f3 >> f4 >> f5 >> f6 >> f7 >> f8 >> f9 >> f10 >> f11 >> f12 >> f13 >> f14;
00093
00094 diameter.push_back(f3);
00095 distance.push_back(f4);
00096 delta.push_back(f6);
00097 ampl.push_back(f7);
00098 ampl_var.push_back(f8);
00099 length.push_back(f10);
00100 ac.push_back(f11);
00101 ac_len.push_back(f12);
00102 acc.push_back(f13);
00103 }
00104 }
00105
00106
00107
00108 void EvalOp::registerParameters(StateP state)
00109 {
00110 state->getRegistry()->registerEntry("operator", (voidP) (new uint(1)), ECF::UINT);
00111 state->getRegistry()->registerEntry("funkcije", (voidP) (new string("")), ECF::STRING);
00112 }
00113
00114
00115
00116
00117 bool EvalOp::initialize(StateP state)
00118 {
00119 state_ = state;
00120
00121
00122 state->getContext()->environment = this;
00123
00124
00125 unimodal.initialize(state);
00126
00127
00128 classes.clear();
00129 operators.clear();
00130 functions.clear();
00131
00132 diameter.clear();
00133 distance.clear();
00134 delta.clear();
00135 ampl.clear();
00136 length.clear();
00137 acc.clear();
00138 ampl_var.clear();
00139 ac.clear();
00140 ac_len.clear();
00141 terminals.clear();
00142
00143
00144 readClasses("ucinkovitost.txt", 24, 13);
00145
00146
00147 readLandscape("sve.txt", 24);
00148
00149
00150 voidP sptr = state->getRegistry()->getEntry("operator");
00151 operatorId = *((uint*) sptr.get());
00152
00153 if(state->getRegistry()->isModified("funkcije")) {
00154 sptr = state->getRegistry()->getEntry("funkcije");
00155 stringstream fun( *((string*) sptr.get()) );
00156
00157 uint fId;
00158 while(fun >> fId)
00159 functions.push_back(fId - 1);
00160 }
00161 else
00162 for(uint i = 0; i < 24; i++)
00163 functions.push_back(i);
00164
00165
00166 Tree::Tree* tree = (Tree::Tree*) state->getGenotypes()[0].get();
00167 Tree::PrimitiveP prim;
00168 bool used;
00169
00170 used = false;
00171 prim = tree->primitiveSet_->getTerminalByName("diameter");
00172 if(prim)
00173 used = true;
00174 terminals.push_back(used);
00175 used = false;
00176 prim = tree->primitiveSet_->getTerminalByName("distance");
00177 if(prim)
00178 used = true;
00179 terminals.push_back(used);
00180 used = false;
00181 prim = tree->primitiveSet_->getTerminalByName("delta");
00182 if(prim)
00183 used = true;
00184 terminals.push_back(used);
00185 used = false;
00186 prim = tree->primitiveSet_->getTerminalByName("ampl");
00187 if(prim)
00188 used = true;
00189 terminals.push_back(used);
00190 used = false;
00191 prim = tree->primitiveSet_->getTerminalByName("length");
00192 if(prim)
00193 used = true;
00194 terminals.push_back(used);
00195 used = false;
00196 prim = tree->primitiveSet_->getTerminalByName("acc");
00197 if(prim)
00198 used = true;
00199 terminals.push_back(used);
00200 used = false;
00201 prim = tree->primitiveSet_->getTerminalByName("ampl_var");
00202 if(prim)
00203 used = true;
00204 terminals.push_back(used);
00205 used = false;
00206 prim = tree->primitiveSet_->getTerminalByName("ac");
00207 if(prim)
00208 used = true;
00209 terminals.push_back(used);
00210 used = false;
00211 prim = tree->primitiveSet_->getTerminalByName("ac_len");
00212 if(prim)
00213 used = true;
00214 terminals.push_back(used);
00215
00216
00217 return true;
00218 }
00219
00220
00221
00222
00223 FitnessP EvalOp::evaluate(IndividualP individual)
00224 {
00225 FitnessP fitness (new FitnessMin);
00226
00227
00228 Tree::Tree* tree = (Tree::Tree*) individual->getGenotype().get();
00229
00230 uint error = 0;
00231
00232
00233 for(uint id = 0; id < functions.size(); id++) {
00234 uint fun = functions[id];
00235
00236
00237 if(terminals[0])
00238 tree->setTerminalValue("diameter", &diameter[fun]);
00239 if(terminals[1])
00240 tree->setTerminalValue("distance", &distance[fun]);
00241 if(terminals[2])
00242 tree->setTerminalValue("delta", &delta[fun]);
00243 if(terminals[3])
00244 tree->setTerminalValue("ampl", &l[fun]);
00245 if(terminals[4])
00246 tree->setTerminalValue("length", &length[fun]);
00247 if(terminals[5])
00248 tree->setTerminalValue("acc", &acc[fun]);
00249 if(terminals[6])
00250 tree->setTerminalValue("ampl_var", &l_var[fun]);
00251 if(terminals[7])
00252 tree->setTerminalValue("ac", &ac[fun]);
00253 if(terminals[8])
00254 tree->setTerminalValue("ac_len", &ac_len[fun]);
00255
00256 double result;
00257 tree->execute(&result);
00258 result = fabs(result);
00259
00260
00261 uint cat = 2;
00262 if(result < 1)
00263 cat = 0;
00264 else if(result < 10)
00265 cat = 1;
00266
00267 uint score = 1;
00268
00269 if(operators[operatorId - 1][fun] == cat)
00270 score = 0;
00271 else if(abs((int) (operators[operatorId - 1][fun] - cat)) > 1)
00272 score = 3;
00273
00274
00275
00276
00277
00278 error += score;
00279
00280 }
00281
00282 fitness->setValue(error);
00283
00284 return fitness;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 bool EvalOp::isAtr(Atribut* atr)
00315 {
00316 atr->operacija;
00317 atr->vrijednost;
00318
00319 return true;
00320
00321
00322
00323
00324
00325 }