Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/12/11 13:41:14 (13 years ago)
Author:
svonolfe
Message:

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

Location:
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs

    r6857 r6907  
    5050      PotvinEncoding child = parent2.Clone() as PotvinEncoding;
    5151
    52       int tourParent1 = random.Next(parent1.Tours.Count);
    53       Tour replacing = parent1.Tours[tourParent1].Clone() as Tour;
     52      if (parent1.Tours.Count > 0 && child.Tours.Count > 0) {
     53        int tourParent1 = random.Next(parent1.Tours.Count);
     54        Tour replacing = parent1.Tours[tourParent1].Clone() as Tour;
    5455
    55       int tourParent2 = random.Next(child.Tours.Count);
    56       Tour replaced = child.Tours[tourParent2];
     56        int tourParent2 = random.Next(child.Tours.Count);
     57        Tour replaced = child.Tours[tourParent2];
    5758
    58       child.Tours.Remove(replaced);
    59       child.Tours.Insert(tourParent2, replacing);
     59        child.Tours.Remove(replaced);
     60        child.Tours.Insert(tourParent2, replacing);
    6061
    6162        Permutation vehicleAssignment = child.VehicleAssignment;
     
    7071            break;
    7172          }
    72         }       
     73        }
    7374
    74       foreach (int city in replaced.Stops)
    75         if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
    76           child.Unrouted.Add(city);
     75        foreach (int city in replaced.Stops)
     76          if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
     77            child.Unrouted.Add(city);
    7778
    78       if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible)
     79        if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible)
     80          return child;
     81        else {
     82          if (random.NextDouble() < 0.5)
     83            return parent1.Clone() as PotvinEncoding;
     84          else
     85            return parent2.Clone() as PotvinEncoding;
     86        }
     87      } else {
    7988        return child;
    80       else {
    81         if(random.NextDouble() < 0.5)
    82           return parent1.Clone() as PotvinEncoding;
    83         else
    84           return parent2.Clone() as PotvinEncoding;
    8589      }
    8690    }
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs

    r6851 r6907  
    5353      int cities = ProblemInstance.Cities.Value;
    5454
    55       int breakPoint1 = random.Next(1, cities + 1);
    56       Tour tour1 = FindRoute(child, breakPoint1);
    57       breakPoint1 = tour1.Stops.IndexOf(breakPoint1);
     55      if (cities > 0) {
     56        int breakPoint1 = random.Next(1, cities + 1);
     57        Tour tour1 = FindRoute(child, breakPoint1);
     58        breakPoint1 = tour1.Stops.IndexOf(breakPoint1);
    5859
    59       for (int i = 0; i < breakPoint1; i++)
    60         newTour.Stops.Add(tour1.Stops[i]);
     60        for (int i = 0; i < breakPoint1; i++)
     61          newTour.Stops.Add(tour1.Stops[i]);
    6162
    62       int breakPoint2 = random.Next(1, cities + 1);
    63       Tour tour2 = FindRoute(parent2, breakPoint2);
    64       breakPoint2 = tour2.Stops.IndexOf(breakPoint2);
     63        int breakPoint2 = random.Next(1, cities + 1);
     64        Tour tour2 = FindRoute(parent2, breakPoint2);
     65        breakPoint2 = tour2.Stops.IndexOf(breakPoint2);
    6566
    66       for (int i = breakPoint2; i < tour2.Stops.Count; i++)
    67         newTour.Stops.Add(tour2.Stops[i]);
     67        for (int i = breakPoint2; i < tour2.Stops.Count; i++)
     68          newTour.Stops.Add(tour2.Stops[i]);
    6869
    69       int tour1Index = child.Tours.IndexOf(tour1);
    70       child.Tours.Remove(tour1);
    71       child.Tours.Insert(tour1Index, newTour);
     70        int tour1Index = child.Tours.IndexOf(tour1);
     71        child.Tours.Remove(tour1);
     72        child.Tours.Insert(tour1Index, newTour);
    7273
    73       foreach (int city in tour1.Stops)
    74         if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
    75           child.Unrouted.Add(city);
     74        foreach (int city in tour1.Stops)
     75          if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
     76            child.Unrouted.Add(city);
    7677
    77       foreach (int city in tour2.Stops)
    78         if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
    79           child.Unrouted.Add(city);
     78        foreach (int city in tour2.Stops)
     79          if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
     80            child.Unrouted.Add(city);
    8081
    81       if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) {
     82        if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) {
     83          return child;
     84        } else {
     85          if (random.NextDouble() < 0.5)
     86            return parent1.Clone() as PotvinEncoding;
     87          else
     88            return parent2.Clone() as PotvinEncoding;
     89        }
     90      } else {
    8291        return child;
    83       } else {
    84         if (random.NextDouble() < 0.5)
    85           return parent1.Clone() as PotvinEncoding;
    86         else
    87           return parent2.Clone() as PotvinEncoding;
    88       }
     92      }
    8993    }
    9094  }
Note: See TracChangeset for help on using the changeset viewer.