本文整理汇总了C++中population::size方法的典型用法代码示例。如果您正苦于以下问题:C++ population::size方法的具体用法?C++ population::size怎么用?C++ population::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population
的用法示例。
在下文中一共展示了population::size方法的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: 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;
}
示例3: 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;
}
}
示例4: 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';
}
}
示例5: 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;
}
示例6: 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;
}
示例7: evolve
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;
}
}
}
示例8: 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);
}
}
}
示例9: 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);
}
示例10: 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);
}
示例11: select
family_vector niche_ga::select(const population&p,int count)
{
dna_cmeans::u_vector data=fill_data_vector(p);
int cluster_count=static_cast<int>(p.size()*0.10);
dna_cmeans::result r;
if(call_nums>=10){
call_nums=0;
cm.m_center_sequence.erase(cm.m_center_sequence.begin(),cm.m_center_sequence.end());
}
if(cm.m_center_sequence.size()==0){
r=cm.cluster(dna_distance,cluster_count,data.begin(),data.end(),data.size(),1.0);
}
else{
r=cm.cluster(dna_distance,data.begin(),data.end(),data.size());
}
call_nums++;
clusters cls=dna_cmeans::u2clsuters(r.u);
Random rnd;
family_vector result(count);
int c=0; // сколько мы уже отобрали для скрещивания
while(c!=count){
int father_cluster=rnd.uniform(0,cls.size()-1);
if(cls[father_cluster].size()<2)
continue;
int father_index=rnd.uniform(0,cls[father_cluster].size()-1);
int mather_cluster=father_cluster;
int mather_index=rnd.uniform(0,cls[mather_cluster].size()-1);
result[c]=std::make_pair(cls[father_cluster][father_index].first,
cls[mather_cluster][mather_index].first);
c++;
}
return result;
}
示例12: 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;
}
示例13: select
// 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
示例14: 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)) );
}
}
}
示例15: eval_population_fitness
bool simpleLSM_job::eval_population_fitness(population &pop)
{
char pwd[FILENAME_MAX];
getcwd(pwd, sizeof(pwd));
log.debug("Current working directory", pwd);
float cputime=0;
float walltime=0;
for (sample &s : pop)
{
prepare_work_dir_for_sample(s);
boost::timer::cpu_timer timer;
eval_sample_fitness(s);
boost::timer::cpu_times elapsed = timer.elapsed();
log.debug(" CPU TIME: ", (elapsed.user + elapsed.system) / 1e9, " seconds", ", WALLCLOCK TIME: ", elapsed.wall / 1e9, " seconds");
cputime+=((elapsed.user + elapsed.system) / 1e9);
walltime+=(elapsed.wall / 1e9);
}
log.debug("total CPU TIME:", cputime, "total WALLCLOCK TIME:", walltime);
log.debug("avergae CPU TIME:", cputime/pop.size(), "avergae WALLCLOCK TIME:", walltime/pop.size());
return true;
}