Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added 2Opt* Move (#1177)

File size: 6.8 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Optimization.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.VehicleRouting.Interfaces;
28using HeuristicLab.Operators;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using System.Collections.Generic;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
34  [Item("PotvinOnePointCrossoverMoveTabuMaker", "Declares a given exchange move as tabu.")]
35  [StorableClass]
36  public class PotvinOnePointCrossoverMoveTabuMaker : SingleSuccessorOperator, ITabuMaker, IPotvinOnePointCrossoverMoveOperator, IPotvinOperator, IVRPMoveOperator {
37    public LookupParameter<ItemList<IItem>> TabuListParameter {
38      get { return (LookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
39    }
40    public ValueLookupParameter<IntValue> TabuTenureParameter {
41      get { return (ValueLookupParameter<IntValue>)Parameters["TabuTenure"]; }
42    }
43    public ILookupParameter<DoubleValue> MoveQualityParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
45    }
46    public ILookupParameter<DoubleValue> QualityParameter {
47      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public IValueLookupParameter<BoolValue> MaximizationParameter {
50      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
51    }
52
53
54    public ILookupParameter<PotvinOnePointCrossoverMove> OnePointCrossoverMoveParameter {
55      get { return (ILookupParameter<PotvinOnePointCrossoverMove>)Parameters["PotvinOnePointCrossoverMove"]; }
56    }
57    public ILookupParameter VRPMoveParameter {
58      get { return OnePointCrossoverMoveParameter; }
59    }
60    public ILookupParameter<IVRPEncoding> VRPToursParameter {
61      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
62    }
63    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
64      get { return (LookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
65    }
66
67    public ILookupParameter<DoubleValue> DistanceParameter {
68      get { return (ILookupParameter<DoubleValue>)Parameters["Distance"]; }
69    }
70    public ILookupParameter<DoubleValue> OverloadParameter {
71      get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
72    }
73    public ILookupParameter<DoubleValue> TardinessParameter {
74      get { return (ILookupParameter<DoubleValue>)Parameters["Tardiness"]; }
75    }
76
77    [StorableConstructor]
78    protected PotvinOnePointCrossoverMoveTabuMaker(bool deserializing) : base(deserializing) { }
79    protected PotvinOnePointCrossoverMoveTabuMaker(PotvinOnePointCrossoverMoveTabuMaker original, Cloner cloner) : base(original, cloner) { }
80    public PotvinOnePointCrossoverMoveTabuMaker()
81      : base() {
82      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list where move attributes are stored."));
83      Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The tenure of the tabu list."));
84      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
85      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the solution."));
86      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
87
88      Parameters.Add(new LookupParameter<PotvinOnePointCrossoverMove>("PotvinOnePointCrossoverMove", "The moves that should be made."));
89      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours considered in the move."));
90      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
91
92      Parameters.Add(new LookupParameter<DoubleValue>("Distance", "The distance of the individual"));
93      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload of the individual"));
94      Parameters.Add(new LookupParameter<DoubleValue>("Tardiness", "The tardiness of the individual"));
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new PotvinOnePointCrossoverMoveTabuMaker(this, cloner);
99    }
100
101    public override IOperation Apply() {
102      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
103      int tabuTenure = TabuTenureParameter.ActualValue.Value;
104
105      int overlength = tabuList.Count - tabuTenure;
106      if (overlength >= 0) {
107        for (int i = 0; i < tabuTenure - 1; i++)
108          tabuList[i] = tabuList[i + overlength + 1];
109        while (tabuList.Count >= tabuTenure)
110          tabuList.RemoveAt(tabuList.Count - 1);
111      }
112
113      double distance = 0;
114      if (DistanceParameter.ActualValue != null)
115        distance = DistanceParameter.ActualValue.Value;
116
117      double overload = 0;
118      if (OverloadParameter.ActualValue != null)
119        overload = OverloadParameter.ActualValue.Value;
120
121      double tardiness = 0;
122      if (TardinessParameter.ActualValue != null)
123        tardiness = TardinessParameter.ActualValue.Value;
124
125      PotvinOnePointCrossoverMove move = OnePointCrossoverMoveParameter.ActualValue;
126      double moveQuality = MoveQualityParameter.ActualValue.Value;
127      double quality = QualityParameter.ActualValue.Value;
128      double baseQuality = moveQuality;
129      if (quality < moveQuality) baseQuality = quality; // we make an uphill move, the lower bound is the solution quality
130
131      List<int> segmentX1;
132      List<int> segmentX2;
133      PotvinOnePointCrossoverMoveMaker.GetSegments(move, out segmentX1, out segmentX2);
134
135      foreach (int city in segmentX1) {
136        tabuList.Add(new PotvinOnePointCrossoverMoveAttribute(baseQuality, move.Tour1, city, distance, overload, tardiness));
137      }
138
139      foreach (int city in segmentX2) {
140        tabuList.Add(new PotvinOnePointCrossoverMoveAttribute(baseQuality, move.Tour2, city, distance, overload, tardiness));
141      }
142     
143      return base.Apply();
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.