Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 5718

Last change on this file since 5718 was 5578, checked in by mkommend, 14 years ago

#1418: Added IHeuristicOptimizationProblem and adapted all according classes.

File size: 36.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
43    public string Filename { get; set; }
44
45    public override Image ItemImage {
46      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
47    }
48
49    #region Parameter Properties
50    public ValueParameter<BoolValue> MaximizationParameter {
51      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
52    }
53    IParameter ISingleObjectiveHeuristicOptimizationProblem.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 IHeuristicOptimizationProblem.SolutionCreatorParameter {
87      get { return SolutionCreatorParameter; }
88    }
89    ValueParameter<IVRPEvaluator> EvaluatorParameter {
90      get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
91    }
92    IParameter IHeuristicOptimizationProblem.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 ISingleObjectiveHeuristicOptimizationProblem.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 IHeuristicOptimizationProblem.SolutionCreator {
171      get { return SolutionCreatorParameter.Value; }
172    }
173    IVRPEvaluator Evaluator {
174      get { return EvaluatorParameter.Value; }
175      set { EvaluatorParameter.Value = value; }
176    }
177    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
178      get { return EvaluatorParameter.Value; }
179    }
180    IEvaluator IHeuristicOptimizationProblem.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    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
200      : base(original, cloner) {
201      operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
202      AttachEventHandlers();
203    }
204    public VehicleRoutingProblem()
205      : base() {
206      IVRPCreator creator = new RandomCreator();
207      IVRPEvaluator evaluator = new VRPEvaluator();
208
209      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
210      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
211      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
212      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
213      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
214      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
215      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
216      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
217      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
218      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
219      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
220      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
221      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
222      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
223      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
224      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
225      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
226
227      Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", creator));
228      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", evaluator));
229
230      creator.VRPToursParameter.ActualName = "VRPTours";
231      evaluator.QualityParameter.ActualName = "VRPQuality";
232
233      InitializeRandomVRPInstance();
234
235      ParameterizeSolutionCreator();
236      ParameterizeEvaluator();
237
238      InitializeOperators();
239      AttachEventHandlers();
240    }
241
242    public override IDeepCloneable Clone(Cloner cloner) {
243      return new VehicleRoutingProblem(this, cloner);
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      BestKnownSolution = null;
622      Coordinates = new DoubleMatrix(parser.Coordinates);
623      Vehicles.Value = parser.Vehicles;
624      Capacity.Value = parser.Capacity;
625      Demand = new DoubleArray(parser.Demands);
626      ReadyTime = new DoubleArray(parser.Readytimes);
627      DueTime = new DoubleArray(parser.Duetimes);
628      ServiceTime = new DoubleArray(parser.Servicetimes);
629
630      OnReset();
631    }
632
633    public void ImportFromTSPLib(string tspFileName) {
634      TSPLIBParser parser = new TSPLIBParser(tspFileName);
635      parser.Parse();
636
637      this.Name = parser.Name;
638      int problemSize = parser.Demands.Length;
639
640      BestKnownSolution = null;
641      Coordinates = new DoubleMatrix(parser.Vertices);
642      if (parser.Vehicles != -1)
643        Vehicles.Value = parser.Vehicles;
644      else
645        Vehicles.Value = problemSize - 1;
646      Capacity.Value = parser.Capacity;
647      Demand = new DoubleArray(parser.Demands);
648      ReadyTime = new DoubleArray(problemSize);
649      DueTime = new DoubleArray(problemSize);
650      ServiceTime = new DoubleArray(problemSize);
651
652      for (int i = 0; i < problemSize; i++) {
653        ReadyTime[i] = 0;
654        DueTime[i] = int.MaxValue;
655        ServiceTime[i] = 0;
656      }
657
658      if (parser.Distance != -1) {
659        DueTime[0] = parser.Distance;
660      }
661
662      if (parser.Depot != 1)
663        ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
664
665      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
666        ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
667
668      OnReset();
669    }
670
671    private void EvalBestKnownSolution() {
672      if (BestKnownSolution != null) {
673        //call evaluator
674        IValueParameter<DoubleMatrix> distMatrix = new ValueLookupParameter<DoubleMatrix>("DistMatrix",
675          DistanceMatrix);
676
677        TourEvaluation eval = VRPEvaluator.Evaluate(
678          BestKnownSolution,
679          Vehicles,
680          DueTime,
681          ServiceTime,
682          ReadyTime,
683          Demand,
684          Capacity,
685          FleetUsageFactorParameter.Value,
686          TimeFactorParameter.Value,
687          DistanceFactorParameter.Value,
688          OverloadPenaltyParameter.Value,
689          TardinessPenaltyParameter.Value,
690          Coordinates,
691          distMatrix,
692          UseDistanceMatrix);
693
694        DistanceMatrix = distMatrix.Value;
695
696        BestKnownQuality = new DoubleValue(eval.Quality);
697      } else {
698        BestKnownQuality = null;
699      }
700    }
701
702    public void ImportSolution(string solutionFileName) {
703      SolutionParser parser = new SolutionParser(solutionFileName);
704      parser.Parse();
705
706      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding();
707
708      int cities = 0;
709      foreach (List<int> route in parser.Routes) {
710        Encodings.Tour tour = new Encodings.Tour();
711        tour.Cities.AddRange(route);
712        cities += tour.Cities.Count;
713
714        encoding.Tours.Add(tour);
715      }
716
717      if (cities != Coordinates.Rows - 1)
718        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond  with the problem data."));
719      else
720        BestKnownSolutionParameter.Value = encoding;
721    }
722
723    public void ImportFromORLib(string orFileName) {
724      ORLIBParser parser = new ORLIBParser(orFileName);
725      parser.Parse();
726
727      this.Name = parser.Name;
728      int problemSize = parser.Demands.Length;
729
730      BestKnownSolution = null;
731      Coordinates = new DoubleMatrix(parser.Vertices);
732      Vehicles.Value = problemSize - 1;
733      Capacity.Value = parser.Capacity;
734      Demand = new DoubleArray(parser.Demands);
735      ReadyTime = new DoubleArray(problemSize);
736      DueTime = new DoubleArray(problemSize);
737      ServiceTime = new DoubleArray(problemSize);
738
739      ReadyTime[0] = 0;
740      DueTime[0] = parser.MaxRouteTime;
741      ServiceTime[0] = 0;
742
743      for (int i = 1; i < problemSize; i++) {
744        ReadyTime[i] = 0;
745        DueTime[i] = int.MaxValue;
746        ServiceTime[i] = parser.ServiceTime;
747      }
748
749      OnReset();
750    }
751
752    private void InitializeRandomVRPInstance() {
753      System.Random rand = new System.Random();
754
755      int cities = 100;
756
757      Coordinates = new DoubleMatrix(cities + 1, 2);
758      Demand = new DoubleArray(cities + 1);
759      DueTime = new DoubleArray(cities + 1);
760      ReadyTime = new DoubleArray(cities + 1);
761      ServiceTime = new DoubleArray(cities + 1);
762
763      Vehicles.Value = 100;
764      Capacity.Value = 200;
765
766      for (int i = 0; i <= cities; i++) {
767        Coordinates[i, 0] = rand.Next(0, 100);
768        Coordinates[i, 1] = rand.Next(0, 100);
769
770        if (i == 0) {
771          Demand[i] = 0;
772          DueTime[i] = Int16.MaxValue;
773          ReadyTime[i] = 0;
774          ServiceTime[i] = 0;
775        } else {
776          Demand[i] = rand.Next(10, 50);
777          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
778          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
779          ServiceTime[i] = 90;
780        }
781      }
782    }
783  }
784}
Note: See TracBrowser for help on using the repository browser.