1  // N Queens                Bill Rubin         June 29, 2010
   2  using System;
   3  
   4  class Program {
   5      /* Input arguments are numbers of queens:
   6       *     If none, execute for default range (see below).
   7       *     If one, output detail for each generation.
   8       *     If more than one, output only summary for each number of queens.
   9      */
  10      static void Main(string[] args) {
  11          Timer timer = new Timer();
  12          try {
  13              // Parse input arguments (number of queens):
  14              byte [] inputs = new byte[args.Length];
  15              for(int k = 0; k<inputs.Length; k++) {
  16                  int N = Convert.ToInt32(args[k]);
  17                  if (N < 1 || N > 255) throw new Exception("Number of queens must be positive and less than 256.");
  18                  inputs[k] = (byte)N;
  19                  }
  20  
  21              if (inputs.Length == 0) {  // Default range:
  22                  int first = 1;
  23                  int numRuns = 150;
  24                  int last1 = first + numRuns;
  25                  inputs = new byte[numRuns];
  26                  for(int k = first; k<last1; k++) inputs[k - first] = (byte)k;
  27                  }                
  28  
  29              Console.Write("Execution at " + DateTime.Now + "\n\n");
  30              Console.Write(Population.parms_);
  31              multipleMode_ = inputs.Length > 1;
  32              if (multipleMode_) Console.Write(multipleHeadings_);   
  33  
  34              // Execute each number of queens:
  35              for(int k = 0; k<inputs.Length; k++) {
  36                  Genome.N_ = inputs[k];
  37  
  38                  if (!multipleMode_) {  // Blurt details:
  39                      Console.Write(Genome.parameters());
  40                      Console.Write(singleHeadings_);
  41                      }
  42  
  43                  Population population = new Population();  // Create initial population
  44                  while (!population.containsSolution() && population.getNumberOfGenerations() < maxNumGenerations_) 
  45                      population.breed();  // Create next generation
  46                  if(!population.containsSolution()) Console.WriteLine(String.Format(
  47                      "{0,4:0}   --------- No solution found after {1:###,##0} generations -----------------", 
  48                      Genome.N_, maxNumGenerations_));  // Blurt if failure to converge.
  49                  population.terminate();
  50                  }
  51              }
  52  
  53          catch(Exception x) {Console.WriteLine(x.Message);}
  54  
  55          Console.WriteLine("\nTotal program elapsed time:  " + timer + " S" 
  56              + ((timer.getSeconds()<60)?"":String.Format(" = {0:0.0} minutes\n", timer.getSeconds()/60)));
  57          }
  58  
  59      public static bool multipleMode_;
  60  
  61      public static readonly uint maxNumGenerations_ = 1000;
  62  
  63      private static readonly string singleHeadings_ = 
  64          "\t\t  Fitness Function\n\t\t-------------------\nGeneration\tMin\tMean\tMax\tInbreeding\n";
  65  
  66      private static readonly string multipleHeadings_ =
  67            "                Total Effort                 Last Generation                     Effort Per Bit of Genome\n"
  68          + "       -----------------------------  ---------------------------   Maximum   ------------------------------\n"
  69          + "Number            Number   Number                       Duplicate  Duplicate             Number   Number\n"
  70          + "  of    Execution   of       of         Mean    Worst    Genomes    Genomes   Execution    of       of\n"
  71          + "Queens  Time (S) Genomes Generations  Fitness  Fitness  (percent)  (percent)   Time (mS) Genomes Generations\n"
  72          + "------ --------- ------- -----------  -------  -------  ---------  ---------  ---------- ------- -----------\n";
  73  }