-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
112 lines (84 loc) · 2.16 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Created by Gianluca on 24/05/2019.
//
#include "Solution.h"
#include <sstream>
Solution::Solution() {
objectiveFunction_ = MAX_VALUE;
fitness_ = MAX_VALUE;
for(int i=0; i<20; i++)
indexes_[i] = false;
isFeasible_ = false;
}
Solution::Solution(double objf, std::array<bool, 20> ind, bool feasible) {
objectiveFunction_ = objf;
indexes_ = ind;
isFeasible_ = feasible;
setFitness();
}
Solution::Solution(std::array<bool, 20> ind) {
objectiveFunction_ = MAX_VALUE;
fitness_ = MAX_VALUE;
indexes_ = ind;
isFeasible_ = false;
}
void Solution::setObjectiveFunction(double objf) {
objectiveFunction_ = objf;
}
double Solution::getObjectiveFunction() {
return objectiveFunction_;
}
void Solution::setArray(std::array<bool, 20> ind) {
indexes_ = ind;
}
void Solution::setArrayValue(int index, bool value) {
indexes_[2] = value;
}
bool Solution::getArrayValue(int index) {
return indexes_[index];
}
void Solution::flip(int index) {
indexes_[index] = !indexes_[index];
}
std::array<bool, 20> Solution::getArray() {
return indexes_;
}
std::string Solution::to_string() {
std::stringstream ss;
ss << objectiveFunction_ << " " << fitness_ << " " << isFeasible_ << std::endl << getArrayString();
return ss.str();
}
std::string Solution::getArrayString() {
std::stringstream ss;
for(auto &value : indexes_)
ss << value << " ";
return ss.str();
}
bool Solution::isFeasible() {
return isFeasible_;
}
bool Solution::setFeasible(bool feasible) {
isFeasible_ = feasible;
}
bool Solution::equals(Solution s1, Solution s2) {
return s1.objectiveFunction_ == s2.objectiveFunction_ && s1.isFeasible_ == s2.isFeasible_ && s1.indexes_ == s2.indexes_;
}
bool Solution::compare(Solution s1, Solution s2) {
return s1.fitness_ < s2.fitness_;
}
double Solution::getFitness() {
return fitness_;
}
void Solution::setFitness() {
fitness_ = computeFitness(objectiveFunction_);
}
int Solution::count1Columns() {
int n = 0;
for(int i=0; i<20; i++){
if(indexes_[i]) n++;
}
return n;
}
double Solution::computeFitness(double objf) {
return objf;
}