コード例 #1
0
        public override IOperation Apply()
        {
            ItemList <IItem> tabuList = TabuListParameter.ActualValue;
            int tabuTenure            = TabuTenureParameter.ActualValue.Value;

            int overlength = tabuList.Count - tabuTenure;

            if (overlength >= 0)
            {
                for (int i = 0; i < tabuTenure - 1; i++)
                {
                    tabuList[i] = tabuList[i + overlength + 1];
                }
                while (tabuList.Count >= tabuTenure)
                {
                    tabuList.RemoveAt(tabuList.Count - 1);
                }
            }

            PotvinPDExchangeMove move = PDExchangeMoveParameter.ActualValue;
            double moveQuality        = MoveQualityParameter.ActualValue.Value;
            double quality            = QualityParameter.ActualValue.Value;
            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;
            }

            tabuList.Add(new PotvinPDRelocateMoveAttribute(baseQuality, move.OldTour, move.City, distance, overload, tardiness, pickupViolations));
            tabuList.Add(new PotvinPDRelocateMoveAttribute(baseQuality, move.Tour, move.Replaced, distance, overload, tardiness, pickupViolations));
            return(base.Apply());
        }
コード例 #2
0
    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();
    }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
    protected PotvinPDExchangeMove(PotvinPDExchangeMove original, Cloner cloner)
      : base(original, cloner) {
      this.City = original.City;
      this.OldTour = original.OldTour;
      this.Tour = original.Tour;
      this.Replaced = original.Replaced;

      this.Individual = cloner.Clone(Individual) as PotvinEncoding;
    }
コード例 #5
0
        protected override void PerformMove()
        {
            PotvinPDExchangeMove move = PDExchangeMoveParameter.ActualValue;

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

            Apply(newSolution, move, ProblemInstance);
            VRPToursParameter.ActualValue = newSolution;
        }
コード例 #6
0
        protected override void EvaluateMove()
        {
            PotvinPDExchangeMove move = PDExchangeMoveParameter.ActualValue;

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

            PotvinPDExchangeMoveMaker.Apply(newSolution, move, ProblemInstance);

            UpdateEvaluation(newSolution);
        }
コード例 #7
0
        protected PotvinPDExchangeMove(PotvinPDExchangeMove original, Cloner cloner)
            : base(original, cloner)
        {
            this.City     = original.City;
            this.OldTour  = original.OldTour;
            this.Tour     = original.Tour;
            this.Replaced = original.Replaced;

            this.Individual = cloner.Clone(Individual) as PotvinEncoding;
        }
コード例 #8
0
        protected override PotvinPDExchangeMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance)
        {
            List <PotvinPDExchangeMove> result = new List <PotvinPDExchangeMove>();

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

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

            return(result.ToArray());
        }
コード例 #9
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());
        }
コード例 #10
0
        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();
        }
    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();
    }
コード例 #12
0
        public override IOperation Apply()
        {
            ItemList <IItem>     tabuList      = TabuListParameter.ActualValue;
            double               moveQuality   = MoveQualityParameter.ActualValue.Value;
            bool                 useAspiration = UseAspirationCriterion.Value;
            bool                 isTabu        = false;
            PotvinPDExchangeMove move          = PDExchangeMoveParameter.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) ||
                            (attribute.City == move.Replaced && attribute.Tour == move.OldTour))
                        {
                            isTabu = true;
                            break;
                        }

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

            MoveTabuParameter.ActualValue = new BoolValue(isTabu);
            return(base.Apply());
        }
コード例 #13
0
    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;
      }
    }