Пример #1
0
index_pair selector::stochastic_roulette(generation& gen)
{
    auto chromosome = std::max_element(gen.begin(), gen.end(),
                                          [](auto& c1, auto& c2) { return c1.fitness > c2.fitness; });

    double max_fitness = chromosome->fitness;

//    double max_fitness = 0.9;
    auto chromosome_index = range::distribution_of(gen);

    auto select = [&](auto& random)
    {
        while(true)
        {
            int index = random(chromosome_index);
            double fitness = gen[index].fitness;
//            double fitness = 0.2;

            if (random(fitness_range) < fitness / max_fitness)
                return index;
        }
    };

    return index_pair(select(random), select(random));
}
Пример #2
0
index_pair selector::ranking(generation& gen)
{
    std::sort(gen.begin(), gen.end(), [](auto& c1, auto& c2) { return c1.fitness < c2.fitness; });

    int sum = 0;
    for (int i = 0; i < gen.size(); ++i)
        sum += i;

    std::uniform_int_distribution<> dis_sum(0, sum);

    auto select = [&](auto& random)
    {
        int probability = random(dis_sum);
        int index = 0;

        while (probability > 0)
        {
            probability -= index + 1;
            ++index;
        }

        return index >= gen.size() ? index - 1 : index;
    };

    return index_pair(select(random), select(random));
}
Пример #3
0
std::vector<chromosome> selector::elitism(generation& gen)
{
    std::sort(gen.begin(), gen.end(), [](auto& c1, auto& c2) { return c1.fitness > c2.fitness; });
    std::vector<chromosome> next_gen;
    next_gen.reserve(elitism_rate);

    for (int i = 0; i < elitism_rate; ++i)
        next_gen.push_back(gen[i]);

    return next_gen;
}
Пример #4
0
index_pair selector::roulette(generation& gen)
{
    double sum = std::accumulate(gen.begin(), gen.end(), 0.0, [](double prev, auto& c) { return prev + c.fitness; });

//    double sum = 30.0;
    std::uniform_real_distribution<> dis_sum(0.0, sum);

    auto select = [&](auto& random)
    {
        double probability = random(dis_sum);
        int index = 0;

        while (probability > 0)
        {
            probability -= gen[index].fitness;
            ++index;
        }

        return index >= gen.size() ? index - 1 : index;
    };

    return index_pair(select(random), select(random));
}
Пример #5
0
void gen_initialize(generation &geninit, int &spop, int &apop, int &idnumber, const int &aleaspace, const int &n_param, const vector<parameters> &params, const double &lambda) {
    
    if(aleaspace==0) {
        
        ///Populate the space with equidistant values. First generation
        int geninit_nindividuals = (int)pow(spop,n_param);
        geninit.construct(geninit_nindividuals, n_param, idnumber, lambda);
        
        ///Determination of parameters_equally_spaced
        mat samples = doe_uniform(spop, n_param, params);
        
        for(int j=0; j<n_param; j++) {
            for(int i=0; i<geninit.size(); i++) {
                geninit.pop[i].p(j) = samples(i,j);
            }
        }
        
    }
    if(aleaspace==1) {
        
        ///Populate the space with equidistant values. First generation
        int geninit_nindividuals = (int)pow(spop,n_param);
        geninit.construct(geninit_nindividuals, n_param, idnumber, lambda);
        
        ///Determination of parameters_equally_spaced
        mat samples = doe_uniform_limit(spop, n_param, params);
        
        for(int j=0; j<n_param; j++) {
            for(int i=0; i<geninit.size(); i++) {
                geninit.pop[i].p(j) = samples(i,j);
            }
        }
    }
    else if(aleaspace==2) {
        
        int geninit_nindividuals=apop;
        geninit.construct(geninit_nindividuals, n_param, idnumber, lambda);
                
        ///Determination of random values
        ///Determination of parameters_equally_spaced
        mat samples = doe_random(geninit_nindividuals, n_param, params);
        
        for(int j=0; j<n_param; j++) {
            for(int i=0; i<geninit.size(); i++) {
                geninit.pop[i].p(j) = samples(i,j);
            }
        }
    }
    else if(aleaspace==3) {
        
        mat samples;
        read_gen(apop, samples, n_param);
        int geninit_nindividuals=apop;
        geninit.construct(geninit_nindividuals, n_param, idnumber, lambda);
        
        for(int j=0; j<n_param; j++) {
            for(int i=0; i<geninit.size(); i++) {
                geninit.pop[i].p(j) = samples(i,j);
            }
        }
    }    
}