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 {"prev_loss", f.prev_loss},
19 {"prev_loss_v", f.prev_loss_v},
20 {"prev_complexity", f.prev_complexity},
21 {"prev_linear_complexity", f.prev_linear_complexity},
22 {"prev_size", f.prev_size},
23 {"prev_depth", f.prev_depth},
24 {"dcounter", f.dcounter},
25 {"dominated", f.dominated},
26 {"rank", f.rank},
27 {"crowding_dist", f.crowding_dist}
28 };
29}
30
31void from_json(const json &j, Fitness& f)
32{
33 j.at("values").get_to( f.values );
34 j.at("weights").get_to( f.weights );
35 j.at("wvalues").get_to( f.wvalues );
36 j.at("loss").get_to( f.loss );
37 j.at("loss_v").get_to( f.loss_v );
38 j.at("complexity").get_to( f.complexity );
39 j.at("linear_complexity").get_to( f.linear_complexity );
40 j.at("size").get_to( f.size );
41 j.at("depth").get_to( f.depth );
42 j.at("prev_loss").get_to( f.prev_loss );
43 j.at("prev_loss_v").get_to( f.prev_loss_v );
44 j.at("prev_complexity").get_to( f.prev_complexity );
45 j.at("prev_linear_complexity").get_to( f.prev_linear_complexity );
46 j.at("prev_size").get_to( f.prev_size );
47 j.at("prev_depth").get_to( f.prev_depth );
48 j.at("dcounter").get_to( f.dcounter );
49 j.at("dominated").get_to( f.dominated );
50 j.at("rank").get_to( f.rank );
51 j.at("crowding_dist").get_to( f.crowding_dist );
52}
53
54
55int Fitness::dominates(const Fitness& b) const
56{
57 int flag1 = 0, // to check if this has a better objective
58 flag2 = 0; // to check if b has a better objective
59
60 // TODO: replace comparison of individual values by using the overloaded operators (here and in nsga2)
61 for (int i=0; i<get_wvalues().size(); ++i) {
62 if (get_wvalues().at(i) > b.get_wvalues().at(i)
63 || std::isnan(b.get_wvalues().at(i))
64 )
65 flag1 = 1;
66 if (get_wvalues().at(i) < b.get_wvalues().at(i)
67 || std::isnan(get_wvalues().at(i))
68 )
69 flag2 = 1;
70 }
71
72 // the proper way of comparing weighted values is considering everything as a MAXIMIIZATIION problem
73 // (this is like deap does, and our fitness is inspired by them)
74 if (flag1==1 && flag2==0)
75 // there is at least one smaller objective for this and none
76 // for b
77 return 1;
78 else if (flag1==0 && flag2==1)
79 // there is at least one smaller objective for b and none
80 // for this
81 return -1;
82 else
83 // no smaller objective or both have one smaller
84 return 0;
85}
86
87} // Brush
< 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
float prev_loss
Definition fitness.h:38
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 prev_complexity
Definition fitness.h:41
float prev_loss_v
Definition fitness.h:39
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
unsigned int prev_linear_complexity
Definition fitness.h:42
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
unsigned int rank
pareto front rank
Definition fitness.h:49
Fitness(const vector< float > &w={})
Definition fitness.h:101
unsigned int depth
Definition fitness.h:34
vector< unsigned int > dominated
individual indices this dominates
Definition fitness.h:48
unsigned int prev_size
Definition fitness.h:43
unsigned int prev_depth
Definition fitness.h:44
int dominates(const Fitness &b) const
set obj vector given a string of objective names
Definition fitness.cpp:55