我正在爲一個項目的GA工作。我正在嘗試使用GA來解決旅行商問題。我用array[]
來存儲數據,我認爲Array
s比List
快得多。但由於任何原因,它需要太多時間。例如用MaxPopulation = 100000
,StartPopulation=1000
該程序持續約1分鐘完成。我想知道這是否是一個問題。如果是這樣,我該如何解決這個問題?用GA(遺傳算法)實現解決TSP是否正常需要很長時間?
守則的一部分,從我的實現:
public void StartAsync()
{
Task.Run(() =>
{
CreatePopulation();
currentPopSize = startPopNumber;
while (currentPopSize < maxPopNumber)
{
Tour[] elits = ElitChromosoms();
for (int i = 0; i < maxCrossingOver; i++)
{
if (currentPopSize >= maxPopNumber)
break;
int x = rnd.Next(elits.Length - 1);
int y = rnd.Next(elits.Length - 1);
Tour parent1 = elits[x];
Tour parent2 = elits[y];
Tour child = CrossingOver(parent1, parent2);
int mut = rnd.Next(100);
if (mutPosibility >= mut)
{
child = Mutation(child);
}
population[currentPopSize] = child;
currentPopSize++;
}
progress = currentPopSize * 100/population.Length;
this.Progress = progress;
GC.Collect();
}
if (GACompleted != null)
GACompleted(this, EventArgs.Empty);
});
}
在這裏「葉利茨」是具有比人口的平均擬合值擬合值的chromosoms。
你在模擬中有多少個城市? – giacomelli
只有7個城市。更有趣的一點是。我是否錯誤地將所有染色體添加到羣體中,而不管它有好還是壞的價值?在新的人羣中更換舊染色體和不染色體的兒童染色體是否更好?通過這樣做,我認爲我可以使用尺寸較小的陣列。 –
你爲什麼叫GC.Collect?它不應該在那裏,試着刪除它。 –