Exemple #1
0
    std::pair<Chromosome, Breeder::BreedStats> Breeder::breed(const Chromosome& mum, const Chromosome& dad, uint32_t crossOverPerMillion)
    {
        BreedStats stats;
        stats.crossOvers = 0;

        std::random_device rd;
        std::uniform_int_distribution<> randomMillion(0, 999999);
        std::uniform_int_distribution<> mumOrDad(0, 1);
        bool startWithMum = mumOrDad(rd) == 0;

        const Gene* currentGene = startWithMum ? &mum.gene() : &dad.gene();
        const Gene* otherGene = startWithMum ? &dad.gene() : &mum.gene();

        Gene resultingGene;
        resultingGene.resize(currentGene->size());

        for(uint32_t i = 0; i < resultingGene.size(); i++)
        {
            resultingGene[i] = (*currentGene)[i];

            if(crossOverPerMillion > randomMillion(rd))
            {
                std::swap(currentGene, otherGene);
                ++stats.crossOvers;
            }
        }

        return {Chromosome(mum.maxCistronIds(), mum.informationDensity(), mum.startSequence(), mum.stopSequence(), resultingGene), stats};
    }
Exemple #2
0
    void Chromosome::replaceCistronValues(const std::vector<Cistron>& cistrons)
    {
        CistronMap cistronMap;

        for(const auto& cistron : cistrons)
        {
            cistronMap.emplace(cistron.id(), cistron);
        }

        size_t start = 0;

        while((start = mGene.find(mStartSequence, start)) != std::string::npos)
        {
            size_t stop = mGene.find(mStopSequence, start);

            if(stop != std::string::npos)
            {
                size_t cistronStart = start + mStartSequence.size();
                Cistron cistron(mGene.substr(cistronStart, stop - cistronStart), mMaxCistronIds, mInformationDensity);

                if(cistronMap.count(cistron.id()))
                {
                    Gene toInsert = mStartSequence;
                    toInsert.append(cistronMap.at(cistron.id()).gene());
                    toInsert.append(mStopSequence);

                    mGene.replace(start, toInsert.size(), toInsert);
                }
            }

            start = stop;
        }
    }