Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/RelocateManipulator.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.0 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("RelocateManipulator", "Relocate manipulation")]
35  [StorableClass]
36  public sealed class RelocateManipulator : PotvinManipulator {
37    [StorableConstructor]
38    private RelocateManipulator(bool deserializing) : base(deserializing) { }
39    private RelocateManipulator(RelocateManipulator original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new RelocateManipulator(this, cloner);
44    }
45    public RelocateManipulator()
46      : base() {
47    }
48
49    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
50      bool feasible;
51
52      do {
53        feasible = true;
54
55        int cities = individual.Cities;
56        int city = 1 + random.Next(cities);
57        Tour originalTour = individual.Tours.Find(t => t.Stops.Contains(city));
58        //consider creating new route
59        individual.Tours.Add(new Tour());
60
61        int position = 1 + random.Next(cities + individual.Tours.Count - 1);
62        if (position >= city) {
63          position++;
64        }
65
66        int originalPosition = originalTour.Stops.IndexOf(city);
67        originalTour.Stops.RemoveAt(originalPosition);
68
69        Tour insertionTour;
70        int insertionPosition;
71        if (position <= cities) {
72          insertionTour = individual.Tours.Find(t => t.Stops.Contains(position));
73          insertionPosition = insertionTour.Stops.IndexOf(position) + 1;
74        } else {
75          insertionTour = individual.Tours[position - cities - 1];
76          insertionPosition = 0;
77        }
78
79        if (!allowInfeasible && insertionTour != originalTour) {
80          bool originalFeasible = problemInstance.TourFeasible(insertionTour, individual);
81
82          if (originalFeasible) {
83            // try insert into insertionTour
84            insertionTour.Stops.Insert(insertionPosition, city);
85            feasible = problemInstance.TourFeasible(insertionTour, individual);
86
87            if (!feasible) {
88              // when insertionTour is not feasible we undo the change and add to the original tour instead
89              insertionTour.Stops.RemoveAt(insertionPosition);
90              originalTour.Stops.Insert(originalPosition, city);
91            }
92          } else feasible = false;
93        }
94        individual.Tours.RemoveAll(t => t.Stops.Count == 0);
95      } while (!feasible);
96    }
97
98    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
99      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
100      Apply(random, individual, ProblemInstance, allowInfeasible);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.