本文整理汇总了C++中StateP类的典型用法代码示例。如果您正苦于以下问题:C++ StateP类的具体用法?C++ StateP怎么用?C++ StateP使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StateP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: onlookerBeesPhase
bool ArtificialBeeColony::onlookerBeesPhase(StateP state, DemeP deme)
{
// jednostavni odabir, uz selFitPropOp
/* for( uint i = 0; i < deme->getSize(); i++ ) { // for each food source
// choose a food source depending on its fitness value (better individuals are more likely to be chosen)
IndividualP food = selFitOp->select(*deme);
createNewFoodSource(food, state, deme);
}
*/
// uz vjerojatnosti, jedinka po jedinka
calculateProbabilities(state, deme);
int demeSize = deme->getSize();
int i = state->getRandomizer()->getRandomInteger(demeSize);
int n = 0;
while( n < demeSize) {
int fact = i++ % demeSize;
IndividualP food = deme->at(fact);
if (state->getRandomizer()->getRandomDouble() < probability_[fact]){
n++;
createNewFoodSource(food, state, deme);
}
}
return true;
}
示例2: initialize
bool initialize(StateP state)
{
voidP lBound = state->getGenotypes()[0]->getParameterValue(state, "lbound");
lbound = *((double*) lBound.get());
voidP uBound = state->getGenotypes()[0]->getParameterValue(state, "ubound");
ubound = *((double*) uBound.get());
voidP dimension_ = state->getGenotypes()[0]->getParameterValue(state, "dimension");
dimension = *((uint*) dimension_.get());
voidP dup_ = getParameterValue(state, "dup");
dup = *((uint*) dup_.get());
if( *((int*) dup_.get()) <= 0 ) {
ECF_LOG(state, 1, "Error: opt-IA requires parameter 'dup' to be an integer greater than 0");
throw "";}
voidP c_ = getParameterValue(state, "c");
c = *((double*) c_.get());
if( c <= 0 ) {
ECF_LOG(state, 1, "Error: opt-IA requires parameter 'c' to be a double greater than 0");
throw "";}
voidP tauB_ = getParameterValue(state, "tauB");
tauB = *((double*) tauB_.get());
if( tauB < 0 ) {
ECF_LOG(state, 1, "Error: opt-IA requires parameter 'tauB' to be a nonnegative double value");
throw "";}
voidP elitism_ = getParameterValue(state, "elitism");
elitism = *((string*) elitism_.get());
if( elitism != "true" && elitism != "false" ) {
ECF_LOG(state, 1, "Error: opt-IA requires parameter 'elitism' to be either 'true' or 'false'");
throw "";}
// algorithm accepts a single FloatingPoint Genotype
FloatingPointP flp (new FloatingPoint::FloatingPoint);
if(state->getGenotypes()[0]->getName() != flp->getName()) {
ECF_LOG_ERROR(state, "Error: opt-IA algorithm accepts only a FloatingPoint genotype!");
throw ("");}
// algorithm adds another FloatingPoint genotype (age)
FloatingPointP flpoint[2];
for(uint iGen = 1; iGen < 2; iGen++) {
flpoint[iGen] = (FloatingPointP) new FloatingPoint::FloatingPoint;
state->setGenotype(flpoint[iGen]);
flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(1));
// initial value of age parameter should be (or as close as possible to) 0
flpoint[iGen]->setParameterValue(state, "lbound", (voidP) new double(0));
flpoint[iGen]->setParameterValue(state, "ubound", (voidP) new double(0.01));
}
ECF_LOG(state, 1, "opt-IA algorithm: added 1 FloatingPoint genotype (antibody age)");
return true;
}
示例3: registerParameters
/**
* \brief Register mutation related but Genotype unrelated parameters
*/
void Mutation::registerParameters(StateP state)
{
state->getRegistry()->registerEntry("mutation.indprob", (voidP) new double(0.3), ECF::DOUBLE,
"individual mutation probability (unless the algorithm overrides it) (default: 0.3)");
// state->getRegistry()->registerEntry("mutation.geneprob", (voidP) new double(0.01), ECF::DOUBLE);
state->getRegistry()->registerEntry("mutation.genotypes", (voidP) new std::string("random"), ECF::STRING,
"if there are multiple genotypes, which to mutate? 'random': a random one, all: mutate all (default: random)");
state->getRegistry()->registerEntry("mutation.protected", (voidP) new std::string(""), ECF::STRING,
"indexes of genotypes (separated by spaces) that are excluded (protected) from mutation (default: none)");
}
示例4: operate
bool TermStagnationOp::operate(StateP state)
{
uint currentGen = state->getGenerationNo();
if(currentGen - state->getPopulation()->getHof()->getLastChange() > termStagnation_) {
state->setTerminateCond();
ECF_LOG(state, 1, "Termination: maximum number of generations without improvement ("
+ uint2str(termStagnation_) + ") reached");
}
return true;
}
示例5: registerParameters
void RegEvalOp::registerParameters(StateP state) {
state->getRegistry()->registerEntry("inputfile", (voidP)(new std::string("learning.txt")), ECF::STRING);
state->getRegistry()->registerEntry("testfile", (voidP)(new std::string("test.txt")), ECF::STRING);
state->getRegistry()->registerEntry("classesfile", (voidP)(new std::string("classes.txt")), ECF::STRING);
state->getRegistry()->registerEntry("resultsfile", (voidP)(new std::string("results.txt")), ECF::STRING);
state->getRegistry()->registerEntry("classesNum", (voidP)(new uint(2)), ECF::UINT);
}
示例6: initialize
bool ArtificialBeeColony::initialize(StateP state)
{
// initialize all operators
selFitOp->initialize(state);
selFitOp->setSelPressure(2);
selBestOp->initialize(state);
selWorstOp->initialize(state);
selRandomOp->initialize(state);
voidP sptr = state->getRegistry()->getEntry("population.size");
uint size = *((uint*) sptr.get());
probability_.resize(size);
// this algorithm accepts a single FloatingPoint Genotype
FloatingPointP flp (new FloatingPoint::FloatingPoint);
if(state->getGenotypes()[0]->getName() != flp->getName()) {
ECF_LOG_ERROR(state, "Error: ABC algorithm accepts only a single FloatingPoint genotype!");
throw ("");
}
voidP limitp = getParameterValue(state, "limit");
limit_ = *((uint*) limitp.get());
voidP lBound = state->getGenotypes()[0]->getParameterValue(state, "lbound");
lbound_ = *((double*) lBound.get());
voidP uBound = state->getGenotypes()[0]->getParameterValue(state, "ubound");
ubound_ = *((double*) uBound.get());
// batch run check
if(isTrialAdded_)
return true;
FloatingPointP flpoint[2];
for(uint iGen = 1; iGen < 2; iGen++) {
flpoint[iGen] = (FloatingPointP) new FloatingPoint::FloatingPoint;
state->setGenotype(flpoint[iGen]);
flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(1));
// initial value of trial parameter should be (as close as possible to) 0
flpoint[iGen]->setParameterValue(state, "lbound", (voidP) new double(0));
flpoint[iGen]->setParameterValue(state, "ubound", (voidP) new double(0.01));
}
ECF_LOG(state, 1, "ABC algorithm: added 1 FloatingPoint genotype (trial)");
// mark adding of trial genotype
isTrialAdded_ = true;
return true;
}
示例7: createNewFoodSource
bool createNewFoodSource(IndividualP food, StateP state, DemeP deme)
{
//for each food source find a neighbour
IndividualP neighbour;
do{
neighbour = selRandomOp->select(*deme);
}while(food->index == neighbour->index);
//potential new food source
IndividualP newFood = copy(food);
FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(0));
std::vector< double > &foodVars = flp->realValue;
flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (neighbour->getGenotype(0));
std::vector< double > &neighbourVars = flp->realValue;
flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newFood->getGenotype(0));
std::vector< double > &newFoodVars = flp->realValue;
uint param = state->getRandomizer()->getRandomInteger((int)foodVars.size());
double factor = state->getRandomizer()->getRandomDouble();
double value = foodVars[param] * (1-2*factor)*(foodVars[param]-neighbourVars[param]);
if (value > ubound)
value = ubound;
else if (value <lbound)
value = lbound;
//produce a modification on the food source (discover a new food source)
newFoodVars[param] = value;
evaluate(newFood);
flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(1));
double &foodTrial = flp->realValue[0];
// d) if the fitness value of the new food source is better than that of the original source,
// memorize the new source, forget the old one and set trial to 0
// otherwise keep the old one and increment trial
if(newFood->fitness->isBetterThan( food->fitness) )
{
foodVars[param] = value;
evaluate(food);
foodTrial = 0;
}
else {
foodTrial +=1;
}
return true;
}
示例8: initialize
bool Individual::initialize(StateP state)
{
state_ = state;
this->clear();
// copy genotypes from State
for(uint i = 0; i < state->getGenotypes().size(); i++) {
this->push_back(static_cast<GenotypeP> (state->getGenotypes()[i]->copy()));
(*this)[i]->setGenotypeId(i);
}
// init genotypes
for(uint i = 0; i < this->size(); i++)
(*this)[i]->initialize(state);
return true;
}
示例9: crossover
//! cross donor vectors with population members to create trial vectors
void DifferentialEvolution::crossover(DemeP deme, uint index, StateP state)
{
// get population member and corresponding donor vector
FloatingPoint::FloatingPoint* flp1 = (FloatingPoint::FloatingPoint*) (deme->at(index)->getGenotype().get());
int dim = (int) flp1->realValue.size();
FloatingPoint::FloatingPoint* flp2 = (FloatingPoint::FloatingPoint*) donor_vector[index]->getGenotype().get();
// crossover their elements (keep the result in donor_vector)
for(uint i = 0; i < flp1->realValue.size(); i++) {
if (state->getRandomizer()->getRandomDouble() <= CR_ || i == state->getRandomizer()->getRandomInteger(dim)) {
}
else {
flp2->realValue[i] = flp1->realValue[i];
}
}
}
示例10: hypermutationPhase
bool hypermutationPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
{
uint M; // M - number of mutations of a single antibody
uint k;
//sort
std::sort (clones.begin(), clones.end(), sortPopulationByFitness);
for( uint i = 0; i < clones.size(); i++ ){ // for each antibody in vector clones
IndividualP antibody = clones.at(i);
FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(0));
std::vector< double > &antibodyVars = flp->realValue;
k = 1 + i/(dup+1);
M =(int) ((1- 1/(double)(k)) * (c*dimension) + (c*dimension));
// mutate M times
for (uint j = 0; j < M; j++){
uint param = state->getRandomizer()->getRandomInteger((int)antibodyVars.size());
double randDouble1 = state->getRandomizer()->getRandomDouble();
double randDouble2 = state->getRandomizer()->getRandomDouble();
double value = antibodyVars[param] + (1-2*randDouble1)* 0.2 * (ubound - lbound) * pow(2, -16*randDouble2 );
if (value > ubound)
value = ubound;
else if (value <lbound)
value = lbound;
//produce a mutation on the antibody
antibodyVars[param] = value;
}
FitnessP parentFitness = antibody->fitness;
evaluate(antibody);
// if the clone is better than its parent, reset clone's age
if(antibody-> fitness->isBetterThan(parentFitness)){
flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1));
double &age = flp->realValue[0];
age = 0;
}
}
return true;
}
示例11: initialize
bool DifferentialEvolution::initialize(StateP state)
{
selRandomOp->initialize(state);
donor_vector.clear();
// read parameters, check defined genotype (only a single FloatingPoint is allowed)
voidP F = getParameterValue(state, "F");
Fconst_ = *((double*) F.get());
voidP CR = getParameterValue(state, "CR");
CR_ = *((double*) CR.get());
FloatingPointP flp (new FloatingPoint::FloatingPoint);
if(state->getGenotypes()[0]->getName() != flp->getName() || state->getGenotypes().size() != 1) {
state->getLogger()->log(1, "Error: DE algorithm accepts only a single FloatingPoint genotype!");
throw ("");
}
return true;
}
示例12: registerParameters
void ModularRobotEvalOp::registerParameters(StateP state)
{
state->getRegistry()->registerEntry("robot.modules", (voidP) (new uint(1)), ECF::UINT, "Number of modules" );
state->getRegistry()->registerEntry("robot.runtime", (voidP) (new uint(10000)), ECF::UINT, "Max robot runtime (ms)" );
state->getRegistry()->registerEntry("robot.timestep", (voidP) (new float(1.0)), ECF::FLOAT, "Time step (ms)" );
state->getRegistry()->registerEntry("robot.configfile", (voidP) (new std::string()), ECF::STRING, "Robot description file");
state->getRegistry()->registerEntry("osc.maxamplitude", (voidP) (new uint(90)), ECF::UINT, "Max amplitude of oscillators");
state->getRegistry()->registerEntry("osc.maxoffset", (voidP) (new uint(90)), ECF::UINT, "Max offset of oscillators");
state->getRegistry()->registerEntry("osc.maxphase", (voidP) (new uint(360)), ECF::UINT, "Max phase of oscillators");
state->getRegistry()->registerEntry("osc.maxfrequency", (voidP) (new float(1.0f)), ECF::FLOAT, "Max frequency of oscillators");
}
示例13: initialize
bool SymbRegEvalOp::initialize(StateP state)
{
domain.clear();
codomain.clear();
double datum;
std::stringstream ss;
// check if the parameters are defined in the conf. file
// if not, we return false so the initialization fails
if(!state->getRegistry()->isModified("domain") ||
!state->getRegistry()->isModified("codomain"))
return false;
voidP sptr = state->getRegistry()->getEntry("domain"); // get parameter value
ss << *((std::string*) sptr.get()); // convert from voidP to user defined type
while(ss >> datum) { // read all the data from string
domain.push_back(datum);
}
ss.str("");
ss.clear(); // reset stringstream object
sptr = state->getRegistry()->getEntry("codomain"); // get codomain parameter
ss << *((std::string*) sptr.get());
while(ss >> datum) { // read values
codomain.push_back(datum);
}
if(domain.size() != codomain.size()) // if the parameters are ill defined, return false
return false;
nSamples = (uint) domain.size();
return true;
//nSamples = 10;
//double x = -10;
//for(uint i = 0; i < nSamples; i++) {
// domain.push_back(x);
// codomain.push_back(x + sin(x));
// x += 2;
//}
//return true;
}
示例14: main
int main(int argc, char **argv)
{
// argc = 2;
// argv[1] = "./examples/GAFunctionMin/parametri.txt";
// PSO algoritam:
//argv[1] = "./examples/GAFunctionMin/parameters_DE.txt";
GenHookeJeevesP alg (new GenHookeJeeves);
StateP state (new State);
state->addAlgorithm(alg);
state->setEvalOp(static_cast<EvaluateOpP> (new FunctionMinEvalOp));
state->initialize(argc, argv);
state->run();
return 0;
}
示例15: initialize
bool TermStagnationOp::initialize(StateP state)
{
voidP sptr = state->getRegistry()->getEntry("term.stagnation");
termStagnation_ = *((uint*) sptr.get());
// define the default criterion
if(termStagnation_ == 0) {
voidP sptr = state->getRegistry()->getEntry("population.size");
uint demeSize = *((uint*) sptr.get());
termStagnation_ = 5000 / demeSize;
if(termStagnation_ < 10)
termStagnation_ = 5;
if(termStagnation_ > 200)
termStagnation_ = 200;
}
if(!state->getRegistry()->isModified("term.stagnation"))
return false;
return true;
}