我正在为一个项目工作。我正在尝试使用GA解决旅行商问题。我使用array[]
来存储数据,我认为Array
比List
快得多。但无论如何,它需要花费太多时间。例如使用MaxPopulation = 100000
,StartPopulation=1000
,该计划将持续约1分钟。我想知道这是不是一个问题。如果是,我该如何解决这个问题?
我的实施中的代码部分:
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);
});
}
在这里,“elits”是具有比人口平均拟合值更大拟合值的染色体。
答案 0 :(得分:1)
科学论文表明人口较少。也许你应该遵循其他作者的写作。拥有大量人口并不会给你带来任何好处。
TSP可以通过GA解决,但也许它不是解决此问题的最有效方法。看看TSP-GA的这种视觉表示:http://www.obitko.com/tutorials/genetic-algorithms/tsp-example.php
答案 1 :(得分:0)
确定。我刚刚找到了解决方案。不要使用大小为maxPopulation的数组,而是使用健康状况不佳的老一代改变新一代。现在,我正在使用一个体积较小的数组,其长度为10000.之前的长度为1,000.000并且花费了太多时间。现在,在每次迭代中,选择最好的1000条染色体并使用这些染色体创建新的染色体作为父母,并替换旧的和坏的染色体。这很完美。
代码示例:
public void StartAsync()
{
CreatePopulation(); //Creates chromosoms for starting
currentProducedPopSize = popNumber; //produced chromosom number, starts with the length of the starting population
while (currentProducedPopSize < maxPopNumber && !stopped)
{
Tour[] elits = ElitChromosoms();//Gets best 1000 chromosoms
Array.Reverse(population);//Orders by descending
this.Best = elits[0];
//Create new chromosom as many as the number of bad chromosoms
for (int i = 0; i < population.Length - elits.Length; i++)
{
if (currentProducedPopSize >= maxPopNumber || stopped)
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[i] = child;//Replace new chromosoms
currentProducedPopSize++;//Increase produced chromosom number
}
progress = currentProducedPopSize * 100 / maxPopNumber;
this.Progress = progress;
GC.Collect();
}
stopped = false;
this.Best = population[population.Length - 1];
if (GACompleted != null)
GACompleted(this, EventArgs.Empty);
}
Tour[] ElitChromosoms()
{
Array.Sort(population);
Tour[] elits = new Tour[popNumber / 10];
Array.Copy(population, elits, elits.Length);
return elits;
}