Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 6875

Last change on this file since 6875 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
200      : base(original, cloner) {
201      operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
202      DistanceMatrixParameter.Value = DistanceMatrixParameter.Value;
203      AttachEventHandlers();
204    }
205    public VehicleRoutingProblem()
206      : base() {
207      IVRPCreator creator = new RandomCreator();
208      IVRPEvaluator evaluator = new VRPEvaluator();
209
210      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
211      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
212      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
213      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
214      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
215      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
216      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
217      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
218      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
219      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
220      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
221      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this TSP instance."));
222      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
223      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
224      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
225      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
226      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
227
228      Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", creator));
229      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", evaluator));
230
231      creator.VRPToursParameter.ActualName = "VRPTours";
232      evaluator.QualityParameter.ActualName = "VRPQuality";
233
234      InitializeRandomVRPInstance();
235
236      ParameterizeSolutionCreator();
237      ParameterizeEvaluator();
238
239      InitializeOperators();
240      AttachEventHandlers();
241    }
242
243    public override IDeepCloneable Clone(Cloner cloner) {
244      return new VehicleRoutingProblem(this, cloner);
245    }
246
247    #region Events
248    public event EventHandler SolutionCreatorChanged;
249    private void OnSolutionCreatorChanged() {
250      EventHandler handler = SolutionCreatorChanged;
251      if (handler != null) handler(this, EventArgs.Empty);
252    }
253    public event EventHandler EvaluatorChanged;
254    private void OnEvaluatorChanged() {
255      EventHandler handler = EvaluatorChanged;
256      if (handler != null) handler(this, EventArgs.Empty);
257    }
258    public event EventHandler OperatorsChanged;
259    private void OnOperatorsChanged() {
260      EventHandler handler = OperatorsChanged;
261      if (handler != null) handler(this, EventArgs.Empty);
262    }
263    public event EventHandler Reset;
264    private void OnReset() {
265      EventHandler handler = Reset;
266      if (handler != null) handler(this, EventArgs.Empty);
267    }
268    void VehiclesValue_ValueChanged(object sender, EventArgs e) {
269      ParameterizeSolutionCreator();
270    }
271    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
272      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
273      Coordinates.Reset += new EventHandler(Coordinates_Reset);
274      ParameterizeSolutionCreator();
275      ClearDistanceMatrix();
276
277      BestKnownSolution = null;
278    }
279    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
280      ClearDistanceMatrix();
281
282      BestKnownSolution = null;
283    }
284    private void Coordinates_Reset(object sender, EventArgs e) {
285      ParameterizeSolutionCreator();
286      ClearDistanceMatrix();
287
288      BestKnownSolution = null;
289    }
290    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
291      ParameterizeSolutionCreator();
292      ParameterizeEvaluator();
293      ParameterizeAnalyzer();
294      ParameterizeOperators();
295      OnSolutionCreatorChanged();
296    }
297    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
298      ParameterizeEvaluator();
299      ParameterizeAnalyzer();
300      ParameterizeOperators();
301    }
302    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
303      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
304      ParameterizeEvaluator();
305      UpdateMoveEvaluators();
306      ParameterizeAnalyzer();
307      OnEvaluatorChanged();
308    }
309    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
310      ParameterizeAnalyzer();
311    }
312    private void TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
313      string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
314      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
315        op.TranslocationMoveParameter.ActualName = name;
316      }
317    }
318
319    void DistanceFactor_ValueChanged(object sender, EventArgs e) {
320      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactorValue_ValueChanged);
321      EvalBestKnownSolution();
322    }
323    void DistanceFactorValue_ValueChanged(object sender, EventArgs e) {
324      EvalBestKnownSolution();
325    }
326    void FleetUsageFactor_ValueChanged(object sender, EventArgs e) {
327      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactorValue_ValueChanged);
328      EvalBestKnownSolution();
329    }
330    void FleetUsageFactorValue_ValueChanged(object sender, EventArgs e) {
331      EvalBestKnownSolution();
332    }
333    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
334      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenaltyValue_ValueChanged);
335      EvalBestKnownSolution();
336    }
337    void OverloadPenaltyValue_ValueChanged(object sender, EventArgs e) {
338      EvalBestKnownSolution();
339    }
340    void TardinessPenalty_ValueChanged(object sender, EventArgs e) {
341      TardinessPenaltyParameter.Value.ValueChanged += new EventHandler(TardinessPenaltyValue_ValueChanged);
342      EvalBestKnownSolution();
343    }
344    void TardinessPenaltyValue_ValueChanged(object sender, EventArgs e) {
345      EvalBestKnownSolution();
346    }
347    void TimeFactor_ValueChanged(object sender, EventArgs e) {
348      TimeFactorParameter.Value.ValueChanged += new EventHandler(TimeFactorValue_ValueChanged);
349      EvalBestKnownSolution();
350    }
351    void TimeFactorValue_ValueChanged(object sender, EventArgs e) {
352      EvalBestKnownSolution();
353    }
354    void DistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
355      if (DistanceMatrix != null) {
356        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
357        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
358      }
359      EvalBestKnownSolution();
360    }
361    void DistanceMatrix_Reset(object sender, EventArgs e) {
362      EvalBestKnownSolution();
363    }
364    void DistanceMatrix_ItemChanged(object sender, EventArgs<int, int> e) {
365      EvalBestKnownSolution();
366    }
367    void UseDistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
368      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
369      EvalBestKnownSolution();
370    }
371    void UseDistanceMatrix_ValueChanged(object sender, EventArgs e) {
372      EvalBestKnownSolution();
373    }
374    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
375      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
376      BestKnownSolution = null;
377    }
378    void Capacity_ValueChanged(object sender, EventArgs e) {
379      BestKnownSolution = null;
380    }
381    void DemandParameter_ValueChanged(object sender, EventArgs e) {
382      Demand.ItemChanged += new EventHandler<EventArgs<int>>(Demand_ItemChanged);
383      Demand.Reset += new EventHandler(Demand_Reset);
384      BestKnownSolution = null;
385    }
386    void Demand_Reset(object sender, EventArgs e) {
387      BestKnownSolution = null;
388    }
389    void Demand_ItemChanged(object sender, EventArgs<int> e) {
390      BestKnownSolution = null;
391    }
392    void DueTimeParameter_ValueChanged(object sender, EventArgs e) {
393      DueTime.ItemChanged += new EventHandler<EventArgs<int>>(DueTime_ItemChanged);
394      DueTime.Reset += new EventHandler(DueTime_Reset);
395      BestKnownSolution = null;
396    }
397    void DueTime_Reset(object sender, EventArgs e) {
398      BestKnownSolution = null;
399    }
400    void DueTime_ItemChanged(object sender, EventArgs<int> e) {
401      BestKnownSolution = null;
402    }
403    void ReadyTimeParameter_ValueChanged(object sender, EventArgs e) {
404      ReadyTime.ItemChanged += new EventHandler<EventArgs<int>>(ReadyTime_ItemChanged);
405      ReadyTime.Reset += new EventHandler(ReadyTime_Reset);
406      BestKnownSolution = null;
407    }
408    void ReadyTime_Reset(object sender, EventArgs e) {
409      BestKnownSolution = null;
410    }
411    void ReadyTime_ItemChanged(object sender, EventArgs<int> e) {
412      BestKnownSolution = null;
413    }
414    void ServiceTimeParameter_ValueChanged(object sender, EventArgs e) {
415      ServiceTime.ItemChanged += new EventHandler<EventArgs<int>>(ServiceTime_ItemChanged);
416      ServiceTime.Reset += new EventHandler(ServiceTime_Reset);
417      BestKnownSolution = null;
418    }
419    void ServiceTime_Reset(object sender, EventArgs e) {
420      BestKnownSolution = null;
421    }
422    void ServiceTime_ItemChanged(object sender, EventArgs<int> e) {
423      BestKnownSolution = null;
424    }
425    void VehiclesParameter_ValueChanged(object sender, EventArgs e) {
426      Vehicles.ValueChanged += new EventHandler(Vehicles_ValueChanged);
427      BestKnownSolution = null;
428    }
429    void Vehicles_ValueChanged(object sender, EventArgs e) {
430      BestKnownSolution = null;
431    }
432    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
433      EvalBestKnownSolution();
434    }
435    #endregion
436
437    #region Helpers
438    [StorableHook(HookType.AfterDeserialization)]
439    private void AfterDeserializationHook() {
440      #region Backwards Compatibility
441      if (!Parameters.ContainsKey("BestKnownSolution")) {
442        Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this TSP instance."));
443      }
444      #endregion
445
446      AttachEventHandlers();
447    }
448
449    private void AttachEventHandlers() {
450      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
451      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
452      Coordinates.Reset += new EventHandler(Coordinates_Reset);
453
454      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
455
456      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
457
458      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
459      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
460
461      DistanceFactorParameter.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
462      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactorValue_ValueChanged);
463      FleetUsageFactorParameter.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
464      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactorValue_ValueChanged);
465      OverloadPenaltyParameter.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
466      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenaltyValue_ValueChanged);
467      TardinessPenaltyParameter.ValueChanged += new EventHandler(TardinessPenalty_ValueChanged);
468      TardinessPenaltyParameter.Value.ValueChanged += new EventHandler(TardinessPenaltyValue_ValueChanged);
469      TimeFactorParameter.ValueChanged += new EventHandler(TimeFactor_ValueChanged);
470      TimeFactorParameter.Value.ValueChanged += new EventHandler(TimeFactorValue_ValueChanged);
471
472      DistanceMatrixParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
473      UseDistanceMatrixParameter.ValueChanged += new EventHandler(UseDistanceMatrixParameter_ValueChanged);
474      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
475
476      CapacityParameter.ValueChanged += new EventHandler(CapacityParameter_ValueChanged);
477      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
478      DemandParameter.ValueChanged += new EventHandler(DemandParameter_ValueChanged);
479      Demand.ItemChanged += new EventHandler<EventArgs<int>>(Demand_ItemChanged);
480      Demand.Reset += new EventHandler(Demand_Reset);
481      DueTimeParameter.ValueChanged += new EventHandler(DueTimeParameter_ValueChanged);
482      DueTime.ItemChanged += new EventHandler<EventArgs<int>>(DueTime_ItemChanged);
483      DueTime.Reset += new EventHandler(DueTime_Reset);
484      ReadyTimeParameter.ValueChanged += new EventHandler(ReadyTimeParameter_ValueChanged);
485      ReadyTime.ItemChanged += new EventHandler<EventArgs<int>>(ReadyTime_ItemChanged);
486      ReadyTime.Reset += new EventHandler(ReadyTime_Reset);
487      ServiceTimeParameter.ValueChanged += new EventHandler(ServiceTimeParameter_ValueChanged);
488      ServiceTime.ItemChanged += new EventHandler<EventArgs<int>>(ServiceTime_ItemChanged);
489      ServiceTime.Reset += new EventHandler(ServiceTime_Reset);
490      VehiclesParameter.ValueChanged += new EventHandler(VehiclesParameter_ValueChanged);
491      Vehicles.ValueChanged += new EventHandler(Vehicles_ValueChanged);
492
493      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
494    }
495
496    private void InitializeOperators() {
497      operators = new List<IOperator>();
498      operators.Add(new BestVRPSolutionAnalyzer());
499      operators.Add(new BestAverageWorstVRPToursAnalyzer());
500      ParameterizeAnalyzer();
501      operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
502      ParameterizeOperators();
503      UpdateMoveEvaluators();
504      InitializeMoveGenerators();
505    }
506    private void InitializeMoveGenerators() {
507      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>()) {
508        if (op is IMoveGenerator) {
509          op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(TranslocationMoveParameter_ActualNameChanged);
510        }
511      }
512
513    }
514    private void UpdateMoveEvaluators() {
515      ParameterizeOperators();
516      OnOperatorsChanged();
517    }
518    private void ParameterizeSolutionCreator() {
519      SolutionCreator.VehiclesParameter.ActualName = VehiclesParameter.Name;
520      SolutionCreator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
521      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
522      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
523      SolutionCreator.CapacityParameter.ActualName = CapacityParameter.Name;
524      SolutionCreator.DemandParameter.ActualName = DemandParameter.Name;
525      SolutionCreator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
526      SolutionCreator.DueTimeParameter.ActualName = DueTimeParameter.Name;
527      SolutionCreator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
528    }
529    private void ParameterizeEvaluator() {
530      Evaluator.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
531      Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
532      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
533      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
534      Evaluator.VehiclesParameter.ActualName = VehiclesParameter.Name;
535      Evaluator.CapacityParameter.ActualName = CapacityParameter.Name;
536      Evaluator.DemandParameter.ActualName = DemandParameter.Name;
537      Evaluator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
538      Evaluator.DueTimeParameter.ActualName = DueTimeParameter.Name;
539      Evaluator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
540      Evaluator.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
541      Evaluator.TimeFactor.ActualName = TimeFactorParameter.Name;
542      Evaluator.DistanceFactor.ActualName = DistanceFactorParameter.Name;
543      Evaluator.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
544      Evaluator.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
545    }
546    private void ParameterizeAnalyzer() {
547      BestVRPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
548      BestVRPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
549      BestVRPSolutionAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
550      BestVRPSolutionAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
551      BestVRPSolutionAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
552      BestVRPSolutionAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
553      BestVRPSolutionAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
554      BestVRPSolutionAnalyzer.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
555      BestVRPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
556
557      BestAverageWorstVRPToursAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
558      BestAverageWorstVRPToursAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
559      BestAverageWorstVRPToursAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
560      BestAverageWorstVRPToursAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
561      BestAverageWorstVRPToursAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
562      BestAverageWorstVRPToursAnalyzer.ResultsParameter.ActualName = "Results";
563    }
564    private void ParameterizeOperators() {
565      foreach (IVRPOperator op in Operators.OfType<IVRPOperator>()) {
566        if (op.CoordinatesParameter != null) op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
567        if (op.DistanceMatrixParameter != null) op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
568        if (op.UseDistanceMatrixParameter != null) op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
569        if (op.VehiclesParameter != null) op.VehiclesParameter.ActualName = VehiclesParameter.Name;
570        if (op.CapacityParameter != null) op.CapacityParameter.ActualName = CapacityParameter.Name;
571        if (op.DemandParameter != null) op.DemandParameter.ActualName = DemandParameter.Name;
572        if (op.ReadyTimeParameter != null) op.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
573        if (op.DueTimeParameter != null) op.DueTimeParameter.ActualName = DueTimeParameter.Name;
574        if (op.ServiceTimeParameter != null) op.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
575      }
576
577      foreach (IVRPMoveOperator op in Operators.OfType<IVRPMoveOperator>()) {
578        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
579      }
580
581      foreach (IPrinsOperator op in Operators.OfType<IPrinsOperator>()) {
582        op.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
583        op.TimeFactor.ActualName = TimeFactorParameter.Name;
584        op.DistanceFactor.ActualName = DistanceFactorParameter.Name;
585        op.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
586        op.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
587      }
588
589      foreach (IVRPMoveEvaluator op in Operators.OfType<IVRPMoveEvaluator>()) {
590        op.FleetUsageFactor.ActualName = FleetUsageFactorParameter.Name;
591        op.TimeFactor.ActualName = TimeFactorParameter.Name;
592        op.DistanceFactor.ActualName = DistanceFactorParameter.Name;
593        op.OverloadPenalty.ActualName = OverloadPenaltyParameter.Name;
594        op.TardinessPenalty.ActualName = TardinessPenaltyParameter.Name;
595        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
596        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
597      }
598      string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IAlbaTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
599      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>())
600        op.TranslocationMoveParameter.ActualName = translocationMove;
601
602      foreach (IVRPCrossover op in Operators.OfType<IVRPCrossover>()) {
603        op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
604        op.ChildParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
605      }
606
607      foreach (IVRPManipulator op in Operators.OfType<IVRPManipulator>()) {
608        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
609      }
610    }
611    private void ClearDistanceMatrix() {
612      DistanceMatrixParameter.Value = null;
613    }
614    #endregion
615
616    public void ImportFromSolomon(string solomonFileName) {
617      SolomonParser parser = new SolomonParser(solomonFileName);
618      parser.Parse();
619
620      this.Name = parser.ProblemName;
621
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
639      int problemSize = parser.Demands.Length;
640
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        throw new Exception("Invalid depot specification");
664
665      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
666        throw new Exception("Invalid weight type");
667
668      OnReset();
669    }
670
671    private void EvalBestKnownSolution() {
672      if (BestKnownSolution != null) {
673        //call evaluator
674        IValueLookupParameter<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        throw new Exception("Invalid solution");
719
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      Coordinates = new DoubleMatrix(parser.Vertices);
731      Vehicles.Value = problemSize - 1;
732      Capacity.Value = parser.Capacity;
733      Demand = new DoubleArray(parser.Demands);
734      ReadyTime = new DoubleArray(problemSize);
735      DueTime = new DoubleArray(problemSize);
736      ServiceTime = new DoubleArray(problemSize);
737
738      ReadyTime[0] = 0;
739      DueTime[0] = parser.MaxRouteTime;
740      ServiceTime[0] = 0;
741
742      for (int i = 1; i < problemSize; i++) {
743        ReadyTime[i] = 0;
744        DueTime[i] = int.MaxValue;
745        ServiceTime[i] = parser.ServiceTime;
746      }
747
748      OnReset();
749    }
750
751    private void InitializeRandomVRPInstance() {
752      System.Random rand = new System.Random();
753
754      int cities = 100;
755
756      Coordinates = new DoubleMatrix(cities + 1, 2);
757      Demand = new DoubleArray(cities + 1);
758      DueTime = new DoubleArray(cities + 1);
759      ReadyTime = new DoubleArray(cities + 1);
760      ServiceTime = new DoubleArray(cities + 1);
761
762      Vehicles.Value = 100;
763      Capacity.Value = 200;
764
765      for (int i = 0; i <= cities; i++) {
766        Coordinates[i, 0] = rand.Next(0, 100);
767        Coordinates[i, 1] = rand.Next(0, 100);
768
769        if (i == 0) {
770          Demand[i] = 0;
771          DueTime[i] = Int16.MaxValue;
772          ReadyTime[i] = 0;
773          ServiceTime[i] = 0;
774        } else {
775          Demand[i] = rand.Next(10, 50);
776          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
777          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
778          ServiceTime[i] = 90;
779        }
780      }
781    }
782  }
783}
Note: See TracBrowser for help on using the repository browser.