Esempio n. 1
0
void selection(int** population) {
    int i,j;
    int sum = 0;
    int result[SIZE_OF_POPULATION];

    //Check how good is every genotype in the population
    for(i=0; i<SIZE_OF_POPULATION; i++) {
        result[i] = result_function(population[i]);
        sum += result[i];
    }

    //Define probability for every genotype to be in the next population
    float prob[SIZE_OF_POPULATION];
    float curr_prob = 0;
    for(i=0; i<SIZE_OF_POPULATION; i++) {
        float prob_i = result[i]/(float)sum;
        curr_prob += prob_i;
        prob[i] = curr_prob;
    }

    //Clean the array
    int count_chosen[SIZE_OF_POPULATION];
    for(i=0; i<SIZE_OF_POPULATION; i++)
        count_chosen[i] = 0;

    //Select new population
    for(i=0; i<SIZE_OF_POPULATION; i++) {
        float key = (float)(rand()%sum);
        key /= (float)sum;

        for(j=0; j<SIZE_OF_POPULATION; j++) {
            if(key <= prob[j]) {
                count_chosen[j]++;
                break;
            }
        }
    }

    //Clone chosen genotypes (replace those which are to be removed)
    int pos_last_dead = 0;
    int k;
    for(i=0; i<SIZE_OF_POPULATION; i++) {
        if (count_chosen[i] > 0) {
            for(j=0; j<count_chosen[i]; j++) {
                for(k = pos_last_dead; k < SIZE_OF_POPULATION; ++k){
                    if (count_chosen[k] == 0) {
                        pos_last_dead = k + 1;
                        mov_genom(population, i, k);
                        break;
                    }
                }
            }
        }
    }

}
Esempio n. 2
0
/* Find best genotype among whole population */
void best_genotype(int** population, int* best_id, int* best_result, int* best_genotype) {
    int i, j, id;
    int best = 0;
    for(i=0; i<SIZE_OF_POPULATION; i++) {
        int curr_result = result_function(population[i]);

        if (curr_result > best) {
            best = curr_result;
            id = i;
            for(j=0; j<NUMBER_OF_ITEMS; j++) {
                best_genotype[j] = population[i][j];
            }
        }
    }

    *best_id = id;
    *best_result = best;
}
Esempio n. 3
0
void async_result<T>::connect(const result_array_function &handler)
{
	auto keeper = std::make_shared<data_keeper>();
	keeper->data_ptr = m_data;
	connect(result_function(), std::bind(aggregator_final_handler, keeper, handler));
}