ECF 1.5
NQueensEvalOp.cpp
1#include <ECF/ECF.h>
2#include "NQueensEvalOp.h"
3
4
5bool NQueensEvalOp::initialize(StateP state)
6{
7 state_ = state;
8 voidP sptr = state->getRegistry()->getEntry("Permutation.size");
9 int problemSize = *((int*) sptr.get());
10
11 // generate reusable objects from the original code
12 problemInstance = ::newProblem(problemSize);
13 solutionInstance = ::allocSolution(problemInstance);
14 ::randomSolution(solutionInstance);
15 solutionInstance2 = ::allocSolution(problemInstance);
16 ::randomSolution(solutionInstance2);
17 moveInstance = ::allocMove(problemInstance);
18 psInstance = ::allocPathState(problemInstance);
19
20 return true;
21}
22
23
24void NQueensEvalOp::populateSolutionInstance(IndividualP ind, struct solution* s)
25{
26 // get Permutation genotype from the individual
27 Permutation::Permutation* perm = (Permutation::Permutation*) ind->getGenotype().get();
28
29 // populate solution instance
30 for(uint i = 0; i < perm->getSize(); i++)
31 s->data[i] = perm->variables[i];
32}
33
34
35void NQueensEvalOp::readSolutionInstance(IndividualP ind, struct solution* s)
36{
37 // get Permutation genotype from the individual
38 Permutation::Permutation* perm = (Permutation::Permutation*) ind->getGenotype().get();
39
40 // read solution instance
41 for(uint i = 0; i < perm->getSize(); i++)
42 perm->variables[i] = s->data[i];
43}
44
45
46MoveP NQueensEvalOp::randomMove(IndividualP ind)
47{
48 populateSolutionInstance(ind, solutionInstance);
49 NQueensMove* m (new NQueensMove(moveInstance));
50 ::randomMove(m->v, solutionInstance);
51 return (MoveP) m;
52}
53
54
55bool NQueensEvalOp::applyMove(IndividualP& ind, MoveP move)
56{
57 NQueensMove* m = (NQueensMove*) move.get();
58 populateSolutionInstance(ind, solutionInstance);
59 //::printMove(m->v);
60 //::printSolution(solutionInstance);
61 ::applyMove(solutionInstance, m->v);
62 //::printSolution(solutionInstance);
63 readSolutionInstance(ind, solutionInstance);
64 return true;
65}
66
67
68PathP NQueensEvalOp::initPathTo(IndividualP ind1, IndividualP ind2)
69{
70 populateSolutionInstance(ind1, solutionInstance);
71 populateSolutionInstance(ind2, solutionInstance2);
72 ::initPathTo(psInstance, solutionInstance, solutionInstance2);
73 NQueensPath* path (new NQueensPath(psInstance));
74 return (PathP) path;
75}
76
77
78int NQueensEvalOp::getPathLength(PathP path)
79{
80 NQueensPath* p = (NQueensPath*) path.get();
81 return ::getPathLength(p->ps);
82}
83
84
85MoveP NQueensEvalOp::nextRandomMove(PathP path)
86{
87 NQueensMove* m (new NQueensMove(moveInstance));
88 ::nextRandomMove(m->v, psInstance);
89 return (MoveP) m;
90}
91
92
93FitnessP NQueensEvalOp::evaluate(IndividualP individual)
94{
95 // minimization
96 FitnessP fitness (new FitnessMin);
97
98 populateSolutionInstance(individual, solutionInstance);
99
100 // evaluate
101 struct solution* s = solutionInstance;
102 int i, j, n = s->n, att, nmod = s->nmod;
103 att = 0;
104 for (i = 0; i < n-1; i++)
105 for (j = i+1; j < n; j++)
106 att += (j - i == abs(s->data[i] - s->data[j]));
107 s->objvalue = att;
108 memcpy(s->olddata, s->data, n * sizeof (int));
109 s->nmod = 0;
110
111 fitness->setValue(s->objvalue);
112
113 return fitness;
114}
Fitness for minimization problems.
Definition: FitnessMin.h:12
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37
Definition: flowshop.h:78