Esempio n. 1
0
        public void InsertPair(Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1)
        {
            int           stops          = tour.Stops.Count;
            VRPEvaluation eval           = problemInstance.EvaluateTour(tour, this);
            double        minCosts       = double.MaxValue;
            int           sourceLocation = -1;
            int           targetLocation = -1;

            for (int i = 0; i <= stops; i++)
            {
                tour.Stops.Insert(i, source);
                VRPEvaluation tourEval    = problemInstance.EvaluateTour(tour, this);
                double        sourceCosts = tourEval.Quality - eval.Quality;

                for (int j = i + 1; j <= stops + 1; j++)
                {
                    if (positionToAvoid != i || positionToAvoid2 != j || stops == 0)
                    {
                        bool   feasible;
                        double targetCosts = problemInstance.GetInsertionCosts(tourEval, this, target, 0, j, out feasible);

                        double costs = sourceCosts + targetCosts;
                        if (costs < minCosts)
                        {
                            minCosts       = costs;
                            sourceLocation = i;
                            targetLocation = j;
                        }
                    }
                }
                tour.Stops.Remove(source);
            }

            tour.Stops.Insert(sourceLocation, source);
            tour.Stops.Insert(targetLocation, target);
        }
Esempio n. 2
0
    public void InsertPair(Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1) {
      int stops = tour.Stops.Count;
      VRPEvaluation eval = problemInstance.EvaluateTour(tour, this);
      double minCosts = double.MaxValue;
      int sourceLocation = -1;
      int targetLocation = -1;

      for (int i = 0; i <= stops; i++) {
        tour.Stops.Insert(i, source);
        VRPEvaluation tourEval = problemInstance.EvaluateTour(tour, this);
        double sourceCosts = tourEval.Quality - eval.Quality;

        for (int j = i + 1; j <= stops + 1; j++) {
          if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
            bool feasible;
            double targetCosts = problemInstance.GetInsertionCosts(tourEval, this, target, 0, j, out feasible);

            double costs = sourceCosts + targetCosts;
            if (costs < minCosts) {
              minCosts = costs;
              sourceLocation = i;
              targetLocation = j;
            }
          }
        }
        tour.Stops.Remove(source);
      }

      tour.Stops.Insert(sourceLocation, source);
      tour.Stops.Insert(targetLocation, target);
    }
    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 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);
        }