Пример #1
0
    private SalesmanGeneration CreateRandomGeneration()
    {
      var generation = new SalesmanGeneration { PopulationInGeneration = _populationInGeneration };

      for (int i = 0; i < generation.PopulationInGeneration; ++i)
      {
        generation.Add(new SalesmanPhenotype(GenotypeEro.RandomGenotype(_cities.Count)));
      }

      generation.Items.Sort();

      return generation;
    }
Пример #2
0
    private SalesmanGeneration CreateGenerationFromClostestNeighbour()
    {
      var route = new ClosestNeighbour(_cities, Distances).FindBest();


      var pointList = route.Select(x => _cities[x]).ToList();

      new Task(() => new Graphics.MapDrawer(_logDirectory + "Initial state - closest neighbour.png",
                                            pointList.Select(x => new System.Drawing.Point((int)x.X, (int)x.Y)),
                                            4000)).Start();

      var generation = new SalesmanGeneration { PopulationInGeneration = _populationInGeneration };
      for (int i = 0; i < generation.PopulationInGeneration; ++i)
      {
        generation.Add(new SalesmanPhenotype(new GenotypeEro((int[])route.Clone())));
      }
      return generation;
    }
Пример #3
0
    private void BestSolutionGenotype3(SalesmanGeneration generation)
    {
      for (int generationCount = 0; _stopwatch.Elapsed < TimeSpan.FromMinutes(_minutesToRun); ++generationCount)
      {
        if (ShortestPath > generation.Items.Last().GenotypeCollection.First().Distance)
        {
          UpdateShortestPath(generation, generationCount);
        }
        else if (generationCount % 10 == 0)
        {
          Console.WriteLine(generationCount);
        }

        var newGeneration = new SalesmanGeneration();
        for (int j = 0; newGeneration.Items.Count < _numberOfRecombinations * generation.Items.Count; ++j)
        {
          GenotypeEro genotype1 = (GenotypeEro)generation.Items[generation.Items.Count - j - 1].GenotypeCollection[0],
                      genotype2 = (GenotypeEro)generation.Items[generation.Items.Count - j - 2].GenotypeCollection[0];

          var randomGenotypes = genotype1.Recombination(genotype2);
          foreach (SalesmanGenotype genotype in randomGenotypes)
          {
            newGeneration.Add(new SalesmanPhenotype(genotype));
          }
        }
        for (int j = 0; newGeneration.Items.Count < generation.Items.Count; ++j)
        {
          for (int mutation = 0; mutation < _numberOfMutations; ++mutation)
          {
            var genotype = (GenotypeEro)generation.Items[generation.Items.Count - j - 1 - mutation].GenotypeCollection[0];
            newGeneration.Add(new SalesmanPhenotype(genotype.Mutate()));
          }
        }

        generation = newGeneration;
        generation.Items.Sort();

      }
    }