Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/OnePointCrossover/PotvinOnePointCrossoverMoveMaker.cs @ 7689

Last change on this file since 7689 was 7689, checked in by svonolfe, 12 years ago

Added 2Opt* Move (#1177)

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