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 linear_complexity;
33 unsigned int size;
34 unsigned int depth;
35
36 // We store the previous values of the objectives everytime we update them.
37 // This is used by the bandits to allow easy calculation of rewards.
38 float prev_loss;
40
41 unsigned int prev_complexity;
43 unsigned int prev_size;
44 unsigned int prev_depth;
45
46 // these can be different depending on the island the individual is
47 unsigned int dcounter;
48 vector<unsigned int> dominated;
49 unsigned int rank;
51
52 // store the values and weights for the objectives
53 vector<float> values;
54 vector<float> weights;
55
56 // weighted values
57 vector<float> wvalues;
58
59 void set_dominated(vector<unsigned int>& dom){ dominated=dom; };
60 vector<unsigned int> get_dominated() const { return dominated; };
61
62 // these setters are going to update the previous value --------------------
63 void set_loss(float f){prev_loss=loss; loss=f; };
64 float get_loss() const { return loss; };
65 float get_prev_loss() const {return prev_loss; };
66
67 void set_loss_v(float f_v){ prev_loss_v=loss_v; loss_v=f_v; };
68 float get_loss_v() const { return loss_v; };
69 float get_prev_loss_v() const {return prev_loss_v; };
70
71 void set_size(unsigned int new_s){ prev_size=size; size=new_s; };
72 unsigned int get_size() const { return size; };
73 unsigned int get_prev_size() const {return prev_size; };
74
75 void set_complexity(unsigned int new_c){
77 unsigned int get_complexity() const { return complexity; };
78 unsigned int get_prev_complexity() const {return prev_complexity; };
79
82 unsigned int get_linear_complexity() const { return linear_complexity; };
83 unsigned int get_prev_linear_complexity() const {return prev_linear_complexity; };
84
85 void set_depth(unsigned int new_d){ prev_depth=depth; depth=new_d; };
86 unsigned int get_depth() const { return depth; };
87 unsigned int get_prev_depth() const { return prev_depth; };
88
89 // -------------------------------------------------------------------------
90
91 void set_dcounter(unsigned int d){ dcounter=d; };
92 unsigned int get_dcounter() const { return dcounter; };
93
94 void set_rank(unsigned r){ rank=r; };
95 size_t get_rank() const { return rank; };
96
97 void set_crowding_dist(float cd){ crowding_dist=cd; };
98 float get_crowding_dist() const { return crowding_dist; };
99
100 // Constructor with initializer list for weights
101 Fitness(const vector<float>& w={}) : values(), wvalues(), weights(w) {
102 dcounter = 0;
103 set_rank(0);
104 set_crowding_dist(0.0f);
105 dominated.resize(0);
106 }
107
108 // Hash function (deap requires individuals (and fitness by induction)
109 // to be hashable)
110 size_t hash() const {
111 std::size_t h = std::hash<vector<float>>{}(wvalues);
112 return h;
113 }
114
115 void set_weights(vector<float>& w) {
116 weights = w;
117 }
118 vector<float> get_weights() const {
119 return weights;
120 }
121 vector<float> get_values() const {
122 return values;
123 }
124 vector<float> get_wvalues() const {
125 return wvalues;
126 }
127
128 // Method to set values
129 void set_values(vector<float>& v) {
130 if (v.size() != weights.size()) {
131 throw std::length_error("Assigned values have not the same length than current values");
132 }
133
134 values.resize(0);
135 for (const auto& element : v) {
136 values.push_back(element);
137 }
138
139 // Minimizing/maximizing problem: negative/positive weight, respectively.
140 wvalues.resize(weights.size());
141
142 // Perform element-wise multiplication
143 std::transform(v.begin(), v.end(),
144 weights.begin(), wvalues.begin(),
145 [](double a, double b) {
146 return a * b;
147 });
148 }
149
150 // Method to clear values
151 void clearValues() {
152 wvalues.clear();
153 }
154
155 bool valid() const {
156 return !wvalues.empty();
157 }
158
159 // Equality comparison
160 bool operator==(const Fitness& other) const {
161 if (wvalues.size() != other.wvalues.size()) {
162 return false;
163 }
164 for (size_t i = 0; i < wvalues.size(); ++i)
165 if (fabs(wvalues[i] - other.wvalues[i]) > 1e-6)
166 return false;
167
168 return true;
169 }
170
171 // Inequality comparison
172 bool operator!=(const Fitness& other) const {
173 return !(*this == other);
174 }
175
176 // Less than comparison
177 bool operator<(const Fitness& other) const {
178 // because of the weights, every objective is a maximization problem
179 return !std::lexicographical_compare(wvalues.begin(), wvalues.end(),
180 other.wvalues.begin(), other.wvalues.end());
181 }
182
183 // Greater than comparison
184 bool operator>(const Fitness& other) const {
185 return other < *this;
186 }
187
188 // Less than or equal to comparison
189 bool operator<=(const Fitness& other) const {
190 return !(other < *this);
191 }
192
193 // Greater than or equal to comparison
194 bool operator>=(const Fitness& other) const {
195 return !(*this < other);
196 }
197
198 // String representation
199 std::string toString() const {
200 // TODO: unify to_string, toString, print, print_models, etc.
201 if (valid()) {
202 string s = "Fitness(";
203 for (auto& v : values)
204 s += to_string(v) + " ";
205 return s+")";
206 } else {
207 return "Fitness()";
208 }
209 }
210
211 // Representation for debugging
212 std::string repr() const {
213 if (valid()) {
214 string s = "Fitness(";
215 for (auto& v : values)
216 s += to_string(v) + " ";
217 return s+")";
218 } else {
219 return "Fitness()";
220 }
221 }
222
224 int dominates(const Fitness& b) const;
225};
226
227void to_json(json &j, const Fitness &f);
228void from_json(const json &j, Fitness& f);
229
230}
231#endif
string to_string(const T &value)
template function to convert objects to string for logging
Definition utils.h:369
static Rnd & r
Definition rnd.h:176
< nsga2 selection operator for getting the front
Definition bandit.cpp:4
void from_json(const json &j, Fitness &f)
Definition fitness.cpp:31
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:184
float prev_loss
Definition fitness.h:38
float crowding_dist
crowding distance on the Pareto front
Definition fitness.h:50
void clearValues()
Definition fitness.h:151
vector< float > weights
Definition fitness.h:54
vector< unsigned int > get_dominated() const
Definition fitness.h:60
float get_crowding_dist() const
Definition fitness.h:98
unsigned int complexity
Definition fitness.h:31
bool operator>=(const Fitness &other) const
Definition fitness.h:194
float loss
aggregate loss score
Definition fitness.h:28
float get_loss() const
Definition fitness.h:64
float get_prev_loss_v() const
Definition fitness.h:69
unsigned int get_prev_depth() const
Definition fitness.h:87
std::string repr() const
Definition fitness.h:212
float get_prev_loss() const
Definition fitness.h:65
unsigned int prev_complexity
Definition fitness.h:41
unsigned int get_prev_size() const
Definition fitness.h:73
vector< float > get_values() const
Definition fitness.h:121
unsigned int get_prev_complexity() const
Definition fitness.h:78
bool valid() const
Definition fitness.h:155
void set_linear_complexity(unsigned int new_lc)
Definition fitness.h:80
std::string toString() const
Definition fitness.h:199
void set_complexity(unsigned int new_c)
Definition fitness.h:75
float prev_loss_v
Definition fitness.h:39
float get_loss_v() const
Definition fitness.h:68
unsigned int linear_complexity
Definition fitness.h:32
size_t hash() const
Definition fitness.h:110
unsigned int dcounter
number of individuals this dominates
Definition fitness.h:47
bool operator==(const Fitness &other) const
Definition fitness.h:160
void set_loss_v(float f_v)
Definition fitness.h:67
void set_dominated(vector< unsigned int > &dom)
Definition fitness.h:59
bool operator!=(const Fitness &other) const
Definition fitness.h:172
bool operator<(const Fitness &other) const
Definition fitness.h:177
vector< float > values
Definition fitness.h:53
void set_depth(unsigned int new_d)
Definition fitness.h:85
unsigned int get_prev_linear_complexity() const
Definition fitness.h:83
unsigned int get_complexity() const
Definition fitness.h:77
size_t get_rank() const
Definition fitness.h:95
vector< float > wvalues
Definition fitness.h:57
unsigned int prev_linear_complexity
Definition fitness.h:42
void set_dcounter(unsigned int d)
Definition fitness.h:91
float loss_v
aggregate validation loss score
Definition fitness.h:29
unsigned int size
Definition fitness.h:33
vector< float > get_wvalues() const
Definition fitness.h:124
bool operator<=(const Fitness &other) const
Definition fitness.h:189
unsigned int rank
pareto front rank
Definition fitness.h:49
void set_values(vector< float > &v)
Definition fitness.h:129
unsigned int get_dcounter() const
Definition fitness.h:92
void set_rank(unsigned r)
Definition fitness.h:94
Fitness(const vector< float > &w={})
Definition fitness.h:101
unsigned int depth
Definition fitness.h:34
void set_size(unsigned int new_s)
Definition fitness.h:71
vector< unsigned int > dominated
individual indices this dominates
Definition fitness.h:48
unsigned int get_depth() const
Definition fitness.h:86
void set_crowding_dist(float cd)
Definition fitness.h:97
unsigned int prev_size
Definition fitness.h:43
unsigned int prev_depth
Definition fitness.h:44
void set_weights(vector< float > &w)
Definition fitness.h:115
int dominates(const Fitness &b) const
set obj vector given a string of objective names
Definition fitness.cpp:55
unsigned int get_linear_complexity() const
Definition fitness.h:82
unsigned int get_size() const
Definition fitness.h:72
vector< float > get_weights() const
Definition fitness.h:118
void set_loss(float f)
Definition fitness.h:63