我有一个关于我的遗传算法输出的问题我正在创建。当我运行项目时,与在特定位置调试时相比,我似乎得到了不同的输出。
我的部分代码如下:
Population mutatePopulation(Population pop)
{
foreach(Chromosome x in pop.population)
{
x.mutateChromosome(x);
}
return pop;
}
在上面的代码中,我希望我的一些Chromosome对象发生变异。这可以通过以下方法完成:
public Chromosome mutateChromosome(Chromosome x)
{
Chromosome result = x;
//SWAP mutation
Random rnd = new Random();
double value = rnd.NextDouble();
if (value < MUTATION_RATE)
{
int index1 = (int)rnd.Next(0, x.customerSequence.Count() - 1);
int index2 = (int)rnd.Next(0, x.customerSequence.Count() - 1);
Console.WriteLine( "Muation at chromosome: " + x.ToString() + ", mutation at indexes [" + index1 + "," + index2+
"], values ["+x.customerSequence[index1].index+","+x.customerSequence[index2].index+"]");
Customer cust1 = x.customerSequence[index1];
Customer cust2 = x.customerSequence[index2];
result.customerSequence[index1] = cust2;
result.customerSequence[index2] = cust1;
}
return result;
}
当我运行我的项目时(有时,取决于随机变量)得到以下输出:
Generation #14
10 12 9 5 4 2 8 13 6 3 7 11 || Fitness: 3209
11 6 12 3 5 9 10 4 7 2 8 13 || Fitness: 3252
13 8 11 4 10 3 6 5 9 2 12 7 || Fitness: 3301
10 9 4 11 6 13 12 3 5 8 2 7 || Fitness: 3315
7 3 9 10 11 4 13 8 6 2 5 12 || Fitness: 3354
6 10 4 8 13 2 7 12 3 5 11 9 || Fitness: 3361
10 7 13 5 8 9 3 6 11 4 2 12 || Fitness: 3394
11 12 5 8 10 4 9 13 2 7 3 6 || Fitness: 3499
11 13 12 9 3 4 7 5 10 6 2 8 || Fitness: 3708
8 9 6 5 2 13 11 7 10 3 4 12 || Fitness: 3819
Generation #15
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [2,13]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [9,4]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [3,5]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [13,3]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [4,8]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [2,12]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [9,6]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [4,13]
Muation at chromosome: XML.Chromosome, mutation at indexes [5,7], values [4,5]
Muation at chromosome: XML.Chromosome, mutation at indexes [1,6], values [9,11]
13 8 11 4 10 5 6 3 9 2 12 7 || Fitness: 3025
11 6 12 3 5 4 10 9 7 2 8 13 || Fitness: 3088
10 7 13 5 8 6 3 9 11 4 2 12 || Fitness: 3112
10 12 9 5 4 13 8 2 6 3 7 11 || Fitness: 3315
6 10 4 8 13 12 7 2 3 5 11 9 || Fitness: 3381
11 13 12 9 3 5 7 4 10 6 2 8 || Fitness: 3689
10 9 4 11 6 3 12 13 5 8 2 7 || Fitness: 3730
7 3 9 10 11 8 13 4 6 2 5 12 || Fitness: 3733
11 12 5 8 10 13 9 4 2 7 3 6 || Fitness: 3755
8 11 6 5 2 13 9 7 10 3 4 12 || Fitness: 3808
似乎当一个染色体发生突变时,所有其他染色体都会发生变异。
然而,当我调试时,我得到了我需要的输出:
Generation #2
2 13 3 12 7 5 4 10 11 9 8 6 || Fitness: 2823
13 6 11 5 2 9 4 10 7 8 12 3 || Fitness: 3019
4 13 12 8 6 10 7 3 5 11 9 2 || Fitness: 3254
3 7 11 4 2 8 9 10 13 5 6 12 || Fitness: 3267
3 4 2 8 9 5 11 7 6 10 13 12 || Fitness: 3309
2 7 5 8 9 4 10 3 6 12 13 11 || Fitness: 3448
12 4 2 5 8 10 6 13 11 3 9 7 || Fitness: 3484
7 12 8 11 5 3 10 13 2 9 6 4 || Fitness: 3712
11 7 5 6 4 3 12 13 2 9 8 10 || Fitness: 3775
2 6 12 10 11 13 3 4 8 9 7 5 || Fitness: 3846
Generation #3
Muation at chromosome: XML.Chromosome, mutation at indexes [1,3], values [6,5]
Muation at chromosome: XML.Chromosome, mutation at indexes [8,7], values [13,10]
Muation at chromosome: XML.Chromosome, mutation at indexes [8,8], values [6,6]
Muation at chromosome: XML.Chromosome, mutation at indexes [4,6], values [9,10]
2 13 3 12 7 5 4 10 11 9 8 6 || Fitness: 2823
3 7 11 4 2 8 9 13 10 5 6 12 || Fitness: 3249
4 13 12 8 6 10 7 3 5 11 9 2 || Fitness: 3254
3 4 2 8 9 5 11 7 6 10 13 12 || Fitness: 3309
13 5 11 6 2 9 4 10 7 8 12 3 || Fitness: 3434
2 7 5 8 10 4 9 3 6 12 13 11 || Fitness: 3443
12 4 2 5 8 10 6 13 11 3 9 7 || Fitness: 3484
7 12 8 11 5 3 10 13 2 9 6 4 || Fitness: 3712
11 7 5 6 4 3 12 13 2 9 8 10 || Fitness: 3775
2 6 12 10 11 13 3 4 8 9 7 5 || Fitness: 3846
有人可以帮我解决问题吗?我预感它与我正在使用的Random对象有关,但我似乎无法弄明白。 谢谢!
(PS。这是我第一次在这里问自己的问题,很抱歉,如果我不符合规则)
答案 0 :(得分:2)
问题是你在mutate方法中创建了new Random()
。不要这样做。如果你足够快地突变足够的染色体,每个Random
实例将获得相同的值,并产生相同的随机数序列。
相反,创建Random
的单个实例并将其用于所有染色体。例如:
Population mutatePopulation(Population pop, Random random)
{
foreach(Chromosome x in pop.population)
{
x.mutateChromosome(x, random);
}
return pop;
}
public Chromosome mutateChromosome(Chromosome x, Random rnd)
{
Chromosome result = x;
//SWAP mutation
double value = rnd.NextDouble();
if (value < MUTATION_RATE)
{
int index1 = (int)rnd.Next(0, x.customerSequence.Count() - 1);
int index2 = (int)rnd.Next(0, x.customerSequence.Count() - 1);
Console.WriteLine( "Muation at chromosome: " + x.ToString() + ", mutation at indexes [" + index1 + "," + index2+
"], values ["+x.customerSequence[index1].index+","+x.customerSequence[index2].index+"]");
Customer cust1 = x.customerSequence[index1];
Customer cust2 = x.customerSequence[index2];
result.customerSequence[index1] = cust2;
result.customerSequence[index2] = cust1;
}
return result;
}
在某些时候,这个单个随机实例将被创建并存储在一个字段中。我不知道在没有理解你创建的体系结构的情况下,最好的位置在哪里,所以我已经将随机实例移动到要传递给需要随机性的方法的参数。