enter
This commit is contained in:
parent
c436a7dbc3
commit
dcaed27c9a
|
|
@ -42,18 +42,18 @@
|
|||
* cycle crossover
|
||||
*/
|
||||
//这段代码实现了遗传算法中的一种交叉操作(Crossover),用于生成新的候选解。
|
||||
//函数的目标是从两个不同的旅行路径(tour1 和 tour2)中创建一个新的路径(offspring)
|
||||
//函数的目标是从两个不同的父代查询计划,生成一个新的子代查询计划
|
||||
int cx(PlannerInfo* root, Gene* tour1, Gene* tour2, Gene* offspring, int num_gene, City* city_table)
|
||||
{
|
||||
int i, start_pos, curr_pos;
|
||||
int count = 0;// 用于计数已经添加到offspring的城市数量
|
||||
int num_diffs = 0; // 用于计数tour1和offspring之间的差异数量
|
||||
int count = 0;
|
||||
int num_diffs = 0;
|
||||
|
||||
/* initialize city table */
|
||||
for (i = 1; i <= num_gene; i++) {
|
||||
city_table[i].used = 0;// 将城市标记为未使用
|
||||
city_table[tour2[i - 1]].tour2_position = i - 1;// 记录tour2中每个城市在数组中的位置
|
||||
city_table[tour1[i - 1]].tour1_position = i - 1;// 记录tour1中每个城市在数组中的位置
|
||||
city_table[i].used = 0;// 将节点表标记为未使用
|
||||
city_table[tour2[i - 1]].tour2_position = i - 1;// 记录tour2中每个基因在数组中的位置
|
||||
city_table[tour1[i - 1]].tour1_position = i - 1;// 记录tour1中每个基因在数组中的位置
|
||||
}
|
||||
|
||||
/* choose random cycle starting position */
|
||||
|
|
@ -71,11 +71,11 @@ int cx(PlannerInfo* root, Gene* tour1, Gene* tour2, Gene* offspring, int num_gen
|
|||
/* cx main part */
|
||||
/* STEP 1 */
|
||||
while (tour2[curr_pos] != tour1[start_pos]) {
|
||||
city_table[(int)tour2[curr_pos]].used = 1;// 标记tour2中的城市为已使用
|
||||
curr_pos = city_table[(int)tour2[curr_pos]].tour1_position;// 切换到tour1中相应城市的位置
|
||||
offspring[curr_pos] = tour1[curr_pos];// 将对应的tour1城市添加到offspring中
|
||||
city_table[(int)tour2[curr_pos]].used = 1;// 标记tour2中的节点为已使用
|
||||
curr_pos = city_table[(int)tour2[curr_pos]].tour1_position;// 切换到tour1中相应节点的位置
|
||||
offspring[curr_pos] = tour1[curr_pos];// 将对应的tour1节点添加到offspring中
|
||||
|
||||
count++;// 增加已添加城市的计数
|
||||
count++;// 增加已添加节点的计数
|
||||
}
|
||||
|
||||
/* STEP 2 */
|
||||
|
|
@ -84,7 +84,7 @@ int cx(PlannerInfo* root, Gene* tour1, Gene* tour2, Gene* offspring, int num_gen
|
|||
for (i = 1; i <= num_gene; i++) {
|
||||
if (!city_table[i].used) {
|
||||
offspring[city_table[i].tour2_position] = tour2[(int)city_table[i].tour2_position];
|
||||
count++;// 增加已添加城市的计数
|
||||
count++;// 增加已添加节点的计数
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ void ox1(PlannerInfo* root, Gene* tour1, Gene* tour2, Gene* offspring, int num_g
|
|||
int left, right, k, p, temp;
|
||||
|
||||
/* initialize city table */
|
||||
for (k = 1; k <= num_gene; k++)// 初始化城市表中的标志位,用于记录城市是否已经在后代中出现
|
||||
for (k = 1; k <= num_gene; k++)// 初始化节点表中的标志位,用于记录城市是否已经在后代中出现
|
||||
city_table[k].used = 0;
|
||||
|
||||
/* select portion to copy from tour1 */
|
||||
|
|
|
|||
Loading…
Reference in New Issue