public static PotvinPDExchangeMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand)
        {
            List <int> cities = new List <int>();

            IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

            for (int i = 1; i <= individual.Cities; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    cities.Add(i);
                }
            }

            if (cities.Count > 1 && individual.Tours.Count > 1)
            {
                PotvinPDExchangeMove move = null;
                while (cities.Count > 1 && move == null)
                {
                    int  city         = cities[rand.Next(cities.Count)];
                    Tour oldTour      = individual.Tours.Find(t => t.Stops.Contains(city));
                    int  oldTourIndex = individual.Tours.IndexOf(oldTour);

                    int max = individual.Tours.Count - 1;

                    int newTourIndex = rand.Next(max);
                    if (newTourIndex >= oldTourIndex)
                    {
                        newTourIndex++;
                    }

                    Tour       newTour    = individual.Tours[newTourIndex];
                    List <int> tourCities = new List <int>();
                    foreach (int stop in newTour.Stops)
                    {
                        if (pdp == null ||
                            (pdp.GetDemand(stop) >= 0 &&
                             pdp.GetPickupDeliveryLocation(stop) != pdp.GetPickupDeliveryLocation(city) &&
                             pdp.GetPickupDeliveryLocation(stop) != city &&
                             pdp.GetPickupDeliveryLocation(city) != stop))
                        {
                            tourCities.Add(stop);
                        }
                    }

                    if (tourCities.Count > 0)
                    {
                        int replaced = tourCities[rand.Next(tourCities.Count)];
                        move = new PotvinPDExchangeMove(city, oldTourIndex, newTourIndex, replaced, individual);
                    }
                }

                return(move);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        protected override PotvinPDExchangeMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance)
        {
            List <PotvinPDExchangeMove>       result = new List <PotvinPDExchangeMove>();
            IPickupAndDeliveryProblemInstance pdp    = problemInstance as IPickupAndDeliveryProblemInstance;

            int max = individual.Tours.Count;

            if (individual.Tours.Count < problemInstance.Vehicles.Value)
            {
                max = max - 1;
            }

            for (int i = 0; i < individual.Tours.Count; i++)
            {
                for (int j = 0; j < individual.Tours[i].Stops.Count; j++)
                {
                    for (int k = 0; k <= max; k++)
                    {
                        if (k != i)
                        {
                            int city1 = individual.Tours[i].Stops[j];
                            if (pdp == null || pdp.GetDemand(city1) >= 0)
                            {
                                for (int l = 0; l < individual.Tours[k].Stops.Count; l++)
                                {
                                    int city2 = individual.Tours[k].Stops[l];
                                    if (pdp == null || pdp.GetDemand(city2) >= 0)
                                    {
                                        bool valid = pdp == null ||
                                                     (pdp.GetPickupDeliveryLocation(city2) != pdp.GetPickupDeliveryLocation(city1) &&
                                                      pdp.GetPickupDeliveryLocation(city2) != city1 &&
                                                      pdp.GetPickupDeliveryLocation(city1) != city2);

                                        if (valid)
                                        {
                                            PotvinPDExchangeMove move = new PotvinPDExchangeMove(
                                                city1, i, k, city2, individual);

                                            result.Add(move);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(result.ToArray());
        }
        protected override PotvinPDShiftMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance)
        {
            List <PotvinPDShiftMove>          result = new List <PotvinPDShiftMove>();
            IPickupAndDeliveryProblemInstance pdp    = problemInstance as IPickupAndDeliveryProblemInstance;

            int max = individual.Tours.Count;

            if (individual.Tours.Count >= problemInstance.Vehicles.Value)
            {
                max = max - 1;
            }

            for (int i = 0; i < individual.Tours.Count; i++)
            {
                for (int j = 0; j < individual.Tours[i].Stops.Count; j++)
                {
                    for (int k = 0; k <= max; k++)
                    {
                        if (k != i)
                        {
                            int city = individual.Tours[i].Stops[j];
                            if (pdp == null || pdp.GetDemand(city) >= 0)
                            {
                                PotvinPDShiftMove move = new PotvinPDShiftMove(
                                    city, i, k, individual);

                                result.Add(move);
                            }
                        }
                    }
                }
            }

            return(result.ToArray());
        }
Exemple #4
0
        public static PotvinPDRearrangeMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand)
        {
            List <int> cities = new List <int>();

            IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

            for (int i = 1; i <= individual.Cities; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    cities.Add(i);
                }
            }

            if (cities.Count > 0)
            {
                int city = cities[rand.Next(cities.Count)];
                int tour = individual.Tours.FindIndex(t => t.Stops.Contains(city));
                return(new PotvinPDRearrangeMove(city, tour, individual));
            }
            else
            {
                return(null);
            }
        }
        protected override PotvinPDRearrangeMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance)
        {
            List <PotvinPDRearrangeMove>      result = new List <PotvinPDRearrangeMove>();
            IPickupAndDeliveryProblemInstance pdp    = problemInstance as IPickupAndDeliveryProblemInstance;

            for (int i = 1; i <= problemInstance.Cities.Value; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    int tour = individual.Tours.FindIndex(t => t.Stops.Contains(i));

                    result.Add(new PotvinPDRearrangeMove(i, tour, individual));
                }
            }

            return(result.ToArray());
        }
        public static PotvinPDShiftMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand)
        {
            List <int> cities = new List <int>();

            IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

            for (int i = 1; i <= individual.Cities; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    cities.Add(i);
                }
            }

            if (cities.Count >= 1)
            {
                int  city         = cities[rand.Next(cities.Count)];
                Tour oldTour      = individual.Tours.Find(t => t.Stops.Contains(city));
                int  oldTourIndex = individual.Tours.IndexOf(oldTour);

                int max = individual.Tours.Count;
                if (individual.Tours.Count >= problemInstance.Vehicles.Value)
                {
                    max = max - 1;
                }

                int newTourIndex = rand.Next(max);
                if (newTourIndex >= oldTourIndex)
                {
                    newTourIndex++;
                }

                return(new PotvinPDShiftMove(city, oldTourIndex, newTourIndex, individual));
            }
            else
            {
                return(null);
            }
        }
Exemple #7
0
        public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows)
        {
            PotvinEncoding result = new PotvinEncoding(instance);

            IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

            List <int> customers = new List <int>();

            for (int i = 1; i <= instance.Cities.Value; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    customers.Add(i);
                }
            }

            customers.Sort((city1, city2) => {
                double angle1 = CalculateAngleToDepot(instance, city1);
                double angle2 = CalculateAngleToDepot(instance, city2);

                return(angle1.CompareTo(angle2));
            });

            Tour currentTour = new Tour();

            result.Tours.Add(currentTour);

            int j = random.Next(customers.Count);

            for (int i = 0; i < customers.Count; i++)
            {
                int index = (i + j) % customers.Count;

                int stopIdx = 0;
                if (currentTour.Stops.Count > 0)
                {
                    stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
                }
                currentTour.Stops.Insert(stopIdx, customers[index]);

                if (pdp != null)
                {
                    stopIdx = result.FindBestInsertionPlace(currentTour, pdp.GetPickupDeliveryLocation(customers[index]));
                    currentTour.Stops.Insert(stopIdx, pdp.GetPickupDeliveryLocation(customers[index]));
                }

                CVRPEvaluation evaluation = instance.EvaluateTour(currentTour, result) as CVRPEvaluation;
                if (result.Tours.Count < instance.Vehicles.Value &&
                    ((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon)))
                {
                    currentTour.Stops.Remove(customers[index]);
                    if (pdp != null)
                    {
                        currentTour.Stops.Remove(pdp.GetPickupDeliveryLocation(customers[index]));
                    }

                    if (currentTour.Stops.Count == 0)
                    {
                        result.Tours.Remove(currentTour);
                    }
                    currentTour = new Tour();
                    result.Tours.Add(currentTour);

                    currentTour.Stops.Add(customers[index]);
                    if (pdp != null)
                    {
                        currentTour.Stops.Add(pdp.GetPickupDeliveryLocation(customers[index]));
                    }
                }
            }

            if (currentTour.Stops.Count == 0)
            {
                result.Tours.Remove(currentTour);
            }

            return(result);
        }
Exemple #8
0
        private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible)
        {
            individual = individual.Clone() as PotvinEncoding;
            IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

            int replacedDest = pdp.GetPickupDeliveryLocation(replaced);
            int replacedSource, replacedTarget;

            if (pdp.GetDemand(replaced) >= 0)
            {
                replacedSource = replaced;
                replacedTarget = replacedDest;
            }
            else
            {
                replacedSource = replacedDest;
                replacedTarget = replaced;
            }
            Tour replacedSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacedSource));
            Tour replacedTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacedTarget));

            int replacingDest = pdp.GetPickupDeliveryLocation(replacing);
            int replacingSource, replacingTarget;

            if (pdp.GetDemand(replacing) >= 0)
            {
                replacingSource = replacing;
                replacingTarget = replacingDest;
            }
            else
            {
                replacingSource = replacingDest;
                replacingTarget = replacing;
            }
            Tour replacingSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacingSource));
            Tour replacingTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacingTarget));

            replacingSourceTour.Stops.Remove(replacingSource);
            replacingTargetTour.Stops.Remove(replacingTarget);

            replacedSourceTour.Stops[replacedSourceTour.Stops.IndexOf(replacedSource)] = replacingSource;
            if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
            {
                return(null);
            }

            replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
            if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
            {
                return(null);
            }

            double bestQuality        = double.MaxValue;
            int    bestTour           = -1;
            int    bestPositionSource = -1;
            int    bestPositionTarget = -1;

            int routeToAvoid = individual.Tours.IndexOf(replacingSourceTour);

            for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++)
            {
                if (tourIdx != routeToAvoid)
                {
                    Tour          tour = individual.Tours[tourIdx];
                    VRPEvaluation eval = instance.EvaluateTour(tour, individual);
                    individual.InsertPair(tour, replacedSource, replacedTarget, instance);
                    VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);

                    double delta = evalNew.Quality - eval.Quality;

                    if (delta < bestQuality &&
                        (instance.Feasible(evalNew) || allowInfeasible))
                    {
                        bestQuality        = delta;
                        bestTour           = tourIdx;
                        bestPositionSource = tour.Stops.IndexOf(replacedSource);
                        bestPositionTarget = tour.Stops.IndexOf(replacedTarget);
                    }

                    tour.Stops.Remove(replacedSource);
                    tour.Stops.Remove(replacedTarget);
                }
            }

            if (bestTour != -1)
            {
                if (bestPositionTarget < bestPositionSource)
                {
                    individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
                    individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
                }
                else
                {
                    individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
                    individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
                }

                return(individual);
            }
            else
            {
                return(null);
            }
        }