Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
bind_selection.h
Go to the documentation of this file.
1#include "module.h"
2
3// TODO: figure out why im having symbol errors (if i dont include the cpp here as well)
12
13#include "../pop/population.cpp"
14#include "../pop/population.h"
15
16namespace py = pybind11;
17namespace nl = nlohmann;
18namespace br = Brush;
19
20template<br::ProgramType PT>
21void bind_selection(py::module& m, string name)
22{
23 using Class = br::Sel::Selection<PT>;
24
25 // TODO: make selection a non-templated class
26 py::class_<Class> sel(m, name.data() );
27
28 sel.def(py::init<>())
29 .def(py::init(
30 [](string type, bool survival){ Class s(type, survival); return s; })
31 )
32 .def("select", [](Class &self,
33 std::vector<br::Pop::Individual<PT>>& individuals,
34 const Parameters& params) {
35
36 // auto sel = Class("nsga2", false);
37 auto pop = br::Pop::Population<PT>();
38
39 pop.init(individuals, params);
40
41 vector<br::Pop::Individual<PT>> pool;
42 pool.resize(0);
43
44 for (int island = 0; island < params.num_islands; ++island)
45 {
46 vector<size_t> selected = self.select(pop, island, params);
47
48 // std::cout << "selecting in island " << island << std::endl;
49
50 for (size_t idx : selected) {
51 pool.push_back(pop[idx]);
52 }
53 }
54
55 return pool;
56 })
57 .def("survive", [](Class &self,
58 std::vector<br::Pop::Individual<PT>>& individuals,
59 const Parameters& params) {
60
61 // auto sel = Class("nsga2", false);
62 auto pop = br::Pop::Population<PT>();
63
64 pop.init(individuals, params);
65
66 vector<br::Pop::Individual<PT>> pool;
67 pool.resize(0);
68
69 for (int island = 0; island < params.num_islands; ++island)
70 {
71 vector<size_t> selected = self.survive(pop, island, params);
72
73 for (size_t idx : selected) {
74 pool.push_back(pop[idx]);
75 }
76 }
77
78 return pool;
79 })
80 .def("migrate", [](Class &self,
81 std::vector<br::Pop::Individual<PT>>& individuals,
82 const Parameters& params) {
83
84 auto pop = br::Pop::Population<PT>();
85
86 pop.init(individuals, params);
87 pop.migrate(); // this will modify island indexes inplace
88
89 vector<br::Pop::Individual<PT>> pool;
90 pool.resize(0);
91
92 for (int island = 0; island < params.num_islands; ++island)
93 {
94 vector<size_t> selected = pop.get_island_indexes(island);
95
96 for (size_t idx : selected) {
97 pool.push_back(pop[idx]);
98 }
99 }
100 return pool;
101 })
102 ;
103}
void bind_engine(py::module &m, string name)
void bind_selection(py::module &m, string name)
< nsga2 selection operator for getting the front
Definition data.cpp:12