本文整理汇总了C++中Chromosome::invert方法的典型用法代码示例。如果您正苦于以下问题:C++ Chromosome::invert方法的具体用法?C++ Chromosome::invert怎么用?C++ Chromosome::invert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chromosome
的用法示例。
在下文中一共展示了Chromosome::invert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GACrossOver
Chromosome Chromosome::GACrossOver(Chromosome parentA, Chromosome parentB) {
int size = parentA._solution.size();
int distance = Chromosome::distance(parentA, parentB);
if (distance > size / 2) {
parentA.invert();
}
vector<char> childSolution(size);
int ones = 0, zeros = 0;
for (int i = 0; i < size; ++i) {
if (parentA._solution[i] == parentB._solution[i]) {
childSolution[i] = parentA._solution[i];
ones += parentA._solution[i];
zeros += parentA._solution[i] ^ 1;
}
else {
childSolution[i] = 2; // the placeholder
}
}
random_device rd;
mt19937 gen(rd());
int addOnes = (size / 2) - ones;
int addZeroes = (size / 2) - zeros;
for (int k = 0; k < size; ++k) {
if (childSolution[k] == 2) {
bernoulli_distribution d(addOnes / (addOnes + addZeroes));
char bit = d(gen);
if (bit) {
addOnes--;
}
else {
addZeroes--;
}
childSolution[k] = bit;
}
}
return Chromosome(childSolution);
}