ECF 1.5
OneMaxEvalOp.cpp
1#include <ecf/ECF.h>
2#include "OneMaxEvalOp.h"
3#include<fstream>
4#include<string>
5
6
8{
9 state->getRegistry()->registerEntry("infile", (voidP) (new std::string), ECF::STRING);
10}
11
12
13bool OneMaxEvalOp::initialize(StateP state)
14{
15 s = state;
16 height = width = 0;
17
18 if(!state->getRegistry()->isModified("infile")) {
19 state->getLogger()->log(1, "no input file defined (parameter 'infile'");
20 return false;
21 }
22
23 voidP sptr = state->getRegistry()->getEntry("infile"); // get parameter value
24 std::string filePath = *((std::string*) sptr.get()); // convert from voidP to user defined type
25
26 ifstream inFile(filePath);
27 std::string str;
28
29 uint genLength = 0, row = 0;
30
31 image.clear();
32 while(getline(inFile, str)) {
33 width = str.length();
34 image.push_back(std::vector<int>(width));
35 genLength += width;
36 for(uint col = 0; col < width; col++)
37 image[row][col] = (str[col] != ' ') ? 1 : 0;
38 row++;
39 }
40
41 // set dimension for BitString genotype
42 state->getRegistry()->modifyEntry("BitString.size", (voidP) new uint(genLength));
43
44 // reinitialize population with updated size
45 state->getPopulation()->initialize(state);
46
47 bestVal = 0;
48 height = row;
49
50 return true;
51}
52
53// evaluate() receives a smart pointer to the individual to evaluate
54FitnessP OneMaxEvalOp::evaluate(IndividualP individual)
55{
56 // evaluation creates a new fitness object using a smart pointer
57 // in our case, we try to maximize the number of ones, so we use FitnessMax fitness (for maximization problems)
58 FitnessP fitness (new FitnessMax);
59
60 // Each individual is a vector of genotypes (defined in the configuration file).
61 // We'll use BitString, and put it as the first and only genotype
62 BitString::BitString* bitstr = (BitString::BitString*) individual->getGenotype().get();
63 //BitStringP bitstr = boost::static_pointer_cast<BitString::BitString> (individual->getGenotype(0)); // don't need zero for the first one
64
65 // count the ones; where are they?
66 // BitString genotype contains a std::vector of bool's named 'bits'
67 uint hits = 0;
68 int row, col;
69 for(uint i = 0; i<bitstr->bits.size(); i++){
70 row = i / width;
71 col = i % width;
72 if(bitstr->bits[i] == true && image[row][col] > 0)
73 hits++ ;
74 else if(bitstr->bits[i] == false && image[row][col] == 0)
75 hits++;
76 }
77 fitness->setValue(hits);
78
79 if(hits > bestVal) {
80 system("cls");
81 char pix = 219;
82 bestVal = hits;
83 cout << endl;
84 for(uint i = 0; i < height; i++) {
85 for(uint j = 0; j < width; j++) {
86 if(bitstr->bits[i*width + j] == true)
87 cout << pix;
88 else
89 cout << " ";
90 }
91 cout << endl;
92 }
93 cout << endl;
94 }
95
96 if(bestVal == bitstr->bits.size()) {
97 //s->setTerminateCond();
98 for(uint i = 0; i < height; i++)
99 for(uint j = 0; j < width; j++)
100 image[i][j] = !image[i][j];
101 bestVal = 0;
102 }
103
104 // return the smart pointer to new fitness
105 return fitness;
106}
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
Fitness for maximization problems.
Definition: FitnessMax.h:13
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
Definition: OneMaxEvalOp.cpp:5
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
Definition: OneMaxEvalOp.cpp:7
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.