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);
            }
        }
        public static void Apply(PotvinEncoding solution, PotvinPDExchangeMove move, IVRPProblemInstance problemInstance)
        {
            if (move.Tour >= solution.Tours.Count)
            {
                solution.Tours.Add(new Tour());
            }
            Tour tour = solution.Tours[move.Tour];

            Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));

            oldTour.Stops.Remove(move.City);

            if (problemInstance is IPickupAndDeliveryProblemInstance)
            {
                IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

                int  location = pdp.GetPickupDeliveryLocation(move.City);
                Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
                oldTour2.Stops.Remove(location);

                location = pdp.GetPickupDeliveryLocation(move.Replaced);
                oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));

                oldTour2.Stops.Remove(location);
                tour.Stops.Remove(move.Replaced);

                solution.InsertPair(tour, move.City, pdp.GetPickupDeliveryLocation(move.City), problemInstance);
                solution.InsertPair(oldTour, move.Replaced, pdp.GetPickupDeliveryLocation(move.Replaced), problemInstance);
            }
            else
            {
                tour.Stops.Remove(move.Replaced);

                int place = solution.FindBestInsertionPlace(tour, move.City);
                tour.Stops.Insert(place, move.City);

                place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
                oldTour.Stops.Insert(place, move.Replaced);
            }

            solution.Repair();
        }
        public static PotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random,
                                                    double alphaValue    = 0.7, double betaValue    = 0.1, double gammaValue     = 0.2,
                                                    double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14)
        {
            PotvinEncoding result = new PotvinEncoding(problemInstance);

            IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
            IMultiDepotProblemInstance        mdp = problemInstance as IMultiDepotProblemInstance;

            double alpha, beta, gamma;

            alpha = N(alphaValue, Math.Sqrt(alphaVariance), random);
            beta  = N(betaValue, Math.Sqrt(betaVariance), random);
            gamma = N(gammaValue, Math.Sqrt(gammaVariance), random);

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

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

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

            if (mdp != null)
            {
                for (int i = 0; i < mdp.Depots.Value; i++)
                {
                    depots.Add(i);
                }
            }
            else
            {
                depots.Add(0);
            }

            Dictionary <int, List <int> > vehicles = new Dictionary <int, List <int> >();

            foreach (int depot in depots)
            {
                vehicles[depot] = new List <int>();

                int vehicleCount = problemInstance.Vehicles.Value;
                if (mdp != null)
                {
                    for (int vehicle = 0; vehicle < mdp.VehicleDepotAssignment.Length; vehicle++)
                    {
                        if (mdp.VehicleDepotAssignment[vehicle] == depot)
                        {
                            vehicles[depot].Add(vehicle);
                        }
                    }
                }
                else
                {
                    for (int vehicle = 0; vehicle < vehicleCount; vehicle++)
                    {
                        vehicles[depot].Add(vehicle);
                    }
                }
            }

            RemoveUnusedDepots(depots, vehicles);
            Dictionary <int, int> depotAssignment = new Dictionary <int, int>();

            unroutedCustomers = SortCustomers(
                problemInstance, unroutedCustomers, depots, depotAssignment,
                alpha, beta, gamma);

            /////////
            Tour tour = new Tour();

            result.Tours.Add(tour);
            int currentCustomer = unroutedCustomers[0];

            unroutedCustomers.RemoveAt(0);

            int currentDepot   = depotAssignment[currentCustomer];
            int currentVehicle = vehicles[currentDepot][0];

            vehicles[currentDepot].RemoveAt(0);
            if (RemoveUnusedDepots(depots, vehicles))
            {
                unroutedCustomers = SortCustomers(
                    problemInstance, unroutedCustomers, depots, depotAssignment,
                    alpha, beta, gamma);
            }

            result.VehicleAssignment[result.Tours.Count - 1] = currentVehicle;

            tour.Stops.Add(currentCustomer);
            if (pdp != null)
            {
                tour.Stops.Add(pdp.GetPickupDeliveryLocation(currentCustomer));
            }
            ////////

            while (unroutedCustomers.Count > 0)
            {
                double minimumCost         = double.MaxValue;
                int    customer            = -1;
                int    indexOfMinimumCost  = -1;
                int    indexOfMinimumCost2 = -1;

                foreach (int unrouted in unroutedCustomers)
                {
                    VRPEvaluation eval          = problemInstance.EvaluateTour(tour, result);
                    double        originalCosts = eval.Quality;

                    for (int i = 0; i <= tour.Stops.Count; i++)
                    {
                        tour.Stops.Insert(i, unrouted);
                        eval = problemInstance.EvaluateTour(tour, result);
                        double tourCost = eval.Quality - originalCosts;

                        if (pdp != null)
                        {
                            for (int j = i + 1; j <= tour.Stops.Count; j++)
                            {
                                bool   feasible;
                                double cost = tourCost +
                                              problemInstance.GetInsertionCosts(eval, result, pdp.GetPickupDeliveryLocation(unrouted), 0, j, out feasible);
                                if (cost < minimumCost && feasible)
                                {
                                    customer            = unrouted;
                                    minimumCost         = cost;
                                    indexOfMinimumCost  = i;
                                    indexOfMinimumCost2 = j;
                                }
                            }
                        }
                        else
                        {
                            double cost     = tourCost;
                            bool   feasible = problemInstance.Feasible(eval);
                            if (cost < minimumCost && feasible)
                            {
                                customer           = unrouted;
                                minimumCost        = cost;
                                indexOfMinimumCost = i;
                            }
                        }

                        tour.Stops.RemoveAt(i);
                    }
                }

                if (indexOfMinimumCost == -1 && vehicles.Count == 0)
                {
                    indexOfMinimumCost  = tour.Stops.Count;
                    indexOfMinimumCost2 = tour.Stops.Count + 1;
                    customer            = unroutedCustomers[0];
                }

                // insert customer if found
                if (indexOfMinimumCost != -1)
                {
                    tour.Stops.Insert(indexOfMinimumCost, customer);
                    if (pdp != null)
                    {
                        tour.Stops.Insert(indexOfMinimumCost2, pdp.GetPickupDeliveryLocation(customer));
                    }

                    unroutedCustomers.Remove(customer);
                }
                else // no feasible customer found
                {
                    tour = new Tour();
                    result.Tours.Add(tour);
                    currentCustomer = unroutedCustomers[0];
                    unroutedCustomers.RemoveAt(0);

                    currentDepot   = depotAssignment[currentCustomer];
                    currentVehicle = vehicles[currentDepot][0];
                    vehicles[currentDepot].RemoveAt(0);
                    if (RemoveUnusedDepots(depots, vehicles))
                    {
                        unroutedCustomers = SortCustomers(
                            problemInstance, unroutedCustomers, depots, depotAssignment,
                            alpha, beta, gamma);
                    }

                    result.VehicleAssignment[result.Tours.Count - 1] = currentVehicle;

                    tour.Stops.Add(currentCustomer);
                    if (pdp != null)
                    {
                        tour.Stops.Add(pdp.GetPickupDeliveryLocation(currentCustomer));
                    }
                }
            }

            if (mdp != null)
            {
                List <int> availableVehicles = new List <int>();
                for (int i = 0; i < mdp.Vehicles.Value; i++)
                {
                    availableVehicles.Add(i);
                }

                for (int i = 0; i < result.VehicleAssignment.Length; i++)
                {
                    if (result.VehicleAssignment[i] != -1)
                    {
                        availableVehicles.Remove(result.VehicleAssignment[i]);
                    }
                }

                for (int i = 0; i < result.VehicleAssignment.Length; i++)
                {
                    if (result.VehicleAssignment[i] == -1)
                    {
                        result.VehicleAssignment[i] = availableVehicles[0];
                        availableVehicles.RemoveAt(0);
                    }
                }
            }

            return(result);
        }
    public static PotvinEncoding ApplyManipulation(IRandom random, PotvinEncoding individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
      PotvinEncoding result = null;
      
      int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
      if (selectedIndex >= 0) {
        bool performed = false;
        Tour route1 = individual.Tours[selectedIndex];

        if (route1.Stops.Count > 0) {
          //randomize customer selection
          Permutation perm = new Permutation(PermutationTypes.Absolute, route1.Stops.Count, random);
          int customer1Position = 0;

          while (customer1Position < route1.Stops.Count) {
            performed = false;

            int customer1 = route1.Stops[perm[customer1Position]];
            int customer2 = -1;

            for (int i = 0; i < individual.Tours.Count; i++) {
              if (i != selectedIndex) {
                Tour tour = individual.Tours[i];
                for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
                  customer2 = tour.Stops[customer2Position];

                  if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
                    result = ReplacePair(individual, pdp, customer2, customer1, allowInfeasible);
                    if (result != null) {
                      individual = result;

                      route1 = individual.Tours[selectedIndex];
                      performed = true;
                      break;
                    }
                  }
                }
              }

              if (performed) {
                break;
              }
            }

            if (!performed)
              customer1Position++;
            else
              break;
          }
        }
      }

      return result;
    }