14const char AntEvalOp::LEFT =
'<';
15const char AntEvalOp::RIGHT =
'>';
16const char AntEvalOp::UP =
'^';
17const char AntEvalOp::DOWN =
'v';
25 for (uint j = 0; j < ant.tmpRow; j++) {
27 for (uint k = 0; k < ant.tmpColumn; k++)
28 os << ant.tmpBoard[j * ant.tmpColumn + k];
37 state->getRegistry()->registerEntry(
"learning_trails", (voidP) (
new std::string(
"learning_trails.txt")), ECF::STRING);
38 state->getRegistry()->registerEntry(
"test_trails", (voidP) (
new std::string(
"test_trails.txt")), ECF::STRING);
49 state->getContext()->environment =
this;
51 voidP sptr = state->getRegistry()->getEntry(
"learning_trails");
52 std::string filePath = *((std::string*) sptr.get());
55 file.open(filePath.c_str());
58 ECF_LOG_ERROR(state,
"Error: can't open input file " + filePath);
66 for (uint k = 0; k < boardNo; k++) {
67 tmpRow = tmpColumn = tmpMaxSteps = 0;
74 rowNo.push_back(tmpRow);
75 columnNo.push_back(tmpColumn);
76 maxSteps.push_back(tmpMaxSteps);
77 foodNo.push_back(tmpFoodNo);
79 tmpBoard = (
char*) malloc (tmpRow * tmpColumn);
82 for (uint i = 0; i < tmpRow; i++ )
83 for (uint j = 0; j < tmpColumn; j++)
84 file >> tmpBoard[i * tmpColumn + j];
86 board.push_back(tmpBoard);
110 output.open(
"./output.txt");
111 output <<
"Number of trails: " << boardNo << endl;
115 for (uint i = 0; i < boardNo; i++) {
124 tmpColumn = columnNo[i];
125 tmpMaxSteps = maxSteps[i];
126 tmpFoodNo = foodNo[i];
128 tmpBoard = (
char*) malloc (tmpRow * tmpColumn);
131 for (uint j = 0; j < tmpRow; j++)
132 for (uint k = 0; k < tmpColumn; k++)
133 tmpBoard[j * tmpColumn + k] = board[i][j * tmpColumn + k];
136 if (tmpBoard[0] ==
'x')
140 if (tmpBoard[0] ==
'x')
142 else tmpBoard[0] =
'o';
145 while(moves_ < tmpMaxSteps && foodEaten_< tmpFoodNo) {
148 allTheFood += foodEaten_;
152 output <<
"Dimension: " << tmpColumn <<
"x" << tmpRow << endl;
153 output <<
"Food: " << tmpFoodNo << endl;
154 output <<
"Max Steps: " << tmpMaxSteps << endl;
155 output <<
"Fitness (eaten food): " << foodEaten_ << endl;
156 output << *
this << endl;
162 cout <<
"Eaten food: " << foodEaten_ << endl;
169 cout <<
"Food eaten in all the enviroments: " << allTheFood << endl;
170 output <<
"Total fitness: " << allTheFood << endl;
174 fitness->setValue(allTheFood);
185 if (moves_ >= tmpMaxSteps)
return;
188 facing_ = (facing_ - 1) % 4;
199 if (moves_ >= tmpMaxSteps)
return;
202 facing_ = (facing_ + 1) % 4;
213 if (moves_ >= tmpMaxSteps)
return;
221 y_ = (y_ - 1) % tmpRow;
224 x_ = (x_ - 1) % tmpColumn;
227 y_ = (y_ + 1) % tmpRow;
230 x_ = (x_ + 1) % tmpColumn;
237 if (tmpBoard[y_*tmpColumn+x_] ==
'x') {
240 tmpBoard[y_*tmpColumn+x_] = -2;
242 tmpBoard[y_*tmpColumn+x_] =
'.';
245 if(tmpBoard[y_*tmpColumn+x_] ==
'.')
246 tmpBoard[y_*tmpColumn+x_] =
'o';
263 case 3: y1 = (y_==0 ? tmpRow-1 : y_-1);
break;
264 case 2: x1 = (x_==0 ? tmpColumn-1 : x_-1);
break;
265 case 1: y1 = (y_+1)%tmpRow;
break;
266 case 0: x1 = (x_+1)%tmpColumn;
break;
268 if (tmpBoard[y1 * tmpColumn + x1] ==
'x')
279 cout <<
"Ant: " << currentTree << endl;
280 cout <<
"Action: " << action <<
", moves: " << moves_ <<
"/" << tmpMaxSteps << endl;
282 char ant, position = tmpBoard[y_ * tmpColumn + x_];
284 case 3: ant = UP;
break;
285 case 2: ant = LEFT;
break;
286 case 1: ant = DOWN;
break;
287 case 0: ant = RIGHT;
break;
290 tmpBoard[y_ * tmpColumn + x_] = ant;
292 tmpBoard[y_ * tmpColumn + x_] = position;
294 cout <<
"(Enter to continue...)";
Artificial ant evaluation class (and environment simulator)
void turnRight()
Turn ant on the right.
void showStep(string action)
Show the ant's current action (interactive)
bool initialize(StateP)
Initialize the simulator, read environments from input file.
static bool step
show the ant's movement interactive step by step (no effect if trace is false!)
static bool trace
trace the ant's movement in the simulator (for visual output)
void moveAhead()
Move ant ahead.
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
FitnessP evaluate(IndividualP individual)
Evaluation function, simulates ant movement and counts the eaten food.
bool facingFood()
Check if there's food in front.
void turnLeft()
Turn ant on the left.
Fitness for maximization problems.
std::string toString()
Output genotype to string.
Tree class - implements genotype as a tree.
void execute(void *)
Execute current tree.