Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 12966

Last change on this file since 12966 was 7383, checked in by ascheibe, 13 years ago

#1745 merged trunk changes into branch

File size: 31.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
33using HeuristicLab.Problems.VehicleRouting.Encodings.General;
34using HeuristicLab.Problems.VehicleRouting.Encodings.Prins;
35
36namespace HeuristicLab.Problems.VehicleRouting {
37  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class VehicleRoutingProblem : SingleObjectiveHeuristicOptimizationProblem<IVRPEvaluator, IVRPCreator>, IStorableContent {
41    public string Filename { get; set; }
42
43    #region Parameter Properties
44    public ValueParameter<DoubleMatrix> CoordinatesParameter {
45      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
46    }
47    public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
48      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
49    }
50    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
51      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
52    }
53    public ValueParameter<IntValue> VehiclesParameter {
54      get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
55    }
56    public ValueParameter<DoubleValue> CapacityParameter {
57      get { return (ValueParameter<DoubleValue>)Parameters["Capacity"]; }
58    }
59    public ValueParameter<DoubleArray> DemandParameter {
60      get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
61    }
62    public ValueParameter<DoubleArray> ReadyTimeParameter {
63      get { return (ValueParameter<DoubleArray>)Parameters["ReadyTime"]; }
64    }
65    public ValueParameter<DoubleArray> DueTimeParameter {
66      get { return (ValueParameter<DoubleArray>)Parameters["DueTime"]; }
67    }
68    public ValueParameter<DoubleArray> ServiceTimeParameter {
69      get { return (ValueParameter<DoubleArray>)Parameters["ServiceTime"]; }
70    }
71    public IValueParameter<DoubleValue> FleetUsageFactorParameter {
72      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
73    }
74    public IValueParameter<DoubleValue> TimeFactorParameter {
75      get { return (IValueParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
76    }
77    public IValueParameter<DoubleValue> DistanceFactorParameter {
78      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
79    }
80    public IValueParameter<DoubleValue> OverloadPenaltyParameter {
81      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
82    }
83    public IValueParameter<DoubleValue> TardinessPenaltyParameter {
84      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
85    }
86    public OptionalValueParameter<IVRPEncoding> BestKnownSolutionParameter {
87      get { return (OptionalValueParameter<IVRPEncoding>)Parameters["BestKnownSolution"]; }
88    }
89    #endregion
90
91    #region Properties
92    public DoubleMatrix Coordinates {
93      get { return CoordinatesParameter.Value; }
94      set { CoordinatesParameter.Value = value; }
95    }
96    public DoubleMatrix DistanceMatrix {
97      get { return DistanceMatrixParameter.Value; }
98      set { DistanceMatrixParameter.Value = value; }
99    }
100    public BoolValue UseDistanceMatrix {
101      get { return UseDistanceMatrixParameter.Value; }
102      set { UseDistanceMatrixParameter.Value = value; }
103    }
104    public IntValue Vehicles {
105      get { return VehiclesParameter.Value; }
106      set { VehiclesParameter.Value = value; }
107    }
108    public DoubleValue Capacity {
109      get { return CapacityParameter.Value; }
110      set { CapacityParameter.Value = value; }
111    }
112    public DoubleArray Demand {
113      get { return DemandParameter.Value; }
114      set { DemandParameter.Value = value; }
115    }
116    public DoubleArray ReadyTime {
117      get { return ReadyTimeParameter.Value; }
118      set { ReadyTimeParameter.Value = value; }
119    }
120    public DoubleArray DueTime {
121      get { return DueTimeParameter.Value; }
122      set { DueTimeParameter.Value = value; }
123    }
124    public DoubleArray ServiceTime {
125      get { return ServiceTimeParameter.Value; }
126      set { ServiceTimeParameter.Value = value; }
127    }
128    public IVRPEncoding BestKnownSolution {
129      get { return BestKnownSolutionParameter.Value; }
130      set { BestKnownSolutionParameter.Value = value; }
131    }
132    private BestVRPSolutionAnalyzer BestVRPSolutionAnalyzer {
133      get { return Operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }
134    }
135    private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer {
136      get { return Operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
137    }
138    #endregion
139
140    // BackwardsCompatibility3.3
141    #region Backwards compatible code, remove with 3.4
142    [Obsolete]
143    [Storable(Name = "operators")]
144    private IEnumerable<IOperator> oldOperators {
145      get { return null; }
146      set {
147        if (value != null && value.Any())
148          Operators.AddRange(value);
149      }
150    }
151    #endregion
152
153    [StorableConstructor]
154    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
155    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
156      : base(original, cloner) {
157      RegisterEventHandlers();
158    }
159    public VehicleRoutingProblem()
160      : base(new VRPEvaluator(), new RandomCreator()) {
161      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
162      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
163      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
164      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
165      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
166      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
167      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
168      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
169      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
170      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
171      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
172      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
173      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
174      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
175      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
176
177      Maximization.Value = false;
178      MaximizationParameter.Hidden = true;
179
180      SolutionCreator.VRPToursParameter.ActualName = "VRPTours";
181      Evaluator.QualityParameter.ActualName = "VRPQuality";
182
183      InitializeRandomVRPInstance();
184
185      ParameterizeSolutionCreator();
186      ParameterizeEvaluator();
187
188      InitializeOperators();
189      RegisterEventHandlers();
190    }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
193      return new VehicleRoutingProblem(this, cloner);
194    }
195
196    #region Events
197    protected override void OnSolutionCreatorChanged() {
198      base.OnSolutionCreatorChanged();
199      ParameterizeSolutionCreator();
200      ParameterizeEvaluator();
201      ParameterizeAnalyzer();
202      ParameterizeOperators();
203    }
204    protected override void OnEvaluatorChanged() {
205      base.OnEvaluatorChanged();
206      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
207      ParameterizeEvaluator();
208      UpdateMoveEvaluators();
209      ParameterizeAnalyzer();
210    }
211    private void VehiclesValue_ValueChanged(object sender, EventArgs e) {
212      ParameterizeSolutionCreator();
213    }
214    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
215      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
216      Coordinates.Reset += new EventHandler(Coordinates_Reset);
217      ParameterizeSolutionCreator();
218      ClearDistanceMatrix();
219
220      BestKnownSolution = null;
221    }
222    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
223      ClearDistanceMatrix();
224
225      BestKnownSolution = null;
226    }
227    private void Coordinates_Reset(object sender, EventArgs e) {
228      ParameterizeSolutionCreator();
229      ClearDistanceMatrix();
230
231      BestKnownSolution = null;
232    }
233    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
234      ParameterizeAnalyzer();
235    }
236
237    void DistanceFactor_ValueChanged(object sender, EventArgs e) {
238      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactorValue_ValueChanged);
239      EvalBestKnownSolution();
240    }
241    void DistanceFactorValue_ValueChanged(object sender, EventArgs e) {
242      EvalBestKnownSolution();
243    }
244    void FleetUsageFactor_ValueChanged(object sender, EventArgs e) {
245      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactorValue_ValueChanged);
246      EvalBestKnownSolution();
247    }
248    void FleetUsageFactorValue_ValueChanged(object sender, EventArgs e) {
249      EvalBestKnownSolution();
250    }
251    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
252      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenaltyValue_ValueChanged);
253      EvalBestKnownSolution();
254    }
255    void OverloadPenaltyValue_ValueChanged(object sender, EventArgs e) {
256      EvalBestKnownSolution();
257    }
258    void TardinessPenalty_ValueChanged(object sender, EventArgs e) {
259      TardinessPenaltyParameter.Value.ValueChanged += new EventHandler(TardinessPenaltyValue_ValueChanged);
260      EvalBestKnownSolution();
261    }
262    void TardinessPenaltyValue_ValueChanged(object sender, EventArgs e) {
263      EvalBestKnownSolution();
264    }
265    void TimeFactor_ValueChanged(object sender, EventArgs e) {
266      TimeFactorParameter.Value.ValueChanged += new EventHandler(TimeFactorValue_ValueChanged);
267      EvalBestKnownSolution();
268    }
269    void TimeFactorValue_ValueChanged(object sender, EventArgs e) {
270      EvalBestKnownSolution();
271    }
272    void DistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
273      if (DistanceMatrix != null) {
274        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
275        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
276      }
277      EvalBestKnownSolution();
278    }
279    void DistanceMatrix_Reset(object sender, EventArgs e) {
280      EvalBestKnownSolution();
281    }
282    void DistanceMatrix_ItemChanged(object sender, EventArgs<int, int> e) {
283      EvalBestKnownSolution();
284    }
285    void UseDistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
286      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
287      EvalBestKnownSolution();
288    }
289    void UseDistanceMatrix_ValueChanged(object sender, EventArgs e) {
290      EvalBestKnownSolution();
291    }
292    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
293      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
294      BestKnownSolution = null;
295    }
296    void Capacity_ValueChanged(object sender, EventArgs e) {
297      BestKnownSolution = null;
298    }
299    void DemandParameter_ValueChanged(object sender, EventArgs e) {
300      Demand.ItemChanged += new EventHandler<EventArgs<int>>(Demand_ItemChanged);
301      Demand.Reset += new EventHandler(Demand_Reset);
302      BestKnownSolution = null;
303    }
304    void Demand_Reset(object sender, EventArgs e) {
305      BestKnownSolution = null;
306    }
307    void Demand_ItemChanged(object sender, EventArgs<int> e) {
308      BestKnownSolution = null;
309    }
310    void DueTimeParameter_ValueChanged(object sender, EventArgs e) {
311      DueTime.ItemChanged += new EventHandler<EventArgs<int>>(DueTime_ItemChanged);
312      DueTime.Reset += new EventHandler(DueTime_Reset);
313      BestKnownSolution = null;
314    }
315    void DueTime_Reset(object sender, EventArgs e) {
316      BestKnownSolution = null;
317    }
318    void DueTime_ItemChanged(object sender, EventArgs<int> e) {
319      BestKnownSolution = null;
320    }
321    void ReadyTimeParameter_ValueChanged(object sender, EventArgs e) {
322      ReadyTime.ItemChanged += new EventHandler<EventArgs<int>>(ReadyTime_ItemChanged);
323      ReadyTime.Reset += new EventHandler(ReadyTime_Reset);
324      BestKnownSolution = null;
325    }
326    void ReadyTime_Reset(object sender, EventArgs e) {
327      BestKnownSolution = null;
328    }
329    void ReadyTime_ItemChanged(object sender, EventArgs<int> e) {
330      BestKnownSolution = null;
331    }
332    void ServiceTimeParameter_ValueChanged(object sender, EventArgs e) {
333      ServiceTime.ItemChanged += new EventHandler<EventArgs<int>>(ServiceTime_ItemChanged);
334      ServiceTime.Reset += new EventHandler(ServiceTime_Reset);
335      BestKnownSolution = null;
336    }
337    void ServiceTime_Reset(object sender, EventArgs e) {
338      BestKnownSolution = null;
339    }
340    void ServiceTime_ItemChanged(object sender, EventArgs<int> e) {
341      BestKnownSolution = null;
342    }
343    void VehiclesParameter_ValueChanged(object sender, EventArgs e) {
344      Vehicles.ValueChanged += new EventHandler(Vehicles_ValueChanged);
345      BestKnownSolution = null;
346    }
347    void Vehicles_ValueChanged(object sender, EventArgs e) {
348      BestKnownSolution = null;
349    }
350    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
351      EvalBestKnownSolution();
352    }
353    #endregion
354
355    #region Helpers
356    [StorableHook(HookType.AfterDeserialization)]
357    private void AfterDeserialization() {
358      // BackwardsCompatibility3.3
359      #region Backwards compatible code, remove with 3.4
360      if (!Parameters.ContainsKey("BestKnownSolution")) {
361        Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this TSP instance."));
362      }
363      #endregion
364
365      RegisterEventHandlers();
366    }
367
368    private void RegisterEventHandlers() {
369      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
370      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
371      Coordinates.Reset += new EventHandler(Coordinates_Reset);
372
373      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
374
375      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
376
377      DistanceFactorParameter.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
378      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactorValue_ValueChanged);
379      FleetUsageFactorParameter.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
380      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactorValue_ValueChanged);
381      OverloadPenaltyParameter.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
382      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenaltyValue_ValueChanged);
383      TardinessPenaltyParameter.ValueChanged += new EventHandler(TardinessPenalty_ValueChanged);
384      TardinessPenaltyParameter.Value.ValueChanged += new EventHandler(TardinessPenaltyValue_ValueChanged);
385      TimeFactorParameter.ValueChanged += new EventHandler(TimeFactor_ValueChanged);
386      TimeFactorParameter.Value.ValueChanged += new EventHandler(TimeFactorValue_ValueChanged);
387
388      DistanceMatrixParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
389      UseDistanceMatrixParameter.ValueChanged += new EventHandler(UseDistanceMatrixParameter_ValueChanged);
390      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
391
392      CapacityParameter.ValueChanged += new EventHandler(CapacityParameter_ValueChanged);
393      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
394      DemandParameter.ValueChanged += new EventHandler(DemandParameter_ValueChanged);
395      Demand.ItemChanged += new EventHandler<EventArgs<int>>(Demand_ItemChanged);
396      Demand.Reset += new EventHandler(Demand_Reset);
397      DueTimeParameter.ValueChanged += new EventHandler(DueTimeParameter_ValueChanged);
398      DueTime.ItemChanged += new EventHandler<EventArgs<int>>(DueTime_ItemChanged);
399      DueTime.Reset += new EventHandler(DueTime_Reset);
400      ReadyTimeParameter.ValueChanged += new EventHandler(ReadyTimeParameter_ValueChanged);
401      ReadyTime.ItemChanged += new EventHandler<EventArgs<int>>(ReadyTime_ItemChanged);
402      ReadyTime.Reset += new EventHandler(ReadyTime_Reset);
403      ServiceTimeParameter.ValueChanged += new EventHandler(ServiceTimeParameter_ValueChanged);
404      ServiceTime.ItemChanged += new EventHandler<EventArgs<int>>(ServiceTime_ItemChanged);
405      ServiceTime.Reset += new EventHandler(ServiceTime_Reset);
406      VehiclesParameter.ValueChanged += new EventHandler(VehiclesParameter_ValueChanged);
407      Vehicles.ValueChanged += new EventHandler(Vehicles_ValueChanged);
408
409      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
410    }
411
412    private void InitializeOperators() {
413      Operators.Add(new BestVRPSolutionAnalyzer());
414      Operators.Add(new BestAverageWorstVRPToursAnalyzer());
415      ParameterizeAnalyzer();
416      Operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
417      ParameterizeOperators();
418      UpdateMoveEvaluators();
419    }
420    private void UpdateMoveEvaluators() {
421      ParameterizeOperators();
422      OnOperatorsChanged();
423    }
424    private void ParameterizeSolutionCreator() {
425      SolutionCreator.VehiclesParameter.ActualName = VehiclesParameter.Name;
426      SolutionCreator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
427      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
428      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
429      SolutionCreator.CapacityParameter.ActualName = CapacityParameter.Name;
430      SolutionCreator.DemandParameter.ActualName = DemandParameter.Name;
431      SolutionCreator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
432      SolutionCreator.DueTimeParameter.ActualName = DueTimeParameter.Name;
433      SolutionCreator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
434    }
435    private void ParameterizeEvaluator() {
436      Evaluator.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
437      Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
438      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
439      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
440      Evaluator.VehiclesParameter.ActualName = VehiclesParameter.Name;
441      Evaluator.CapacityParameter.ActualName = CapacityParameter.Name;
442      Evaluator.DemandParameter.ActualName = DemandParameter.Name;
443      Evaluator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
444      Evaluator.DueTimeParameter.ActualName = DueTimeParameter.Name;
445      Evaluator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
446      Evaluator.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
447      Evaluator.TimeFactor.ActualName = TimeFactorParameter.Name;
448      Evaluator.DistanceFactor.ActualName = DistanceFactorParameter.Name;
449      Evaluator.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
450      Evaluator.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
451    }
452    private void ParameterizeAnalyzer() {
453      BestVRPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
454      BestVRPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
455      BestVRPSolutionAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
456      BestVRPSolutionAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
457      BestVRPSolutionAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
458      BestVRPSolutionAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
459      BestVRPSolutionAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
460      BestVRPSolutionAnalyzer.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
461      BestVRPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
462
463      BestAverageWorstVRPToursAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
464      BestAverageWorstVRPToursAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
465      BestAverageWorstVRPToursAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
466      BestAverageWorstVRPToursAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
467      BestAverageWorstVRPToursAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
468      BestAverageWorstVRPToursAnalyzer.ResultsParameter.ActualName = "Results";
469    }
470    private void ParameterizeOperators() {
471      foreach (IVRPOperator op in Operators.OfType<IVRPOperator>()) {
472        if (op.CoordinatesParameter != null) op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
473        if (op.DistanceMatrixParameter != null) op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
474        if (op.UseDistanceMatrixParameter != null) op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
475        if (op.VehiclesParameter != null) op.VehiclesParameter.ActualName = VehiclesParameter.Name;
476        if (op.CapacityParameter != null) op.CapacityParameter.ActualName = CapacityParameter.Name;
477        if (op.DemandParameter != null) op.DemandParameter.ActualName = DemandParameter.Name;
478        if (op.ReadyTimeParameter != null) op.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
479        if (op.DueTimeParameter != null) op.DueTimeParameter.ActualName = DueTimeParameter.Name;
480        if (op.ServiceTimeParameter != null) op.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
481      }
482
483      foreach (IVRPMoveOperator op in Operators.OfType<IVRPMoveOperator>()) {
484        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
485      }
486
487      foreach (IPrinsOperator op in Operators.OfType<IPrinsOperator>()) {
488        op.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
489        op.TimeFactor.ActualName = TimeFactorParameter.Name;
490        op.DistanceFactor.ActualName = DistanceFactorParameter.Name;
491        op.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
492        op.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
493      }
494
495      foreach (IVRPMoveEvaluator op in Operators.OfType<IVRPMoveEvaluator>()) {
496        op.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
497        op.TimeFactor.ActualName = TimeFactorParameter.Name;
498        op.DistanceFactor.ActualName = DistanceFactorParameter.Name;
499        op.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
500        op.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
501        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
502        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
503      }
504      string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IAlbaTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
505      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>())
506        op.TranslocationMoveParameter.ActualName = translocationMove;
507
508      foreach (IVRPCrossover op in Operators.OfType<IVRPCrossover>()) {
509        op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
510        op.ChildParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
511      }
512
513      foreach (IVRPManipulator op in Operators.OfType<IVRPManipulator>()) {
514        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
515      }
516
517      foreach (var op in Operators.OfType<IVRPMultiNeighborhoodShakingOperator>()) {
518        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
519      }
520    }
521    private void ClearDistanceMatrix() {
522      DistanceMatrixParameter.Value = null;
523    }
524    #endregion
525
526    public void ImportFromSolomon(string solomonFileName) {
527      SolomonParser parser = new SolomonParser(solomonFileName);
528      parser.Parse();
529
530      this.Name = parser.ProblemName;
531
532      BestKnownSolution = null;
533      Coordinates = new DoubleMatrix(parser.Coordinates);
534      Vehicles.Value = parser.Vehicles;
535      Capacity.Value = parser.Capacity;
536      Demand = new DoubleArray(parser.Demands);
537      ReadyTime = new DoubleArray(parser.Readytimes);
538      DueTime = new DoubleArray(parser.Duetimes);
539      ServiceTime = new DoubleArray(parser.Servicetimes);
540
541      OnReset();
542    }
543
544    public void ImportFromTSPLib(string tspFileName) {
545      TSPLIBParser parser = new TSPLIBParser(tspFileName);
546      parser.Parse();
547
548      this.Name = parser.Name;
549      int problemSize = parser.Demands.Length;
550
551      BestKnownSolution = null;
552      Coordinates = new DoubleMatrix(parser.Vertices);
553      if (parser.Vehicles != -1)
554        Vehicles.Value = parser.Vehicles;
555      else
556        Vehicles.Value = problemSize - 1;
557      Capacity.Value = parser.Capacity;
558      Demand = new DoubleArray(parser.Demands);
559      ReadyTime = new DoubleArray(problemSize);
560      DueTime = new DoubleArray(problemSize);
561      ServiceTime = new DoubleArray(problemSize);
562
563      for (int i = 0; i < problemSize; i++) {
564        ReadyTime[i] = 0;
565        DueTime[i] = int.MaxValue;
566        ServiceTime[i] = 0;
567      }
568
569      if (parser.Distance != -1) {
570        DueTime[0] = parser.Distance;
571      }
572
573      if (parser.Depot != 1)
574        ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
575
576      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
577        ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
578
579      OnReset();
580    }
581
582    private void EvalBestKnownSolution() {
583      if (BestKnownSolution != null) {
584        //call evaluator
585        IValueParameter<DoubleMatrix> distMatrix = new ValueLookupParameter<DoubleMatrix>("DistMatrix",
586          DistanceMatrix);
587
588        TourEvaluation eval = VRPEvaluator.Evaluate(
589          BestKnownSolution,
590          Vehicles,
591          DueTime,
592          ServiceTime,
593          ReadyTime,
594          Demand,
595          Capacity,
596          FleetUsageFactorParameter.Value,
597          TimeFactorParameter.Value,
598          DistanceFactorParameter.Value,
599          OverloadPenaltyParameter.Value,
600          TardinessPenaltyParameter.Value,
601          Coordinates,
602          distMatrix,
603          UseDistanceMatrix);
604
605        DistanceMatrix = distMatrix.Value;
606
607        BestKnownQuality = new DoubleValue(eval.Quality);
608      } else {
609        BestKnownQuality = null;
610      }
611    }
612
613    public void ImportSolution(string solutionFileName) {
614      SolutionParser parser = new SolutionParser(solutionFileName);
615      parser.Parse();
616
617      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding();
618
619      int cities = 0;
620      foreach (List<int> route in parser.Routes) {
621        Encodings.Tour tour = new Encodings.Tour();
622        tour.Cities.AddRange(route);
623        cities += tour.Cities.Count;
624
625        encoding.Tours.Add(tour);
626      }
627
628      if (cities != Coordinates.Rows - 1)
629        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond  with the problem data."));
630      else
631        BestKnownSolutionParameter.Value = encoding;
632    }
633
634    public void ImportFromORLib(string orFileName) {
635      ORLIBParser parser = new ORLIBParser(orFileName);
636      parser.Parse();
637
638      this.Name = parser.Name;
639      int problemSize = parser.Demands.Length;
640
641      BestKnownSolution = null;
642      Coordinates = new DoubleMatrix(parser.Vertices);
643      Vehicles.Value = problemSize - 1;
644      Capacity.Value = parser.Capacity;
645      Demand = new DoubleArray(parser.Demands);
646      ReadyTime = new DoubleArray(problemSize);
647      DueTime = new DoubleArray(problemSize);
648      ServiceTime = new DoubleArray(problemSize);
649
650      ReadyTime[0] = 0;
651      DueTime[0] = parser.MaxRouteTime;
652      ServiceTime[0] = 0;
653
654      for (int i = 1; i < problemSize; i++) {
655        ReadyTime[i] = 0;
656        DueTime[i] = int.MaxValue;
657        ServiceTime[i] = parser.ServiceTime;
658      }
659
660      OnReset();
661    }
662
663    private void InitializeRandomVRPInstance() {
664      System.Random rand = new System.Random();
665
666      int cities = 100;
667
668      Coordinates = new DoubleMatrix(cities + 1, 2);
669      Demand = new DoubleArray(cities + 1);
670      DueTime = new DoubleArray(cities + 1);
671      ReadyTime = new DoubleArray(cities + 1);
672      ServiceTime = new DoubleArray(cities + 1);
673
674      Vehicles.Value = 100;
675      Capacity.Value = 200;
676
677      for (int i = 0; i <= cities; i++) {
678        Coordinates[i, 0] = rand.Next(0, 100);
679        Coordinates[i, 1] = rand.Next(0, 100);
680
681        if (i == 0) {
682          Demand[i] = 0;
683          DueTime[i] = Int16.MaxValue;
684          ReadyTime[i] = 0;
685          ServiceTime[i] = 0;
686        } else {
687          Demand[i] = rand.Next(10, 50);
688          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
689          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
690          ServiceTime[i] = 90;
691        }
692      }
693    }
694  }
695}
Note: See TracBrowser for help on using the repository browser.