2013-04-23 57 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