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>
167 vector<Individual<T>> new_pop;
169 for (
int k=0; k<survivors.at(0).
size(); ++k){
171 new_pop.push_back( *
individuals.at(survivors.at(0).at(k)) );
181 auto delta = idx_end - idx_start;
195 &&
" update ended up with a different popsize");
198 for (
auto ind : new_pop)
209 &&
" number of new individuals is different from pop size");
218template<ProgramType T>
229 output +=
"island " +
to_string(j) +
" ";
238template<ProgramType T>
244 vector<vector<size_t>> pf_islands;
252 for (
int i=0; i<indices.size(); ++i)
255 if (
individuals.at(indices.at(i))->fitness.rank == rank)
266 pf.resize(std::distance(pf.begin(),it));
267 pf_islands.at(j) = pf;
273template<ProgramType T>
285 vector<size_t> merged_islands(0);
290 for (
int i=0; i<indices.size(); ++i)
292 merged_islands.push_back(indices.at(i));
301 for (
int i = 0; i < merged_islands.size(); ++i) {
306 for (
int j = 0; j < merged_islands.size(); ++j) {
317 hof.push_back(merged_islands[i]);
328 hof.resize(std::distance(hof.begin(),it));
333template<ProgramType T>
342 vector<vector<size_t>> new_island_indexes;
347 new_island_indexes.at(island).resize(0);
350 for (
unsigned int i=0; i<indices.size(); ++i)
354 size_t migrating_idx;
357 iota(other_islands.begin(), other_islands.end(), 0);
360 auto it = other_islands.begin();
361 std::advance(it, island);
362 for (;it != other_islands.end(); ++it) {
367 int other_island = *
r.select_randomly(
368 other_islands.begin(),
369 other_islands.end());
371 migrating_idx = *
r.select_randomly(
375 new_island_indexes.at(island).push_back(migrating_idx);
379 new_island_indexes.at(island).push_back(indices.at(i));
388 vector<Individual<T>> new_pop;
392 for (
int k=0; k<new_island_indexes.at(j).
size(); ++k){
401 auto delta = idx_end - idx_start;
403 assert(
delta == new_island_indexes.at(j).size()
404 &&
" new pop has the wrong number of new individuals");
413 &&
" migration ended up with a different popsize");
416 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...