本文整理汇总了C++中population类的典型用法代码示例。如果您正苦于以下问题:C++ population类的具体用法?C++ population怎么用?C++ population使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了population类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:ou-real,项目名称:finch,代码行数:30,代码来源:random_breeder.cpp
示例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';
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:25,代码来源:problem.cpp
示例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);
}
}
}
}
开发者ID:Spydrouge,项目名称:opencog_Spydr,代码行数:60,代码来源:generation.cpp
示例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;
}
开发者ID:lysevi,项目名称:concret,代码行数:8,代码来源:selector_rnd.cpp
示例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;
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:11,代码来源:problem.cpp
示例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;
}
开发者ID:irwenqiang,项目名称:pagmo,代码行数:11,代码来源:best_s_policy.cpp
示例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));
}
}
}
}
}
开发者ID:Spydrouge,项目名称:opencog_Spydr,代码行数:14,代码来源:generation.cpp
示例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;
}
}
}
开发者ID:DominicDirkx,项目名称:pagmo,代码行数:32,代码来源:ms.cpp
示例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
}
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:14,代码来源:problem.cpp
示例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++;
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:15,代码来源:problem.cpp
示例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);
}
开发者ID:vcbin,项目名称:stupidalgorithm,代码行数:7,代码来源:alg_datastruct.cpp
示例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);
}
开发者ID:vcbin,项目名称:stupidalgorithm,代码行数:7,代码来源:alg_datastruct.cpp
示例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;
}
开发者ID:DominicDirkx,项目名称:pagmo,代码行数:32,代码来源:random_r_policy.cpp
示例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;
}
开发者ID:DominicDirkx,项目名称:pagmo,代码行数:21,代码来源:base.cpp
示例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";
}
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:44,代码来源:problem.cpp
示例16: 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);
}
}
开发者ID:pthomalla,项目名称:evo,代码行数:10,代码来源:problem.cpp
示例17: pagmo_assert
std::vector<population::individual_type> best_kill_s_policy::select(population &pop) const
{
pagmo_assert(get_n_individuals(pop) <= pop.size());
// Gets the number of individuals to select
const population::size_type migration_rate = get_n_individuals(pop);
// Create a temporary array of individuals.
std::vector<population::individual_type> result;
// Gets the indexes of the best individuals
std::vector<population::size_type> best_idx = pop.get_best_idx(migration_rate);
// Puts the best individuals in results
for (population::size_type i =0; i< migration_rate; ++i) {
result.push_back(pop.get_individual(best_idx[i]));
}
// Remove them from the original population
// (note: the champion will still carry information on the best guy ...)
for (population::size_type i=0 ; i<migration_rate; ++i) {
pop.reinit(best_idx[i]);
}
return result;
}
开发者ID:DinCahill,项目名称:pagmo,代码行数:20,代码来源:best_kill_s_policy.cpp
示例18: tmp_x
/// Evolve method.
void monte_carlo::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_max_eval == 0) {
return;
}
// Initialise temporary decision vector, fitness vector and decision vector.
decision_vector tmp_x(prob_dimension);
fitness_vector tmp_f(prob.get_f_dimension());
constraint_vector tmp_c(prob.get_c_dimension());
// Main loop.
for (std::size_t i = 0; i < m_max_eval; ++i) {
// Generate a random decision vector.
for (problem::base::size_type j = 0; j < prob_dimension - prob_i_dimension; ++j) {
tmp_x[j] = boost::uniform_real<double>(lb[j],ub[j])(m_drng);
}
for (problem::base::size_type j = prob_dimension - prob_i_dimension; j < prob_dimension; ++j) {
tmp_x[j] = boost::uniform_int<int>(lb[j],ub[j])(m_urng);
}
// Compute fitness and constraints.
prob.objfun(tmp_f,tmp_x);
prob.compute_constraints(tmp_c,tmp_x);
// Locate the worst individual.
const population::size_type worst_idx = pop.get_worst_idx();
if (prob.compare_fc(tmp_f,tmp_c,pop.get_individual(worst_idx).cur_f,pop.get_individual(worst_idx).cur_c)) {
pop.set_x(worst_idx,tmp_x);
}
}
}
开发者ID:DinCahill,项目名称:pagmo,代码行数:35,代码来源:monte_carlo.cpp
示例19: pop_merged
// perform (\lambda+\mu) -> (\lambda) selection
void dgea_alg::select(population& pop, population& child_pop)
{
int pop_size=pop.size();
int num_dims=m_ppara->get_dim();
int i;
population pop_merged(pop_size);
vector<vector<double> > prev_x(pop_size);
for ( i=0;i<pop_size;i++ )
{
prev_x[i].resize(num_dims);
pop_merged[i]=pop[i];
// record previous x
prev_x[i]=pop[i].x;
}// for every individual
// push child population at the tail of merged population
copy( child_pop.begin(),child_pop.end(),back_inserter(pop_merged) );
nth_element(pop_merged.begin(), pop_merged.begin()+pop_size, pop_merged.end());
//// heap sorting
//make_heap(pop_merged.begin(),pop_merged.end());
//for ( i=0;i<pop_size;i++ )
//{
// // record delta x for stat purpose
// pop_heap(pop_merged.begin(),pop_merged.end());
// pop_merged.pop_back();
// const individual& new_ind=pop_merged.front();
// pop[i]=new_ind;
// m_alg_stat.delta_x[i]=pop[i].x-prev_x[i];// pop_merged:{parents,offsprings}
//}
for ( i=0;i<pop_size;i++ )
{
pop[i]=pop_merged[i];
// record delta x
m_alg_stat.delta_x[i] = (pop[i].x-prev_x[i]);
}// for every individual
}// end function select
开发者ID:vcbin,项目名称:stupidalgorithm,代码行数:39,代码来源:ga_alg.cpp
示例20: pagmo_throw
/**
* Updates the constraints scaling vector with the given population.
* @param[in] population pop.
*/
void cstrs_self_adaptive::update_c_scaling(const population &pop)
{
if(*m_original_problem != pop.problem()) {
pagmo_throw(value_error,"The problem linked to the population is not the same as the problem given in argument.");
}
// Let's store some useful variables.
const population::size_type pop_size = pop.size();
// get the constraints dimension
//constraint_vector c(m_original_problem->get_c_dimension(), 0.);
problem::base::c_size_type prob_c_dimension = m_original_problem->get_c_dimension();
problem::base::c_size_type number_of_eq_constraints =
m_original_problem->get_c_dimension() -
m_original_problem->get_ic_dimension();
const std::vector<double> &c_tol = m_original_problem->get_c_tol();
m_c_scaling.resize(m_original_problem->get_c_dimension());
std::fill(m_c_scaling.begin(),m_c_scaling.end(),0.);
// evaluates the scaling factor
for(population::size_type i=0; i<pop_size; i++) {
// updates the current constraint vector
const population::individual_type ¤t_individual = pop.get_individual(i);
const constraint_vector &c = current_individual.cur_c;
// computes scaling with the right definition of the constraints (can be in base problem? currently used
// by con2mo as well)
for(problem::base::c_size_type j=0; j<number_of_eq_constraints; j++) {
m_c_scaling[j] = std::max(m_c_scaling[j], std::max(0., (std::abs(c.at(j)) - c_tol.at(j))) );
}
for(problem::base::c_size_type j=number_of_eq_constraints; j<prob_c_dimension; j++) {
m_c_scaling[j] = std::max(m_c_scaling[j], std::max(0., c.at(j) - c_tol.at(j)) );
}
}
}
开发者ID:DinCahill,项目名称:pagmo,代码行数:42,代码来源:cstrs_self_adaptive.cpp
注:本文中的population类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论