Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8978 was 7904, checked in by svonolfe, 12 years ago

Made VRP version 3.3 obsolete (#1177)

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