2013-04-23 33 views
0

我有遗传算法的Roullete Wheel的代码。我的程序显示错误时,我的迭代超过1000次迭代,但它工作得很好,当迭代低于1000 这里是我的代码索引超出了数组的范围。 C#

private double[] roulleteWheel(double[] nilaiFitnessRil) 
    { 
     double[] resultRW = new double[nilaiFitnessRil.GetLength(0)]; 
     double[] probKromosom = new double[nilaiFitnessRil.GetLength(0)]; 
     double[] probKumulatif = new double[nilaiFitnessRil.GetLength(0)]; 
     double pilih=0; 
     Random random = new Random(); 
     double rnd; 
     double total = 0; 
     double temp = 0; 
     //count total fitness 
     for (int i = 0; i < nilaiFitnessRil.Length; i++) 
     { 
      total += nilaiFitnessRil[i]; 

     } 
     listBox1.Items.Add(string.Format("total fitness adalah {0}",total)); 
     //count probability for each chromosome 
     listBox1.Items.Add("result of probabilty for each chromosome"); 
     for (int i = 0; i < nilaiFitnessRil.Length; i++) 
     { 
      probKromosom[i] = Math.Round(nilaiFitnessRil[i]/total, 4); 
      listBox1.Items.Add(probKromosom[i].ToString()); 
     } 
     //count cumulative probability 
     listBox1.Items.Add("result of cumulative probabilty "); 
     for (int i = 0; i < probKromosom.Length; i++) 
     { 
      temp += probKromosom[i]; 
      probKumulatif[i] = temp; 
      listBox1.Items.Add(probKumulatif[i].ToString()); 
     } 
     //selecting a chromosome by its cumulative probability with a random value 

     listBox1.Items.Add(" roullete wheel"); 
     for (int n = 0; n < resultRil.Length; n++) 
     { 
      rnd = random.NextDouble() * 1.0 - 0.0; 
      //listBox1.Items.Add(rnd.ToString()); 
      for (int i = 0; i < probKumulatif.Length; i++) 
      { 
      //this is where the Index was outside the bounds of the array appear 
       if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd) 
       { 
        pilih = resultRil[i + 1]; 
       } 
       else if (rnd <= probKumulatif[0]) 
       { 
        pilih = resultRil[0]; 
       } 


      } 
      resultRil[n] = pilih; 
      resultRW[n] = resultRil[n]; 
     } 
     PrintArray(resultRW, listBox1); 
      return resultRW; 
    } 

这是程序被终止指数的原因是该范围之外该阵列

if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd) 
       { 
        pilih = resultRil[i + 1]; 
       } 
       else if (rnd <= probKumulatif[0]) 
       { 
        pilih = resultRil[0]; 
       } 
+4

藏在你发布的任何问题吗? – 2013-04-23 03:31:27

+0

当迭代次数超过1000次时,程序总是终止,但是当它小于1000时,程序非常好。迭代我应该怎么做? – Christy 2013-04-23 03:36:26

回答

3

的你得到的索引越界,因为你指的是i + 1内环路,当你通过每一个项目循环。因此,您将尝试访问不存在的probKumulatif[probKumulatif.Length]的最后一次迭代。尝试使用i,而不是n循环通过对probKumulatif.Length - 1

// *** On this line *** 
for (int i = 0; i < probKumulatif.Length - 1; i++) 
{ 
    //this is where the Index was outside the bounds of the array appear 
    if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd) 
    { 
     pilih = resultRil[i + 1]; 
    } 
    else if (rnd <= probKumulatif[0]) 
    { 
     pilih = resultRil[0]; 
    } 
} 

你也提到resultRil。如果你的意思是n那么同样可以根据上面您访问resultRil[i + 1]

for (int n = 0; n < resultRil.Length - 1; n++) 
{ 
    ... 
} 

应用则可能需要使用nresultRil在内环

pilih = resultRil[n + 1]; 
+0

它的工作原理。非常感谢丹尼尔。即时引用resultRil,因为我想通过选择ProbKumulatif和rnd来选择resultRil。但我想我应该把probkumulatif和resultRil放在一个结构体中,这样我就可以在1个循环中选择这两个。你怎么看? – Christy 2013-04-23 03:51:54

+0

如果它们是同一个对象的一部分,那么将它们放在一起就好。 – 2013-04-23 04:24:35

+0

哦,好的,谢谢你的建议。 :) – Christy 2013-04-23 04:42:10

1
for (int i = 0; i < probKumulatif.Length; i++) 
{ 
    //this is where the Index was outside the bounds of the array appear 
    if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd) 

上会发生什么这个循环的最后一次迭代,当i == probKumulatif.Length - 1

如果probKumulatif[i] <= rnd是真的,那么probKumulatif[i + 1] > rnd将被评估,并自i == probKumulatif.Length - 1,然后i + 1 == probKumulatif.Length - 让您尝试访问probKumulatif[probKumulatif.Length],这会导致你的异常!

请注意,由于rnd随机,这只会有时发生,更有可能发生的次数越多!

+0

是啊当我== probKumulatif.Length - 1它的作品。非常感谢你Blorgbeard。 – Christy 2013-04-23 03:54:00

0

简单。因为在循环的最后一次运行中,probKumulatif [i + 1]试图达到不允许的probKumulatif [probKumulatif.Length]。您可以达到的最大指标是probKumulatif [probKumulatif.Length-1]。请在发布之前尝试调试您的代码。

+0

谢谢。有用。 – Christy 2013-04-23 03:54:30

0

发生该错误的原因是您尝试访问超出其范围的索引处的数组。 你可以简单地检查如下:

if (probKumulatif[i] <= rnd && (probKumulatif.Length >= i+1 && probKumulatif[i + 1] > rnd)) 
{ 
    pilih = resultRil[i + 1]; 
} 
else if (rnd <= probKumulatif[0]) 
{ 
    pilih = resultRil[0]; 
} 

,但我会建议对土长的循环insteads:

for (int i = 0; i < probKumulatif.Length-1; i++) 
{ 
//Keep it the same. 
} 
+0

谢谢你。有用。 :) – Christy 2013-04-23 04:01:48