Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptStarManipulator.cs @ 9750

Last change on this file since 9750 was 9750, checked in by gkronber, 11 years ago

#1591: adapted FLA branch to reference most recent version of ALGLIB (3.7.0) and VRP (3.4). Several major changes were necessary to port the implementation to the new VRP problem.

File size: 4.5 KB
Line 
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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
29using HeuristicLab.Problems.VehicleRouting.Encodings;
30using HeuristicLab.Problems.VehicleRouting;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32
33namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
34  [Item("TwoOptStarManipulator", "Two opt star manipulation")]
35  [StorableClass]
36  public sealed class TwoOptStarManipulator : PotvinManipulator {
37    [StorableConstructor]
38    private TwoOptStarManipulator(bool deserializing) : base(deserializing) { }
39    private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new TwoOptStarManipulator(this, cloner);
44    }
45    public TwoOptStarManipulator()
46      : base() {
47    }
48
49    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
50      bool feasible;
51
52      //consider creating new tour
53      individual.Tours.Add(new Tour());
54
55      do {
56        feasible = true;
57
58        int route1Idx = random.Next(individual.Tours.Count);
59        int route2Idx = random.Next(individual.Tours.Count - 1);
60        if (route2Idx >= route1Idx)
61          route2Idx++;
62
63        Tour route1 = individual.Tours[route1Idx];
64        Tour route2 = individual.Tours[route2Idx];
65
66        int x1 = random.Next(route1.Stops.Count + 1);
67        int x2 = random.Next(route2.Stops.Count + 1);
68
69        if (!allowInfeasible) {
70          bool originalFeasible = problemInstance.TourFeasible(route1, individual) &&
71                                  problemInstance.TourFeasible(route2, individual);
72
73          if (originalFeasible) {
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            }
80
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            }
87
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);
99              }
100            } else {
101              // undo changes
102              route1.Stops.RemoveRange(x1, segmentX2.Count);
103              route1.Stops.AddRange(segmentX1);
104              route2.Stops.AddRange(segmentX2);
105            }
106
107          } else {
108            feasible = false;
109          }
110        }
111      } while (!feasible);
112
113      individual.Tours.RemoveAll(t => t.Stops.Count == 0);
114    }
115
116    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
117      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
118      Apply(random, individual, ProblemInstance, allowInfeasible);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.