Exemplo n.º 1
0
 //randomly swap genes(cities)
 private static void Mutate(Route tour)
 {
     for (int tourPos1 = 0; tourPos1 < tour.tour.Count; tourPos1++)
     {
         if (rnd.Next(RouteManager.NumberOfCities) < mutationRate)
         {
             var tourPos2 = rnd.Next(RouteManager.NumberOfCities);
             var city1    = tour.GetCity(tourPos1);
             var city2    = tour.GetCity(tourPos2);
             tour.SetCity(tourPos2, city1);
             tour.SetCity(tourPos1, city2);
         }
     }
 }
Exemplo n.º 2
0
        //Randomly get cities from first parent and insert them into the child
        //If there are no cities fill them with the cities from second parent
        private static Route Crossover(Route parent1, Route parent2)
        {
            var child = new Route();

            var startPos = rnd.Next(RouteManager.NumberOfCities);
            var endPos   = rnd.Next(RouteManager.NumberOfCities);


            for (int i = 0; i < RouteManager.NumberOfCities; i++)
            {
                if (startPos < endPos && i > startPos && i < endPos)
                {
                    child.SetCity(i, parent1.GetCity(i));
                }
                else if (startPos > endPos)
                {
                    if (!(i < startPos && i > endPos))
                    {
                        child.SetCity(i, parent1.GetCity(i));
                    }
                }
            }


            for (int i = 0; i < parent2.tour.Count; i++)
            {
                if (!child.ContainsCity(parent2.GetCity(i)))
                {
                    for (int j = 0; j < RouteManager.NumberOfCities; j++)
                    {
                        //fill the empty genes with the genes of the second parent
                        if (child.GetCity(j) == null)
                        {
                            child.SetCity(j, parent2.GetCity(i));
                            break;
                        }
                    }
                }
            }

            return(child);
        }