Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
fitness.h
Go to the documentation of this file.
1#ifndef FITNESS_H
2#define FITNESS_H
3
4#include <functional>
5#include "../init.h"
6#include "../util/utils.h"
7
8using namespace nlohmann;
9
10namespace Brush{
11
25struct Fitness {
26 // the loss is used in evolutionary functions
27
28 float loss;
29 float loss_v;
30
31 unsigned int complexity;
32 unsigned int size;
33 unsigned int depth;
34
35 // these can be different depending on the island the individual is
36 unsigned int dcounter;
37 vector<unsigned int> dominated;
38 unsigned int rank;
40
41 vector<float> values;
42 vector<float> weights;
43
44 // weighted values
45 vector<float> wvalues;
46
47 void set_dominated(vector<unsigned int>& dom){ dominated=dom; };
48 vector<unsigned int> get_dominated() const { return dominated; };
49
50 void set_loss(float f){ loss=f; };
51 float get_loss() const { return loss; };
52
53 void set_loss_v(float f_v){ loss_v=f_v; };
54 float get_loss_v() const { return loss_v; };
55
56 void set_size(unsigned int new_s){ size=new_s; };
57 unsigned int get_size() const { return size; };
58
59 void set_complexity(unsigned int new_c){ complexity=new_c; };
60 unsigned int get_complexity() const { return complexity; };
61
62 void set_depth(unsigned int new_d){ depth=new_d; };
63 unsigned int get_depth() const { return depth; };
64
65 void set_dcounter(unsigned int d){ dcounter=d; };
66 unsigned int get_dcounter() const { return dcounter; };
67
68 void set_rank(unsigned r){ rank=r; };
69 size_t get_rank() const { return rank; };
70
72 float get_crowding_dist() const { return crowding_dist; };
73
74 // Constructor with initializer list for weights
75 Fitness(const vector<float>& w={}) : values(), wvalues(), weights(w) {
76 dcounter = 0;
77 set_rank(0);
79 dominated.resize(0);
80 }
81
82 // Hash function (deap requires individuals (and fitness by induction)
83 // to be hashable)
84 size_t hash() const {
85 std::size_t h = std::hash<vector<float>>{}(wvalues);
86 return h;
87 }
88
89 void set_weights(vector<float>& w) {
90 weights = w;
91 }
92 vector<float> get_weights() const {
93 return weights;
94 }
95 vector<float> get_values() const {
96 return values;
97 }
98 vector<float> get_wvalues() const {
99 return wvalues;
100 }
101
102 // Method to set values
103 void set_values(vector<float>& v) {
104 if (v.size() != weights.size()) {
105 throw std::length_error("Assigned values have not the same length than current values");
106 }
107
108 values.resize(0);
109 for (const auto& element : v) {
110 values.push_back(element);
111 }
112
113 // Minimizing/maximizing problem: negative/positive weight, respectively.
114 wvalues.resize(weights.size());
115
116 // Perform element-wise multiplication
117 std::transform(v.begin(), v.end(),
118 weights.begin(), wvalues.begin(),
119 [](double a, double b) {
120 return a * b;
121 });
122 }
123
124 // Method to clear values
125 void clearValues() {
126 wvalues.clear();
127 }
128
129 bool valid() const {
130 return !wvalues.empty();
131 }
132
133 // Equality comparison
134 bool operator==(const Fitness& other) const {
135 return wvalues == other.wvalues;
136 }
137
138 // Inequality comparison
139 bool operator!=(const Fitness& other) const {
140 return !(*this == other);
141 }
142
143 // Less than comparison
144 bool operator<(const Fitness& other) const {
145 // because of the weights, every objective is a maximization problem
146 return !std::lexicographical_compare(wvalues.begin(), wvalues.end(),
147 other.wvalues.begin(), other.wvalues.end());
148 }
149
150 // Greater than comparison
151 bool operator>(const Fitness& other) const {
152 return other < *this;
153 }
154
155 // Less than or equal to comparison
156 bool operator<=(const Fitness& other) const {
157 return !(other < *this);
158 }
159
160 // Greater than or equal to comparison
161 bool operator>=(const Fitness& other) const {
162 return !(*this < other);
163 }
164
165 // String representation
166 std::string toString() const {
167 if (valid()) {
168 string s = "Fitness(";
169 for (auto& v : values)
170 s += to_string(v) + " ";
171 return s+")";
172 } else {
173 return "Fitness()";
174 }
175 }
176
177 // Representation for debugging
178 std::string repr() const {
179 if (valid()) {
180 string s = "Fitness(";
181 for (auto& v : values)
182 s += to_string(v) + " ";
183 return s+")";
184 } else {
185 return "Fitness()";
186 }
187 }
188
190 int dominates(const Fitness& b) const;
191};
192
193void to_json(json &j, const Fitness &f);
194void from_json(const json &j, Fitness& f);
195
196}
197#endif
void bind_engine(py::module &m, string name)
< nsga2 selection operator for getting the front
Definition data.cpp:12
void from_json(const json &j, Fitness &f)
Definition fitness.cpp:24
void to_json(json &j, const Fitness &f)
Definition fitness.cpp:6
Represents the fitness of an individual in the Brush namespace.
Definition fitness.h:25
bool operator>(const Fitness &other) const
Definition fitness.h:151
float crowding_dist
crowding distance on the Pareto front
Definition fitness.h:39
void clearValues()
Definition fitness.h:125
vector< float > weights
Definition fitness.h:42
vector< unsigned int > get_dominated() const
Definition fitness.h:48
float get_crowding_dist() const
Definition fitness.h:72
unsigned int complexity
Definition fitness.h:31
bool operator>=(const Fitness &other) const
Definition fitness.h:161
float loss
aggregate loss score
Definition fitness.h:28
float get_loss() const
Definition fitness.h:51
std::string repr() const
Definition fitness.h:178
vector< float > get_values() const
Definition fitness.h:95
bool valid() const
Definition fitness.h:129
std::string toString() const
Definition fitness.h:166
void set_complexity(unsigned int new_c)
Definition fitness.h:59
float get_loss_v() const
Definition fitness.h:54
size_t hash() const
Definition fitness.h:84
unsigned int dcounter
number of individuals this dominates
Definition fitness.h:36
bool operator==(const Fitness &other) const
Definition fitness.h:134
void set_loss_v(float f_v)
Definition fitness.h:53
void set_dominated(vector< unsigned int > &dom)
Definition fitness.h:47
bool operator!=(const Fitness &other) const
Definition fitness.h:139
bool operator<(const Fitness &other) const
Definition fitness.h:144
vector< float > values
Definition fitness.h:41
void set_depth(unsigned int new_d)
Definition fitness.h:62
unsigned int get_complexity() const
Definition fitness.h:60
size_t get_rank() const
Definition fitness.h:69
vector< float > wvalues
Definition fitness.h:45
void set_dcounter(unsigned int d)
Definition fitness.h:65
float loss_v
aggregate validation loss score
Definition fitness.h:29
unsigned int size
Definition fitness.h:32
vector< float > get_wvalues() const
Definition fitness.h:98
bool operator<=(const Fitness &other) const
Definition fitness.h:156
unsigned int rank
pareto front rank
Definition fitness.h:38
void set_values(vector< float > &v)
Definition fitness.h:103
unsigned int get_dcounter() const
Definition fitness.h:66
void set_rank(unsigned r)
Definition fitness.h:68
Fitness(const vector< float > &w={})
Definition fitness.h:75
unsigned int depth
Definition fitness.h:33
void set_size(unsigned int new_s)
Definition fitness.h:56
vector< unsigned int > dominated
individual indices this dominates
Definition fitness.h:37
unsigned int get_depth() const
Definition fitness.h:63
void set_crowding_dist(float cd)
Definition fitness.h:71
void set_weights(vector< float > &w)
Definition fitness.h:89
int dominates(const Fitness &b) const
set obj vector given a string of objective names
Definition fitness.cpp:41
unsigned int get_size() const
Definition fitness.h:57
vector< float > get_weights() const
Definition fitness.h:92
void set_loss(float f)
Definition fitness.h:50