Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPProblemInstance.cs @ 5127

Last change on this file since 5127 was 5127, checked in by svonolfe, 13 years ago

Implemented unified tabu search (WIP) (#1177)

File size: 12.6 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Common;
34
35namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
36  [Item("VRPProblemInstance", "Represents a VRP instance.")]
37  [StorableClass]
38  public abstract class VRPProblemInstance: ParameterizedNamedItem, IVRPProblemInstance {
39    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
40      get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
41    }
42
43    public IValueParameter<IVRPCreator> SolutionCreatorParameter {
44      get { return (ValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
45    }
46
47    protected abstract IEnumerable<IOperator> GetOperators();
48    protected abstract IEnumerable<IOperator> GetAnalyzers();
49
50    public IEnumerable<IOperator> Operators {
51      get {
52        return GetOperators().Union(GetAnalyzers());
53      }
54    }
55   
56    protected ValueParameter<DoubleMatrix> CoordinatesParameter {
57      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
58    }
59    protected OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
60      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
61    }
62    protected ValueParameter<BoolValue> UseDistanceMatrixParameter {
63      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
64    }
65    protected ValueParameter<IntValue> VehiclesParameter {
66      get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
67    }
68    protected ValueParameter<DoubleArray> DemandParameter {
69      get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
70    }
71
72    protected IValueParameter<DoubleValue> FleetUsageFactorParameter {
73      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
74    }
75    protected IValueParameter<DoubleValue> DistanceFactorParameter {
76      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
77    }
78
79    protected OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
80      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
81    }
82    protected OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
83      get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
84    }
85
86    IValueParameter<DoubleValue> IVRPProblemInstance.BestKnownQualityParameter {
87      get { return BestKnownQualityParameter; }
88    }
89
90    IValueParameter<VRPSolution> IVRPProblemInstance.BestKnownSolutionParameter {
91      get { return BestKnownSolutionParameter; }
92    }
93
94    public DoubleMatrix Coordinates {
95      get { return CoordinatesParameter.Value; }
96      set { CoordinatesParameter.Value = value; }
97    }
98
99    public DoubleMatrix DistanceMatrix {
100      get { return DistanceMatrixParameter.Value; }
101      set { DistanceMatrixParameter.Value = value; }
102    }
103    public BoolValue UseDistanceMatrix {
104      get { return UseDistanceMatrixParameter.Value; }
105      set { UseDistanceMatrixParameter.Value = value; }
106    }
107    public IntValue Vehicles {
108      get { return VehiclesParameter.Value; }
109      set { VehiclesParameter.Value = value; }
110    }
111    public DoubleArray Demand {
112      get { return DemandParameter.Value; }
113      set { DemandParameter.Value = value; }
114    }
115    public virtual IntValue Cities {
116      get { return new IntValue(Demand.Length); }
117    }
118
119    public DoubleValue FleetUsageFactor {
120      get { return FleetUsageFactorParameter.Value; }
121      set { FleetUsageFactorParameter.Value = value; }
122    }
123    public DoubleValue DistanceFactor {
124      get { return DistanceFactorParameter.Value; }
125      set { DistanceFactorParameter.Value = value; }
126    }
127
128    public DoubleValue BestKnownQuality {
129      get { return BestKnownQualityParameter.Value; }
130      set { BestKnownQualityParameter.Value = value; }
131    }
132    public VRPSolution BestKnownSolution {
133      get { return BestKnownSolutionParameter.Value; }
134      set { BestKnownSolutionParameter.Value = value; }
135    }
136
137    private double CalculateDistance(int start, int end) {
138      double distance = 0.0;
139
140      distance =
141          Math.Sqrt(
142            Math.Pow(Coordinates[start, 0] - Coordinates[end, 0], 2) +
143            Math.Pow(Coordinates[start, 1] - Coordinates[end, 1], 2));
144
145      return distance;
146    }
147
148    private DoubleMatrix CreateDistanceMatrix() {
149      DoubleMatrix distanceMatrix = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows);
150
151      for (int i = 0; i < distanceMatrix.Rows; i++) {
152        for (int j = i; j < distanceMatrix.Columns; j++) {
153          double distance = CalculateDistance(i, j);
154
155          distanceMatrix[i, j] = distance;
156          distanceMatrix[j, i] = distance;
157        }
158      }
159
160      return distanceMatrix;
161    }
162   
163    public double GetDistance(int start, int end) {
164      double distance = 0.0;
165      DoubleMatrix distanceMatrix = DistanceMatrix;
166
167      if (distanceMatrix == null && UseDistanceMatrix.Value) {
168        distanceMatrix = DistanceMatrix = CreateDistanceMatrix();
169      }
170
171      if (distanceMatrix != null)
172        distance = distanceMatrix[start, end];
173      else
174        distance = CalculateDistance(start, end);
175
176      return distance;
177    }
178
179    public bool Feasible(IVRPEncoding solution) {
180      return EvaluatorParameter.Value.Feasible(
181        EvaluatorParameter.Value.Evaluate(
182          this, solution));
183    }
184
185    public bool Feasible(Tour tour) {
186      return EvaluatorParameter.Value.Feasible(
187        EvaluatorParameter.Value.Evaluate(
188        this, tour));
189    }
190
191    public double Evaluate(IVRPEncoding solution) {
192      return EvaluatorParameter.Value.Evaluate(this, solution).Quality;
193    }
194
195    public double Evaluate(Tour tour) {
196      return EvaluatorParameter.Value.Evaluate(this, tour).Quality;
197    }
198
199    protected void EvalBestKnownSolution() {
200      if (BestKnownSolution != null) {
201        //call evaluator
202        BestKnownQuality = new DoubleValue(Evaluate(BestKnownSolution.Solution));
203        BestKnownSolution.Quality = BestKnownQuality;
204      } else {
205        BestKnownQuality = null;
206      }
207    }
208
209    protected abstract IVRPEvaluator Evaluator { get; }
210    protected abstract IVRPCreator Creator { get; }
211   
212    [StorableConstructor]
213    protected VRPProblemInstance(bool deserializing) : base(deserializing) { }
214
215    public VRPProblemInstance()
216      : base() {
217      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
218      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
219      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
220      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
221      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
222
223      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(0)));
224      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
225
226      Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", Creator));
227      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", Evaluator));
228
229      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
230      Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
231
232      AttachEventHandlers();
233    }
234
235    protected VRPProblemInstance(VRPProblemInstance original, Cloner cloner)
236      : base(original, cloner) {
237        AttachEventHandlers();
238    }
239
240    [StorableHook(HookType.AfterDeserialization)]
241    private void AfterDeserializationHook() {
242      #region Backwards Compatibility
243      if (!Parameters.ContainsKey("BestKnownSolution")) {
244        Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this TSP instance."));
245      }
246      if (!Parameters.ContainsKey("BestKnownQuality")) {
247        Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
248      }
249      #endregion
250     
251      AttachEventHandlers();
252    }
253
254    private void AttachEventHandlers() {
255      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
256      DistanceFactorParameter.ValueChanged += new EventHandler(DistanceFactorParameter_ValueChanged);
257      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
258      FleetUsageFactorParameter.ValueChanged += new EventHandler(FleetUsageFactorParameter_ValueChanged);
259      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
260      DistanceMatrixParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
261      if (DistanceMatrix != null) {
262        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
263        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
264      }
265      UseDistanceMatrixParameter.ValueChanged += new EventHandler(UseDistanceMatrixParameter_ValueChanged);
266      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
267    }
268
269    #region Event handlers
270    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
271      EvalBestKnownSolution();
272    }
273    void DistanceFactorParameter_ValueChanged(object sender, EventArgs e) {
274      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
275      EvalBestKnownSolution();
276    }
277    void DistanceFactor_ValueChanged(object sender, EventArgs e) {
278      EvalBestKnownSolution();
279    }
280    void FleetUsageFactorParameter_ValueChanged(object sender, EventArgs e) {
281      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
282      EvalBestKnownSolution();
283    }
284    void FleetUsageFactor_ValueChanged(object sender, EventArgs e) {
285      EvalBestKnownSolution();
286    }
287    void DistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
288      if (DistanceMatrix != null) {
289        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
290        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
291      }
292      EvalBestKnownSolution();
293    }
294    void DistanceMatrix_Reset(object sender, EventArgs e) {
295      EvalBestKnownSolution();
296    }
297    void DistanceMatrix_ItemChanged(object sender, EventArgs<int, int> e) {
298      EvalBestKnownSolution();
299    }
300    void UseDistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
301      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
302      EvalBestKnownSolution();
303    }
304    void UseDistanceMatrix_ValueChanged(object sender, EventArgs e) {
305      EvalBestKnownSolution();
306    }
307    #endregion
308  }
309}
Note: See TracBrowser for help on using the repository browser.