17template<ProgramType T>
20 if (new_individuals.size() != params.
pop_size
21 && new_individuals.size() != 2*params.
pop_size ) {
22 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()));
43 auto delta = idx_end - idx_start;
48 if (new_individuals.size() == 2*params.
pop_size) {
56 for (
int j=0; j< new_individuals.size(); j++) {
57 individuals.at(j) = std::make_shared<Individual<T>>(new_individuals.at(j));
61template<ProgramType T>
80 auto delta = idx_end - idx_start;
89 for (
int i = 0; i< p; ++i)
92 individuals.at(i) = std::make_shared<Individual<T>>();
100template<ProgramType T>
104 if (!filename.empty())
107 out.open(
"population.json");
113 logger.log(
"Saved population to file " + filename, 1);
116template<ProgramType T>
119 std::ifstream indata;
120 indata.open(filename);
127 json j = json::parse(line);
130 logger.log(
"Loaded population from " + filename +
" of size = "
137template<ProgramType T>
143 size_t idx_start = std::floor(island*p/
num_islands);
144 size_t idx_end = std::floor((island+1)*p/
num_islands);
146 auto delta = idx_end - idx_start;
160template<ProgramType T>
165 vector<Individual<T>> new_pop;
167 for (
int k=0; k<survivors.at(0).
size(); ++k){
168 new_pop.push_back( *
individuals.at(survivors.at(0).at(k)) );
171 assert(new_pop.size() ==
individuals.size() &&
" migration ended up with a different popsize");
179 auto delta = idx_end - idx_start;
188 &&
" update ended up with a different popsize");
191 for (
auto ind : new_pop)
202 &&
" number of new individuals is different from pop size");
211template<ProgramType T>
222 output +=
"island " +
to_string(j) +
" ";
231template<ProgramType T>
237 vector<vector<size_t>> pf_islands;
245 for (
int i=0; i<indices.size(); ++i)
248 if (
individuals.at(indices.at(i))->fitness.rank == rank)
259 pf.resize(std::distance(pf.begin(),it));
260 pf_islands.at(j) = pf;
266template<ProgramType T>
278 vector<size_t> merged_islands(0);
283 for (
int i=0; i<indices.size(); ++i)
285 merged_islands.push_back(indices.at(i));
294 for (
int i = 0; i < merged_islands.size(); ++i) {
299 for (
int j = 0; j < merged_islands.size(); ++j) {
310 hof.push_back(merged_islands[i]);
321 hof.resize(std::distance(hof.begin(),it));
326template<ProgramType T>
335 vector<vector<size_t>> new_island_indexes;
340 new_island_indexes.at(island).resize(0);
343 for (
unsigned int i=0; i<indices.size(); ++i)
347 size_t migrating_idx;
350 iota(other_islands.begin(), other_islands.end(), 0);
353 auto it = other_islands.begin();
354 std::advance(it, island);
355 for (;it != other_islands.end(); ++it) {
360 int other_island = *
r.select_randomly(
361 other_islands.begin(),
362 other_islands.end());
364 migrating_idx = *
r.select_randomly(
368 new_island_indexes.at(island).push_back(migrating_idx);
372 new_island_indexes.at(island).push_back(indices.at(i));
381 vector<Individual<T>> new_pop;
385 for (
int k=0; k<new_island_indexes.at(j).
size(); ++k){
394 auto delta = idx_end - idx_start;
396 assert(
delta == new_island_indexes.at(j).size()
397 &&
" new pop has the wrong number of new individuals");
406 &&
" migration ended up with a different popsize");
409 for (
auto ind : new_pop)
string get_model(string fmt="compact", bool pretty=false)
Fitness fitness
aggregate fitness score
bool linear_complexity
Indicates if the user set linear_complexity instead of recursive complexity.
int size()
returns population size (the effective size of the individuals)
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
vector< std::shared_ptr< Individual< T > > > individuals
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< vector< size_t > > island_indexes
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
#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
bool in(const V &v, const T &i)
check if element is in vector.
< nsga2 selection operator for getting the front
std::string toString() const
int dominates(const Fitness &b) const
set obj vector given a string of objective names
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...