Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs @ 15377

Last change on this file since 15377 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27
28namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
29  [Item("PotvinRouteBasedCrossover", "The RBX crossover for a VRP representations.  It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
30  [StorableClass]
31  public sealed class PotvinRouteBasedCrossover : PotvinCrossover {
32    [StorableConstructor]
33    private PotvinRouteBasedCrossover(bool deserializing) : base(deserializing) { }
34
35    public PotvinRouteBasedCrossover()
36      : base() { }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new PotvinRouteBasedCrossover(this, cloner);
40    }
41
42    private PotvinRouteBasedCrossover(PotvinRouteBasedCrossover original, Cloner cloner)
43      : base(original, cloner) {
44    }
45
46    public static PotvinEncoding Apply(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2, IVRPProblemInstance problemInstance, bool allowInfeasible) {
47      PotvinEncoding child = parent2.Clone() as PotvinEncoding;
48
49      if (parent1.Tours.Count > 0 && child.Tours.Count > 0) {
50        int tourParent1 = random.Next(parent1.Tours.Count);
51        Tour replacing = parent1.Tours[tourParent1].Clone() as Tour;
52
53        int tourParent2 = random.Next(child.Tours.Count);
54        Tour replaced = child.Tours[tourParent2];
55
56        child.Tours.Remove(replaced);
57        child.Tours.Insert(tourParent2, replacing);
58
59        Permutation vehicleAssignment = child.VehicleAssignment;
60
61        int vehicle = vehicleAssignment[tourParent2];
62        int vehicle2 = parent1.VehicleAssignment[tourParent1];
63        vehicleAssignment[tourParent2] = vehicle2;
64
65        for (int i = 0; i < vehicleAssignment.Length; i++) {
66          if (vehicleAssignment[i] == vehicle2 && i != tourParent2) {
67            vehicleAssignment[i] = vehicle;
68            break;
69          }
70        }
71
72        foreach (int city in replaced.Stops)
73          if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
74            child.Unrouted.Add(city);
75
76        if (Repair(random, child, replacing, problemInstance, allowInfeasible) || allowInfeasible)
77          return child;
78        else {
79          if (random.NextDouble() < 0.5)
80            return parent1.Clone() as PotvinEncoding;
81          else
82            return parent2.Clone() as PotvinEncoding;
83        }
84      } else {
85        return child;
86      }
87    }
88
89    protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
90      return Apply(random, parent1, parent2, ProblemInstance, AllowInfeasibleSolutions.Value.Value);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.