2#include "KnapsackEvalOp.h"
8 state->getRegistry()->registerEntry(
"knapsack.infile", (voidP) (
new std::string), ECF::STRING);
16 if(!state->getRegistry()->isModified(
"knapsack.infile")) {
17 state->getLogger()->log(1,
"Error: no input file defined! (parameter 'knapsack.infile'");
21 voidP sptr = state->getRegistry()->getEntry(
"knapsack.infile");
22 std::string filePath = *((std::string*) sptr.get());
25 problemInstance = newProblem(filePath.c_str());
26 solutionInstance = allocSolution(problemInstance);
27 randomSolution(solutionInstance);
30 state->getRegistry()->modifyEntry(
"BitString.size", (voidP)
new uint(problemInstance->n));
33 state->getPopulation()->initialize(state);
39MoveP KnapsackEvalOp::randomMove(IndividualP ind)
42 move->data = state_->getRandomizer()->getRandomInteger(problemInstance->n);
47bool KnapsackEvalOp::applyMove(IndividualP& ind, MoveP
move)
49 struct problem *p = problemInstance;
53 if (i < 0 || i >= p->n)
55 if (s->bits[i] == 0) {
64PathP KnapsackEvalOp::initPathTo(IndividualP ind1, IndividualP ind2)
70 int i, k, n = problemInstance->n;
72 for (i = 0, k = 0; i < n; i++)
73 if (s1->bits[i] != s2->bits[i])
74 path->pos.push_back(i);
75 path->distance = path->pos.size();
80int KnapsackEvalOp::getPathLength(PathP path)
86MoveP KnapsackEvalOp::nextRandomMove(PathP path)
92 if (ps->distance == 0) {
97 r = state_->getRandomizer()->getRandomInteger(0, --ps->distance);
100 ps->pos[r] = ps->pos[ps->distance];
117 struct solution* s = solutionInstance;
118 struct problem *p = problemInstance;
120 s->profit = 0, s->weight = 0;
121 for (
int i = 0; i < n; i++)
122 if (bitstr->bits[i]) {
123 s->profit += p->profit[i];
124 s->weight += p->weight[i];
126 s->objvalue = s->weight - p->capacity;
127 if (s->objvalue <= 0)
128 s->objvalue = -s->profit;
130 fitness->setValue(s->objvalue);
BitString class - implements genotype as a series of bits.
Fitness for minimization problems.
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
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.