Changeset 6909


Ignore:
Timestamp:
10/12/11 15:45:48 (9 years ago)
Author:
svonolfe
Message:

Fixed more isses that occured in dynamic instances (#1177)

Location:
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeMultiMoveGenerator.cs

    r6773 r6909  
    6464
    6565      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
    66         result.Add(PotvinPDExchangeSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue));
     66        var move = PotvinPDExchangeSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue);
     67        if (move != null)
     68          result.Add(move);
    6769      }
    6870
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeSingleMoveGenerator.cs

    r6856 r6909  
    6666      }
    6767
    68       PotvinPDExchangeMove move = null;
    69       while (cities.Count > 0 && move == null) {
    70         int city = cities[rand.Next(cities.Count)];
    71         Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
    72         int oldTourIndex = individual.Tours.IndexOf(oldTour);
     68      if (cities.Count > 1 && individual.Tours.Count > 1) {
     69        PotvinPDExchangeMove move = null;
     70        while (cities.Count > 1 && move == null) {
     71          int city = cities[rand.Next(cities.Count)];
     72          Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
     73          int oldTourIndex = individual.Tours.IndexOf(oldTour);
    7374
    74         int max = individual.Tours.Count;
    75         if (individual.Tours.Count < problemInstance.Vehicles.Value)
    76           max = max - 1;
     75          int max = individual.Tours.Count;
     76          if (individual.Tours.Count < problemInstance.Vehicles.Value)
     77            max = max - 1;
    7778
    78         int newTourIndex = rand.Next(max);
    79         if (newTourIndex >= oldTourIndex)
    80           newTourIndex++;
     79          int newTourIndex = rand.Next(max);
     80          if (newTourIndex >= oldTourIndex)
     81            newTourIndex++;
    8182
    82         Tour newTour = individual.Tours[newTourIndex];
    83         List<int> tourCities = new List<int>();
    84         foreach (int stop in newTour.Stops) {
    85           if (pdp == null ||
    86             (pdp.GetDemand(stop) >= 0 &&
    87             pdp.GetPickupDeliveryLocation(stop) != pdp.GetPickupDeliveryLocation(city) &&
    88             pdp.GetPickupDeliveryLocation(stop) != city &&
    89             pdp.GetPickupDeliveryLocation(city) != stop))
    90             tourCities.Add(stop);
     83          Tour newTour = individual.Tours[newTourIndex];
     84          List<int> tourCities = new List<int>();
     85          foreach (int stop in newTour.Stops) {
     86            if (pdp == null ||
     87              (pdp.GetDemand(stop) >= 0 &&
     88              pdp.GetPickupDeliveryLocation(stop) != pdp.GetPickupDeliveryLocation(city) &&
     89              pdp.GetPickupDeliveryLocation(stop) != city &&
     90              pdp.GetPickupDeliveryLocation(city) != stop))
     91              tourCities.Add(stop);
     92          }
     93
     94          if (tourCities.Count > 0) {
     95            int replaced = tourCities[rand.Next(tourCities.Count)];
     96            move = new PotvinPDExchangeMove(city, oldTourIndex, newTourIndex, replaced, individual);
     97          }
    9198        }
    9299
    93         if (tourCities.Count > 0) {
    94           int replaced = tourCities[rand.Next(tourCities.Count)];
    95           move = new PotvinPDExchangeMove(city, oldTourIndex, newTourIndex, replaced, individual);
    96         }
    97       }
    98 
    99       return move;
     100        return move;
     101      } else {
     102        return null;
     103      }             
    100104    }
    101105
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeMultiMoveGenerator.cs

    r6773 r6909  
    6464
    6565      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
    66         result.Add(PotvinPDRearrangeSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue));
     66        var move = PotvinPDRearrangeSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue);
     67        if (move != null)
     68          result.Add(move);
    6769      }
    6870
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeSingleMoveGenerator.cs

    r6851 r6909  
    6262      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
    6363      for (int i = 1; i <= individual.Cities; i++) {
    64         if(pdp == null || pdp.GetDemand(i) >= 0)
     64        if (pdp == null || pdp.GetDemand(i) >= 0)
    6565          cities.Add(i);
    6666      }
    6767
    68       int city = cities[rand.Next(cities.Count)];
    69       int tour = individual.Tours.FindIndex(t => t.Stops.Contains(city));
    70       return new PotvinPDRearrangeMove(city, tour, individual);
     68      if (cities.Count > 0) {
     69        int city = cities[rand.Next(cities.Count)];
     70        int tour = individual.Tours.FindIndex(t => t.Stops.Contains(city));
     71        return new PotvinPDRearrangeMove(city, tour, individual);
     72      } else {
     73        return null;
     74      }
    7175    }
    7276
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftMultiMoveGenerator.cs

    r6773 r6909  
    6464
    6565      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
    66         result.Add(PotvinPDShiftSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue));
     66        var move = PotvinPDShiftSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue);
     67        if (move != null)
     68          result.Add(move);
    6769      }
    6870
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftSingleMoveGenerator.cs

    r6851 r6909  
    6262      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
    6363      for (int i = 1; i <= individual.Cities; i++) {
    64         if(pdp == null || pdp.GetDemand(i) >= 0)
     64        if (pdp == null || pdp.GetDemand(i) >= 0)
    6565          cities.Add(i);
    6666      }
    6767
    68       int city = cities[rand.Next(cities.Count)];
    69       Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
    70       int oldTourIndex = individual.Tours.IndexOf(oldTour);
     68      if (cities.Count > 1 && individual.Tours.Count > 1) {
     69        int city = cities[rand.Next(cities.Count)];
     70        Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
     71        int oldTourIndex = individual.Tours.IndexOf(oldTour);
    7172
    72       int max = individual.Tours.Count;
    73       if (individual.Tours.Count < problemInstance.Vehicles.Value)
    74         max = max - 1;
     73        int max = individual.Tours.Count;
     74        if (individual.Tours.Count < problemInstance.Vehicles.Value)
     75          max = max - 1;
    7576
    76       int newTourIndex = rand.Next(max);
    77       if (newTourIndex >= oldTourIndex)
    78         newTourIndex++;
     77        int newTourIndex = rand.Next(max);
     78        if (newTourIndex >= oldTourIndex)
     79          newTourIndex++;
    7980
    80       return new PotvinPDShiftMove(city, oldTourIndex, newTourIndex, individual);
     81        return new PotvinPDShiftMove(city, oldTourIndex, newTourIndex, individual);
     82      } else {
     83        return null;
     84      }             
    8185    }
    8286
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/VehicleAssignment/PotvinVehicleAssignmentMultiMoveGenerator.cs

    r6857 r6909  
    6464
    6565      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
    66         result.Add(PotvinVehicleAssignmentSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue));
     66        var move = PotvinVehicleAssignmentSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue);
     67        if(move != null)
     68          result.Add(move);
    6769      }
    6870
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/VehicleAssignment/PotvinVehicleAssignmentSingleMoveGenerator.cs

    r6857 r6909  
    6565
    6666    public static PotvinVehicleAssignmentMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
    67       int tour1 = rand.Next(individual.Tours.Count);
    68       int tour2 = rand.Next(problemInstance.Vehicles.Value);
     67      if (individual.Tours.Count > 1) {
     68        int tour1 = rand.Next(individual.Tours.Count);
     69        int tour2 = rand.Next(problemInstance.Vehicles.Value);
    6970
    70       while (tour2 == tour1)
    71         tour2 = rand.Next(problemInstance.Vehicles.Value);
     71        while (tour2 == tour1)
     72          tour2 = rand.Next(problemInstance.Vehicles.Value);
    7273
    73       return new PotvinVehicleAssignmentMove(tour1, tour2, individual);
     74        return new PotvinVehicleAssignmentMove(tour1, tour2, individual);
     75      } else {
     76        return null;
     77      }
    7478    }
    7579
Note: See TracChangeset for help on using the changeset viewer.