Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13820 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
RevLine 
[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
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;
[9750]31using HeuristicLab.Problems.VehicleRouting.Interfaces;
[7145]32
[7159]33namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
34  [Item("RelocateManipulator", "Relocate manipulation")]
[7145]35  [StorableClass]
[7159]36  public sealed class RelocateManipulator : PotvinManipulator {
[7145]37    [StorableConstructor]
[7159]38    private RelocateManipulator(bool deserializing) : base(deserializing) { }
39    private RelocateManipulator(RelocateManipulator original, Cloner cloner)
[7145]40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
[7159]43      return new RelocateManipulator(this, cloner);
[7145]44    }
[7159]45    public RelocateManipulator()
[7145]46      : base() {
47    }
48
[9750]49    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
[7145]50      bool feasible;
[9836]51      int count = 0;
[7145]52
53      do {
54        feasible = true;
55
56        int cities = individual.Cities;
57        int city = 1 + random.Next(cities);
[9750]58        Tour originalTour = individual.Tours.Find(t => t.Stops.Contains(city));
[7145]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
[9836]67        var originalFeasible = problemInstance.TourFeasible(originalTour, individual);
68
[9750]69        int originalPosition = originalTour.Stops.IndexOf(city);
70        originalTour.Stops.RemoveAt(originalPosition);
[7145]71
72        Tour insertionTour;
73        int insertionPosition;
74        if (position <= cities) {
[9750]75          insertionTour = individual.Tours.Find(t => t.Stops.Contains(position));
76          insertionPosition = insertionTour.Stops.IndexOf(position) + 1;
[9836]77        } else { // additional insertion positions at beginning of tours
[7145]78          insertionTour = individual.Tours[position - cities - 1];
79          insertionPosition = 0;
80        }
[9836]81        originalFeasible &= problemInstance.TourFeasible(insertionTour, individual);
82        insertionTour.Stops.Insert(insertionPosition, city);
[7145]83
[9836]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          }
[7145]91        }
[9750]92        individual.Tours.RemoveAll(t => t.Stops.Count == 0);
[9836]93      } while (!feasible && count++ < 100);
[7145]94    }
95
96    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
97      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
[9750]98      Apply(random, individual, ProblemInstance, allowInfeasible);
[7145]99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.