Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs @ 6703

Last change on this file since 6703 was 6441, checked in by gkronber, 14 years ago

#1553: implemented unit test to create and run the GA VRP sample.

File size: 36.1 KB
RevLine 
[3938]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3938]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;
[4068]24using System.Drawing;
[3938]25using System.Linq;
[4068]26using HeuristicLab.Common;
[3938]27using HeuristicLab.Core;
[4068]28using HeuristicLab.Data;
29using HeuristicLab.Encodings.PermutationEncoding;
[3938]30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
[4068]32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
[3938]34using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
[4179]35using HeuristicLab.Problems.VehicleRouting.Encodings.General;
[4352]36using HeuristicLab.Problems.VehicleRouting.Encodings.Prins;
[3938]37
38namespace HeuristicLab.Problems.VehicleRouting {
39  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
40  [Creatable("Problems")]
41  [StorableClass]
[5809]42  public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
[4419]43    public string Filename { get; set; }
44
[3938]45    public override Image ItemImage {
[5287]46      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[3938]47    }
48
49    #region Parameter Properties
50    public ValueParameter<BoolValue> MaximizationParameter {
51      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
52    }
[5809]53    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
[3938]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    }
[5809]86    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
[3938]87      get { return SolutionCreatorParameter; }
88    }
89    ValueParameter<IVRPEvaluator> EvaluatorParameter {
90      get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
91    }
[5809]92    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
[3938]93      get { return EvaluatorParameter; }
94    }
[4619]95    public IValueParameter<DoubleValue> FleetUsageFactorParameter {
[4179]96      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
[3947]97    }
[4619]98    public IValueParameter<DoubleValue> TimeFactorParameter {
[4179]99      get { return (IValueParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
[3947]100    }
[4619]101    public IValueParameter<DoubleValue> DistanceFactorParameter {
[4179]102      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
[3947]103    }
[4619]104    public IValueParameter<DoubleValue> OverloadPenaltyParameter {
[4179]105      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
[3947]106    }
[4619]107    public IValueParameter<DoubleValue> TardinessPenaltyParameter {
[4179]108      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
[3947]109    }
[3938]110    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
111      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
112    }
[5809]113    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
[3938]114      get { return BestKnownQualityParameter; }
115    }
[4852]116    public OptionalValueParameter<IVRPEncoding> BestKnownSolutionParameter {
117      get { return (OptionalValueParameter<IVRPEncoding>)Parameters["BestKnownSolution"]; }
[4619]118    }
[3938]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    }
[4852]162    public IVRPEncoding BestKnownSolution {
[4619]163      get { return BestKnownSolutionParameter.Value; }
164      set { BestKnownSolutionParameter.Value = value; }
165    }
[6441]166    public IVRPCreator SolutionCreator {
[3938]167      get { return SolutionCreatorParameter.Value; }
168      set { SolutionCreatorParameter.Value = value; }
169    }
[5809]170    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
[3938]171      get { return SolutionCreatorParameter.Value; }
172    }
[6441]173    public IVRPEvaluator Evaluator {
[3938]174      get { return EvaluatorParameter.Value; }
175      set { EvaluatorParameter.Value = value; }
176    }
[5809]177    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
[3938]178      get { return EvaluatorParameter.Value; }
179    }
[5809]180    IEvaluator IHeuristicOptimizationProblem.Evaluator {
[3938]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    }
[4352]189    private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer {
190      get { return operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
191    }
[3938]192    #endregion
193
[4098]194    [Storable]
195    private List<IOperator> operators;
196
197    [StorableConstructor]
[4118]198    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
[4722]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    }
[3938]204    public VehicleRoutingProblem()
205      : base() {
[4179]206      IVRPCreator creator = new RandomCreator();
[3938]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."));
[4857]220      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
[4179]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)));
[3938]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
[4179]230      creator.VRPToursParameter.ActualName = "VRPTours";
[3938]231      evaluator.QualityParameter.ActualName = "VRPQuality";
[4138]232
233      InitializeRandomVRPInstance();
234
[3938]235      ParameterizeSolutionCreator();
236      ParameterizeEvaluator();
237
[4098]238      InitializeOperators();
239      AttachEventHandlers();
[3938]240    }
241
242    public override IDeepCloneable Clone(Cloner cloner) {
[4722]243      return new VehicleRoutingProblem(this, cloner);
[3938]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();
[4619]275
276      BestKnownSolution = null;
[3938]277    }
278    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
279      ClearDistanceMatrix();
[4619]280
281      BestKnownSolution = null;
[3938]282    }
283    private void Coordinates_Reset(object sender, EventArgs e) {
284      ParameterizeSolutionCreator();
285      ClearDistanceMatrix();
[4619]286
287      BestKnownSolution = null;
[3938]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    }
[4179]311    private void TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
[3938]312      string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
313      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
314        op.TranslocationMoveParameter.ActualName = name;
315      }
316    }
[4619]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) {
[4626]354      if (DistanceMatrix != null) {
355        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
356        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
357      }
[4619]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    }
[4620]431    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
432      EvalBestKnownSolution();
433    }
[3938]434    #endregion
435
436    #region Helpers
437    [StorableHook(HookType.AfterDeserialization)]
[4118]438    private void AfterDeserializationHook() {
[4625]439      #region Backwards Compatibility
440      if (!Parameters.ContainsKey("BestKnownSolution")) {
[4852]441        Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this TSP instance."));
[4625]442      }
443      #endregion
444
[4118]445      AttachEventHandlers();
446    }
447
[4098]448    private void AttachEventHandlers() {
[3938]449      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
450      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
451      Coordinates.Reset += new EventHandler(Coordinates_Reset);
[4619]452
453      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
454
[3938]455      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
[4722]456
[3938]457      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
458      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[4619]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);
[4620]491
492      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
[3938]493    }
[4619]494
[3938]495    private void InitializeOperators() {
496      operators = new List<IOperator>();
497      operators.Add(new BestVRPSolutionAnalyzer());
[4352]498      operators.Add(new BestAverageWorstVRPToursAnalyzer());
[3938]499      ParameterizeAnalyzer();
[4352]500      operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
[3938]501      ParameterizeOperators();
502      UpdateMoveEvaluators();
503      InitializeMoveGenerators();
504    }
505    private void InitializeMoveGenerators() {
[4047]506      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>()) {
[3938]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() {
[4179]529      Evaluator.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[3938]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;
[4619]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;
[3938]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;
[4352]551      BestVRPSolutionAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
552      BestVRPSolutionAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
[4179]553      BestVRPSolutionAnalyzer.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[3938]554      BestVRPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
[4352]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";
[3938]562    }
563    private void ParameterizeOperators() {
[4154]564      foreach (IVRPOperator op in Operators.OfType<IVRPOperator>()) {
[4722]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;
[4154]574      }
[4722]575
[4154]576      foreach (IVRPMoveOperator op in Operators.OfType<IVRPMoveOperator>()) {
[4179]577        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[4154]578      }
579
[4352]580      foreach (IPrinsOperator op in Operators.OfType<IPrinsOperator>()) {
[4619]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;
[4352]586      }
587
[4154]588      foreach (IVRPMoveEvaluator op in Operators.OfType<IVRPMoveEvaluator>()) {
[4619]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;
[4154]594        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
[4179]595        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[3938]596      }
[4047]597      string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IAlbaTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
598      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>())
[3938]599        op.TranslocationMoveParameter.ActualName = translocationMove;
600
601      foreach (IVRPCrossover op in Operators.OfType<IVRPCrossover>()) {
[4179]602        op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
603        op.ChildParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[3938]604      }
[4150]605
[3938]606      foreach (IVRPManipulator op in Operators.OfType<IVRPManipulator>()) {
[4179]607        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
[3938]608      }
[6042]609
610      foreach (var op in Operators.OfType<IVRPMultiNeighborhoodShakingOperator>()) {
611        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
612      }
[3938]613    }
614    private void ClearDistanceMatrix() {
615      DistanceMatrixParameter.Value = null;
616    }
617    #endregion
[4098]618
619    public void ImportFromSolomon(string solomonFileName) {
620      SolomonParser parser = new SolomonParser(solomonFileName);
621      parser.Parse();
622
623      this.Name = parser.ProblemName;
624
[4847]625      BestKnownSolution = null;
[4098]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    }
[4138]636
[4352]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
[4847]644      BestKnownSolution = null;
[4352]645      Coordinates = new DoubleMatrix(parser.Vertices);
646      if (parser.Vehicles != -1)
647        Vehicles.Value = parser.Vehicles;
[4722]648      else
[4352]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)
[4847]667        ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
[4352]668
669      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
[4847]670        ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
[4352]671
672      OnReset();
673    }
674
[4619]675    private void EvalBestKnownSolution() {
676      if (BestKnownSolution != null) {
677        //call evaluator
[4852]678        IValueParameter<DoubleMatrix> distMatrix = new ValueLookupParameter<DoubleMatrix>("DistMatrix",
[4619]679          DistanceMatrix);
680
681        TourEvaluation eval = VRPEvaluator.Evaluate(
[4852]682          BestKnownSolution,
[4619]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)
[4857]722        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond  with the problem data."));
[4852]723      else
724        BestKnownSolutionParameter.Value = encoding;
[4619]725    }
726
[4352]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
[4847]734      BestKnownSolution = null;
[4352]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
[4138]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);
[4154]781          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
[4138]782          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
783          ServiceTime[i] = 90;
784        }
785      }
786    }
[3938]787  }
788}
Note: See TracBrowser for help on using the repository browser.