Skip to content

Commit

Permalink
Starting to implement Variation class
Browse files Browse the repository at this point in the history
Old functions still exist to maintain python wrapper functional
  • Loading branch information
gAldeia committed Nov 7, 2023
1 parent 8a2d920 commit 392aa7c
Show file tree
Hide file tree
Showing 7 changed files with 833 additions and 612 deletions.
2 changes: 2 additions & 0 deletions src/bindings/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void bind_dataset(py::module &);
void bind_search_space(py::module &);
void bind_programs(py::module &);
void bind_params(py::module &);
void bind_cbrush(py::module &);

PYBIND11_MODULE(_brush, m) {

Expand All @@ -31,6 +32,7 @@ PYBIND11_MODULE(_brush, m) {
bind_params(m);
bind_dataset(m);
bind_search_space(m);
bind_cbrush(m);
py::module_ m2 = m.def_submodule("program", "Contains Program classes.");
bind_programs(m2);

Expand Down
17 changes: 8 additions & 9 deletions src/population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Brush{
namespace Pop{


template<Brush::ProgramType T>
template<ProgramType T>
void Population<T>::set_island_ranges()
{
// everytime we change popsize, this function must be called
Expand All @@ -29,7 +29,7 @@ void Population<T>::set_island_ranges()
};
}

template<Brush::ProgramType T>
template<ProgramType T>
Population<T>::Population(int p, int n_islands)
{
individuals.resize(p);
Expand All @@ -43,7 +43,7 @@ Population<T>::Population(int p, int n_islands)
offspring_ready = false;
}

template<Brush::ProgramType T>
template<ProgramType T>
void Population<T>::init(const SearchSpace& ss, const Parameters& params)
{
// TODO: load file (like feat)
Expand All @@ -55,7 +55,7 @@ void Population<T>::init(const SearchSpace& ss, const Parameters& params)
}

/// update individual vector size and island indexes
template<Brush::ProgramType T>
template<ProgramType T>
void Population<T>::prep_offspring_slots()
{
if (offspring_ready)
Expand Down Expand Up @@ -84,7 +84,7 @@ void Population<T>::prep_offspring_slots()
offspring_ready = true;
}

template<Brush::ProgramType T>
template<ProgramType T>
void Population<T>::update(vector<size_t> survivors)
{
if (!offspring_ready)
Expand All @@ -104,7 +104,7 @@ void Population<T>::update(vector<size_t> survivors)
offspring_ready = false;
}

template<Brush::ProgramType T>
template<ProgramType T>
string Population<T>::print_models(bool just_offspring, string sep)
{
// not printing the island each individual belongs to
Expand All @@ -129,10 +129,10 @@ string Population<T>::print_models(bool just_offspring, string sep)
return output;
}

template<Brush::ProgramType T>
template<ProgramType T>
vector<vector<size_t>> Population<T>::sorted_front(unsigned rank)
{
// this is used to update archive at the end of a generation. Supose islands without offspring
// this is used to update archive at the end of a generation. expect islands without offspring

/* Returns individuals on the Pareto front, sorted by increasign complexity. */
vector<vector<size_t>> pf_islands;
Expand All @@ -159,6 +159,5 @@ vector<vector<size_t>> Population<T>::sorted_front(unsigned rank)
return pf_islands;
}


} // Pop
} // Brush
10 changes: 7 additions & 3 deletions src/program/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ license: GNU/GPL v3
#include "../params.h"
#include "../util/utils.h"
#include "functions.h"
// #include "../variation.h"
// #include "weight_optimizer.h"


Expand Down Expand Up @@ -530,6 +531,7 @@ template<PT PType> struct Program
////////////////////////////////////////////////////////////////////////////////
// weight optimization
#include "optimizer/weight_optimizer.h"
#include "../variation.h"
namespace Brush{

template<ProgramType PType>
Expand All @@ -542,20 +544,20 @@ void Program<PType>::update_weights(const Dataset& d)
WO.update((*this), d);
};


////////////////////////////////////////////////////////////////////////////////
// mutation and crossover
#include "../variation.h"
template<ProgramType PType>
std::optional<Program<PType>> Program<PType>::mutate() const
{
return variation::mutate(*this, this->SSref.value().get());
return Brush::Var::mutate(*this, this->SSref.value().get());
};

/// swaps subtrees between this and other (note the pass by copy)
template<ProgramType PType>
std::optional<Program<PType>> Program<PType>::cross(Program<PType> other) const
{
return variation::cross(*this, other);
return Brush::Var::cross(*this, other);
};


Expand All @@ -577,4 +579,6 @@ void from_json(const json &j, Program<PType>& p)

}//namespace Brush



#endif
1 change: 1 addition & 0 deletions src/search_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license: GNU/GPL v3
#include "program/nodetype.h"
#include "program/tree_node.h"
// #include "program/program.h"
#include "util/error.h"
#include "util/utils.h"
#include "util/rnd.h"
#include "params.h"
Expand Down
5 changes: 2 additions & 3 deletions src/util/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ namespace Brush{ namespace Util {
///prints error to stderr and returns
void HandleErrorNoThrow(string err, const char *file, int line );

#define HANDLE_ERROR_THROW( err ) (Brush::Util::HandleErrorThrow( err, __FILE__, __LINE__ ))
#define HANDLE_WARNING( err ) (Brush::Util::HandleErrorNoThrow( err, __FILE__, __LINE__ ))

// TODO: have more errors
}}

#define HANDLE_ERROR_THROW( err ) (Util::HandleErrorThrow( err, __FILE__, __LINE__ ))
#define HANDLE_WARNING( err ) (Util::HandleErrorNoThrow( err, __FILE__, __LINE__ ))
#endif
Loading

0 comments on commit 392aa7c

Please sign in to comment.