-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmoea_util.hpp
90 lines (78 loc) · 2.75 KB
/
cmoea_util.hpp
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
/*
* cmoea_util.hpp
*
* Created on: Jun 6, 2017
* Author: joost
*/
#ifndef MODULES_CMOEA_CMOEA_UTIL_HPP_
#define MODULES_CMOEA_CMOEA_UTIL_HPP_
#include <modules/datatools/common_compare.hpp>
namespace sferes {
namespace cmoea {
void calculate_bin_fitness(const std::vector<float>& task_performance, std::vector<float>& bin_fitness){
unsigned nr_of_bins = pow(2, task_performance.size()) - 1;
bin_fitness.clear();
std::vector<float> fitness_combination;
for(unsigned i=0; i<nr_of_bins; ++i){
fitness_combination.clear();
for(unsigned j=0; j<task_performance.size(); ++j){
if(int(i/(1<<j))%2 == 0){
fitness_combination.push_back(task_performance[j]);
}
}
float local_fitness = compare::average(fitness_combination);
dbg::out(dbg::info, "binfit") << "obj: " << i << " fitness " << local_fitness << std::endl;
bin_fitness.push_back(local_fitness);
}
}
void calculate_bin_fitness_mult(const std::vector<float>& task_performance, std::vector<float>& bin_fitness){
unsigned nr_of_bins = pow(2, task_performance.size()) - 1;
bin_fitness.clear();
std::vector<float> fitness_combination;
for(unsigned i=0; i<nr_of_bins; ++i){
fitness_combination.clear();
for(unsigned j=0; j<task_performance.size(); ++j){
if(int(i/(1<<j))%2 == 0){
fitness_combination.push_back(task_performance[j]);
}
}
float local_fitness = compare::mult(fitness_combination);
dbg::out(dbg::info, "binfit") << "obj: " << i << " fitness " << local_fitness << std::endl;
bin_fitness.push_back(local_fitness);
}
}
// Not an efficient implementation, but it at least guarentees that the objectives returned here
// are the objectives associated with that bin
std::vector<int> getTaskIndices(int bin_index, int nr_of_tasks){
std::vector<int> result;
unsigned nr_of_bins = pow(2, nr_of_tasks) - 1;
for(unsigned j=0; j<nr_of_tasks; ++j){
if(int(bin_index/(1<<j))%2 == 0){
result.push_back(j);
}
}
return result;
}
std::string getTaskIndicesStr(int bin_index, int nr_of_tasks){
std::vector<int> temp = getTaskIndices(bin_index, nr_of_tasks);
std::string result = "";
for(unsigned i=0; i<temp.size(); ++i){
result += boost::lexical_cast<std::string>(temp[i]);
if(i<temp.size()-1){
result += "_";
}
}
return result;
}
unsigned getNrOfObjectives(unsigned nr_of_bins){
unsigned result = 0;
nr_of_bins+=1;
while(nr_of_bins != 1){
nr_of_bins = nr_of_bins >> 1;
result+=1;
}
return result;
}
} // namespace cmoea
} // namespace sferes
#endif /* MODULES_CMOEA_CMOEA_UTIL_HPP_ */