public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance) {
      bool newTour = false;

      if (move.Tour >= solution.Tours.Count) {
        solution.Tours.Add(new Tour());
        newTour = true;
      }
      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);

        solution.InsertPair(tour, move.City, location, problemInstance);
      } else {
        int place = solution.FindBestInsertionPlace(tour, move.City);
        tour.Stops.Insert(place, move.City);
      }

      if (newTour) {
        List<int> vehicles = new List<int>();
        for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++) {
          vehicles.Add(solution.GetVehicleAssignment(i));
        }

        double bestQuality = double.MaxValue;
        int bestVehicle = -1;

        int originalVehicle = solution.GetVehicleAssignment(move.Tour);
        foreach (int vehicle in vehicles) {
          solution.VehicleAssignment[move.Tour] = vehicle;

          double quality = problemInstance.EvaluateTour(tour, solution).Quality;
          if (quality < bestQuality) {
            bestQuality = quality;
            bestVehicle = vehicle;
          }
        }

        solution.VehicleAssignment[move.Tour] = originalVehicle;

        int index = -1;
        for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++) {
          if (solution.VehicleAssignment[i] == bestVehicle) {
            index = i;
            break;
          }
        }
        solution.VehicleAssignment[index] = originalVehicle;
        solution.VehicleAssignment[move.Tour] = bestVehicle;
      }

      solution.Repair();
    }
        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());
        }
    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();
    }
예제 #4
0
    protected PotvinPDShiftMove(PotvinPDShiftMove original, Cloner cloner)
      : base(original, cloner) {
      this.City = original.City;
      this.OldTour = original.OldTour;
      this.Tour = original.Tour;

      this.Individual = cloner.Clone(Individual) as PotvinEncoding;
    }
        protected override void PerformMove()
        {
            PotvinPDShiftMove move = PDShiftMoveParameter.ActualValue;

            PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;

            Apply(newSolution, move, ProblemInstance);
            VRPToursParameter.ActualValue = newSolution;
        }
예제 #6
0
        protected PotvinPDShiftMove(PotvinPDShiftMove original, Cloner cloner)
            : base(original, cloner)
        {
            this.City    = original.City;
            this.OldTour = original.OldTour;
            this.Tour    = original.Tour;

            this.Individual = cloner.Clone(Individual) as PotvinEncoding;
        }
        protected override void EvaluateMove()
        {
            PotvinPDShiftMove move = PDShiftMoveParameter.ActualValue;

            PotvinEncoding newSolution = PDShiftMoveParameter.ActualValue.Individual.Clone() as PotvinEncoding;

            PotvinPDShiftMoveMaker.Apply(newSolution, move, ProblemInstance);

            UpdateEvaluation(newSolution);
        }
        protected override PotvinPDShiftMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance)
        {
            List <PotvinPDShiftMove> result = new List <PotvinPDShiftMove>();

            PotvinPDShiftMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);

            if (move != null)
            {
                result.Add(move);
            }

            return(result.ToArray());
        }
예제 #9
0
        protected override IItem GetTabuAttribute(bool maximization, double quality, double moveQuality)
        {
            PotvinPDShiftMove move        = PDShiftMoveParameter.ActualValue;
            double            baseQuality = moveQuality;

            if (quality < moveQuality)
            {
                baseQuality = quality;                  // we make an uphill move, the lower bound is the solution quality
            }
            double distance = 0;

            if (DistanceParameter.ActualValue != null)
            {
                distance = DistanceParameter.ActualValue.Value;
            }

            double overload = 0;

            if (OverloadParameter.ActualValue != null)
            {
                overload = OverloadParameter.ActualValue.Value;
            }

            double tardiness = 0;

            if (TardinessParameter.ActualValue != null)
            {
                tardiness = TardinessParameter.ActualValue.Value;
            }

            int pickupViolations = 0;

            if (PickupViolationsParameter.ActualValue != null)
            {
                pickupViolations = PickupViolationsParameter.ActualValue.Value;
            }

            return(new PotvinPDRelocateMoveAttribute(baseQuality, move.OldTour, move.City, distance, overload, tardiness, pickupViolations));
        }
        public override IOperation Apply()
        {
            ItemList <IItem>  tabuList      = TabuListParameter.ActualValue;
            double            moveQuality   = MoveQualityParameter.ActualValue.Value;
            bool              useAspiration = UseAspirationCriterion.Value;
            bool              isTabu        = false;
            PotvinPDShiftMove move          = PDShiftMoveParameter.ActualValue;

            foreach (IItem tabuMove in tabuList)
            {
                PotvinPDRelocateMoveAttribute attribute = tabuMove as PotvinPDRelocateMoveAttribute;

                if (attribute != null)
                {
                    double distance = 0;
                    if (MoveDistanceParameter.ActualValue != null)
                    {
                        distance = MoveDistanceParameter.ActualValue.Value;
                    }

                    double overload = 0;
                    if (MoveOverloadParameter.ActualValue != null)
                    {
                        overload = MoveOverloadParameter.ActualValue.Value;
                    }

                    double tardiness = 0;
                    if (MoveTardinessParameter.ActualValue != null)
                    {
                        tardiness = MoveTardinessParameter.ActualValue.Value;
                    }

                    IVRPProblemInstance instance = ProblemInstanceParameter.ActualValue;
                    double quality = attribute.Distance * instance.DistanceFactor.Value;

                    IHomogenousCapacitatedProblemInstance cvrp = instance as IHomogenousCapacitatedProblemInstance;
                    if (cvrp != null)
                    {
                        quality += attribute.Overload * cvrp.OverloadPenalty.Value;
                    }

                    ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
                    if (vrptw != null)
                    {
                        quality += attribute.Tardiness * vrptw.TardinessPenalty.Value;
                    }

                    IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
                    if (pdp != null)
                    {
                        quality += attribute.PickupViolations * pdp.PickupViolationPenalty.Value;
                    }

                    if (!useAspiration || moveQuality >= quality)
                    {
                        if (attribute.City == move.City && attribute.Tour == move.Tour)
                        {
                            isTabu = true;
                            break;
                        }

                        if (attribute.Distance == distance && attribute.Overload == overload && attribute.Tardiness == tardiness)
                        {
                            isTabu = true;
                            break;
                        }
                    }
                }
            }

            MoveTabuParameter.ActualValue = new BoolValue(isTabu);
            return(base.Apply());
        }
        public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance)
        {
            bool newTour = false;

            if (move.Tour >= solution.Tours.Count)
            {
                solution.Tours.Add(new Tour());
                newTour = true;
            }
            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);

                solution.InsertPair(tour, move.City, location, problemInstance);
            }
            else
            {
                int place = solution.FindBestInsertionPlace(tour, move.City);
                tour.Stops.Insert(place, move.City);
            }

            if (newTour)
            {
                List <int> vehicles = new List <int>();
                for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++)
                {
                    vehicles.Add(solution.GetVehicleAssignment(i));
                }

                double bestQuality = double.MaxValue;
                int    bestVehicle = -1;

                int originalVehicle = solution.GetVehicleAssignment(move.Tour);
                foreach (int vehicle in vehicles)
                {
                    solution.VehicleAssignment[move.Tour] = vehicle;

                    double quality = problemInstance.EvaluateTour(tour, solution).Quality;
                    if (quality < bestQuality)
                    {
                        bestQuality = quality;
                        bestVehicle = vehicle;
                    }
                }

                solution.VehicleAssignment[move.Tour] = originalVehicle;

                int index = -1;
                for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++)
                {
                    if (solution.VehicleAssignment[i] == bestVehicle)
                    {
                        index = i;
                        break;
                    }
                }
                solution.VehicleAssignment[index]     = originalVehicle;
                solution.VehicleAssignment[move.Tour] = bestVehicle;
            }

            solution.Repair();
        }