[7145] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Encodings;
|
---|
| 30 | using HeuristicLab.Problems.VehicleRouting;
|
---|
[9750] | 31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[7145] | 32 |
|
---|
[7159] | 33 | namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
|
---|
| 34 | [Item("TwoOptStarManipulator", "Two opt star manipulation")]
|
---|
[7145] | 35 | [StorableClass]
|
---|
[7159] | 36 | public sealed class TwoOptStarManipulator : PotvinManipulator {
|
---|
[7145] | 37 | [StorableConstructor]
|
---|
[7159] | 38 | private TwoOptStarManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 39 | private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner)
|
---|
[7145] | 40 | : base(original, cloner) {
|
---|
| 41 | }
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[7159] | 43 | return new TwoOptStarManipulator(this, cloner);
|
---|
[7145] | 44 | }
|
---|
[7159] | 45 | public TwoOptStarManipulator()
|
---|
[7145] | 46 | : base() {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[9750] | 49 | public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
|
---|
[7145] | 50 | bool feasible;
|
---|
| 51 |
|
---|
[7151] | 52 | //consider creating new tour
|
---|
| 53 | individual.Tours.Add(new Tour());
|
---|
| 54 |
|
---|
[7145] | 55 | do {
|
---|
| 56 | feasible = true;
|
---|
| 57 |
|
---|
[7151] | 58 | int route1Idx = random.Next(individual.Tours.Count);
|
---|
| 59 | int route2Idx = random.Next(individual.Tours.Count - 1);
|
---|
| 60 | if (route2Idx >= route1Idx)
|
---|
| 61 | route2Idx++;
|
---|
[7145] | 62 |
|
---|
[7151] | 63 | Tour route1 = individual.Tours[route1Idx];
|
---|
| 64 | Tour route2 = individual.Tours[route2Idx];
|
---|
[7145] | 65 |
|
---|
[9750] | 66 | int x1 = random.Next(route1.Stops.Count + 1);
|
---|
| 67 | int x2 = random.Next(route2.Stops.Count + 1);
|
---|
[7145] | 68 |
|
---|
[7151] | 69 | if (!allowInfeasible) {
|
---|
[9750] | 70 | bool originalFeasible = problemInstance.TourFeasible(route1, individual) &&
|
---|
| 71 | problemInstance.TourFeasible(route2, individual);
|
---|
[7145] | 72 |
|
---|
[7151] | 73 | if (originalFeasible) {
|
---|
[9750] | 74 | int count = route1.Stops.Count - x1;
|
---|
| 75 | List<int> segmentX1 = new List<int>();
|
---|
| 76 | if (count > 0) {
|
---|
| 77 | segmentX1 = route1.Stops.GetRange(x1, count);
|
---|
| 78 | route1.Stops.RemoveRange(x1, count);
|
---|
| 79 | }
|
---|
[7151] | 80 |
|
---|
[9750] | 81 | count = route2.Stops.Count - x2;
|
---|
| 82 | List<int> segmentX2 = new List<int>();
|
---|
| 83 | if (count > 0) {
|
---|
| 84 | segmentX2 = route2.Stops.GetRange(x2, count);
|
---|
| 85 | route2.Stops.RemoveRange(x2, count);
|
---|
| 86 | }
|
---|
[7145] | 87 |
|
---|
[9750] | 88 | route1.Stops.AddRange(segmentX2);
|
---|
| 89 | feasible = problemInstance.TourFeasible(route1, individual);
|
---|
| 90 | if (feasible) {
|
---|
| 91 | route2.Stops.AddRange(segmentX1);
|
---|
| 92 | feasible = problemInstance.TourFeasible(route2, individual);
|
---|
| 93 | if (!feasible) {
|
---|
| 94 | // undo all changes
|
---|
| 95 | route1.Stops.RemoveRange(x1, segmentX2.Count);
|
---|
| 96 | route1.Stops.AddRange(segmentX1);
|
---|
| 97 | route2.Stops.RemoveRange(x2, segmentX1.Count);
|
---|
| 98 | route2.Stops.AddRange(segmentX2);
|
---|
[7145] | 99 | }
|
---|
[9750] | 100 | } else {
|
---|
| 101 | // undo changes
|
---|
| 102 | route1.Stops.RemoveRange(x1, segmentX2.Count);
|
---|
| 103 | route1.Stops.AddRange(segmentX1);
|
---|
| 104 | route2.Stops.AddRange(segmentX2);
|
---|
[7145] | 105 | }
|
---|
| 106 |
|
---|
[9750] | 107 | } else {
|
---|
| 108 | feasible = false;
|
---|
[7151] | 109 | }
|
---|
[7145] | 110 | }
|
---|
| 111 | } while (!feasible);
|
---|
[7151] | 112 |
|
---|
[9750] | 113 | individual.Tours.RemoveAll(t => t.Stops.Count == 0);
|
---|
[7145] | 114 | }
|
---|
| 115 |
|
---|
| 116 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
| 117 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
[9750] | 118 | Apply(random, individual, ProblemInstance, allowInfeasible);
|
---|
[7145] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|