00001 #include "ECF.h" 00002 00003 // Za svaki primjer je potrebno: 00004 // a) definirati odgovarajuci EvaluateOp objekt koji se primjenjuje za evaluaciju jedinke 00005 // b) definirati genotip (po zelji i druge parametre) u konf fajlu 00006 // 00007 // Svaki primjer ima posebnu funkciju main() - otkomentirati 00008 00009 00010 // 0. primjer: kako ispitati fitnes jedinke (spremljene u ind.txt) 00011 00012 00013 00014 00015 00016 // 1. primjer: GA OneMax problem 00017 /* 00018 #include "examples/GAonemax/OneMaxEvalOp.h" 00019 int main(int argc, char **argv) 00020 { 00021 argc = 2; // hard coded za lakse isprobavanje :) 00022 argv[1] = "./examples/GAOneMax/parametri.txt"; 00023 00024 StateP state (new State); 00025 00026 //state->setEvalOp(static_cast<EvaluateOpP> (new OneMaxEvalOp)); 00027 state->setEvalOp(new OneMaxEvalOp); 00028 00029 state->initialize(argc, argv); 00030 state->run(); 00031 00032 return 0; 00033 } 00034 */ 00035 00036 00037 00038 00039 00040 // 2. primjer: GA minimizacija funkcije 00041 00042 #include "examples/GAFunctionMin/FunctionMinEvalOp.h" 00043 #include "AlgGenHookeJeeves.h" 00044 #include "AlgDifferentialEvolution.h" 00045 int main(int argc, char **argv) 00046 { 00047 // argc = 2; 00048 // argv[1] = "./examples/GAFunctionMin/parametri.txt"; 00049 00050 // PSO algoritam: 00051 //argv[1] = "./examples/GAFunctionMin/parameters_DE.txt"; 00052 00053 GenHookeJeevesP alg (new GenHookeJeeves); 00054 00055 StateP state (new State); 00056 state->addAlgorithm(alg); 00057 00058 state->setEvalOp(static_cast<EvaluateOpP> (new FunctionMinEvalOp)); 00059 00060 state->initialize(argc, argv); 00061 state->run(); 00062 00063 return 0; 00064 } 00065 00066 00067 00068 namespace Tree { 00069 00070 // primjer tipa podataka 00071 struct my_type 00072 { 00073 double v; 00074 bool b; 00075 }; 00076 00077 // terminal za doticni tip 00078 class MyTerminal : public Primitives::Primitive 00079 { 00080 public: 00081 my_type value_; 00082 00083 MyTerminal() 00084 { 00085 nArguments_ = 0; 00086 } 00087 void execute(void* result, Tree& tree) 00088 { 00089 my_type& res = *(my_type*)result; 00090 res = value_; 00091 } 00092 void setValue(void* value) 00093 { 00094 value_ = *(my_type*)value; 00095 } 00096 ~MyTerminal() 00097 { } 00098 }; 00099 00100 // primjer funkcije za korisnicki tip podataka 00101 class MyFunc : public Primitives::Primitive 00102 { 00103 public: 00104 MyFunc() 00105 { 00106 nArguments_ = 2; 00107 name_ = "func"; 00108 } 00109 void execute(void* result, Tree& tree) 00110 { 00111 my_type first, second; 00112 my_type& func = *(my_type*)result; 00113 00114 getNextArgument(&first, tree); 00115 getNextArgument(&second, tree); 00116 00117 func.b = first.b && second.b; 00118 func.v = first.v + second.v; 00119 } 00120 ~MyFunc() 00121 { } 00122 }; 00123 00124 } 00125 00126 00127 00128 // 3. primjer: GP simbolicka regresija 00129 /* 00130 #include "examples/GPSymbReg/SymbRegEvalOp.h" 00131 #include "examples/GPSymbReg/zbr.h" 00132 int main(int argc, char **argv) 00133 { 00134 argc = 2; 00135 argv[1] = "./examples/GPSymbReg/parametri.txt"; 00136 00137 StateP state = static_cast<StateP> (new State); 00138 00139 state->setEvalOp(static_cast<EvaluateOpP> (new SymbRegEvalOp)); 00140 00141 // primjer: dodavanje korisnickog operatora 00142 // MyOpP myOp = (MyOpP) (new MyOp); 00143 // state->addOperator(myOp); 00144 00145 // primjer: dodavanje korisnickog algoritma 00146 // MyAlgP myAlg = (MyAlgP) (new MyAlg); 00147 // state->addAlgorithm(myAlg); 00148 00149 // primjer: dodavanje korisnickog genotipa 00150 // MyGenotypeP myGenotype = (MyGenotypeP) (new MyGenotype); 00151 // state->addGenotype(myGenotype); 00152 00153 // primjer: dodavanje korisnicke funkcije za stablo 00154 TreeP tree = (TreeP) new Tree::Tree; 00155 Tree::PrimitiveP zbr = (Tree::PrimitiveP) new Tree::Ad; 00156 tree->addFunction(zbr); 00157 00158 Tree::PrimitiveP myFunc = (Tree::PrimitiveP) new Tree::MyFunc; 00159 tree->addFunction(myFunc); 00160 00161 Tree::PrimitiveP myTerm = (Tree::PrimitiveP) new Tree::MyTerminal; 00162 myTerm->setName("term"); 00163 tree->addTerminal(myTerm); 00164 00165 00166 state->addGenotype(tree); 00167 00168 //for(int i = 0; i < 100; i++) { 00169 state->initialize(argc, argv); 00170 state->run(); 00171 //} 00172 00173 //std::vector<IndividualP> vec = state->getPopulation()->hof_->getBest(); 00174 //IndividualP ind = vec[0]; 00175 //state->getAlgorithm()->evaluate(ind); 00176 //XMLNode out; 00177 //ind->write(out); 00178 //std::cout << out.createXMLString() << std::endl; 00179 00180 return 0; 00181 } 00182 */ 00183 00184 00185 //4. primjer: GA problem trgovackog putnika, 29 gradova 00186 /* 00187 #include "examples/GATSP/TSPEvalOp.h" 00188 int main(int argc, char **argv) 00189 { 00190 argc = 2; 00191 argv[1] = "./examples/GATSP/parameters.txt"; 00192 00193 StateP state = static_cast<StateP> (new State); 00194 00195 state->setEvalOp(static_cast<EvaluateOpP> (new TSPEvalOp)); 00196 00197 state->initialize(argc, argv); 00198 state->run(); 00199 00200 return 0; 00201 } 00202 */ 00203 00204 00205 00206 //5. primjer: GA problem aproksimacije funkcije 00207 /* 00208 #include "examples/GAApprox/ApproxEvalOp.h" 00209 int main(int argc, char **argv) 00210 { 00211 // argc = 2; 00212 // argv[1] = "./examples/GAApprox/parameters.txt"; 00213 00214 StateP state(new State); 00215 00216 state->setEvalOp(EvaluateOpP (new AproxEvalOp)); 00217 00218 state->initialize(argc, argv); 00219 state->run(); 00220 00221 return 0; 00222 } 00223 */ 00224 00225 00226 00227 //6. primjer: GP evolucija pravila rasporedjivanja 00228 /* 00229 #include "examples/GPScheduling/SchedulingEvalOp.h" 00230 int main(int argc, char **argv) 00231 { 00232 argc = 2; 00233 //argv[1] = "./examples/GPScheduling/parameters.txt"; 00234 argv[1] = "./parameters.txt"; 00235 00236 StateP state = static_cast<StateP> (new State); 00237 00238 state->setEvalOp(static_cast<EvaluateOpP> (new SchedulingEvalOp)); 00239 00240 state->initialize(argc, argv); 00241 state->run(); 00242 00243 // XMLNode xInd = XMLNode::parseFile("./ind.txt", "Individual"); 00244 // IndividualP ind = (IndividualP) new Individual(state); 00245 // ind->read(xInd); 00246 // state->getAlgorithm()->evaluate(ind); 00247 // std::cout << ind->toString(); 00248 00249 return 0; 00250 } 00251 */ 00252 00253 00254 //7. primjer: XCS 00255 /* 00256 #include "examples/XCSmux/MuxEvalOp.h" 00257 #include "examples/XCSmaze/SingleObjMazeEnv.h" 00258 #include "examples/XCSmaze/SeqObjMazeEnv.h" 00259 #include "examples/XCSmaze/TwoObjMazeEnv.h" 00260 #include "examples/XCSmaze/ThreeObjMazeEnv.h" 00261 00262 int main(int argc, char **argv) 00263 { 00264 argc = 2; 00265 StateP state = static_cast<StateP> (new State); 00266 MazeEnvP maze; 00267 00268 //Multistep: 00269 00270 // - sigle-objective maze: 00271 //argv[1] = "examples/XCSmaze/single-obj params.txt"; 00272 //maze = static_cast<MazeEnvP> (new SingleObjMazeEnv(state)); 00273 //maze->setMazeFile("examples/XCSmaze/Environments/single-objective/Maze1.txt"); 00274 00275 // - multi-objective maze: 00276 //argv[1] = "examples/XCSmaze/seq-obj params.txt"; 00277 //maze = static_cast<MazeEnvP> (new SeqObjMazeEnv(state)); 00278 //maze->setMazeFile("examples/XCSmaze/Environments/multi-objective/Maze1k.txt"); 00279 00280 argv[1] = "examples/XCSmaze/three-obj params.txt"; 00281 maze = static_cast<MazeEnvP> (new ThreeObjMazeEnv(state,0)); 00282 maze->setMazeFile("examples/XCSmaze/Environments/multi-objective/Maze1em.txt"); 00283 00284 maze->setResultsFile("examples/XCSmaze/Maze1k results.txt"); 00285 state->setEvalOp(maze); 00286 00287 //Singlestep: 00288 00289 // - 6-multiplexor problem: 00290 // argv[1] = "./examples/XCSmux/parametri.txt"; 00291 // state->setEvalOp(static_cast<EvaluateOpP> (new MuxEvalOp(state))); 00292 00293 state->initialize(argc, argv); 00294 00295 state->run(); 00296 00297 int a; 00298 cin >> a; 00299 return 0; 00300 } 00301 */ 00302 00303 00304 00305 //8. primjer: Kartezijski GP - feedforward 00306 /* 00307 #include "examples/CGPFeedForward/FeedForwardEvalOp.h" 00308 using namespace cart; 00309 00310 int main(int argc, char **argv) 00311 { 00312 argc = 2; 00313 argv[1] = "./examples/CGPFeedForward/parametri.txt"; 00314 00315 StateP state (new State); 00316 00317 //izabrati koji tip 00318 //state->setEvalOp(new cart::FeedForwardEvalOpInt); 00319 state->setEvalOp(static_cast<EvaluateOpP> (new cart::FeedForwardEvalOpDouble)); 00320 //state->setEvalOp(static_cast<EvaluateOpP> (new cart::CircuitEvalOpUint)); 00321 00322 state->initialize(argc, argv); 00323 state->run(); 00324 00325 return 0; 00326 } 00327 */