Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2707_HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinEdgePreservingSequenceBasedCrossover.cs @ 17011

Last change on this file since 17011 was 17011, checked in by pfleck, 5 years ago

#2707 Adapted to HEAL.Attic changes

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