本文整理汇总了C++中population::push_back方法的典型用法代码示例。如果您正苦于以下问题:C++ population::push_back方法的具体用法?C++ population::push_back怎么用?C++ population::push_back使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population
的用法示例。
在下文中一共展示了population::push_back方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init_prev_population
void init_prev_population()
{
for(int i = 0; i < population_size; ++i)
{
specimen s;
s.perm = permutation(N);
s.eval = s.adapt = 0.0;
prev_population.push_back(s);
}
}
示例2: enumerate_program_trees
void enumerate_program_trees(generation_table& gtable, int depth, combo::type_tree& ttree, population& pop, const reduct::rule& reduction_rule) {
pop.clear();
// For each generation node with the right return-type, add it to the pop
for (std::vector<generation_node>::iterator it = gtable.begin(); it != gtable.end(); ++it) {
if (combo::equal_type_tree(it->node, combo::get_signature_output(ttree))) {
for (node_list::iterator it2 = it->glist.begin(); it2 != it->glist.end(); it2++)
pop.push_back(combo::combo_tree(*it2));
break;
}
}
// add the right number of arguments
int from_arg = combo::get_signature_inputs(ttree).size();
combo::arity_t needed_arg_count = combo::type_tree_arity(ttree);
std::cout << ttree << " " << needed_arg_count << std::endl;
for (int i = 1; i < depth; i++) {
fill_leaves(pop, from_arg);
reduce(pop, reduction_rule);
increase_tree_depth(gtable, pop, i, needed_arg_count, from_arg, reduction_rule);
}
for (population::iterator it = pop.begin(); it != pop.end();) {
bool erased = false;
for (combo::combo_tree::leaf_iterator lit = it->begin_leaf(); lit != it->end_leaf(); ++lit) {
if (get_arity(*lit) != 0 && !combo::is_argument(*lit)) {
erased = true;
break;
}
}
if (!combo::does_contain_all_arg_up_to(*it, needed_arg_count)) {
erased = true;
}
if (erased)
it = pop.erase(it);
else
++it;
}
}
示例3: evolve
/**
* Run the co-evolution algorithm
*
* @param[in,out] pop input/output pagmo::population to be evolved.
*/
void cstrs_co_evolution::evolve(population &pop) const
{
// Let's store some useful variables.
const problem::base &prob = pop.problem();
const population::size_type pop_size = pop.size();
const problem::base::size_type prob_dimension = prob.get_dimension();
// get the constraints dimension
problem::base::c_size_type prob_c_dimension = prob.get_c_dimension();
//We perform some checks to determine wether the problem/population are suitable for co-evolution
if(prob_c_dimension < 1) {
pagmo_throw(value_error,"The problem is not constrained and co-evolution is not suitable to solve it");
}
if(prob.get_f_dimension() != 1) {
pagmo_throw(value_error,"The problem is multiobjective and co-evolution is not suitable to solve it");
}
// Get out if there is nothing to do.
if(pop_size == 0) {
return;
}
//get the dimension of the chromosome of P2
unsigned int pop_2_dim = 0;
switch(m_method)
{
case algorithm::cstrs_co_evolution::SIMPLE:
{
pop_2_dim = 2;
break;
}
case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
{
pop_2_dim = 4;
break;
}
case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
pop_2_dim = 2*prob.get_c_dimension();
break;
default:
pagmo_throw(value_error,"The constraints co-evolutionary method is not valid.");
break;
}
// split the population into two populations
// the population P1 associated to the modified problem with penalized fitness
// and population P2 encoding the penalty weights
// Populations size
population::size_type pop_1_size = pop_size;
population::size_type pop_2_size = m_pop_penalties_size;
//Creates problem associated to P2
problem::cstrs_co_evolution_penalty prob_2(prob,pop_2_dim,pop_2_size);
prob_2.set_bounds(m_pen_lower_bound,m_pen_upper_bound);
//random initialization of the P2 chromosome (needed for the fist generation)
std::vector<decision_vector> pop_2_x(pop_2_size);
std::vector<fitness_vector> pop_2_f(pop_2_size);
for(population::size_type j=0; j<pop_2_size; j++) {
pop_2_x[j] = decision_vector(pop_2_dim,0.);
// choose random coefficients between lower bound and upper bound
for(population::size_type i=0; i<pop_2_dim;i++) {
pop_2_x[j][i] = boost::uniform_real<double>(m_pen_lower_bound,m_pen_upper_bound)(m_drng);
}
}
//vector of the population P1. Initialized with clones of the original population
std::vector<population> pop_1_vector;
for(population::size_type i=0; i<pop_2_size; i++){
pop_1_vector.push_back(population(pop));
}
// Main Co-Evolution loop
for(int k=0; k<m_gen; k++) {
// for each individuals of pop 2, evolve the current population,
// and store the position of the feasible idx
for(population::size_type j=0; j<pop_2_size; j++) {
problem::cstrs_co_evolution prob_1(prob, pop_1_vector.at(j), m_method);
// modify the problem by setting decision vector encoding penalty
// coefficients w1 and w2 in prob 1
prob_1.set_penalty_coeff(pop_2_x.at(j));
// creating the POPULATION 1 instance based on the
// updated prob 1
// prob_1 is a BASE_META???? THE CLONE OF prob_1 IN POP_1 IS AT THE LEVEL OF
// THE BASE CLASS AND NOT AT THE LEVEL OF THE BASE_META, NO?!?
population pop_1(prob_1,0);
// initialize P1 chromosomes. The fitnesses related to problem 1 are computed
//.........这里部分代码省略.........
示例4: evolve
void ihs::evolve(population &pop) const
{
// Let's store some useful variables.
const problem::base &prob = pop.problem();
const problem::base::size_type prob_dimension = prob.get_dimension(), prob_i_dimension = prob.get_i_dimension();
const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub();
const population::size_type pop_size = pop.size();
// Get out if there is nothing to do.
if (pop_size == 0 || m_gen == 0) {
return;
}
decision_vector lu_diff(prob_dimension);
for (problem::base::size_type i = 0; i < prob_dimension; ++i) {
lu_diff[i] = ub[i] - lb[i];
}
// Int distribution to be used when picking random individuals.
boost::uniform_int<population::size_type> uni_int(0,pop_size - 1);
const double c = std::log(m_bw_min/m_bw_max) / m_gen;
// Temporary individual used during evolution.
population::individual_type tmp;
tmp.cur_x.resize(prob_dimension);
tmp.cur_f.resize(prob.get_f_dimension());
tmp.cur_c.resize(prob.get_c_dimension());
for (std::size_t g = 0; g < m_gen; ++g) {
const double ppar_cur = m_ppar_min + ((m_ppar_max - m_ppar_min) * g) / m_gen, bw_cur = m_bw_max * std::exp(c * g);
// Continuous part.
for (problem::base::size_type i = 0; i < prob_dimension - prob_i_dimension; ++i) {
if (m_drng() < m_phmcr) {
// tmp's i-th chromosome element is the one from a randomly chosen individual.
tmp.cur_x[i] = pop.get_individual(uni_int(m_urng)).cur_x[i];
// Do pitch adjustment with ppar_cur probability.
if (m_drng() < ppar_cur) {
// Randomly, add or subtract pitch from the current chromosome element.
if (m_drng() > .5) {
tmp.cur_x[i] += m_drng() * bw_cur * lu_diff[i];
} else {
tmp.cur_x[i] -= m_drng() * bw_cur * lu_diff[i];
}
// Handle the case in which we added or subtracted too much and ended up out
// of boundaries.
if (tmp.cur_x[i] > ub[i]) {
tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng);
} else if (tmp.cur_x[i] < lb[i]) {
tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng);
}
}
} else {
// Pick randomly within the bounds.
tmp.cur_x[i] = boost::uniform_real<double>(lb[i],ub[i])(m_drng);
}
}
//Integer Part
for (problem::base::size_type i = prob_dimension - prob_i_dimension; i < prob_dimension; ++i) {
if (m_drng() < m_phmcr) {
tmp.cur_x[i] = pop.get_individual(uni_int(m_urng)).cur_x[i];
if (m_drng() < ppar_cur) {
if (m_drng() > .5) {
tmp.cur_x[i] += double_to_int::convert(m_drng() * bw_cur * lu_diff[i]);
} else {
tmp.cur_x[i] -= double_to_int::convert(m_drng() * bw_cur * lu_diff[i]);
}
// Wrap over in case we went past the bounds.
if (tmp.cur_x[i] > ub[i]) {
tmp.cur_x[i] = lb[i] + double_to_int::convert(tmp.cur_x[i] - ub[i]) % static_cast<int>(lu_diff[i]);
} else if (tmp.cur_x[i] < lb[i]) {
tmp.cur_x[i] = ub[i] - double_to_int::convert(lb[i] - tmp.cur_x[i]) % static_cast<int>(lu_diff[i]);
}
}
} else {
// Pick randomly within the bounds.
tmp.cur_x[i] = boost::uniform_int<int>(lb[i],ub[i])(m_urng);
}
}
// And we push him back
pop.push_back(tmp.cur_x);
// We locate the worst individual.
const population::size_type worst_idx = pop.get_worst_idx();
// And we get rid of him :)
pop.erase(worst_idx);
}
}
示例5: evolve
/**
* Run the CORE algorithm
*
* @param[in,out] pop input/output pagmo::population to be evolved.
*/
void cstrs_core::evolve(population &pop) const
{
// store useful variables
const problem::base &prob = pop.problem();
const population::size_type pop_size = pop.size();
const problem::base::size_type prob_dimension = prob.get_dimension();
// get the constraints dimension
problem::base::c_size_type prob_c_dimension = prob.get_c_dimension();
//We perform some checks to determine wether the problem/population are suitable for CORE
if(prob_c_dimension < 1) {
pagmo_throw(value_error,"The problem is not constrained and CORE is not suitable to solve it");
}
if(prob.get_f_dimension() != 1) {
pagmo_throw(value_error,"The problem is multiobjective and CORE is not suitable to solve it");
}
// Get out if there is nothing to do.
if(pop_size == 0) {
return;
}
// generates the unconstrained problem
problem::con2uncon prob_unconstrained(prob);
// associates the population to this problem
population pop_uncon(prob_unconstrained);
// fill this unconstrained population
pop_uncon.clear();
for(population::size_type i=0; i<pop_size; i++) {
pop_uncon.push_back(pop.get_individual(i).cur_x);
}
// vector containing the infeasibles positions
std::vector<population::size_type> pop_infeasibles;
// Main CORE loop
for(int k=0; k<m_gen; k++) {
if(k%m_repair_frequency == 0) {
pop_infeasibles.clear();
// get the infeasible individuals
for(population::size_type i=0; i<pop_size; i++) {
if(!prob.feasibility_c(pop.get_individual(i).cur_c)) {
pop_infeasibles.push_back(i);
}
}
// random shuffle of infeasibles?
population::size_type number_of_repair = (population::size_type)(m_repair_ratio * pop_infeasibles.size());
// repair the infeasible individuals
for(population::size_type i=0; i<number_of_repair; i++) {
const population::size_type ¤t_individual_idx = pop_infeasibles.at(i);
pop.repair(current_individual_idx, m_repair_algo);
}
// the population is repaired, it can be now used in the new unconstrained population
// only the repaired individuals are put back in the population
for(population::size_type i=0; i<number_of_repair; i++) {
population::size_type current_individual_idx = pop_infeasibles.at(i);
pop_uncon.set_x(current_individual_idx, pop.get_individual(current_individual_idx).cur_x);
}
}
m_original_algo->evolve(pop_uncon);
// push back the population in the main problem
pop.clear();
for(population::size_type i=0; i<pop_size; i++) {
pop.push_back(pop_uncon.get_individual(i).cur_x);
}
// Check the exit conditions (every 40 generations, just as DE)
if(k % 40 == 0) {
decision_vector tmp(prob_dimension);
double dx = 0;
for(decision_vector::size_type i=0; i<prob_dimension; i++) {
tmp[i] = pop.get_individual(pop.get_worst_idx()).best_x[i] - pop.get_individual(pop.get_best_idx()).best_x[i];
dx += std::fabs(tmp[i]);
}
if(dx < m_xtol ) {
if (m_screen_output) {
std::cout << "Exit condition -- xtol < " << m_xtol << std::endl;
}
break;
}
double mah = std::fabs(pop.get_individual(pop.get_worst_idx()).best_f[0] - pop.get_individual(pop.get_best_idx()).best_f[0]);
//.........这里部分代码省略.........