0

I have data set that I have generate every permutation, then check some properties on it to see if is an object that I want to keep and use. The number of permutations is staggering, in the quadrillions. Is there anything that you can see in the code below that I can use to speed this up? I suspect that I can't speed it up to a reasonable amount of time, so I'm also looking at possibly sharding it onto multiple servers to process, but I'm having a hard time deciding where to shard it.

Any opinions or ideas is appreciated.

 var boats = _warMachineRepository.AllBoats();
 var marines = _warMachineRepository.AllMarines();
 var bombers = _warMachineRepository.AllBombers().ToList();
 var carriers = _warMachineRepository.AllCarriers().ToList();
 var tanks = _warMachineRepository.AllTanks().ToList();
 var submarines = _warMachineRepository.AllSubmarines();



           var armies = new List<Army>();
           int processed = 0;
           Console.WriteLine((long)boats.Count*marines.Count*bombers.Count*carriers.Count*tanks.Count*submarines.Count);
           // 70k of these
           Parallel.ForEach(boats, new ParallelOptions(){MaxDegreeOfParallelism = Environment.ProcessorCount},boat =>
           {
                // 7500 of these
                foreach (var marine in marines)
                {
                     // 200 of these
                     foreach (var bomber in bombers)
                     {
                          // 200 of these
                          foreach (var carrier in carriers)
                          {
                               // 400 of these
                               foreach (var tank in tanks)
                               {
                                    // 50 of these
                                    foreach (var submarine in submarines)
                                    {
                                         var lineup = new Army()
                                         {
                                              Tank = tank,
                                              Submarine = submarine,
                                              Carrier = carrier,
                                              Marine = marine,
                                              Bomber = bomber,
                                              Boats = boat
                                         };

                                         if (army.Hitpoints > 50000)
                                         {
                                              lock (lockObject)
                                              {
                                                   armies.Add(lineup);
                                              }

                                         }

                                         processed++;

                                         if (processed%10000000 == 0)
                                         {
                                              Console.WriteLine("Processed: {0}, valid: {1}, DateTime: {2}", processed, armies.Count, DateTime.Now);
                                         }

                                    }
                               }
                          }
                     }
                }
           });



           return armies;
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • This is a variation of Counting Change problem. See http://rosettacode.org/wiki/Count_the_coins and http://stackoverflow.com/questions/4243831/how-to-count-possible-combination-for-coin-problem for more info and a sample code. – brz Sep 08 '14 at 03:58
  • What is (army.Hitpoints)? Is Hitpoints calculation intensive? If not then it might be a way to dropout of the loop faster. – CharlesNRice Sep 08 '14 at 15:46

1 Answers1

0

If this code is referring to a simulation you might want to add some optimizations by:

  • Mark an object as changed (put it in a list) when it changes so there is no need to search multiple times
  • Decrease/throttle/tune the object update frequency
  • Use other information available to filter objects: are objects close to one another so they might affect/hurt/heal each other -> only then investigate changes
  • Change the data structure; by putting all attributes of all objects in a smartly setup matrix you might be able to use simple matrix multiplication to have the object interact. You might even be able to offload the multiplication to the GPU
  • You might be asking too much: so scale out by using more nodes/machines.
Emond
  • 50,210
  • 11
  • 84
  • 115