ECF 1.5
FlowshopEvalOp.cpp
1#include <ECF/ECF.h>
2#include "FlowshopEvalOp.h"
3
4
6{
7 state->getRegistry()->registerEntry("flowshop.infile", (voidP) (new std::string), ECF::STRING);
8}
9
10
12{
13 state_ = state;
14
15 if(!state->getRegistry()->isModified("flowshop.infile")) {
16 state->getLogger()->log(1, "Error: no input file defined! (parameter 'flowshop.infile'");
17 return false;
18 }
19
20 voidP sptr = state->getRegistry()->getEntry("flowshop.infile"); // get parameter value
21 std::string filePath = *((std::string*) sptr.get()); // convert from voidP to user defined type
22
23 // generate reusable objects from the original code
24 problemInstance = ::newProblem(filePath.c_str());
25 solutionInstance = ::allocSolution(problemInstance);
26 ::randomSolution(solutionInstance);
27 solutionInstance2 = ::allocSolution(problemInstance);
28 ::randomSolution(solutionInstance2);
29 moveInstance = ::allocMove(problemInstance);
30 psInstance = ::allocPathState(problemInstance);
31
32 // adjust Permutation genotype size
33 state->getRegistry()->modifyEntry("Permutation.size", (voidP) new uint(problemInstance->n));
34
35 // reinitialize population with updated size
36 state->getPopulation()->initialize(state);
37
38 return true;
39}
40
41
42void FlowshopEvalOp::populateSolutionInstance(IndividualP ind, struct solution* s)
43{
44 // get Permutation genotype from the individual
45 Permutation::Permutation* perm = (Permutation::Permutation*) ind->getGenotype().get();
46
47 // populate solution instance
48 for(uint i = 0; i < perm->getSize(); i++)
49 s->data[i] = perm->variables[i];
50}
51
52
53void FlowshopEvalOp::readSolutionInstance(IndividualP ind, struct solution* s)
54{
55 // get Permutation genotype from the individual
56 Permutation::Permutation* perm = (Permutation::Permutation*) ind->getGenotype().get();
57
58 // read solution instance
59 for(uint i = 0; i < perm->getSize(); i++)
60 perm->variables[i] = s->data[i];
61}
62
63
64MoveP FlowshopEvalOp::randomMove(IndividualP ind)
65{
66 populateSolutionInstance(ind, solutionInstance);
67 FlowshopMove* m (new FlowshopMove(moveInstance));
68 ::randomMove(m->v, solutionInstance);
69 return (MoveP) m;
70}
71
72
73bool FlowshopEvalOp::applyMove(IndividualP& ind, MoveP move)
74{
75 FlowshopMove* m = (FlowshopMove*) move.get();
76 populateSolutionInstance(ind, solutionInstance);
77 ::applyMove(solutionInstance, m->v);
78 readSolutionInstance(ind, solutionInstance);
79 return true;
80}
81
82
83PathP FlowshopEvalOp::initPathTo(IndividualP ind1, IndividualP ind2)
84{
85 populateSolutionInstance(ind1, solutionInstance);
86 populateSolutionInstance(ind2, solutionInstance2);
87 ::initPathTo(psInstance, solutionInstance, solutionInstance2);
88 FlowshopPath* path (new FlowshopPath(psInstance));
89 return (PathP) path;
90}
91
92
93int FlowshopEvalOp::getPathLength(PathP path)
94{
95 FlowshopPath* p = (FlowshopPath*) path.get();
96 return ::getPathLength(p->ps);
97}
98
99
100MoveP FlowshopEvalOp::nextRandomMove(PathP path)
101{
102 FlowshopMove* m (new FlowshopMove(moveInstance));
103 ::nextRandomMove(m->v, psInstance);
104 return (MoveP) m;
105}
106
107
108FitnessP FlowshopEvalOp::evaluate(IndividualP individual)
109{
110 // minimization
111 FitnessP fitness (new FitnessMin);
112
113 populateSolutionInstance(individual, solutionInstance);
114
115 // evaluate
116 double value = ::getObjectiveValue(solutionInstance);
117
118 fitness->setValue(value);
119
120 return fitness;
121}
Fitness for minimization problems.
Definition: FitnessMin.h:12
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
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.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37
Definition: flowshop.h:78