我编写了以下代码来生成6条染色体的群体,其中每条染色体是5x5的数组。然后我用另一种方法打印每个染色体。问题是我每次都得到相同的数组!!
static List<int[,]> PopulationChromosomes = new List<int[,]>();
private void moveGenetic_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("C:/temp/intialPopulation.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter("C:/temp/listOfChromosomesForAllRounds.txt", true);
population = new int[6][,]; // jagged array
Random rnd = new Random(); // to gereate random number (either 0 or 1)
auvChromosomes = new int[5, 5];
for (int i = 0; i < population.Length; i++)
{
population[i] = new int[5, 5];
}
using (StreamWriter sw = new StreamWriter(fs))
{
for (int i = 0; i < population.Length; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
auvChromosomes[j, k] = rnd.Next(0, 2);
sw.Write("|" + auvChromosomes[j, k] + "|");
} // end-inner-for
sw.WriteLine();
} // end-outer-inner-for
PopulationChromosomes.Add(auvChromosomes);
Array.Clear(auvChromosomes, 0, auvChromosomes.Length);
} // end-outer-for
} // end-using
Chromosomes(PopulationChromosomes, 1);
}
答案 0 :(得分:1)
您的问题是您将int [,]视为值类型,而实际上它是一个引用类型。将它添加到列表中时
PopulationChromosomes.Add(auvChromosomes);
然后清除它
Array.Clear(auvChromosomes, 0, auvChromosomes.Length);
您正在清除刚刚放入列表中的实例。所以列表中的数组将充满零。
即使没有清楚,你也会一遍又一遍地添加相同的数组实例。您需要为每次迭代创建一个新数组。
解决方案:
在你的for循环中创建一个新的数组实例,跳过Array.Clear部分,如果你创建一个新实例,则不需要它
for (int i = 0; i < population.Length; i++)
{
auvChromosomes = new int[5, 5];
...
答案 1 :(得分:0)
@Michal S的回答已经解决了主要问题,但我也想帮你清理你的代码。
static List<int[,]> PopulationChromosomes = new List<int[,]>();
#region AUV Genetic Movement
private Random rnd = new Random(); // to generate random number (either 0 or 1)
private void moveGenetic_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter(new FileStream("C:/Users/Welcome/Desktop/intialPopulation.txt", FileMode.Append, FileAccess.Write)))
{
var population = new int[6][,]; // define this in the scope where you use it
for (int i = 0; i < population.Length; i++)
{
population[i] = new int[5, 5];
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
population[i][j, k] = rnd.Next(0, 2);
sw.Write("|{0}|", population[i][j, k]);
} // end-inner-for
sw.WriteLine();
} // end-outer-inner-for
PopulationChromosomes.Add(population[i]);
} // end-outer-for
} // end-using
Chromosomes(PopulationChromosomes, 1);
}
和
private const string lineSeparator = new string('-', 90);
public void Chromosomes(IEnumerable<int[,]> population, int round)
{
//////////////// print each chromosome matrix ////////////////
using (writer = new StreamWriter("C:/Users/Welcome/Desktop/listOfChromosomesForAllRounds.txt", true))
{
writer.WriteLine("{0}\n********************************* List of Chrmomsomes (Round : {1})**********************************\n{2}", lineSeparator, round, lineSeparator);
foreach (int[,] chromosme in population)
{
for (int h = 0; h < chromosme.GetLength(0); h++)
{
for (int k = 0; k < chromosme.GetLength(1); k++)
{
writer.Write("|{0}|", chromosme[h, k]);
}
writer.WriteLine();
}
writer.WriteLine(lineSeparator);
}
}
}
更深入一点,你真的应该有一个Chromesome
类来封装二维数组。