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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
27 |
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
30 | [Item("PotvinEdgePreservingSequenceBasedCrossover", "An enhanced variant of the SBX crossover 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. In this SBX variant the second tour segment continues after the last stop of the first segment")]
|
---|
31 | public sealed class PotvinEdgePreservingSequenceBasedCrossover : PotvinCrossover {
|
---|
32 | private PotvinEdgePreservingSequenceBasedCrossover(bool deserializing) : base(deserializing) { }
|
---|
33 |
|
---|
34 | public PotvinEdgePreservingSequenceBasedCrossover() : base() { }
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
36 | return new PotvinEdgePreservingSequenceBasedCrossover(this, cloner);
|
---|
37 | }
|
---|
38 |
|
---|
39 | private PotvinEdgePreservingSequenceBasedCrossover(PotvinEdgePreservingSequenceBasedCrossover original,
|
---|
40 | Cloner cloner) : base(original, cloner) {
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static PotvinEncoding Apply(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2,
|
---|
44 | IVRPProblemInstance problemInstance, bool allowInfeasible) {
|
---|
45 | PotvinEncoding child = parent1.Clone() as PotvinEncoding;
|
---|
46 | Tour newTour = new Tour();
|
---|
47 |
|
---|
48 | int cities = problemInstance.Cities.Value;
|
---|
49 |
|
---|
50 | if (cities > 0) {
|
---|
51 | // find random break point first tour
|
---|
52 | int breakPoint1 = random.Next(1, cities + 1);
|
---|
53 | Tour tour1 = FindRoute(child, breakPoint1);
|
---|
54 | breakPoint1 = tour1.Stops.IndexOf(breakPoint1);
|
---|
55 |
|
---|
56 | // add all stops of the tour from parent1 before breakPoint1
|
---|
57 | // to the resulting child as first tour segment
|
---|
58 | for (int i = 0; i < breakPoint1; i++)
|
---|
59 | newTour.Stops.Add(tour1.Stops[i]);
|
---|
60 |
|
---|
61 | if (newTour.Stops.Count < 1) {
|
---|
62 | Console.Write("x");
|
---|
63 | } else {
|
---|
64 | Console.Write(newTour.Stops.Count);
|
---|
65 | }
|
---|
66 |
|
---|
67 | int lastStop = (newTour.Stops.Count > 0) ? newTour.Stops[newTour.Stops.Count - 1] : random.Next(1, cities + 1);
|
---|
68 |
|
---|
69 | // find the stop in advance to the breakPoint1
|
---|
70 | // within the tour set of parent2
|
---|
71 | // difference to standard SBX: breakPoint2 is not randomly set
|
---|
72 | Tour tour2 = FindRoute(parent2, lastStop);
|
---|
73 | int breakPoint2 = tour2.Stops.IndexOf(lastStop);
|
---|
74 | breakPoint2++; // add everything after the last added stop
|
---|
75 |
|
---|
76 | for (int i = breakPoint2; i < tour2.Stops.Count; i++) {
|
---|
77 | newTour.Stops.Add(tour2.Stops[i]);
|
---|
78 | }
|
---|
79 |
|
---|
80 | int tour1Index = child.Tours.IndexOf(tour1);
|
---|
81 | child.Tours.Remove(tour1);
|
---|
82 | child.Tours.Insert(tour1Index, newTour);
|
---|
83 |
|
---|
84 | foreach (int city in tour1.Stops) {
|
---|
85 | if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
|
---|
86 | child.Unrouted.Add(city);
|
---|
87 | }
|
---|
88 |
|
---|
89 | foreach (int city in tour2.Stops) {
|
---|
90 | if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))
|
---|
91 | child.Unrouted.Add(city);
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (Repair(random, child, newTour, problemInstance, allowInfeasible) || allowInfeasible) {
|
---|
95 | return child;
|
---|
96 | } else {
|
---|
97 | if (random.NextDouble() < 0.5)
|
---|
98 | return parent1.Clone() as PotvinEncoding;
|
---|
99 | else
|
---|
100 | return parent2.Clone() as PotvinEncoding;
|
---|
101 | }
|
---|
102 | } else {
|
---|
103 | return child;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
|
---|
108 | return Apply(random, parent1, parent2, ProblemInstance, AllowInfeasibleSolutions.Value.Value);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|