Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 6727

Last change on this file since 6727 was 6618, checked in by mkommend, 13 years ago

#1479: Integrated trunk changes.

File size: 36.1 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    public IVRPCreator SolutionCreator {
167      get { return SolutionCreatorParameter.Value; }
168      set { SolutionCreatorParameter.Value = value; }
169    }
170    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
171      get { return SolutionCreatorParameter.Value; }
172    }
173    public 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      foreach (var op in Operators.OfType<IVRPMultiNeighborhoodShakingOperator>()) {
611        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
612      }
613    }
614    private void ClearDistanceMatrix() {
615      DistanceMatrixParameter.Value = null;
616    }
617    #endregion
618
619    public void ImportFromSolomon(string solomonFileName) {
620      SolomonParser parser = new SolomonParser(solomonFileName);
621      parser.Parse();
622
623      this.Name = parser.ProblemName;
624
625      BestKnownSolution = null;
626      Coordinates = new DoubleMatrix(parser.Coordinates);
627      Vehicles.Value = parser.Vehicles;
628      Capacity.Value = parser.Capacity;
629      Demand = new DoubleArray(parser.Demands);
630      ReadyTime = new DoubleArray(parser.Readytimes);
631      DueTime = new DoubleArray(parser.Duetimes);
632      ServiceTime = new DoubleArray(parser.Servicetimes);
633
634      OnReset();
635    }
636
637    public void ImportFromTSPLib(string tspFileName) {
638      TSPLIBParser parser = new TSPLIBParser(tspFileName);
639      parser.Parse();
640
641      this.Name = parser.Name;
642      int problemSize = parser.Demands.Length;
643
644      BestKnownSolution = null;
645      Coordinates = new DoubleMatrix(parser.Vertices);
646      if (parser.Vehicles != -1)
647        Vehicles.Value = parser.Vehicles;
648      else
649        Vehicles.Value = problemSize - 1;
650      Capacity.Value = parser.Capacity;
651      Demand = new DoubleArray(parser.Demands);
652      ReadyTime = new DoubleArray(problemSize);
653      DueTime = new DoubleArray(problemSize);
654      ServiceTime = new DoubleArray(problemSize);
655
656      for (int i = 0; i < problemSize; i++) {
657        ReadyTime[i] = 0;
658        DueTime[i] = int.MaxValue;
659        ServiceTime[i] = 0;
660      }
661
662      if (parser.Distance != -1) {
663        DueTime[0] = parser.Distance;
664      }
665
666      if (parser.Depot != 1)
667        ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
668
669      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
670        ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
671
672      OnReset();
673    }
674
675    private void EvalBestKnownSolution() {
676      if (BestKnownSolution != null) {
677        //call evaluator
678        IValueParameter<DoubleMatrix> distMatrix = new ValueLookupParameter<DoubleMatrix>("DistMatrix",
679          DistanceMatrix);
680
681        TourEvaluation eval = VRPEvaluator.Evaluate(
682          BestKnownSolution,
683          Vehicles,
684          DueTime,
685          ServiceTime,
686          ReadyTime,
687          Demand,
688          Capacity,
689          FleetUsageFactorParameter.Value,
690          TimeFactorParameter.Value,
691          DistanceFactorParameter.Value,
692          OverloadPenaltyParameter.Value,
693          TardinessPenaltyParameter.Value,
694          Coordinates,
695          distMatrix,
696          UseDistanceMatrix);
697
698        DistanceMatrix = distMatrix.Value;
699
700        BestKnownQuality = new DoubleValue(eval.Quality);
701      } else {
702        BestKnownQuality = null;
703      }
704    }
705
706    public void ImportSolution(string solutionFileName) {
707      SolutionParser parser = new SolutionParser(solutionFileName);
708      parser.Parse();
709
710      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding();
711
712      int cities = 0;
713      foreach (List<int> route in parser.Routes) {
714        Encodings.Tour tour = new Encodings.Tour();
715        tour.Cities.AddRange(route);
716        cities += tour.Cities.Count;
717
718        encoding.Tours.Add(tour);
719      }
720
721      if (cities != Coordinates.Rows - 1)
722        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond  with the problem data."));
723      else
724        BestKnownSolutionParameter.Value = encoding;
725    }
726
727    public void ImportFromORLib(string orFileName) {
728      ORLIBParser parser = new ORLIBParser(orFileName);
729      parser.Parse();
730
731      this.Name = parser.Name;
732      int problemSize = parser.Demands.Length;
733
734      BestKnownSolution = null;
735      Coordinates = new DoubleMatrix(parser.Vertices);
736      Vehicles.Value = problemSize - 1;
737      Capacity.Value = parser.Capacity;
738      Demand = new DoubleArray(parser.Demands);
739      ReadyTime = new DoubleArray(problemSize);
740      DueTime = new DoubleArray(problemSize);
741      ServiceTime = new DoubleArray(problemSize);
742
743      ReadyTime[0] = 0;
744      DueTime[0] = parser.MaxRouteTime;
745      ServiceTime[0] = 0;
746
747      for (int i = 1; i < problemSize; i++) {
748        ReadyTime[i] = 0;
749        DueTime[i] = int.MaxValue;
750        ServiceTime[i] = parser.ServiceTime;
751      }
752
753      OnReset();
754    }
755
756    private void InitializeRandomVRPInstance() {
757      System.Random rand = new System.Random();
758
759      int cities = 100;
760
761      Coordinates = new DoubleMatrix(cities + 1, 2);
762      Demand = new DoubleArray(cities + 1);
763      DueTime = new DoubleArray(cities + 1);
764      ReadyTime = new DoubleArray(cities + 1);
765      ServiceTime = new DoubleArray(cities + 1);
766
767      Vehicles.Value = 100;
768      Capacity.Value = 200;
769
770      for (int i = 0; i <= cities; i++) {
771        Coordinates[i, 0] = rand.Next(0, 100);
772        Coordinates[i, 1] = rand.Next(0, 100);
773
774        if (i == 0) {
775          Demand[i] = 0;
776          DueTime[i] = Int16.MaxValue;
777          ReadyTime[i] = 0;
778          ServiceTime[i] = 0;
779        } else {
780          Demand[i] = rand.Next(10, 50);
781          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
782          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
783          ServiceTime[i] = 90;
784        }
785      }
786    }
787  }
788}
Note: See TracBrowser for help on using the repository browser.