我正在尝试使用 C# 中的遗传算法解决旅行推销员问题。但在我的应用程序中,最佳值变化如此缓慢。我尝试过不同的交叉方法,例如经典、贪婪和 pmx,但我从来没有得到我想要的。在遗传算法中导致缓慢逼近局部最小值的最有效原因是什么?不是交叉方法吗?
我认为我的 CO 方法是正确的,不是吗?我的代码:
Tour ClassicCrossingOver(Tour mother, Tour father)
{
int pos = N / 2;
City[] gens = new City[N];
for (int i = 0; i < pos; i++)
{
gens[i] = mother.Cities[i];
}
List<int> nonPos = new List<int>(); //Handles duplicate city positions
for (int i = pos; i < gens.Length; i++)
{
if (gens.Contains(father.Cities[i]))
nonPos.Add(i);
gens[i] = father.Cities[i];
}
List<City> noneGenes = new List<City>(); //Handles cities that doesnt exists in the child
foreach (City gene in map.Cities)
{
if (gens.Contains(gene)) continue;
noneGenes.Add(gene);
}
for (int i = 0; i < noneGenes.Count; i++)
{
int j = rnd.Next(nonPos.Count - 1);
gens[nonPos[j]] = noneGenes[i];
nonPos.RemoveAt(j);
}
Tour tur = new Tour(map) { Cities = gens };
return tur;
}