Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
fitness.cpp
Go to the documentation of this file.
1#include "fitness.h"
2
3namespace Brush
4{
5
6void to_json(json &j, const Fitness &f)
7{
8 j = json{
9 {"values", f.values},
10 {"weights", f.weights},
11 {"wvalues", f.wvalues},
12 {"loss", f.loss},
13 {"loss_v", f.loss_v},
14 {"complexity", f.complexity},
15 {"linear_complexity", f.linear_complexity},
16 {"size", f.size},
17 {"depth", f.depth},
18 {"dcounter", f.dcounter},
19 {"dominated", f.dominated},
20 {"rank", f.rank},
21 {"crowding_dist", f.crowding_dist}
22 };
23}
24
25void from_json(const json &j, Fitness& f)
26{
27 j.at("values").get_to( f.values );
28 j.at("weights").get_to( f.weights );
29 j.at("wvalues").get_to( f.wvalues );
30 j.at("loss").get_to( f.loss );
31 j.at("loss_v").get_to( f.loss_v );
32 j.at("complexity").get_to( f.complexity );
33 j.at("linear_complexity").get_to( f.linear_complexity );
34 j.at("size").get_to( f.size );
35 j.at("depth").get_to( f.depth );
36 j.at("dcounter").get_to( f.dcounter );
37 j.at("dominated").get_to( f.dominated );
38 j.at("rank").get_to( f.rank );
39 j.at("crowding_dist").get_to( f.crowding_dist );
40}
41
42
43int Fitness::dominates(const Fitness& b) const
44{
45 int flag1 = 0, // to check if this has a better objective
46 flag2 = 0; // to check if b has a better objective
47
48 // TODO: replace comparison of individual values by using the overloaded operators (here and in nsga2)
49 for (int i=0; i<get_wvalues().size(); ++i) {
50 if (get_wvalues().at(i) > b.get_wvalues().at(i)
51 || std::isnan(b.get_wvalues().at(i))
52 )
53 flag1 = 1;
54 if (get_wvalues().at(i) < b.get_wvalues().at(i)
55 || std::isnan(get_wvalues().at(i))
56 )
57 flag2 = 1;
58 }
59
60 // the proper way of comparing weighted values is considering everything as a MAXIMIIZATIION problem
61 // (this is like deap does, and our fitness is inspired by them)
62 if (flag1==1 && flag2==0)
63 // there is at least one smaller objective for this and none
64 // for b
65 return 1;
66 else if (flag1==0 && flag2==1)
67 // there is at least one smaller objective for b and none
68 // for this
69 return -1;
70 else
71 // no smaller objective or both have one smaller
72 return 0;
73}
74
75} // Brush
< 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
float crowding_dist
crowding distance on the Pareto front
Definition fitness.h:50
vector< float > weights
Definition fitness.h:54
unsigned int complexity
Definition fitness.h:31
float loss
aggregate loss score
Definition fitness.h:28
unsigned int linear_complexity
Definition fitness.h:32
unsigned int dcounter
number of individuals this dominates
Definition fitness.h:47
vector< float > values
Definition fitness.h:53
vector< float > wvalues
Definition fitness.h:57
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
unsigned int rank
pareto front rank
Definition fitness.h:49
Fitness(const vector< float > &w={})
Definition fitness.h:106
unsigned int depth
Definition fitness.h:34
vector< unsigned int > dominated
individual indices this dominates
Definition fitness.h:48
int dominates(const Fitness &b) const
set obj vector given a string of objective names
Definition fitness.cpp:43