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