Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/RelocateManipulator.cs @ 14777

Last change on this file since 14777 was 9836, checked in by epitzer, 11 years ago

Fix VRP FLA manipulators and adapt to VRP-3.4 (#1703)

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      int count = 0;
52
53      do {
54        feasible = true;
55
56        int cities = individual.Cities;
57        int city = 1 + random.Next(cities);
58        Tour originalTour = individual.Tours.Find(t => t.Stops.Contains(city));
59        //consider creating new route
60        individual.Tours.Add(new Tour());
61
62        int position = 1 + random.Next(cities + individual.Tours.Count - 1);
63        if (position >= city) {
64          position++;
65        }
66
67        var originalFeasible = problemInstance.TourFeasible(originalTour, individual);
68
69        int originalPosition = originalTour.Stops.IndexOf(city);
70        originalTour.Stops.RemoveAt(originalPosition);
71
72        Tour insertionTour;
73        int insertionPosition;
74        if (position <= cities) {
75          insertionTour = individual.Tours.Find(t => t.Stops.Contains(position));
76          insertionPosition = insertionTour.Stops.IndexOf(position) + 1;
77        } else { // additional insertion positions at beginning of tours
78          insertionTour = individual.Tours[position - cities - 1];
79          insertionPosition = 0;
80        }
81        originalFeasible &= problemInstance.TourFeasible(insertionTour, individual);
82        insertionTour.Stops.Insert(insertionPosition, city);
83
84        if (!allowInfeasible && originalFeasible) {
85          feasible = problemInstance.TourFeasible(insertionTour, individual) &&
86                     problemInstance.TourFeasible(originalTour, individual);
87          if (!feasible) {
88            insertionTour.Stops.RemoveAt(insertionPosition);
89            originalTour.Stops.Insert(originalPosition, city);
90          }
91        }
92        individual.Tours.RemoveAll(t => t.Stops.Count == 0);
93      } while (!feasible && count++ < 100);
94    }
95
96    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
97      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
98      Apply(random, individual, ProblemInstance, allowInfeasible);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.