示例#1
0
        private static Tour Crossover(Tour parent1, Tour parent2)
        {
            Tour   child  = new Tour();
            Random random = new Random();

            double startPos = random.Next(0, 11) / 10.0 * parent1.TourSize();
            double endPos   = random.Next(0, 11) / 10.0 * parent1.TourSize();

            for (int i = 0; i < child.TourSize(); 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));
                    }
                    ;
                }
            }

            //Loop throught parent2's city tour
            for (int i = 0; i < parent2.TourSize(); i++)
            {
                if (!child.ContainsCity(parent2.GetCity(i)))
                {
                    for (int j = 0; j < child.TourSize(); j++)
                    {
                        if (child.GetCity(j) == null)
                        {
                            child.SetCity(j, parent2.GetCity(i));
                            break;
                        }
                    }
                }
            }

            return(child);
        }
        private static Tour Crossover(Tour parent1, Tour parent2)
        {
            Tour   child  = new Tour();
            Random random = new Random();

            double startPos = random.Next(0, 11) / 10.0 * parent1.TourSize();
            double endPos   = random.Next(0, 11) / 10.0 * parent1.TourSize();

            for (int i = 0; i < child.TourSize(); i++)
            {
                if (startPos < endPos && i > startPos && i < endPos)
                {
                    if (i != 0)
                    {
                        child.trucksLoad[parent1.GetCity(i).Truck] -= parent1.GetCity(i).Weight;
                    }
                    child.SetCity(i, parent1.GetCity(i));
                }
                else if (startPos > endPos)
                {
                    if (!(i < startPos && i > endPos))
                    {
                        if (i != 0)
                        {
                            child.trucksLoad[parent1.GetCity(i).Truck] -= parent1.GetCity(i).Weight;
                        }
                        child.SetCity(i, parent1.GetCity(i));
                    }
                    ;
                }
            }

            //Loop throught parent2's city tour
            for (int i = 0; i < parent2.TourSize(); i++)
            {
                if (!child.ContainsCity(parent2.GetCity(i)))
                {
                    for (int j = 0; j < child.TourSize(); j++)
                    {
                        if (child.GetCity(j) == null)
                        {
                            AlgCity city    = new AlgCity(parent2.GetCity(i));
                            Boolean choosed = false;
                            while (!choosed)
                            {
                                city.Truck = rand.Next(1, 4);
                                if (child.trucksLoad[city.Truck] >= city.Weight)
                                {
                                    if (i != 0)
                                    {
                                        child.trucksLoad[city.Truck] -= city.Weight;
                                    }
                                    child.SetCity(j, city);

                                    choosed = true;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            return(child);
        }