Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/TwoOptStar/PotvinTwoOptStarMoveMaker.cs @ 17852

Last change on this file since 17852 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.8 KB
RevLine 
[7689]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7689]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
[8053]22using System.Collections.Generic;
23using HeuristicLab.Common;
[7689]24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
[16565]27using HEAL.Attic;
[7689]28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
[7865]31  [Item("PotvinTwoOptStarMoveMaker", "Peforms the two opt star move on a given VRP encoding and updates the quality.")]
[16565]32  [StorableType("3DCD4D99-DED9-48FB-953C-332F27A6E4D9")]
[7865]33  public class PotvinTwoOptStarMoveMaker : PotvinMoveMaker, IPotvinTwoOptStarMoveOperator, IMoveMaker {
34    public ILookupParameter<PotvinTwoOptStarMove> TwoOptStarMoveParameter {
35      get { return (ILookupParameter<PotvinTwoOptStarMove>)Parameters["PotvinTwoOptStarMove"]; }
[7689]36    }
37
38    public override ILookupParameter VRPMoveParameter {
[8053]39      get { return TwoOptStarMoveParameter; }
[7689]40    }
41
42    [StorableConstructor]
[16565]43    protected PotvinTwoOptStarMoveMaker(StorableConstructorFlag _) : base(_) { }
[7689]44
[7865]45    public PotvinTwoOptStarMoveMaker()
[7689]46      : base() {
[7865]47      Parameters.Add(new LookupParameter<PotvinTwoOptStarMove>("PotvinTwoOptStarMove", "The moves that should be made."));
[7689]48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
[7865]51      return new PotvinTwoOptStarMoveMaker(this, cloner);
[7689]52    }
53
[7865]54    protected PotvinTwoOptStarMoveMaker(PotvinTwoOptStarMoveMaker original, Cloner cloner)
[7689]55      : base(original, cloner) {
56    }
57
[7865]58    public static void GetSegments(PotvinTwoOptStarMove move, out List<int> segmentX1, out List<int> segmentX2) {
[7689]59      PotvinEncoding solution = move.Individual as PotvinEncoding;
[8053]60
[7689]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
[7865]80    public static void Apply(PotvinEncoding solution, PotvinTwoOptStarMove move, IVRPProblemInstance problemInstance) {
[7689]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    }
[8053]96
[7689]97    protected override void PerformMove() {
[7865]98      PotvinTwoOptStarMove move = TwoOptStarMoveParameter.ActualValue;
[7689]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.