diff --git a/brush/pybrush.py b/brush/pybrush.py index cac154c6..d0665a1c 100644 --- a/brush/pybrush.py +++ b/brush/pybrush.py @@ -1,11 +1,11 @@ -from _brush import CBrush # TODO: stop calling cbrush +from _brush import CBrush, Dataset, SearchSpace # TODO: stop calling cbrush, rename it from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin, TransformerMixin # TODO? LOGGER AND ARCHIVE # TODO: GET DOCUMENTATION BACK -class PybrushEstimator(BaseEstimator): +class BrushEstimator(BaseEstimator): def __init__(self): self.cbrush_ = CBrush() @@ -28,12 +28,12 @@ def score(self,X,y,Z=None): pass -class PybrushRegressor(PybrushEstimator): +class BrushRegressor(BrushEstimator): def __init__(self,**kwargs): pass -class PybrushClassifier(PybrushEstimator): +class BrushClassifier(BrushEstimator): def __init__(self,**kwargs): pass diff --git a/tests/cpp/test_evolution_step.cpp b/tests/cpp/test_evaluation.cpp similarity index 100% rename from tests/cpp/test_evolution_step.cpp rename to tests/cpp/test_evaluation.cpp diff --git a/tests/cpp/test_params.cpp b/tests/cpp/test_params.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/cpp/test_population.cpp b/tests/cpp/test_population.cpp index 68a407ee..0f7f53d5 100644 --- a/tests/cpp/test_population.cpp +++ b/tests/cpp/test_population.cpp @@ -1,18 +1,143 @@ #include "testsHeader.h" +#include "../../src/individual.cpp" +#include "../../src/population.cpp" // TODO: figure out if thats ok to include cpps instead of headers +#include "../../src/eval/evaluation.cpp" +#include "../../src/selection/nsga2.cpp" +#include "../../src/selection/selection.cpp" + +using namespace Brush::Pop; +using namespace Brush::Sel; +using namespace Brush::Eval; + TEST(Population, PopulationTests) -{ +{ + // works with even and uneven pop sizes. (TODO: PARAMETERIZE this test to do it with even and uneven, and single individual pop) + + MatrixXf X(4,2); + VectorXf y(4); + + X << 0,1, + 0.47942554,0.87758256, + 0.84147098, 0.54030231, + 0.99749499, 0.0707372; + y << 3.0, 3.59159876, 3.30384889, 2.20720158; + + fmt::print("Initializing all classes;\n"); + Dataset data(X,y); + + SearchSpace SS; + SS.init(data); - Population pop; + Parameters params; + Population pop = Population(params.pop_size, params.num_islands); + + // aux classes (they are not tested in-depth in this file) + Evaluation evaluator = Evaluation(params.scorer_); + Selection selector = Selection(params.sel, false); + Selection survivor = Selection(params.surv, true); + Variation variator = Variation(params, SS); + + selector.set_operator(); + survivor.set_operator(); + + // size, all individuals were initialized + ASSERT_TRUE(pop.size() == pop.individuals.size() + && pop.size() == params.pop_size); + + fmt::print("Initializing individuals in the population:\n"); + pop.init(SS, params); + for (auto& ind : pop.individuals) + { + fmt::print("Individual: {}\n", ind.program.get_model("compact", true)); + } - // size, - // island sizes growns and comes back to the same, - // update and prep offspring slots. - // no overlap in island indexes. - // works with even and uneven pop sizes. - // initialize population works? - // migrate? // print models + fmt::print("Printing from population method:\n{}\n", pop.print_models()); + + // no overlap in island indexes + + fmt::print("Testing island ranges\n"); + for (std::size_t i = 0; i < pop.island_ranges.size() - 1; ++i) { + int last = std::get<1>(pop.island_ranges.at(i)); + int next_first = std::get<0>(pop.island_ranges.at(i+1)); + + //(last index from one island is EQUAL than first) (no gaps between island) + // (this assumes that we will never iterate to the last index in for loops. TODO: make sure we dont) + ASSERT_TRUE(last == next_first); + + // difference between island sizes is at most 1 + auto delta = last - std::get<0>(pop.island_ranges.at(i)); + auto next_delta = std::get<1>(pop.island_ranges.at(i+1)) - next_first; + ASSERT_TRUE(delta <= next_delta+1 && next_delta <= delta+1); + } + + // island sizes increases and comes back to the same values after update + fmt::print("Performing all steps of an evolution\n"); + auto original_islands = pop.island_ranges; + for (int i=0; i<10; ++i) // update and prep offspring slots works properly + { // wax on wax off + + fmt::print("Evaluating population\n"); + vector> island_parents; + island_parents.resize(pop.n_islands); + for (int j=0; j(pop.get_island_range(j)), + std::get<1>(pop.get_island_range(j)) ); + + fmt::print("Fitness\n"); + // we can calculate the fitness for each island + evaluator.fitness(pop, pop.get_island_range(j), data, params, true, false); + + fmt::print("Selection\n"); + // just so we can call the update method + vector parents = selector.select(pop, pop.get_island_range(j), params, data); + + ASSERT_TRUE(parents.size() > 0); + fmt::print("Updating parents\n"); + island_parents.at(j) = parents; + } + fmt::print("Preparing offspring\n"); + pop.prep_offspring_slots(); + ASSERT_TRUE(pop.size() == params.pop_size*2); + + fmt::print("Preparing survivors\n"); + vector survivors(params.pop_size); + for (int j=0; j