Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/TwoOptStar/PotvinTwoOptStarMoveMaker.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Optimization;
26using HeuristicLab.Parameters;
27using HEAL.Attic;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
31  [Item("PotvinTwoOptStarMoveMaker", "Peforms the two opt star move on a given VRP encoding and updates the quality.")]
32  [StorableType("3DCD4D99-DED9-48FB-953C-332F27A6E4D9")]
33  public class PotvinTwoOptStarMoveMaker : PotvinMoveMaker, IPotvinTwoOptStarMoveOperator, IMoveMaker {
34    public ILookupParameter<PotvinTwoOptStarMove> TwoOptStarMoveParameter {
35      get { return (ILookupParameter<PotvinTwoOptStarMove>)Parameters["PotvinTwoOptStarMove"]; }
36    }
37
38    public override ILookupParameter VRPMoveParameter {
39      get { return TwoOptStarMoveParameter; }
40    }
41
42    [StorableConstructor]
43    protected PotvinTwoOptStarMoveMaker(StorableConstructorFlag _) : base(_) { }
44
45    public PotvinTwoOptStarMoveMaker()
46      : base() {
47      Parameters.Add(new LookupParameter<PotvinTwoOptStarMove>("PotvinTwoOptStarMove", "The moves that should be made."));
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new PotvinTwoOptStarMoveMaker(this, cloner);
52    }
53
54    protected PotvinTwoOptStarMoveMaker(PotvinTwoOptStarMoveMaker original, Cloner cloner)
55      : base(original, cloner) {
56    }
57
58    public static void GetSegments(PotvinTwoOptStarMove move, out List<int> segmentX1, out List<int> segmentX2) {
59      PotvinEncoding solution = move.Individual as PotvinEncoding;
60
61      Tour route1 = solution.Tours[move.Tour1];
62      Tour route2 = solution.Tours[move.Tour2];
63
64      int x1 = move.X1;
65      int x2 = move.X2;
66
67      int count = route1.Stops.Count - x1;
68      segmentX1 = new List<int>();
69      if (count > 0) {
70        segmentX1 = route1.Stops.GetRange(x1, count);
71      }
72
73      count = route2.Stops.Count - x2;
74      segmentX2 = new List<int>();
75      if (count > 0) {
76        segmentX2 = route2.Stops.GetRange(x2, count);
77      }
78    }
79
80    public static void Apply(PotvinEncoding solution, PotvinTwoOptStarMove move, IVRPProblemInstance problemInstance) {
81      List<int> segmentX1;
82      List<int> segmentX2;
83      GetSegments(move, out segmentX1, out segmentX2);
84
85      Tour route1 = solution.Tours[move.Tour1];
86      Tour route2 = solution.Tours[move.Tour2];
87
88      foreach (int stop in segmentX1)
89        route1.Stops.Remove(stop);
90      route1.Stops.AddRange(segmentX2);
91
92      foreach (int stop in segmentX2)
93        route2.Stops.Remove(stop);
94      route2.Stops.AddRange(segmentX1);
95    }
96
97    protected override void PerformMove() {
98      PotvinTwoOptStarMove move = TwoOptStarMoveParameter.ActualValue;
99
100      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
101      Apply(newSolution, move, ProblemInstance);
102      newSolution.Repair();
103      VRPToursParameter.ActualValue = newSolution;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.