enter
This commit is contained in:
parent
77b21465dd
commit
72bc09be29
|
|
@ -37,22 +37,29 @@
|
|||
|
||||
void geqo_mutation(PlannerInfo* root, Gene* tour, int num_gene)
|
||||
{
|
||||
// 定义两个整数变量来表示要交换的基因的索引
|
||||
int swap1;
|
||||
int swap2;
|
||||
// 计算要执行的交换次数,这里使用随机数,num_gene/3表示最大交换次数
|
||||
int num_swaps = geqo_randint(root, num_gene / 3, 0);
|
||||
Gene temp;
|
||||
Gene temp;// 临时变量,用于存储基因交换时的中间结果
|
||||
|
||||
while (num_swaps > 0) {
|
||||
swap1 = geqo_randint(root, num_gene - 1, 0);
|
||||
while (num_swaps > 0) {// 当还有交换次数时循环执行交换
|
||||
swap1 = geqo_randint(root, num_gene - 1, 0);// 随机选择两个不同的基因索引
|
||||
swap2 = geqo_randint(root, num_gene - 1, 0);
|
||||
|
||||
while (swap1 == swap2)
|
||||
while (swap1 == swap2)// 确保选到的两个索引不相同
|
||||
swap2 = geqo_randint(root, num_gene - 1, 0);
|
||||
|
||||
temp = tour[swap1];
|
||||
temp = tour[swap1];// 执行基因交换,将选中的两个基因进行互换
|
||||
tour[swap1] = tour[swap2];
|
||||
tour[swap2] = temp;
|
||||
|
||||
num_swaps -= 1;
|
||||
num_swaps -= 1;// 减少剩余的交换次数
|
||||
}
|
||||
}
|
||||
/*
|
||||
这段代码的功能是在给定基因序列(tour)中执行基因交换操作。
|
||||
它首先确定要执行的交换次数,然后随机选择两个不同的基因位置进行交换。
|
||||
通过这种方式,它可以引入随机性,有助于在搜索空间中找到更多可能的解决方案,以提高查询优化的效果
|
||||
*/
|
||||
Loading…
Reference in New Issue