繁体   English   中英

指数数组的边界之外。 C#

[英]Index was outside the bounds of the array. C#

我有一个遗传算法Roullete Wheel的代码。 当我的迭代次数超过1000次时,我的程序显示错误,但是当迭代次数少于1000次时,它的效果很好。这是我的代码

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];
                }

您获得了超出范围的索引,因为遍历每个项目时,您是在循环中引用i + 1 因此,您将尝试访问不存在的最后一个迭代probKumulatif[probKumulatif.Length] 尝试循环到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];
    }
}

您还使用i而不是n引用resultRil 如果您的意思是n那么在访问resultRil[i + 1]可以像上面一样应用

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

您可能需要在内循环中使用n引用resultRil

pilih = resultRil[n + 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为true,则将probKumulatif[i + 1] > rnd ,由于i == probKumulatif.Length - 1 ,则i + 1 == probKumulatif.Length因此,您尝试访问probKumulatif[probKumulatif.Length] ,这会导致您的异常!

请注意,由于rndrandom ,因此仅在某些情况下才会发生,您运行它的次数越多,发生的可能性就越大!

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

发生错误是因为您尝试访问超出其范围的索引处的数组。 您只需检查以下内容:

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

但我建议改用for循环:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM