Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
nsga2.h
Go to the documentation of this file.
1#ifndef NSGA2_H
2#define NSGA2_H
3
5
6namespace Brush {
7namespace Sel {
8
9using namespace Brush;
10using namespace Pop;
11using namespace Sel;
12
13template<ProgramType T>
14class NSGA2 : public SelectionOperator<T>
15{
16public:
17 // should operate only on a given island index
20 // if any of the islands have overlapping indexes, parallel access and modification should be ok (because i dont increase or decrease pop size, not change island ranges inside selection)
21
22 NSGA2(bool surv=false);
23 ~NSGA2(){};
24
26 vector<size_t> select(Population<T>& pop, int island,
27 const Parameters& p);
28
30 vector<size_t> survive(Population<T>& pop, int island,
31 const Parameters& p);
32
33 //< Fast non-dominated sorting
34 vector<vector<int>> fast_nds(Population<T>&, vector<size_t>&);
35
36 // front cannot be an attribute because selection will be executed in different threads for different islands (this is a modificationf rom original FEAT code that I got inspiration)
37
38 //< crowding distance of a front i
39 void crowding_distance(Population<T>&, vector<vector<int>>&, int);
40
41 private:
43 struct sort_n
44 {
46
48
49 bool operator() (int i, int j) {
50 // TODO: Improve operator[], and decrease use of pop.individuals.at(). Also, decrease number of auto declarations
51 auto ind1 = pop.individuals[i];
52 auto ind2 = pop.individuals[j];
53
54 if (ind1->fitness.get_rank() < ind2->fitness.get_rank())
55 return true;
56 else if (ind1->fitness.get_rank() == ind2->fitness.get_rank() &&
57 ind1->fitness.crowding_dist > ind2->fitness.crowding_dist)
58 return true;
59 return false;
60 };
61 };
62
65 {
67 int m;
68
71
72 // because of the weighted values, every objective is a maximization problem
73 bool operator() (int i, int j) {
74 return pop[i].fitness.get_wvalues()[m] > pop[j].fitness.get_wvalues()[m]; };
75 };
76
77 size_t tournament(Population<T>& pop, size_t i, size_t j) const;
78};
79
80} // selection
81} // Brush
82#endif
void bind_engine(py::module &m, string name)
void crowding_distance(Population< T > &, vector< vector< int > > &, int)
Definition nsga2.cpp:201
size_t tournament(Population< T > &pop, size_t i, size_t j) const
Definition nsga2.cpp:18
vector< size_t > survive(Population< T > &pop, int island, const Parameters &p)
survival according to the survival scheme of NSGA-II
Definition nsga2.cpp:69
NSGA2(bool surv=false)
Definition nsga2.cpp:11
vector< vector< int > > fast_nds(Population< T > &, vector< size_t > &)
Definition nsga2.cpp:116
vector< size_t > select(Population< T > &pop, int island, const Parameters &p)
selection according to the survival scheme of NSGA-II
Definition nsga2.cpp:39
The SelectionOperator class represents a base class for selection operators in a genetic algorithm.
< nsga2 selection operator for getting the front
Definition data.cpp:12
sort based on objective m
Definition nsga2.h:65
bool operator()(int i, int j)
Definition nsga2.h:73
int m
objective index
Definition nsga2.h:67
const Population< T > & pop
population address
Definition nsga2.h:66
comparator_obj(const Population< T > &population, int index)
Definition nsga2.h:69
sort based on rank, breaking ties with crowding distance
Definition nsga2.h:44
bool operator()(int i, int j)
Definition nsga2.h:49
const Population< T > & pop
population address
Definition nsga2.h:45
sort_n(const Population< T > &population)
Definition nsga2.h:47