16template<ProgramType T>
21 throw std::runtime_error(
"Individual vector has different number of individuals than pop_size. popsize is "+
to_string(params.
pop_size)+
", number of individuals is " +
to_string(
new_individuals.size()));
28 island_indexes.resize(num_islands);
33 individuals.resize(2*
p);
34 std::fill(individuals.begin(), individuals.end(),
nullptr);
36 for (
int i=0;
i<num_islands; ++
i)
39 size_t idx_end = std::floor((
i+1)*
p/num_islands);
43 island_indexes.at(
i).resize(
delta);
44 iota(island_indexes.at(
i).begin(), island_indexes.at(
i).end(),
idx_start);
47 island_indexes.at(
i).resize(
delta*2);
49 island_indexes.at(
i).begin() +
delta, island_indexes.at(
i).end(),
59template<ProgramType T>
68 island_indexes.resize(num_islands);
72 for (
int i=0;
i<num_islands; ++
i)
75 size_t idx_end = std::floor((
i+1)*
p/num_islands);
79 island_indexes.at(
i).resize(
delta);
80 iota(island_indexes.at(
i).begin(), island_indexes.at(
i).end(),
idx_start);
84 individuals.resize(2*
p);
86 for (
int i = 0;
i<
p; ++
i)
89 individuals.at(
i) = std::make_shared<Individual<T>>();
90 individuals.at(
i)->init(ss, params);
91 individuals.at(
i)->set_objectives(params.
objectives);
94 individuals.at(
p+
i) =
nullptr;
98template<ProgramType T>
105 out.open(
"population.json");
114template<ProgramType T>
125 json
j = json::parse(
line);
135template<ProgramType T>
158template<ProgramType T>
164 for (
int j=0;
j<num_islands; ++
j)
172 size_t idx_start = std::floor(
j*pop_size/num_islands);
173 size_t idx_end = std::floor((
j+1)*pop_size/num_islands);
178 &&
" migration ended up with a different popsize");
181 island_indexes.at(
j).clear();
182 island_indexes.at(
j).resize(
delta);
183 iota(island_indexes.at(
j).begin(), island_indexes.at(
j).end(),
idx_start);
187 &&
" update ended up with a different popsize");
189 this->individuals.resize(0);
196 individuals.push_back(
200 assert(individuals.size() == pop_size
201 &&
" number of new individuals is different from pop size");
203 for (
int i=0;
i< pop_size; ++
i)
206 individuals.push_back(
nullptr);
210template<ProgramType T>
216 for (
int j=0;
j<num_islands; ++
j)
220 for (
int k=0;
k<island_indexes.at(
j).size(); ++
k) {
230template<ProgramType T>
239 for (
int j=0;
j<num_islands; ++
j)
241 auto indices = island_indexes.at(
j);
247 if (individuals.at(
indices.at(
i))->fitness.rank == rank)
254 pf.resize(std::distance(
pf.begin(),
it));
261template<ProgramType T>
271 vector<size_t>
pf(0);
273 for (
int j=0;
j<num_islands; ++
j)
275 auto indices = island_indexes.at(
j);
278 if (individuals.at(
indices.at(
i))->fitness.rank == rank)
286 pf.resize(std::distance(
pf.begin(),
it));
291template<ProgramType T>
350 for (
int j=0;
j<num_islands; ++
j)
358 size_t idx_start = std::floor(
j*pop_size/num_islands);
359 size_t idx_end = std::floor((
j+1)*pop_size/num_islands);
364 &&
" new pop has the wrong number of new individuals");
367 island_indexes.at(
j).clear();
368 island_indexes.at(
j).resize(
delta);
369 iota(island_indexes.at(
j).begin(), island_indexes.at(
j).end(),
idx_start);
373 &&
" migration ended up with a different popsize");
375 this->individuals.resize(0);
382 individuals.push_back(
385 for (
int i=0;
i< pop_size; ++
i)
388 individuals.push_back(
nullptr);
void bind_engine(py::module &m, string name)
string get_model(string fmt="compact", bool pretty=false)
void add_offspring_indexes(int island)
update individual vector size, distributing the expressions in num_islands
void save(string filename)
vector< vector< size_t > > sorted_front(unsigned rank=1)
return complexity-sorted Pareto front indices for each island
void init(SearchSpace &ss, const Parameters ¶ms)
initialize population of programs with a starting model and/or from file
string print_models(string sep="\n")
return population equations.
vector< size_t > hall_of_fame(unsigned rank=1)
void load(string filename)
void update(vector< vector< size_t > > survivors)
reduce programs to the indices in survivors. Not thread safe,as it removes elements
string log(string m, int v, string sep="\n") const
Prints a log message with verbosity control.
Iter select_randomly(Iter start, Iter end)
#define HANDLE_ERROR_THROW(err)
void to_json(json &j, const Individual< T > &p)
void from_json(const json &j, Individual< T > &p)
string to_string(const T &value)
template function to convert objects to string for logging
< nsga2 selection operator for getting the front
vector< string > objectives
check for same fitness and complexity to filter uniqueness.
Sort each island in increasing complexity. This is not thread safe. I should set complexities of the ...
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...