简体   繁体   中英

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.

As you can see in my code i have a list which stores 60 random values. 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.

        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. 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? And it's size must be 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 )

Here's a way of generating the randomWeights list, although it seems you did that just fine

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

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.

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.

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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