2#include "SelRandomOp.h"
11 uint* freq =
new uint(0);
12 state->getRegistry()->registerEntry(
"migration.freq", (voidP) freq, ECF::UINT,
13 "individuals are exchanged each 'freq' generations (default: none)");
15 uint* num =
new uint(1);
16 state->getRegistry()->registerEntry(
"migration.number", (voidP) num, ECF::UINT,
17 "number of individuals to be sent to another deme (default: 1)");
25 voidP sptr = state->getRegistry()->getEntry(
"migration.freq");
26 migFrequency_ = *((uint*) sptr.get());
28 if(migFrequency_ == 0 && state->getPopulation()->getNoDemes() > 1) {
29 ECF_LOG_ERROR(state,
"Warning: migration operator is not configured (migration will not occur between demes).");
32 if(migFrequency_ == 0)
35 sptr = state->getRegistry()->getEntry(
"migration.number");
36 nEmigrants_ = *((uint*) sptr.get());
38 if(nEmigrants_ >= state->getPopulation()->getLocalDeme()->getSize()) {
39 ECF_LOG_ERROR(state,
"Error: number of emmigrants greater than deme size!");
43 if(state->getPopulation()->getNoDemes() == 1) {
44 ECF_LOG_ERROR(state,
"Warning: migration operator not applicable with a single deme population!");
48 selOp_.push_back(
static_cast<SelectionOperatorP
> (
new SelBestOp));
49 selOp_.push_back(
static_cast<SelectionOperatorP
> (
new SelRandomOp));
51 selOp_[BEST]->initialize(state);
52 selOp_[RANDOM]->initialize(state);
62 if(migFrequency_ == 0 || state->getGenerationNo() % migFrequency_ != 0)
65 std::vector< std::vector<IndividualP> > outPool_, inPool_;
66 outPool_.resize(state->getPopulation()->getNoDemes());
67 inPool_.resize(state->getPopulation()->getNoDemes());
73 for(uint iDeme = 0; iDeme < state->getPopulation()->getNoDemes(); iDeme++) {
75 DemeP myDeme = state->getPopulation()->at(iDeme);
76 std::vector<IndividualP> emigrants;
77 IndividualP myBest = selOp_[BEST]->select(*myDeme);
78 emigrants.push_back(myBest);
79 for(uint i = 1; i < nEmigrants_; i++)
80 emigrants.push_back(selOp_[RANDOM]->select(*myDeme));
82 outPool_[iDeme] = emigrants;
86 for(uint i = 0; i < outPool_.size(); i++)
87 for(uint j = 0; j < outPool_[i].size(); j++)
88 outPool_[i][j] = (IndividualP) outPool_[i][j]->copy();
93 for(uint iDeme = 0; iDeme < state->getPopulation()->getNoDemes(); iDeme++) {
95 uint destDeme = (iDeme + 1) % state->getPopulation()->getNoDemes();
96 inPool_[destDeme] = outPool_[iDeme];
102 for(uint iDeme = 0; iDeme < state->getPopulation()->getNoDemes(); iDeme++) {
104 DemeP myDeme = state->getPopulation()->at(iDeme);
105 IndividualP myBest = selOp_[BEST]->select(*myDeme);
106 std::vector<IndividualP> immigrants = inPool_[iDeme];
107 ECF_LOG(state, 4,
"Received inds fitness: " + dbl2str(immigrants[0]->fitness->getValue()));
109 for(
int i = (
int) immigrants.size() - 1; i >= 0; i--) {
112 victim = selOp_[RANDOM]->select(*myDeme);
113 while(victim == myBest);
115 state->getAlgorithm()->replaceWith(victim, immigrants[i]);
127 if(migFrequency_ == 0 || state->getGenerationNo() % migFrequency_ != 0)
130 CommunicatorP comm = state->getCommunicator();
136 DemeP myDeme = state->getPopulation()->at(0);
137 std::vector<IndividualP> emigrants;
138 IndividualP myBest = selOp_[BEST]->select(*myDeme);
139 emigrants.push_back(myBest);
140 for(uint i = 1; i < nEmigrants_; i++)
141 emigrants.push_back(selOp_[RANDOM]->select(*myDeme));
146 uint destDeme = (state->getPopulation()->getLocalDemeId() + 1) % state->getPopulation()->getNoDemes();
147 uint destProcess = comm->getDemeMaster(destDeme);
148 comm->sendIndividualsGlobal(emigrants, destProcess);
153 std::vector<IndividualP> immigrants = comm->recvIndividualsGlobal();
154 ECF_LOG(state, 4,
"Received inds fitness: " + dbl2str(immigrants[0]->fitness->getValue()));
156 for(
int i = (
int) immigrants.size() - 1; i >= 0; i--) {
159 victim = selOp_[RANDOM]->select(*myDeme);
160 while(victim == myBest);
162 state->getAlgorithm()->replaceWith(victim, immigrants[i]);
void registerParameters(StateP)
Register parameters with the Registry. Called before Operator::initialize.
bool initialize(StateP)
Perform initialization. Called before Operator::operate. By default, if the return value is false,...
Best individual selection operator.
Random individual selection operator.