Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.