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