2#include "NQueensEvalOp.h"
8 voidP sptr = state->getRegistry()->getEntry(
"Permutation.size");
9 int problemSize = *((
int*) sptr.get());
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);
24void NQueensEvalOp::populateSolutionInstance(IndividualP ind,
struct solution* s)
30 for(uint i = 0; i < perm->getSize(); i++)
31 s->data[i] = perm->variables[i];
35void NQueensEvalOp::readSolutionInstance(IndividualP ind,
struct solution* s)
41 for(uint i = 0; i < perm->getSize(); i++)
42 perm->variables[i] = s->data[i];
46MoveP NQueensEvalOp::randomMove(IndividualP ind)
48 populateSolutionInstance(ind, solutionInstance);
50 ::randomMove(m->v, solutionInstance);
55bool NQueensEvalOp::applyMove(IndividualP& ind, MoveP
move)
58 populateSolutionInstance(ind, solutionInstance);
61 ::applyMove(solutionInstance, m->v);
63 readSolutionInstance(ind, solutionInstance);
68PathP NQueensEvalOp::initPathTo(IndividualP ind1, IndividualP ind2)
70 populateSolutionInstance(ind1, solutionInstance);
71 populateSolutionInstance(ind2, solutionInstance2);
72 ::initPathTo(psInstance, solutionInstance, solutionInstance2);
78int NQueensEvalOp::getPathLength(PathP path)
81 return ::getPathLength(p->ps);
85MoveP NQueensEvalOp::nextRandomMove(PathP path)
88 ::nextRandomMove(m->v, psInstance);
98 populateSolutionInstance(individual, solutionInstance);
101 struct solution* s = solutionInstance;
102 int i, j, n = s->n, att, nmod = s->nmod;
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]));
108 memcpy(s->olddata, s->data, n * sizeof (
int));
111 fitness->setValue(s->objvalue);
Fitness for minimization problems.
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)