Пример #1
0
void testPopulationIO_Binary()
{
    if (os_) *os_ << "testPopulationIO_Binary()\n";

    Random random;

    Population::Config config;
    config.size = 10;
    config.populationID = 0;
    config.chromosomePairCount = 1;
    Population p;
    p.create_organisms(config);
    unit_assert(p.organisms().size() == 10);
    if (os_) *os_ << "p:\n" << p << endl;

    ostringstream oss;
    p.write(oss);

    Population q;
    unit_assert(p != q);

    istringstream iss(oss.str());
    q.read(iss);
    if (os_) *os_ << "q:\n" << q << endl;
    unit_assert(p == q);

    if (os_) *os_ << endl;
}
Пример #2
0
void testPopulation_generated()
{
    if (os_) *os_ << "testPopulation_generated()\n";

    Random random;

    Organism::recombinationPositionGenerator_ =
        shared_ptr<RecombinationPositionGenerator>(new RecombinationPositionGenerator_Trivial(random));

    Population::Config config0;
    config0.size = 10;
    config0.populationID = 0;
    config0.chromosomePairCount = 1;

    Population::Config config1;
    config1.size = 10;
    config1.populationID = 1;
    config1.chromosomePairCount = 1;

    shared_ptr<Population> p0(new Population());
    p0->create_organisms(config0);
    shared_ptr<Population> p1(new Population());
    p1->create_organisms(config1);

    if (os_) *os_ << "p0:\n" << *p0 << endl;
    if (os_) *os_ << "p1:\n" << *p1 << endl;

    PopulationPtrs populations;
    populations.push_back(p0);
    populations.push_back(p1);
    
    Population::Config config_nextgen;
    config_nextgen.size = 100;
    config_nextgen.matingDistribution.push_back(.5, make_pair(0,0));
    config_nextgen.matingDistribution.push_back(.3, make_pair(1,0));
    config_nextgen.matingDistribution.push_back(.2, make_pair(1,1));

    DataVectorPtrs dummy_fitnesses(populations.size());

    Population nextgen;
    nextgen.create_organisms(config_nextgen, populations, dummy_fitnesses, random);
    if (os_) *os_ << "nextgen:\n" << nextgen << endl;

    int count00 = 0;
    int count01 = 0;
    int count11 = 0;
    
    for (vector<Organism>::const_iterator it=nextgen.organisms().begin(); it!=nextgen.organisms().end(); ++it)
    {
        Chromosome::ID id1(it->chromosomePairs()[0].first.blocks()[0].id);
        Chromosome::ID id2(it->chromosomePairs()[0].second.blocks()[0].id);

        if (id1.population==0 && id2.population==0) count00++;
        else if (id1.population==1 && id2.population==1) count11++;
        else count01++;
    }

    if (os_) *os_ << "count00: " << count00 << endl;
    if (os_) *os_ << "count01: " << count01 << endl;
    if (os_) *os_ << "count11: " << count11 << endl;
    if (os_) *os_ << endl;

    Organism::recombinationPositionGenerator_ = shared_ptr<RecombinationPositionGenerator>();
}