简体   繁体   English

C# Arrays列表

[英]C# List of Arrays

I'm working on a algorithm that will have a list of populations which will then each have a list or array of 60 weights that are randomly generated numbers.我正在研究一种算法,该算法将有一个人口列表,然后每个人口都有一个由 60 个权重组成的列表或数组,这些权重是随机生成的数字。

As you can see in my code i have a list which stores 60 random values.正如您在我的代码中看到的,我有一个存储 60 个随机值的列表。 I now need to take these values and assign them to an list of population members.我现在需要获取这些值并将它们分配给人口成员列表。 I'm having trouble finding out how i can add random weights to either my population list or testing list.我无法找出如何将随机权重添加到我的人口列表或测试列表中。 Population would be size 60 and all would have 60 random weights.人口规模为 60,所有人都有 60 个随机权重。

        List<double> randomWeights = new List<double>();
        List<List<double>> population = new List<List<double>>(); 
        List<double[]>[] testing = new List<double[]>[60];

        //These represent random weights 
        for (int i = 0; i < 60; i++)
        {
            randomWeights.Add(((_rand.NextDouble() * 2) - 1 ));
        }

        for (int i = 0; i < 60; i++)
        {
            testing[i] = new List<double[]>();
        }

        //See how these weights perform on the task when applied to the network
        double T = GetResults(randomWeights);

I was trying to do it as a list inside of a list however my lecture told me it would be better to have an array inside of a list.我试图将其作为列表中的列表来执行,但是我的讲座告诉我,在列表中包含一个数组会更好。 Any help would be appreciated.任何帮助,将不胜感激。

If you in advance know that your population size will be 60 elements and every population will have 60 random weights elements the more effective way is to use the jagged array than lists for this purpose.如果您事先知道您的人口规模将是 60 个元素,并且每个人口将有 60 个随机权重元素,那么更有效的方法是为此目的使用锯齿状数组而不是列表。 The code example is below:代码示例如下:

class Program
{
    static void Main(string[] args)
    {
        Random random = new Random();

        double [][]population = new double[60][];

        for (int populationIndex = 0; populationIndex < 60; populationIndex++)
        {
            double[] randomWeights = new double[60];
            for (int i = 0; i < 60; i++)
            {
                randomWeights[i] = random.NextDouble() * 2 - 1;
            }
            population[populationIndex] = randomWeights;
        }

    }
}

Your question isn't very clear, from what I understood the population list is essentially a list of lists, where each item contains the randomWeights values?你的问题不是很清楚,据我了解,人口列表本质上是一个列表列表,其中每个项目都包含 randomWeights 值? And it's size must be 60?它的大小必须是60?

If so, once you've generated the randomWeights list, you could instantiate and initialize the population list using Enumerable.Repeat() , which will duplicate the passed argument ( randomWeights ) n times ( 60 )如果是这样,一旦您生成了randomWeights列表,您可以使用Enumerable.Repeat()实例化和初始化population列表,这将复制传递的参数 ( randomWeights ) n 次 ( 60 )

Here's a way of generating the randomWeights list, although it seems you did that just fine这是一种生成randomWeights列表的方法,尽管您似乎做得很好

Random r = new Random();
List<double> randomWeights = new List<double>(new double[60].Select(x => r.NextDouble() * 2 - 1));

And this is the aforementioned population list initialization这就是前面提到的population列表初始化

List<List<double>> population = new List<List<double>>(Enumerable.Repeat(randomWeights, 60));

As for population being a list of arrays instead of a list of lists , in my opinion, it doesn't really matter in this particular case, but if you must make it so, I will edit my code.至于populationlist of arrays而不是list of lists ,在我看来,在这种特殊情况下并不重要,但如果你必须这样做,我将编辑我的代码。

I managed to get this working by myself.我设法自己完成了这项工作。 I found that i can create an array of lists with the array being the size of my population and then each list being the weights(random).我发现我可以创建一个列表数组,该数组是我的人口大小,然后每个列表都是权重(随机)。 So now i have 60 population and all of them have 60 weights.所以现在我有 60 个人口,他们都有 60 个权重。

        var populationSize = 60;
        var weights = 60;

        List<double>[] populationWeights = new List<double>[populationSize];
        List<double> results = new List<double>();

        for (int i = 0; i < populationSize; i++)
        {
            populationWeights[i] = new List<double>();
            for(int j = 0; j < weights; j++)
            {
                populationWeights[i].Add(((_rand.NextDouble() * 2) - 1));
            }
        }
        //See how these weights perform on the task when applied to the network
        for(int i = 0; i < populationWeights.Length; i++)
        {
            results.Add(GetResults(populationWeights[i]));
            Console.WriteLine(results[i]);
        }
        Console.ReadLine();

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

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