Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 4656

Last change on this file since 4656 was 4626, checked in by svonolfe, 14 years ago

Fixed null reference exception #1236

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