Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs @ 6855

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

Added support for the multi depot CVRP with time windows (#1177)

File size: 15.7 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.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.VehicleRouting.Interfaces;
35using HeuristicLab.Problems.VehicleRouting.Parsers;
36using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
37using HeuristicLab.Problems.VehicleRouting.Variants;
38
39namespace HeuristicLab.Problems.VehicleRouting {
40  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
41  [Creatable("Problems")]
42  [StorableClass]
43  public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
44    public string Filename { get; set; }
45   
46    public override Image ItemImage {
47      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
48    }
49
50    #region Parameter Properties
51    public ValueParameter<BoolValue> MaximizationParameter {
52      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
53    }
54    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
55      get { return MaximizationParameter; }
56    }
57    public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
58      get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
59    }
60    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
61      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
62    }
63    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
64      get { return BestKnownQualityParameter; }
65    }
66    public OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
67      get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
68    }
69    public IValueParameter<IVRPCreator> SolutionCreatorParameter {
70      get { return (IValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
71    }
72    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
73      get { return SolutionCreatorParameter; }
74    }
75    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
76      get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
77    }
78    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
79      get { return EvaluatorParameter; }
80    }
81    #endregion
82
83    #region Properties
84    public IVRPProblemInstance ProblemInstance {
85      get { return ProblemInstanceParameter.Value; }
86      set { ProblemInstanceParameter.Value = value; }
87    }
88
89    public VRPSolution BestKnownSolution {
90      get { return BestKnownSolutionParameter.Value; }
91      set { BestKnownSolutionParameter.Value = value; }
92    }
93
94    public ISingleObjectiveEvaluator Evaluator {
95      get { return ProblemInstance.EvaluatorParameter.Value; }
96    }
97
98    IEvaluator IHeuristicOptimizationProblem.Evaluator {
99      get { return this.Evaluator; }
100    }
101
102    public ISolutionCreator SolutionCreator {
103      get { return ProblemInstance.SolutionCreatorParameter.Value; }
104    }
105
106    [Storable]
107    private List<IOperator> operators;
108
109    public IEnumerable<IOperator> Operators {
110      get { return operators; }
111    }
112    #endregion
113
114    [StorableConstructor]
115    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
116    public VehicleRoutingProblem()
117      : base() {
118      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
119      Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
120      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
121      Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
122
123      operators = new List<IOperator>();
124
125      InitializeRandomVRPInstance();
126      InitializeOperators();
127
128      AttachEventHandlers();
129      AttachProblemInstanceEventHandlers();
130    }
131
132    public override IDeepCloneable Clone(Cloner cloner) {
133      return new VehicleRoutingProblem(this, cloner);
134    }
135
136    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
137      : base(original, cloner) {
138      this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
139      this.AttachEventHandlers();
140    }
141
142    #region Events
143    public event EventHandler SolutionCreatorChanged;
144    private void OnSolutionCreatorChanged() {
145      EventHandler handler = SolutionCreatorChanged;
146      if (handler != null) handler(this, EventArgs.Empty);
147    }
148    public event EventHandler EvaluatorChanged;
149    private void OnEvaluatorChanged() {
150      EventHandler handler = EvaluatorChanged;
151      if (handler != null) handler(this, EventArgs.Empty);
152    }
153    public event EventHandler OperatorsChanged;
154    private void OnOperatorsChanged() {
155      EventHandler handler = OperatorsChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158    public event EventHandler Reset;
159    private void OnReset() {
160      EventHandler handler = Reset;
161      if (handler != null) handler(this, EventArgs.Empty);
162    } 
163    #endregion
164
165    #region Helpers
166    [StorableHook(HookType.AfterDeserialization)]
167    private void AfterDeserializationHook() {
168      AttachEventHandlers();
169      AttachProblemInstanceEventHandlers();
170    }
171
172    private void AttachEventHandlers() {
173      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
174    }
175
176    private void AttachProblemInstanceEventHandlers() {
177      if (Parameters.ContainsKey("Evaluator"))
178        Parameters.Remove("Evaluator");
179
180      if (Parameters.ContainsKey("SolutionCreator"))
181        Parameters.Remove("SolutionCreator");
182
183      if (Parameters.ContainsKey("BestKnownSolution"))
184        Parameters.Remove("BestKnownSolution");
185
186      if (Parameters.ContainsKey("BestKnownQuality"))
187        Parameters.Remove("BestKnownQuality");
188
189
190      if (ProblemInstance != null) {
191        ProblemInstance.SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
192        ProblemInstance.EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
193        Parameters.Add(ProblemInstance.EvaluatorParameter);
194        Parameters.Add(ProblemInstance.SolutionCreatorParameter);
195
196        Parameters.Add(ProblemInstance.BestKnownQualityParameter);
197        Parameters.Add(ProblemInstance.BestKnownSolutionParameter);
198      }
199    }
200
201    void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
202      AttachProblemInstanceEventHandlers();
203      InitializeOperators();
204
205      OnSolutionCreatorChanged();
206      OnEvaluatorChanged();
207      OnOperatorsChanged();
208    }
209    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
210      ParameterizeSolutionCreator();
211     
212      OnSolutionCreatorChanged();
213    }
214    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
215      OnEvaluatorChanged();
216    }
217
218    private void InitializeOperators() {
219      operators = new List<IOperator>();
220
221      if (ProblemInstance != null) {
222        operators.AddRange(
223        ProblemInstance.Operators.Concat(
224          ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
225      }
226
227      ParameterizeOperators();
228    }
229
230    private void ParameterizeSolutionCreator() {
231      if (SolutionCreator is IMultiVRPOperator) {
232        (SolutionCreator as IMultiVRPOperator).SetOperators(Operators);
233      }
234    }
235
236    private void ParameterizeOperators() {
237      foreach (IOperator op in Operators) {
238        if (op is IMultiVRPOperator) {
239          (op as IMultiVRPOperator).SetOperators(Operators);
240        }
241      }
242    }
243    #endregion
244
245    public void ImportFromCordeau(string cordeauFileName) {
246      CordeauParser parser = new CordeauParser(cordeauFileName);
247      parser.Parse();
248
249      MDCVRPTWProblemInstance problem = new MDCVRPTWProblemInstance();
250
251      problem.Coordinates = new DoubleMatrix(parser.Coordinates);
252      problem.Vehicles.Value = parser.Vehicles;
253      problem.Capacity = new DoubleArray(parser.Capacity);
254      problem.Demand = new DoubleArray(parser.Demands);
255      problem.Depots.Value = parser.Depots;
256      problem.VehicleDepotAssignment = new IntArray(parser.Vehicles);
257      problem.ReadyTime = new DoubleArray(parser.Readytimes);
258      problem.ServiceTime = new DoubleArray(parser.Servicetimes);
259      problem.DueTime = new DoubleArray(parser.Duetimes);
260
261      int depot = 0;
262      int i = 0;
263      while(i < parser.Vehicles) {
264        problem.VehicleDepotAssignment[i] = depot;
265
266        i++;
267        if (i % (parser.Vehicles / parser.Depots) == 0)
268          depot++;
269      }
270
271      this.ProblemInstance = problem;
272
273      OnReset();
274    }
275
276    public void ImportFromLiLim(string liLimFileName) {
277      LiLimParser parser = new LiLimParser(liLimFileName);
278      parser.Parse();
279
280      CVRPPDTWProblemInstance problem = new CVRPPDTWProblemInstance();
281
282      problem.Coordinates = new DoubleMatrix(parser.Coordinates);
283      problem.Vehicles.Value = parser.Vehicles;
284      problem.Capacity.Value = parser.Capacity;
285      problem.Demand = new DoubleArray(parser.Demands);
286      problem.ReadyTime = new DoubleArray(parser.Readytimes);
287      problem.DueTime = new DoubleArray(parser.Duetimes);
288      problem.ServiceTime = new DoubleArray(parser.Servicetimes);
289      problem.PickupDeliveryLocation = new IntArray(parser.PickupDeliveryLocations);
290
291      this.ProblemInstance = problem;
292
293      OnReset();
294    }
295
296    public void ImportFromSolomon(string solomonFileName) {
297      SolomonParser parser = new SolomonParser(solomonFileName);
298      parser.Parse();
299
300      this.Name = parser.ProblemName;
301      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
302     
303      problem.Coordinates = new DoubleMatrix(parser.Coordinates);
304      problem.Vehicles.Value = parser.Vehicles;
305      problem.Capacity.Value = parser.Capacity;
306      problem.Demand = new DoubleArray(parser.Demands);
307      problem.ReadyTime = new DoubleArray(parser.Readytimes);
308      problem.DueTime = new DoubleArray(parser.Duetimes);
309      problem.ServiceTime = new DoubleArray(parser.Servicetimes);
310
311      this.ProblemInstance = problem;
312
313      OnReset();
314    }
315
316    public void ImportFromTSPLib(string tspFileName) {
317      TSPLIBParser parser = new TSPLIBParser(tspFileName);
318      parser.Parse();
319
320      this.Name = parser.Name;
321      int problemSize = parser.Demands.Length;
322
323      if (parser.Depot != 1) {
324        ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
325        return;
326      }
327
328      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)  {
329        ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
330        return;
331      }
332
333      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
334      problem.Coordinates = new DoubleMatrix(parser.Vertices);
335      if (parser.Vehicles != -1)
336        problem.Vehicles.Value = parser.Vehicles;
337      else
338        problem.Vehicles.Value = problemSize - 1;
339      problem.Capacity.Value = parser.Capacity;
340      problem.Demand = new DoubleArray(parser.Demands);
341      problem.ReadyTime = new DoubleArray(problemSize);
342      problem.DueTime = new DoubleArray(problemSize);
343      problem.ServiceTime = new DoubleArray(problemSize);
344
345      for (int i = 0; i < problemSize; i++) {
346        problem.ReadyTime[i] = 0;
347        problem.DueTime[i] = int.MaxValue;
348        problem.ServiceTime[i] = 0;
349      }
350
351      if (parser.Distance != -1) {
352        problem.DueTime[0] = parser.Distance;
353      }
354
355      this.ProblemInstance = problem;
356
357      OnReset();
358    }
359
360    public void ImportFromORLib(string orFileName) {
361      ORLIBParser parser = new ORLIBParser(orFileName);
362      parser.Parse();
363
364      this.Name = parser.Name;
365      int problemSize = parser.Demands.Length;
366
367      CVRPProblemInstance problem = new CVRPProblemInstance();
368
369      problem.Coordinates = new DoubleMatrix(parser.Vertices);
370      problem.Vehicles.Value = problemSize - 1;
371      problem.Capacity.Value = parser.Capacity;
372      problem.Demand = new DoubleArray(parser.Demands);
373
374      this.ProblemInstance = problem;
375
376      OnReset();
377    }
378
379    private void InitializeRandomVRPInstance() {
380      System.Random rand = new System.Random();
381
382      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
383      int cities = 100;
384
385      problem.Coordinates = new DoubleMatrix(cities + 1, 2);
386      problem.Demand = new DoubleArray(cities + 1);
387      problem.DueTime = new DoubleArray(cities + 1);
388      problem.ReadyTime = new DoubleArray(cities + 1);
389      problem.ServiceTime = new DoubleArray(cities + 1);
390
391      problem.Vehicles.Value = 100;
392      problem.Capacity.Value = 200;
393
394      for (int i = 0; i <= cities; i++) {
395        problem.Coordinates[i, 0] = rand.Next(0, 100);
396        problem.Coordinates[i, 1] = rand.Next(0, 100);
397
398        if (i == 0) {
399          problem.Demand[i] = 0;
400          problem.DueTime[i] = Int16.MaxValue;
401          problem.ReadyTime[i] = 0;
402          problem.ServiceTime[i] = 0;
403        } else {
404          problem.Demand[i] = rand.Next(10, 50);
405          problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i, null)), 1200);
406          problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
407          problem.ServiceTime[i] = 90;
408        }
409      }
410
411      this.ProblemInstance = problem;
412    }
413
414    public void ImportSolution(string solutionFileName) {
415      SolutionParser parser = new SolutionParser(solutionFileName);
416      parser.Parse();
417
418      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
419
420      int cities = 0;
421      foreach (List<int> route in parser.Routes) {
422        Tour tour = new Tour();
423        tour.Stops.AddRange(route);
424        cities += tour.Stops.Count;
425
426        encoding.Tours.Add(tour);
427      }
428
429      if (cities != ProblemInstance.Coordinates.Rows - 1)
430        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
431      else {
432        VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
433        ProblemInstance.BestKnownSolutionParameter.Value = solution;
434      }
435    }
436  }
437}
Note: See TracBrowser for help on using the repository browser.