与调试相比不同的输出版本
Different output release compared to debug
我对我正在创建的遗传算法的输出有疑问。与在特定位置进行调试相比,当我只是 运行 我的项目时,我似乎得到了不同的输出。
我的部分代码如下所示:
Population mutatePopulation(Population pop)
{
foreach(Chromosome x in pop.population)
{
x.mutateChromosome(x);
}
return pop;
}
在上面的代码中,我希望我的一些染色体对象发生变异。这是通过以下方法完成的:
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。第一次在这里提出自己的问题,如有不妥之处请见谅)
问题是您在 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;
}
在某个时候,这个单一的随机实例将被创建并存储在一个字段中。如果不了解您创建的架构,我不能说最好的地方在哪里,所以我将随机实例移动为一个参数,传递给需要随机性的方法。
我对我正在创建的遗传算法的输出有疑问。与在特定位置进行调试相比,当我只是 运行 我的项目时,我似乎得到了不同的输出。
我的部分代码如下所示:
Population mutatePopulation(Population pop)
{
foreach(Chromosome x in pop.population)
{
x.mutateChromosome(x);
}
return pop;
}
在上面的代码中,我希望我的一些染色体对象发生变异。这是通过以下方法完成的:
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。第一次在这里提出自己的问题,如有不妥之处请见谅)
问题是您在 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;
}
在某个时候,这个单一的随机实例将被创建并存储在一个字段中。如果不了解您创建的架构,我不能说最好的地方在哪里,所以我将随机实例移动为一个参数,传递给需要随机性的方法。