Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/07/11 17:39:39 (13 years ago)
Author:
svonolfe
Message:

Changed behavior of TwoOptStar operator such that new tours can be created (#1703)

File:
1 edited

Legend:

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

    r7145 r7151  
    5858      bool feasible;
    5959
     60      //consider creating new tour
     61      individual.Tours.Add(new Tour());
     62
    6063      do {
    6164        feasible = true;
    6265
    63         if (individual.Tours.Count > 1) {
    64           int route1Idx = random.Next(individual.Tours.Count);
    65           int route2Idx = random.Next(individual.Tours.Count - 1);
    66           if (route2Idx >= route1Idx)
    67             route2Idx++;
     66        int route1Idx = random.Next(individual.Tours.Count);
     67        int route2Idx = random.Next(individual.Tours.Count - 1);
     68        if (route2Idx >= route1Idx)
     69          route2Idx++;
    6870
    69           Tour route1 = individual.Tours[route1Idx];
    70           Tour route2 = individual.Tours[route2Idx];
     71        Tour route1 = individual.Tours[route1Idx];
     72        Tour route2 = individual.Tours[route2Idx];
    7173
    72           int x1 = random.Next(route1.Cities.Count + 1);
    73           int x2 = random.Next(route2.Cities.Count + 1);
     74        int x1 = random.Next(route1.Cities.Count + 1);
     75        int x2 = random.Next(route2.Cities.Count + 1);
    7476
    75           if (!allowInfeasible) {
    76             bool originalFeasible =
    77               RouteFeasible(route1, demand, capacity) &&
    78               RouteFeasible(route2, demand, capacity);
     77        if (!allowInfeasible) {
     78          bool originalFeasible =
     79            RouteFeasible(route1, demand, capacity) &&
     80            RouteFeasible(route2, demand, capacity);
    7981
    80             if (originalFeasible) {
    81               double routeLoad = 0;
    82               for (int i = 0; i < x1; i++)
     82          if (originalFeasible) {
     83            double routeLoad = 0;
     84            for (int i = 0; i < x1; i++)
     85              routeLoad += demand[route1.Cities[i]];
     86            for (int i = x2; i < route2.Cities.Count; i++)
     87              routeLoad += demand[route2.Cities[i]];
     88
     89            if (routeLoad > capacity.Value) {
     90              feasible = false;
     91            } else {
     92              routeLoad = 0;
     93              for (int i = 0; i < x2; i++)
     94                routeLoad += demand[route2.Cities[i]];
     95              for (int i = x1; i < route1.Cities.Count; i++)
    8396                routeLoad += demand[route1.Cities[i]];
    84               for (int i = x2; i < route2.Cities.Count; i++)
    85                 routeLoad += demand[route2.Cities[i]];
    8697
    8798              if (routeLoad > capacity.Value) {
    8899                feasible = false;
    89               } else {
    90                 routeLoad = 0;
    91                 for (int i = 0; i < x2; i++)
    92                   routeLoad += demand[route2.Cities[i]];
    93                 for (int i = x1; i < route1.Cities.Count; i++)
    94                   routeLoad += demand[route1.Cities[i]];
    95 
    96                 if (routeLoad > capacity.Value) {
    97                   feasible = false;
    98                 }
    99100              }
    100101            }
    101102          }
     103        }
    102104
    103           if (feasible) {
    104             int count = route1.Cities.Count - x1;
    105             List<int> segmentX1 = new List<int>();
    106             if (count > 0) {
    107               segmentX1 = route1.Cities.GetRange(x1, count);
    108               route1.Cities.RemoveRange(x1, count);
    109             }
     105        if (feasible) {
     106          int count = route1.Cities.Count - x1;
     107          List<int> segmentX1 = new List<int>();
     108          if (count > 0) {
     109            segmentX1 = route1.Cities.GetRange(x1, count);
     110            route1.Cities.RemoveRange(x1, count);
     111          }
    110112
    111             count = route2.Cities.Count - x2;
    112             List<int> segmentX2 = new List<int>();
    113             if (count > 0) {
    114               segmentX2 = route2.Cities.GetRange(x2, count);
    115               route2.Cities.RemoveRange(x2, count);
    116             }
     113          count = route2.Cities.Count - x2;
     114          List<int> segmentX2 = new List<int>();
     115          if (count > 0) {
     116            segmentX2 = route2.Cities.GetRange(x2, count);
     117            route2.Cities.RemoveRange(x2, count);
     118          }
    117119
    118             route1.Cities.AddRange(segmentX2);
    119             route2.Cities.AddRange(segmentX1);
    120 
    121             individual.Tours.RemoveAll(t => t.Cities.Count == 0);
    122           }
     120          route1.Cities.AddRange(segmentX2);
     121          route2.Cities.AddRange(segmentX1);
    123122        }
    124123      } while (!feasible);
     124
     125      individual.Tours.RemoveAll(t => t.Cities.Count == 0);
    125126    }
    126127
Note: See TracChangeset for help on using the changeset viewer.