Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/13 13:55:01 (11 years ago)
Author:
epitzer
Message:

Fix VRP FLA manipulators and adapt to VRP-3.4 (#1703)

File:
1 edited

Legend:

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

    r9750 r9836  
    3030using HeuristicLab.Problems.VehicleRouting;
    3131using System;
     32using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3233
    3334namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
     
    4748    }
    4849
    49     public static void Apply(IRandom random, PotvinEncoding individual) {
    50       List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 4);
     50    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) {
     51      List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 2);
     52      if (tours.Count == 0) return;
    5153
    52       if (tours.Count > 0) {
     54      bool feasible;
     55      int count = 0;
     56
     57      do {
     58        feasible = true;
    5359        int tourIdx = random.Next(tours.Count);
    5460        Tour tour = tours[tourIdx];
    5561
    56         int a;
    57         if (tour.Stops.Count == 4) {
    58             a = 0;
    59         } else if (tour.Stops.Count == 5) {
    60           int idx = random.Next(4);
    61           if (idx >= 2)
    62             idx++;
    63           a = idx;
     62        int first;
     63        int length;
     64        if (tour.Stops.Count == 2) {
     65          first = 0;
     66          length = 2;
    6467        } else {
    65           a = random.Next(tour.Stops.Count);
     68          first = random.Next(tour.Stops.Count - 1);
     69          if (first == tour.Stops.Count - 2) {
     70            length = tour.Stops.Count - first;
     71          } else {
     72            length = random.Next(tour.Stops.Count - first - 1) + 2;
     73          }
    6674        }
    6775
    68         int b;
    69         List<int> indices = new List<int>();
    70         for (int i = 0; i < tour.Stops.Count; i++) {
    71           if (Math.Abs(i - a) > 2) {
    72             indices.Add(i);
     76        var originalFeasible = instance.TourFeasible(tour, individual);
     77
     78        var segment = tour.Stops.GetRange(first, length);
     79        tour.Stops.RemoveRange(first, length);
     80        segment.Reverse();
     81        tour.Stops.InsertRange(first, segment);
     82
     83        if (!allowInfeasible && originalFeasible) {
     84          feasible = instance.TourFeasible(tour, individual);
     85          if (!feasible) {
     86            tour.Stops.RemoveRange(first, length);
     87            segment.Reverse();
     88            tour.Stops.InsertRange(first, segment);
    7389          }
    7490        }
    75         b = indices[random.Next(indices.Count)];
    7691
    77         if (b < a) {
    78           int tmp = a;
    79           a = b;
    80           b = tmp;
    81         }
    82 
    83         int index = a + 1;
    84         int count = b - a - 1;
    85         List<int> segment = tour.Stops.GetRange(index, count);
    86         tour.Stops.RemoveRange(index, count);
    87         segment.Reverse();
    88         tour.Stops.InsertRange(index, segment);
    89       }
     92      } while(!feasible && count++ < 100);
    9093    }
    9194
    9295    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
    93       Apply(random, individual);
     96      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
     97      Apply(random, individual, ProblemInstance, allowInfeasible);
    9498    }
    9599  }
Note: See TracChangeset for help on using the changeset viewer.