Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/13 09:21:45 (11 years ago)
Author:
gkronber
Message:

#1591: adapted FLA branch to reference most recent version of ALGLIB (3.7.0) and VRP (3.4). Several major changes were necessary to port the implementation to the new VRP problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/ExchangeManipulator.cs

    r7159 r9750  
    2929using HeuristicLab.Problems.VehicleRouting.Encodings;
    3030using HeuristicLab.Problems.VehicleRouting;
     31using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3132
    3233namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
     
    4647    }
    4748
    48     private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) {
    49       double routeLoad = 0;
    50       for (int i = 0; i < tour.Cities.Count; i++) {
    51         routeLoad += demand[tour.Cities[i]];
    52       }
    53 
    54       return routeLoad <= capacity.Value;
    55     }
    56 
    57     public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) {
     49    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
    5850      bool feasible;
    5951
     
    7062          Tour tour2 = individual.Tours[tour2Idx];
    7163
    72           int index1 = random.Next(tour1.Cities.Count);
    73           int city1 = tour1.Cities[index1];
     64          int index1 = random.Next(tour1.Stops.Count);
     65          int city1 = tour1.Stops[index1];
    7466
    75           int index2 = random.Next(tour2.Cities.Count);
    76           int city2 = tour2.Cities[index2];
     67          int index2 = random.Next(tour2.Stops.Count);
     68          int city2 = tour2.Stops[index2];
    7769
    7870          if (!allowInfeasible) {
    79             bool originalFeasible =
    80               RouteFeasible(tour1, demand, capacity) &&
    81               RouteFeasible(tour2, demand, capacity);
     71            bool originalFeasible = problemInstance.TourFeasible(tour1, individual) &&
     72                                    problemInstance.TourFeasible(tour2, individual);
    8273
    8374            if (originalFeasible) {
    84               double routeLoad = 0;
    85               for (int i = 0; i < tour1.Cities.Count; i++) {
    86                 if (i != index1)
    87                   routeLoad += demand[tour1.Cities[i]];
     75              var tmpCity1 = tour1.Stops[index1];
     76              tour1.Stops.RemoveAt(index1);
     77              tour1.Stops.Insert(index1, city2);
     78
     79              if (problemInstance.TourFeasible(tour1, individual)) {
     80                var tmpCity2 = tour2.Stops[index2];
     81                tour2.Stops.RemoveAt(index2);
     82                tour2.Stops.Insert(index2, city1);
     83
     84                feasible = problemInstance.TourFeasible(tour2, individual);
     85
     86                if (!feasible) {
     87                  // undo tour1 and tour2 changes
     88                  tour2.Stops.RemoveAt(index2);
     89                  tour2.Stops.Insert(index2, tmpCity2);
     90
     91                  tour1.Stops.RemoveAt(index1);
     92                  tour1.Stops.Insert(index1, tmpCity1);
     93                }
     94              } else {
     95                feasible = false;
     96                // undo tour1 change
     97                tour1.Stops.RemoveAt(index1);
     98                tour1.Stops.Insert(index1, tmpCity1);
    8899              }
    89               routeLoad += demand[city2];
    90 
    91               if (routeLoad > capacity.Value) {
    92                 feasible = false;
    93               } else {
    94                 routeLoad = 0;
    95                 for (int i = 0; i < tour2.Cities.Count; i++) {
    96                   if (i != index2)
    97                     routeLoad += demand[tour2.Cities[i]];
    98                 }
    99                 routeLoad += demand[city1];
    100 
    101                 if (routeLoad > capacity.Value) {
    102                   feasible = false;
    103                 }
    104               }
    105 
    106             }
    107           }
    108 
    109           if (feasible) {
    110             tour1.Cities.RemoveAt(index1);
    111             tour1.Cities.Insert(index1, city2);
    112 
    113             tour2.Cities.RemoveAt(index2);
    114             tour2.Cities.Insert(index2, city1);
     100            } else feasible = false;
    115101          }
    116102        }
     
    119105
    120106    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
    121       BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
    122       DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
    123       DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
    124       DoubleArray demand = DemandParameter.ActualValue;
    125       DoubleValue capacity = CapacityParameter.ActualValue;
    126 
    127107      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
    128       Apply(random, individual, demand, capacity, allowInfeasible);
     108      Apply(random, individual, ProblemInstance, allowInfeasible);
    129109    }
    130110  }
Note: See TracChangeset for help on using the changeset viewer.