ECF 1.5
MOFitness.cpp
1#include "ECF_base.h"
2#include "MOFitness.h"
3
4
5bool MOFitness::isBetterThan(FitnessP other)
6{
7 MOFitnessP p = boost::static_pointer_cast<MOFitness> (other);
8 uint objCount = (uint) p->size();
9
10 for (uint i = 0; i<objCount; i++) {
11 if ( this->rank < p->rank) {
12 return true;
13 }
14 if (p->rank < this->rank) {
15 return false;
16 }
17 if (this->crowding_distance > p->crowding_distance) {
18 return true;
19 }
20 return false;
21
22 }
23
24 return true;
25}
26
28{
29 MOFitness *newObject = new MOFitness(*this);
30 // copy individual Fitness objects from the original
31 for(int i = 0; i < (int) this->size(); i++) {
32 (*newObject)[i] = (FitnessP) ((*this)[i]->copy());
33 }
34 newObject->rank = this->rank;
35 newObject->nc = this->nc;
36 newObject->Sp = this->Sp;
37 newObject->crowding_distance = this->crowding_distance; // ako se kopija ukljuci u populaciju crowding_distance ce biti 0 za obje jedinke
38
39 return newObject;
40}
41
42void MOFitness::write(XMLNode &xFitness)
43{
44 xFitness = XMLNode::createXMLTopNode("MOFitness");
45 uint size = (uint) this->size();
46 for(uint i = 0; i < size; i++) {
47 std::stringstream sValue;
48 sValue << this->at(i)->getValue();
49 std::string value = "value" + uint2str(i + 1);
50 xFitness.addAttribute(value.c_str(), sValue.str().c_str());
51 }
52}
53
55{
56 return this->rank;
57}
58
59double MOFitness::getValueOfObjective(int objective)
60{
61 return this->at(objective)->getValue();
62}
63
64double MOFitness::getProperty(std::string prop, int objective = -1) {
65 if (prop.compare("objective") == 0) {
66 if (objective >= 0 && objective < (int) this->size()) {
67 return getValueOfObjective(objective);
68 } else {
69 // zatrazena funkcija cilja ne postoji
70 throw "";
71 }
72 } else if (prop.compare("rank") == 0) {
73 return (double) this->rank;
74 } else if (prop.compare("crowding_distance") == 0) {
75 return this->crowding_distance;
76 } else {
77 // zatrazeni property ne postoji
78 throw "";
79 }
80}
double getValue()
Return fitness value.
Definition: MOFitness.cpp:54
bool isBetterThan(FitnessP other)
Mandatory comparison operator.
Definition: MOFitness.cpp:5
MOFitness * copy()
object copy (for duplication of an individual)
Definition: MOFitness.cpp:27
void write(XMLNode &xFitness)
Write fitness object to XMLNode.
Definition: MOFitness.cpp:42