Skip to content

Commit

Permalink
metapop ctor craziness
Browse files Browse the repository at this point in the history
  • Loading branch information
dknoester committed Jul 8, 2014
1 parent b1f6e54 commit 26fe67a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 48 deletions.
12 changes: 8 additions & 4 deletions libea/include/ea/ancestors.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ namespace ealib {

namespace ancestors {

//! Generates a default-constructed subpopulation.
//! Generates a default subpopulation.
struct default_subpopulation {
template <typename EA>
typename EA::individual_type operator()(EA& ea) {
return typename EA::individual_type();
template <typename MEA>
typename MEA::individual_type operator()(MEA& mea) {
typename MEA::individual_type sea;
sea.md() = mea.md();
sea.reset_rng(mea.rng().seed());
sea.initialize();
return sea;
}
};

Expand Down
23 changes: 16 additions & 7 deletions libea/include/ea/digital_evolution/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

namespace ealib {

/*! The position_type is contained by individuals to describe their position
LIBEA_MD_DECL(SPATIAL_X, "ea.environment.x", int);
LIBEA_MD_DECL(SPATIAL_Y, "ea.environment.y", int);

/*! The position_type is contained by individuals to describe their position
and orientation in the environment. It can be thought of as an index into
the environment.
Expand Down Expand Up @@ -406,15 +409,21 @@ namespace ealib {
void face_org(individual_type& ind1, individual_type& ind2) {
position_type& p1 = ind1.position();
position_type& p2 = ind2.position();

int x = p1.r[0] - p2.r[0];
int y = p1.r[1] - p2.r[1];
if(abs(x) > 1) { // crossing torus boundary
x = static_cast<int>(std::copysign(1.0, -x));
}

assert((x>=-1) && (x<=1));
assert((y>=-1) && (y<=1));
int y = p1.r[1] - p2.r[1];
if(abs(y) > 1) { // crossing torus boundary
y = static_cast<int>(std::copysign(1.0, -y));
}

p1.h[0] = -x; p2.h[0] = x;
p1.h[1] = -y; p2.h[1] = y;
// p1.h = (-x,-y)
p1.h[0] = -x; p1.h[1] = -y;
// p2.h == (x,y)
p2.h[0] = x; p2.h[1] = y;
}

protected:
Expand Down
36 changes: 9 additions & 27 deletions libea/include/ea/generational_models/generational.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,27 @@
#define _EA_GENERATIONAL_MODELS_GENERATIONAL_H_

#include <ea/metadata.h>
#include <ea/selection/proportionate.h>
#include <ea/selection/tournament.h>

namespace ealib {

LIBEA_MD_DECL(GENERATIONAL_REPLACEMENT_RATE_P, "ea.generational_model.generational.replacement_rate.p", double);

namespace generational_models {

/*! Generational model.
This generational model defines the traditional genetic algorithm
crossover/mutate/select loop~\cite{eiben2007introduction}.
Parents are selected from the current population and recombined to produce
offspring. Some of the resulting offspring are then mutated. Finally,
individuals from the joint population of parents and offspring are selected
for inclusion in the next generation.
/*! Generation-based generational model.
This generational model selects parents from the existing population,
recombines them to produce offspring, and then the offspring are mutated
and replace the parents.
*/
template <
typename ParentSelectionStrategy=selection::proportionate< >,
typename SurvivorSelectionStrategy=selection::tournament< > >
struct generational {
template <typename ParentSelectionStrategy=selection::tournament< > >
struct generational {
typedef ParentSelectionStrategy parent_selection_type;
typedef SurvivorSelectionStrategy survivor_selection_type;

//! Apply this generational model to the EA to produce a single new generation.
template <typename Population, typename EA>
void operator()(Population& population, EA& ea) {
// build the offspring:
Population offspring;
std::size_t n = static_cast<std::size_t>(get<GENERATIONAL_REPLACEMENT_RATE_P>(ea)*population.size());
std::size_t n = population.size();

recombine_n(population, offspring,
parent_selection_type(n,population,ea),
Expand All @@ -62,15 +51,8 @@ namespace ealib {
// mutate them:
mutate(offspring.begin(), offspring.end(), ea);

// add the offspring to the current population:
population.insert(population.end(), offspring.begin(), offspring.end());

// select individuals for survival:
Population next_gen;
select_n<survivor_selection_type>(population, next_gen, get<POPULATION_SIZE>(ea), ea);

// and swap it in for the current population:
std::swap(population, next_gen);
std::swap(population, offspring);
}
};

Expand Down
6 changes: 3 additions & 3 deletions libea/include/ea/generational_models/periodic_competition.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <ea/algorithm.h>
#include <ea/metadata.h>
#include <ea/generational_models/isolated_subpopulations.h>
#include <ea/generational_models/moran_process.h>
#include <ea/generational_models/generational.h>

namespace ealib {
LIBEA_MD_DECL(METAPOP_COMPETITION_PERIOD, "ea.metapopulation.competition_period", unsigned int);
Expand All @@ -42,8 +42,8 @@ namespace ealib {
of a subpopulation can change over time, then it must be nonstationary.
*/
template
< typename UpdateModel=isolated_subpopulations
, typename CompetitionModel=moran_process< >
< typename CompetitionModel=generational< >
, typename UpdateModel=isolated_subpopulations
> struct periodic_competition {
typedef UpdateModel update_model_type;
typedef CompetitionModel competition_model_type;
Expand Down
2 changes: 0 additions & 2 deletions libea/include/ea/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ namespace ealib {
LIBEA_MD_DECL(REPRESENTATION_MIN_SIZE, "ea.representation.min_size", int);
LIBEA_MD_DECL(REPRESENTATION_MAX_SIZE, "ea.representation.max_size", int);

LIBEA_MD_DECL(SPATIAL_X, "ea.environment.x", int);
LIBEA_MD_DECL(SPATIAL_Y, "ea.environment.y", int);

// ea.individual.*
LIBEA_MD_DECL(INDIVIDUAL_COUNT, "ea.individual.count", long);
Expand Down
19 changes: 14 additions & 5 deletions libea/include/ea/metapopulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,29 @@ namespace ealib {
}

//! Returns a new individual built from the given individual (subpopulation).
individual_ptr_type make_individual(const individual_type& r=individual_type()) {
individual_ptr_type make_individual() {
individual_ptr_type p(new individual_type());
// p->md() += md(); // WARNING: Meta-data comes from the meta-population.
p->reset_rng(_rng.seed());
// p->initialize();
return p;
}

//! Returns a new individual built from the given individual (subpopulation).
individual_ptr_type make_individual(const individual_type& r) {
individual_ptr_type p(new individual_type(r));
p->md() += md(); // WARNING: Meta-data comes from the meta-population.
// p->md() += md(); // WARNING: Meta-data comes from the meta-population.
p->reset_rng(_rng.seed());
p->initialize();
// p->initialize();
return p;
}

//! Returns a copy of an individual.
individual_ptr_type copy_individual(const individual_type& ind) {
individual_ptr_type p(new individual_type(ind));
p->md() += ind.md(); // WARNING: Meta-data comes from the individual.
// p->md() += ind.md(); // WARNING: Meta-data comes from the individual.
p->reset_rng(_rng.seed());
p->initialize();
// p->initialize();
return p;
}

Expand Down
2 changes: 2 additions & 0 deletions libea/include/ea/selection/tournament.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

#include <algorithm>
#include <iterator>
#include <ea/access.h>
#include <ea/algorithm.h>
#include <ea/comparators.h>
#include <ea/metadata.h>


namespace ealib {
LIBEA_MD_DECL(TOURNAMENT_SELECTION_N, "ea.selection.tournament.n", int);
LIBEA_MD_DECL(TOURNAMENT_SELECTION_K, "ea.selection.tournament.k", int);
Expand Down

0 comments on commit 26fe67a

Please sign in to comment.