This commit is contained in:
Cachuela 2023-09-25 18:40:35 +08:00
parent 77b21465dd
commit 72bc09be29
1 changed files with 13 additions and 6 deletions

View File

@ -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
*/