ECF 1.5
FunctionMinEvalOp.cpp
1#include <ecf/ECF.h>
2#include "FunctionMinEvalOp.h"
3#include <vector>
4using namespace std;
5
6
8{
9 state->getRegistry()->registerEntry("function", (voidP) (new uint(1)), ECF::UINT);
10}
11
12
13bool FunctionMinEvalOp::initialize(StateP state)
14{
15 voidP sptr = state->getRegistry()->getEntry("function"); // get parameter value
16 iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
17
18 voidP lbound_ptr = state->getGenotypes()[0]->getParameterValue(state, "lbound");
19 double lbound = *((double*)lbound_ptr.get());
20
21
22 return true;
23}
24
25
26double calculateDistance(double s1[], double s2[])
27{
28 double d = 0;
29 for(uint i = 0; i < 3; i++)
30 d += (s1[i] - s2[i]) * (s1[i] - s2[i]);
31 d = sqrt(d);
32 return d;
33}
34
35
36FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
37{
38 const double c = 300000; // light speed in km/s
39
40 // ovo bi GA trebao otkriti:
41 double receiver[] = {1, -1, 0.25};
42 double receiverTimeError = 0.000001;
43
44 // koordinate satelita, x, y, z
45 double s1[] = {0, -400, 1000};
46 double s2[] = {300, 0, 1000};
47 double s3[] = {-600, 0, 1000};
48 double s4[] = {0, 700, 1000};
49 double* sats[4] = {s1, s2, s3, s4};
50
51 // izracunaj udaljenosti
52 double d1 = calculateDistance(s1, receiver);
53 double d2 = calculateDistance(s2, receiver);
54 double d3 = calculateDistance(s3, receiver);
55 double d4 = calculateDistance(s4, receiver);
56 double dist[4] = {d1, d2, d3, d4};
57
58 // izracunaj trajanja putovanja signala
59 double signalSendTime = 0;
60 double signalTravelTime[4];
61 double signalReceiveTime[4];
62 for(uint i = 0; i < 4; i++) {
63 signalTravelTime[i] = dist[i] / c;
64 signalReceiveTime[i] = signalSendTime + signalTravelTime[i] + receiverTimeError;
65 }
66
67 // dohvati trenutnu jedinku
68 FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
69
70 double fitnessValue = 0;
71 for(uint i = 0; i < 4; i++) {
72 double posError =
73 pow(gen->realValue[0] - sats[i][0], 2) +
74 pow(gen->realValue[1] - sats[i][1], 2) +
75 pow(gen->realValue[2] - sats[i][2], 2);
76 double timeError =
77 // A: bez uzimanja u obzir greske u satu primatelja
78 //pow(c * (signalReceiveTime[i] - receiverTimeError - signalSendTime), 2);
79 // B: uz racuanje greske u satu primatelja
80 pow(c * (signalReceiveTime[i] - gen->realValue[3]/100000 - signalSendTime), 2);
81 double satError = posError - timeError;
82 fitnessValue += satError * satError;
83 }
84
85 fitnessValue = sqrt(fitnessValue) / 4;
86
87 FitnessP fitness (new FitnessMin);
88 fitness->setValue(fitnessValue);
89
90 return fitness;
91}
Fitness for minimization problems.
Definition: FitnessMin.h:12
FloatingPoint class - implements genotype as a vector of floating point values.
Definition: FloatingPoint.h:39
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
uint iFunction_
function index
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
std::vector< double > realValue
vector of floating point values