本文整理汇总了C++中Problem::fitness方法的典型用法代码示例。如果您正苦于以下问题:C++ Problem::fitness方法的具体用法?C++ Problem::fitness怎么用?C++ Problem::fitness使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Problem
的用法示例。
在下文中一共展示了Problem::fitness方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: crossover
const population TwoPoint::crossover(const population & mates, const Problem & problem) const {
// two-point crossover
population children = mates;
int_dist percent(0, 100);
if (chance || percent(rg.engine) < int(100 * chance)) {
int_dist gene_dist(0, children[0].size() - 1);
int start = gene_dist(rg.engine);
int length = gene_dist(rg.engine);
for (Individual & child : children)
std::rotate(child.begin(), child.begin() + start, child.end());
for (int i = 0; i < length; i++)
std::swap(children[0][i], children[1][i]);
}
// update fitnesses
for (Individual & child : children) child.fitness = problem.fitness(child);
return children;
}
示例2: mutate
const Individual Creep::mutate(const Problem & problem, const Individual & subject) const {
// basic mutation by delta distribution and chance
Individual mutant = subject; // non-const copy to mutate
int_dist percent(0, 100);
// distribution of real delta values, scaled for problem domain
real_dist delta_dist(-problem.delta * (std::abs(problem.domain_min) +
std::abs(problem.domain_max)) / 2,
problem.delta * (std::abs(problem.domain_min) +
std::abs(problem.domain_max)) / 2);
for (parameter & gene : mutant)
// short circuit for problem.chance == 1
if (problem.chance || percent(rg.engine) < int(100 * problem.chance))
mutant.mutate(gene, gene + delta_dist(rg.engine));
// update fitness
mutant.fitness = problem.fitness(mutant);
return mutant;
}