ECF 1.5
KnapsackEvalOp.cpp
1#include <ECF/ECF.h>
2#include "KnapsackEvalOp.h"
3#include "knapsack.h"
4
5
7{
8 state->getRegistry()->registerEntry("knapsack.infile", (voidP) (new std::string), ECF::STRING);
9}
10
11
13{
14 state_ = state;
15
16 if(!state->getRegistry()->isModified("knapsack.infile")) {
17 state->getLogger()->log(1, "Error: no input file defined! (parameter 'knapsack.infile'");
18 return false;
19 }
20
21 voidP sptr = state->getRegistry()->getEntry("knapsack.infile"); // get parameter value
22 std::string filePath = *((std::string*) sptr.get()); // convert from voidP to user defined type
23
24 // API calls
25 problemInstance = newProblem(filePath.c_str());
26 solutionInstance = allocSolution(problemInstance);
27 randomSolution(solutionInstance);
28
29 // adjust BitString genotype size
30 state->getRegistry()->modifyEntry("BitString.size", (voidP) new uint(problemInstance->n));
31
32 // reinitialize population with updated size
33 state->getPopulation()->initialize(state);
34
35 return true;
36}
37
38
39MoveP KnapsackEvalOp::randomMove(IndividualP ind)
40{
42 move->data = state_->getRandomizer()->getRandomInteger(problemInstance->n);
43 return (MoveP) move;
44}
45
46
47bool KnapsackEvalOp::applyMove(IndividualP& ind, MoveP move)
48{
49 struct problem *p = problemInstance;
50 KnapsackMove* m = (KnapsackMove*) move.get();
51 BitString::BitString* s = (BitString::BitString*) ind->getGenotype().get();
52 int i = m->data;
53 if (i < 0 || i >= p->n) /* null move, do nothing */
54 return false;
55 if (s->bits[i] == 0) {
56 s->bits[i] = 1;
57 } else {
58 s->bits[i] = 0;
59 }
60 return true;
61}
62
63
64PathP KnapsackEvalOp::initPathTo(IndividualP ind1, IndividualP ind2)
65{
66 KnapsackPath* path (new KnapsackPath);
67 BitString::BitString* s1 = (BitString::BitString*) ind1->getGenotype().get();
68 BitString::BitString* s2 = (BitString::BitString*) ind2->getGenotype().get();
69
70 int i, k, n = problemInstance->n;
71 /* Indices of the bits in which the solutions differ */
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();
76 return (PathP) path;
77}
78
79
80int KnapsackEvalOp::getPathLength(PathP path)
81{
82 return ((KnapsackPath*) path.get())->distance;
83}
84
85
86MoveP KnapsackEvalOp::nextRandomMove(PathP path)
87{
89 KnapsackPath* ps = (KnapsackPath*) path.get();
90
91 int r;
92 if (ps->distance == 0) { /* end of path has been reached, no move is possible */
93 v->data = -1; /* null move */
94 return (MoveP) v;
95 }
96 /* Draw an index at random */
97 r = state_->getRandomizer()->getRandomInteger(0, --ps->distance);
98 v->data = ps->pos[r];
99 /* Update pathState - remember which moves are still available (and only those) */
100 ps->pos[r] = ps->pos[ps->distance];
101
102 return (MoveP) v;
103}
104
105
106FitnessP KnapsackEvalOp::evaluate(IndividualP individual)
107{
108 // minimization
109 FitnessP fitness (new FitnessMin);
110
111 // Each individual is a vector of genotypes (defined in the configuration file).
112 // We'll use BitString, and put it as the first and only genotype
113 BitString::BitString* bitstr = (BitString::BitString*) individual->getGenotype().get();
114 //BitStringP bitstr = boost::static_pointer_cast<BitString::BitString> (individual->getGenotype(0)); // don't need zero for the first one
115
116 // evaluate
117 struct solution* s = solutionInstance;
118 struct problem *p = problemInstance;
119 int n = s->n;
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];
125 }
126 s->objvalue = s->weight - p->capacity;
127 if (s->objvalue <= 0)
128 s->objvalue = -s->profit;
129
130 fitness->setValue(s->objvalue);
131
132 return fitness;
133}
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
Fitness for minimization problems.
Definition: FitnessMin.h:12
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.
Definition: flowshop.h:78