ECF 1.5
EvalOp.cpp
1#include <cmath>
2#include <ecf/ECF.h>
3#include "EvalOp.h"
4#include <iostream>
5#include <fstream>
6#include <vector>
7#include <iostream>
8#include <string>
9using namespace std;
10
11
12// from WriteTT.h
13extern bool evaluateVerbose;
14
15
16//
17// ispis u infix formatu
18//
19uint EvalOp::showTree(string& output, Tree::Tree* tree, uint iNode, uint prefix)
20{
21 Tree::PrimitiveP prim = tree->at(iNode)->primitive_;
22 int arity = prim->getNumberOfArguments();
23
24 if(arity == 0) {
25 output += prim->getName();
26 }
27 else if(prim->getName() == "P2") {
28 output += "P2(";
29 iNode++;
30 iNode = showTree(output, tree, iNode, prefix);
31 output += ", ";
32 iNode++;
33 iNode = showTree(output, tree, iNode, prefix);
34 output += ")";
35 }
36 else if(prim->getName() == "IF") {
37 output += "IF(";
38 iNode++;
39 iNode = showTree(output, tree, iNode, prefix);
40 output += ", ";
41 iNode++;
42 iNode = showTree(output, tree, iNode, prefix);
43 output += ", ";
44 iNode++;
45 iNode = showTree(output, tree, iNode, prefix);
46 output += ")";
47 }
48 else if(prim->getName() == "ADD" ||
49 prim->getName() == "AND" ||
50 prim->getName() == "AND2" ||
51 prim->getName() == "XOR" ||
52 prim->getName() == "XNOR" ||
53 prim->getName() == "OR") {
54 output += "(";
55 iNode++;
56 iNode = showTree(output, tree, iNode, prefix);
57 output += " " + prim->getName() + " ";
58 iNode++;
59 iNode = showTree(output, tree, iNode, prefix);
60 output += ")";
61 }
62 else if(prim->getName() == "MUL") {
63 output += "(";
64 iNode++;
65 iNode = showTree(output, tree, iNode, prefix);
66 output += " * ";
67 iNode++;
68 iNode = showTree(output, tree, iNode, prefix);
69 output += ")";
70 }
71 else if(prim->getName() == "NOT") {
72 output += "~";
73 output += "(";
74 for(int child = 0; child < arity; child++) {
75 iNode++;
76 iNode = showTree(output, tree, iNode, prefix);
77 }
78 output += ")";
79 }
80 else if(arity == 2) {
81 output += "(";
82 iNode++;
83 iNode = showTree(output, tree, iNode, prefix);
84 output += " " + prim->getName() + " ";
85 iNode++;
86 iNode = showTree(output, tree, iNode, prefix);
87 output += ")";
88 }
89 else if(arity == 1) {
90 output += prim->getName();
91 output += "(";
92 for(int child = 0; child < arity; child++) {
93 iNode++;
94 iNode = showTree(output, tree, iNode, prefix);
95 }
96 output += ")";
97 }
98 return iNode;
99}
100
101
102
104{
105 state->getRegistry()->registerEntry("variables", (voidP) (new uint(1)), ECF::UINT);
106 state->getRegistry()->registerEntry("variant", (voidP) (new int(1)), ECF::INT);
107 state->getRegistry()->registerEntry("nelinearnost", (voidP) (new int(0)), ECF::INT);
108}
109
110
111bool EvalOp::initialize(StateP state)
112{
113 state_ = state;
114 showTruth = false;
115 inputNames.clear();
116 inputMap.clear();
117
118 voidP sptr = state->getRegistry()->getEntry("variables");
119 nVariables = *((uint*)sptr.get());
120
121 sptr = state->getRegistry()->getEntry("variant");
122 varijanta = *((int*)sptr.get());
123
124 sptr = state->getRegistry()->getEntry("nelinearnost");
125 targetNl = *((int*)sptr.get());
126
127
129 for(uint var = 0; var < nVariables; var++)
130 inputNames.push_back("v" + uint2str(var));
131
132 // izgradi potpunu tablicu kombinacija ulaznih varijabli
133 inputMap.resize(nVariables);
134 bool t = true, f = false, val;
135 uint tries = (uint) pow(2., (int) nVariables);
136
137 results.resize(tries);
138 tt.resize(tries);
139 anfPos.resize(tries);
140
141 for(uint count = 0; count < tries; count++) {
142 uint mask = count;
143 for(uint var = 0; var < nVariables; var++) {
144 if(mask % 2)
145 val = true;
146 else
147 val = false;
148 mask /= 2;
149 inputMap[var].push_back(val);
150 }
151 }
152
153 // zadaj vrijednosti obicnim varijablama (ne mijenjaju se tijekom evolucije!)
154 Tree::Tree* tree = (Tree::Tree*) state->getGenotypes().at(0).get();
155 for (uint var = 0; var < (nVariables); var++) {
156 tree->setTerminalValue(inputNames[var], &inputMap[var]);
157 }
158
159
160 return true;
161}
162
163
164// obicna verzija fitnesa
165FitnessP EvalOp::evaluate(IndividualP individual)
166{
167 // greater fitness means better individual
168 FitnessP fitness (new FitnessMax);
169
170 // get tree from the individual
171 Tree::Tree* tree = (Tree::Tree*) individual->getGenotype().get();
172
173 // build TT
174 tree->execute(&results);
175
176 // ako treba prijepis u int
177 uint tries = (uint) tt.size();
178 for(uint i = 0; i < tries; i++) {
179 tt[i] = results[i];
180 }
181
182
183
184
185 fitness->setValue(0);
186
187 if(evaluateVerbose) {
188 ECF_LOG(state_, 1, "Truth table:");
189 stringstream ss;
190 for(uint i = 0; i < tt.size(); i++)
191 ss << tt[i];// << " ";
192 ss << endl;
193 string output;
194 showTree(output, tree);
195 ss << "infix: " << output;
196 ECF_LOG(state_, 1, ss.str());
197
198 // ispis TT u datoteku (ako treba)
199 ofstream out("funkcije.txt", ios_base::app);
200 for (uint i = 0; i < tt.size(); i++)
201 out << tt[i];
202 out << endl;
203 out.close();
204 }
205
206 return fitness;
207}
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
Definition: EvalOp.cpp:103
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
Definition: EvalOp.cpp:165
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Definition: EvalOp.cpp:111
Fitness for maximization problems.
Definition: FitnessMax.h:13
std::string getName()
Return genotype's name (each genotype is uniquely identified with its name).
Definition: Genotype.h:86
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
void setTerminalValue(std::string, void *)
Set a terminal's value.
Definition: Tree.cpp:504
void execute(void *)
Execute current tree.
Definition: Tree.cpp:362