Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs @ 9616

Last change on this file since 9616 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.7 KB
RevLine 
[4376]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4376]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
[8053]22using HeuristicLab.Common;
[4376]23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8346]26using HeuristicLab.Problems.VehicleRouting.Interfaces;
[4376]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() { }
[4752]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    }
[8053]45
[8346]46    public static PotvinEncoding Apply(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2, IVRPProblemInstance problemInstance, bool allowInfeasible) {
[4376]47      PotvinEncoding child = parent2.Clone() as PotvinEncoding;
48
[6907]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;
[4376]52
[6907]53        int tourParent2 = random.Next(child.Tours.Count);
54        Tour replaced = child.Tours[tourParent2];
[4376]55
[6907]56        child.Tours.Remove(replaced);
57        child.Tours.Insert(tourParent2, replacing);
[4376]58
[6857]59        Permutation vehicleAssignment = child.VehicleAssignment;
[6851]60
61        int vehicle = vehicleAssignment[tourParent2];
[6857]62        int vehicle2 = parent1.VehicleAssignment[tourParent1];
[6851]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          }
[6907]70        }
[6851]71
[6907]72        foreach (int city in replaced.Stops)
73          if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
74            child.Unrouted.Add(city);
[4376]75
[8346]76        if (Repair(random, child, replacing, problemInstance, allowInfeasible) || allowInfeasible)
[6907]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 {
[4376]85        return child;
86      }
87    }
[8346]88
89    protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
90      return Apply(random, parent1, parent2, ProblemInstance, AllowInfeasibleSolutions.Value.Value);
91    }
[4376]92  }
93}
Note: See TracBrowser for help on using the repository browser.