本文整理汇总了C++中population类的典型用法代码示例。如果您正苦于以下问题:C++ population类的具体用法?C++ population怎么用?C++ population使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了population类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: breed
population random_breeder::breed(const population &generation, const std::vector<double> &fitnesses) const
{
using namespace std;
population ret;
tree_crossover_reproducer repr;
node_crossover_reproducer mrepr;
builder random_builder;
experimental_parameters e = exp_params();
const uint32_t growth_tree_min = e["growth_tree_min"];
const uint32_t growth_tree_max = e["growth_tree_max"];
while(ret.size() < generation.size())
{
const agent &mother = generation[rand() % generation.size()];
const agent &father = generation[rand() % generation.size()];
agent mutant = random_builder.grow(program_types_set, growth_tree_min, growth_tree_max);
const vector<agent> children = repr.reproduce(vector<agent> { mother, father });
if(children.size() > 0)
{
agent res0 = mrepr.reproduce(vector<agent> { children[0], mutant })[0];
ret.push_back(res0);
}
if(children.size() > 1)
{
agent res1 = mrepr.reproduce(vector<agent> { children[1], mutant })[0];
ret.push_back(res1);
}
}
return ret;
}
示例2: report
void report(population& p)
{
switch(cfg.report_every)
{
case config::report::none:
break;
case config::report::avg:
{
float avg = 0.0;
for(auto i = p.begin(); i != p.end(); ++i)
avg += i->eval;
avg /= static_cast<float>(p.size());
std::cout << iter << ' ' << avg << '\n';
}
break;
case config::report::best:
std::cout << iter << ' ' << p[0].eval << '\n';
break;
}
if(cfg.report_var)
{
std::cout << iter << ' ' << statistics::variance(p) << '\n';
}
}
示例3: increase_tree_depth
void increase_tree_depth(generation_table& gtable, population& pop, int& from_depth, combo::arity_t& needed_arg_count, int fill_from_arg, const reduct::rule& reduction_rule) {
for (population::iterator it = pop.begin(); it != pop.end(); ++it) {
int number_of_combinations = 1;
bool can_have_leaves = false;
std::vector<generation_table::iterator> assignment;
std::vector<combo::combo_tree::leaf_iterator> leaves;
for (combo::combo_tree::leaf_iterator lit = it->begin_leaf(); lit != it->end_leaf(); ++lit) {
if (combo::is_argument(*lit))
if (combo::get_argument(*lit).abs_idx() <= needed_arg_count)
continue;
for (std::vector<generation_node>::iterator it2 = gtable.begin(); it2 != gtable.end(); it2++)
if (combo::equal_type_tree(it2->node, combo::infer_vertex_type(*it, lit))) {
if (it2->glist.size() != 0)
can_have_leaves = true;
assignment.push_back(it2);
number_of_combinations *= it2->glist.size();
break;
}
}
if (!can_have_leaves)
number_of_combinations = 0;
for (int i = 0; i < number_of_combinations; i++) {
combo::combo_tree temp_tree(*it);
int ongoing_count = 1;
leaves.clear();
for (combo::combo_tree::leaf_iterator lit = temp_tree.begin_leaf(); lit != temp_tree.end_leaf(); ++lit)
leaves.push_back(lit);
std::vector<generation_table::iterator>::iterator it2 = assignment.begin();
for (std::vector<combo::combo_tree::leaf_iterator>::iterator lit = leaves.begin(); lit != leaves.end(); ++lit) {
if (combo::is_argument(**lit))
if (combo::get_argument(**lit).abs_idx() <= needed_arg_count)
continue;
if ((*it2)->glist.size() != 0) {
node_list::iterator it3 = (*it2)->glist.begin();
for (int n = 0; n < (int)(i/(number_of_combinations/(ongoing_count * (*it2)->glist.size())) % (*it2)->glist.size()); n++)
it3++;
temp_tree.replace(*lit, *it3);
ongoing_count *= (*it2)->glist.size();
}
it2++;
}
bool erased = true;
for (combo::combo_tree::leaf_iterator lit = temp_tree.begin_leaf(); lit != temp_tree.end_leaf(); ++lit) {
if (combo::get_arity(*lit) != 0 && !combo::is_argument(*lit)) {
erased = false;
break;
}
}
if (combo::does_contain_all_arg_up_to(temp_tree, needed_arg_count)) {
erased = false;
}
if (!erased) {
//Uses less memory but is very slow
/*fill_leaves_single(temp_tree, fill_from_arg);
reduced_insertion(new_pop, temp_tree, reduction_rule);*/
pop.push_front(temp_tree);
}
}
}
}
示例4: select
family_vector rnd_selector::select(const population&p,const int fcount)
{
family_vector result(fcount);
for(int i=0;i<fcount;i++)
result[i]=std::make_pair(m_random.uniform(0,p.size()-1),
m_random.uniform(0,p.size()-1));
return result;
}
示例5: adapt_population
void adapt_population(population& p)
{
float F_min = min_element(p.begin(),p.end(),eval_comp)->eval;
for(unsigned int i=0; i<p.size(); i++)
{
float sum = 0.0;
for(unsigned int j=0; j<p.size(); j++)
sum += p[j].eval - F_min;
p[i].adapt = (p[i].eval - F_min)/sum;
}
}
示例6: get_n_individuals
std::vector<population::individual_type> best_s_policy::select(const population &pop) const
{
const population::size_type migration_rate = get_n_individuals(pop);
// Create a temporary array of individuals.
std::vector<population::individual_type> result(pop.begin(),pop.end());
// Sort the individuals (best go first).
std::sort(result.begin(),result.end(),dom_comp(pop));
// Leave only desired number of elements in the result.
result.erase(result.begin() + migration_rate,result.end());
return result;
}
示例7: fill_leaves
void fill_leaves(population& pop, int& from_arg) {
for (population::iterator it = pop.begin(); it != pop.end(); ++it) {
int local_from_arg = from_arg;
for (combo::combo_tree::leaf_iterator lit = it->begin_leaf(); lit != it->end_leaf(); ++lit) {
if (!combo::is_argument(*lit)) {
combo::arity_t number_of_leaves = combo::get_arity(*lit);
if (number_of_leaves < 0) number_of_leaves = -1 * number_of_leaves + 1;
for (combo::arity_t count = 0; count < number_of_leaves; count++) {
(*it).append_child(lit, combo::argument(++local_from_arg));
}
}
}
}
}
示例8: working_pop
void ms::evolve(population &pop) const
{
// Let's store some useful variables.
const population::size_type NP = pop.size();
// Get out if there is nothing to do.
if (m_starts == 0 || NP == 0) {
return;
}
// Local population used in the algorithm iterations.
population working_pop(pop);
//ms main loop
for (int i=0; i< m_starts; ++i)
{
working_pop.reinit();
m_algorithm->evolve(working_pop);
if (working_pop.problem().compare_fc(working_pop.get_individual(working_pop.get_best_idx()).cur_f,working_pop.get_individual(working_pop.get_best_idx()).cur_c,
pop.get_individual(pop.get_worst_idx()).cur_f,pop.get_individual(pop.get_worst_idx()).cur_c
) )
{
//update best population replacing its worst individual with the good one just produced.
pop.set_x(pop.get_worst_idx(),working_pop.get_individual(working_pop.get_best_idx()).cur_x);
pop.set_v(pop.get_worst_idx(),working_pop.get_individual(working_pop.get_best_idx()).cur_v);
}
if (m_screen_output)
{
std::cout << i << ". " << "\tCurrent iteration best: " << working_pop.get_individual(working_pop.get_best_idx()).cur_f << "\tOverall champion: " << pop.champion().f << std::endl;
}
}
}
示例9: mutation_function
void mutation_function(population& p)
{
for(auto i = p.begin(); i != p.end(); ++i)
{
float prob = 1 - i->adapt; // probability of mutation
float r = uniform_random(); // random float between <0,1)
if(r < prob)
{
mutation::random_transposition(i->perm);
i->eval = evaluation(i->perm); // after mutation is done we have to evaluate this specimen again
}
}
}
示例10: replacement
void replacement(population& p)
{
std::sort(p.begin(), p.end(), eval_cmp());
p.resize(population_size);
if( p[0].eval < best_specimen.eval )
{
best_specimen = p[0];
deviate_count = 0;
}
else
{
deviate_count++;
}
}
示例11: allocate_pop
void allocate_pop(population &pop,size_t NumDims,int stra_num,int num_obj)
{
size_t pop_size=pop.size();
size_t i;
for ( i=0;i<pop_size;i++ )
allocate_ind(pop[i],NumDims,stra_num,num_obj);
}
示例12: reallocate_stra
void reallocate_stra(population &pop,int stra_idx,int size,double val)
{
int pop_size=pop.size();
int i;
for ( i=0;i<pop_size;i++ )
pop[i].stra[stra_idx].assign(size,val);
}
示例13: immigrants_idx
std::vector<std::pair<population::size_type,std::vector<population::individual_type>::size_type> >
random_r_policy::select(const std::vector<population::individual_type> &immigrants, const population &dest) const
{
const population::size_type rate_limit = std::min<population::size_type>(get_n_individuals(dest),boost::numeric_cast<population::size_type>(immigrants.size()));
// Temporary vectors to store sorted indices of the populations.
std::vector<population::size_type> immigrants_idx(boost::numeric_cast<std::vector<population::size_type>::size_type>(immigrants.size()));
std::vector<population::size_type> dest_idx(boost::numeric_cast<std::vector<population::size_type>::size_type>(dest.size()));
// Fill in the arrays of indices.
iota(immigrants_idx.begin(),immigrants_idx.end(),population::size_type(0));
iota(dest_idx.begin(),dest_idx.end(),population::size_type(0));
// Permute the indices (immigrants).
for (population::size_type i = 0; i < rate_limit; ++i) {
population::size_type next_idx = i + (m_urng() % (rate_limit - i));
if (next_idx != i) {
std::swap(immigrants_idx[i], immigrants_idx[next_idx]);
}
}
// Permute the indices (destination).
for (population::size_type i = 0; i < rate_limit; ++i) {
population::size_type next_idx = i + (m_urng() % (dest.size() - i));
if (next_idx != i) {
std::swap(dest_idx[i], dest_idx[next_idx]);
}
}
// Return value.
std::vector<std::pair<population::size_type,std::vector<population::individual_type>::size_type> >
retval;
for (population::size_type i = 0; i < rate_limit; ++i) {
retval.push_back(std::make_pair(dest_idx[i],immigrants_idx[i]));
}
return retval;
}
示例14: switch
/**
* @param[in] pop input population.
*
* @return the number of individuals to be migrated from/to the population according to the current migration rate and type.
*/
population::size_type base::get_n_individuals(const population &pop) const
{
population::size_type retval = 0;
switch (m_type) {
case absolute:
retval = boost::numeric_cast<population::size_type>(m_rate);
if (retval > pop.size()) {
pagmo_throw(value_error,"absolute migration rate is higher than population size");
}
break;
case fractional:
retval = boost::numeric_cast<population::size_type>(double_to_int::convert(m_rate * pop.size()));
pagmo_assert(retval <= pop.size());
}
return retval;
}
示例15: report_end
void report_end(population& p)
{
if(cfg.report_every == config::report::none)
{
if(cfg.report_population)
{
if(cfg.debug)
{
std::cout << "Reporting population\n";
std::cout << "Evaluation = [ permutation ]\n";
}
std::sort(p.begin(), p.end(), eval_cmp());
for(auto i = p.begin(); i != p.end(); ++i)
std::cout << i->eval << " = " << i->perm << std::endl;
}
if(cfg.report_best)
{
if(cfg.debug)
{
std::cout << "Reporting best speciman\n";
std::cout << "Evaluation = [ permutation ]\n";
}
std::cout << best_specimen.eval << " = " << best_specimen.perm << std::endl;
}
if(cfg.optimum > -1)
{
if(cfg.debug)
{
std::cout << "Reporting number of iterations needed to compute best specimen with given optimum value or --max-iter if optimum is not reached\n";
}
std::cout << iter << "\n";
}
if(cfg.compare_operators)
{
std::cout << "ox = " << ox_count << "\n";
std::cout << "cx = " << cx_count << "\n";
std::cout << "pmx = " << pmx_count << "\n";
std::cout << "crossovers = " << x_count << "\n";
}
}
}