Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7284 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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  [Creatable("Problems")]
39  [StorableClass]
[6938]40  public sealed class VehicleRoutingProblem : SingleObjectiveHeuristicOptimizationProblem<IVRPEvaluator, IVRPCreator>, IStorableContent {
[4419]41    public string Filename { get; set; }
42
[3938]43    #region Parameter Properties
44    public ValueParameter<DoubleMatrix> CoordinatesParameter {
45      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
46    }
47    public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
48      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
49    }
50    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
51      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
52    }
53    public ValueParameter<IntValue> VehiclesParameter {
54      get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
55    }
56    public ValueParameter<DoubleValue> CapacityParameter {
57      get { return (ValueParameter<DoubleValue>)Parameters["Capacity"]; }
58    }
59    public ValueParameter<DoubleArray> DemandParameter {
60      get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
61    }
62    public ValueParameter<DoubleArray> ReadyTimeParameter {
63      get { return (ValueParameter<DoubleArray>)Parameters["ReadyTime"]; }
64    }
65    public ValueParameter<DoubleArray> DueTimeParameter {
66      get { return (ValueParameter<DoubleArray>)Parameters["DueTime"]; }
67    }
68    public ValueParameter<DoubleArray> ServiceTimeParameter {
69      get { return (ValueParameter<DoubleArray>)Parameters["ServiceTime"]; }
70    }
[4619]71    public IValueParameter<DoubleValue> FleetUsageFactorParameter {
[4179]72      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
[3947]73    }
[4619]74    public IValueParameter<DoubleValue> TimeFactorParameter {
[4179]75      get { return (IValueParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
[3947]76    }
[4619]77    public IValueParameter<DoubleValue> DistanceFactorParameter {
[4179]78      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
[3947]79    }
[4619]80    public IValueParameter<DoubleValue> OverloadPenaltyParameter {
[4179]81      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
[3947]82    }
[4619]83    public IValueParameter<DoubleValue> TardinessPenaltyParameter {
[4179]84      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
[3947]85    }
[4852]86    public OptionalValueParameter<IVRPEncoding> BestKnownSolutionParameter {
87      get { return (OptionalValueParameter<IVRPEncoding>)Parameters["BestKnownSolution"]; }
[4619]88    }
[3938]89    #endregion
90
91    #region Properties
92    public DoubleMatrix Coordinates {
93      get { return CoordinatesParameter.Value; }
94      set { CoordinatesParameter.Value = value; }
95    }
96    public DoubleMatrix DistanceMatrix {
97      get { return DistanceMatrixParameter.Value; }
98      set { DistanceMatrixParameter.Value = value; }
99    }
100    public BoolValue UseDistanceMatrix {
101      get { return UseDistanceMatrixParameter.Value; }
102      set { UseDistanceMatrixParameter.Value = value; }
103    }
104    public IntValue Vehicles {
105      get { return VehiclesParameter.Value; }
106      set { VehiclesParameter.Value = value; }
107    }
108    public DoubleValue Capacity {
109      get { return CapacityParameter.Value; }
110      set { CapacityParameter.Value = value; }
111    }
112    public DoubleArray Demand {
113      get { return DemandParameter.Value; }
114      set { DemandParameter.Value = value; }
115    }
116    public DoubleArray ReadyTime {
117      get { return ReadyTimeParameter.Value; }
118      set { ReadyTimeParameter.Value = value; }
119    }
120    public DoubleArray DueTime {
121      get { return DueTimeParameter.Value; }
122      set { DueTimeParameter.Value = value; }
123    }
124    public DoubleArray ServiceTime {
125      get { return ServiceTimeParameter.Value; }
126      set { ServiceTimeParameter.Value = value; }
127    }
[4852]128    public IVRPEncoding BestKnownSolution {
[4619]129      get { return BestKnownSolutionParameter.Value; }
130      set { BestKnownSolutionParameter.Value = value; }
131    }
[3938]132    private BestVRPSolutionAnalyzer BestVRPSolutionAnalyzer {
[6938]133      get { return Operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }
[3938]134    }
[4352]135    private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer {
[6938]136      get { return Operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
[4352]137    }
[3938]138    #endregion
139
[6938]140    // BackwardsCompatibility3.3
141    #region Backwards compatible code, remove with 3.4
142    [Obsolete]
143    [Storable(Name = "operators")]
144    private IEnumerable<IOperator> oldOperators {
145      get { return null; }
146      set {
147        if (value != null && value.Any())
148          Operators.AddRange(value);
149      }
150    }
151    #endregion
[4098]152
153    [StorableConstructor]
[4118]154    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
[4722]155    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
156      : base(original, cloner) {
157      AttachEventHandlers();
158    }
[3938]159    public VehicleRoutingProblem()
[6938]160      : base(new VRPEvaluator(), new RandomCreator()) {
[3938]161      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
162      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
163      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
164      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
165      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
166      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
167      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
168      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
169      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
[4857]170      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
[4179]171      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
172      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
173      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
174      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
175      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
[3938]176
[6939]177      Maximization.Value = false;
178      MaximizationParameter.Hidden = true;
179
[6938]180      SolutionCreator.VRPToursParameter.ActualName = "VRPTours";
181      Evaluator.QualityParameter.ActualName = "VRPQuality";
[3938]182
[4138]183      InitializeRandomVRPInstance();
184
[3938]185      ParameterizeSolutionCreator();
186      ParameterizeEvaluator();
187
[4098]188      InitializeOperators();
189      AttachEventHandlers();
[3938]190    }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
[4722]193      return new VehicleRoutingProblem(this, cloner);
[3938]194    }
195
196    #region Events
[6938]197    protected override void OnSolutionCreatorChanged() {
198      base.OnSolutionCreatorChanged();
199      ParameterizeSolutionCreator();
200      ParameterizeEvaluator();
201      ParameterizeAnalyzer();
202      ParameterizeOperators();
[3938]203    }
[6938]204    protected override void OnEvaluatorChanged() {
205      base.OnEvaluatorChanged();
206      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
207      ParameterizeEvaluator();
208      UpdateMoveEvaluators();
209      ParameterizeAnalyzer();
[3938]210    }
[6938]211    private void VehiclesValue_ValueChanged(object sender, EventArgs e) {
[3938]212      ParameterizeSolutionCreator();
213    }
214    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
215      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
216      Coordinates.Reset += new EventHandler(Coordinates_Reset);
217      ParameterizeSolutionCreator();
218      ClearDistanceMatrix();
[4619]219
220      BestKnownSolution = null;
[3938]221    }
222    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
223      ClearDistanceMatrix();
[4619]224
225      BestKnownSolution = null;
[3938]226    }
227    private void Coordinates_Reset(object sender, EventArgs e) {
228      ParameterizeSolutionCreator();
229      ClearDistanceMatrix();
[4619]230
231      BestKnownSolution = null;
[3938]232    }
233    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
234      ParameterizeAnalyzer();
235    }
[4619]236
237    void DistanceFactor_ValueChanged(object sender, EventArgs e) {
238      DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactorValue_ValueChanged);
239      EvalBestKnownSolution();
240    }
241    void DistanceFactorValue_ValueChanged(object sender, EventArgs e) {
242      EvalBestKnownSolution();
243    }
244    void FleetUsageFactor_ValueChanged(object sender, EventArgs e) {
245      FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactorValue_ValueChanged);
246      EvalBestKnownSolution();
247    }
248    void FleetUsageFactorValue_ValueChanged(object sender, EventArgs e) {
249      EvalBestKnownSolution();
250    }
251    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
252      OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenaltyValue_ValueChanged);
253      EvalBestKnownSolution();
254    }
255    void OverloadPenaltyValue_ValueChanged(object sender, EventArgs e) {
256      EvalBestKnownSolution();
257    }
258    void TardinessPenalty_ValueChanged(object sender, EventArgs e) {
259      TardinessPenaltyParameter.Value.ValueChanged += new EventHandler(TardinessPenaltyValue_ValueChanged);
260      EvalBestKnownSolution();
261    }
262    void TardinessPenaltyValue_ValueChanged(object sender, EventArgs e) {
263      EvalBestKnownSolution();
264    }
265    void TimeFactor_ValueChanged(object sender, EventArgs e) {
266      TimeFactorParameter.Value.ValueChanged += new EventHandler(TimeFactorValue_ValueChanged);
267      EvalBestKnownSolution();
268    }
269    void TimeFactorValue_ValueChanged(object sender, EventArgs e) {
270      EvalBestKnownSolution();
271    }
272    void DistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
[4626]273      if (DistanceMatrix != null) {
274        DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
275        DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
276      }
[4619]277      EvalBestKnownSolution();
278    }
279    void DistanceMatrix_Reset(object sender, EventArgs e) {
280      EvalBestKnownSolution();
281    }
282    void DistanceMatrix_ItemChanged(object sender, EventArgs<int, int> e) {
283      EvalBestKnownSolution();
284    }
285    void UseDistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
286      UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
287      EvalBestKnownSolution();
288    }
289    void UseDistanceMatrix_ValueChanged(object sender, EventArgs e) {
290      EvalBestKnownSolution();
291    }
292    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
293      Capacity.ValueChanged += new EventHandler(Capacity_ValueChanged);
294      BestKnownSolution = null;
295    }
296    void Capacity_ValueChanged(object sender, EventArgs e) {
297      BestKnownSolution = null;
298    }
299    void DemandParameter_ValueChanged(object sender, EventArgs e) {
300      Demand.ItemChanged += new EventHandler<EventArgs<int>>(Demand_ItemChanged);
301      Demand.Reset += new EventHandler(Demand_Reset);
302      BestKnownSolution = null;
303    }
304    void Demand_Reset(object sender, EventArgs e) {
305      BestKnownSolution = null;
306    }
307    void Demand_ItemChanged(object sender, EventArgs<int> e) {
308      BestKnownSolution = null;
309    }
310    void DueTimeParameter_ValueChanged(object sender, EventArgs e) {
311      DueTime.ItemChanged += new EventHandler<EventArgs<int>>(DueTime_ItemChanged);
312      DueTime.Reset += new EventHandler(DueTime_Reset);
313      BestKnownSolution = null;
314    }
315    void DueTime_Reset(object sender, EventArgs e) {
316      BestKnownSolution = null;
317    }
318    void DueTime_ItemChanged(object sender, EventArgs<int> e) {
319      BestKnownSolution = null;
320    }
321    void ReadyTimeParameter_ValueChanged(object sender, EventArgs e) {
322      ReadyTime.ItemChanged += new EventHandler<EventArgs<int>>(ReadyTime_ItemChanged);
323      ReadyTime.Reset += new EventHandler(ReadyTime_Reset);
324      BestKnownSolution = null;
325    }
326    void ReadyTime_Reset(object sender, EventArgs e) {
327      BestKnownSolution = null;
328    }
329    void ReadyTime_ItemChanged(object sender, EventArgs<int> e) {
330      BestKnownSolution = null;
331    }
332    void ServiceTimeParameter_ValueChanged(object sender, EventArgs e) {
333      ServiceTime.ItemChanged += new EventHandler<EventArgs<int>>(ServiceTime_ItemChanged);
334      ServiceTime.Reset += new EventHandler(ServiceTime_Reset);
335      BestKnownSolution = null;
336    }
337    void ServiceTime_Reset(object sender, EventArgs e) {
338      BestKnownSolution = null;
339    }
340    void ServiceTime_ItemChanged(object sender, EventArgs<int> e) {
341      BestKnownSolution = null;
342    }
343    void VehiclesParameter_ValueChanged(object sender, EventArgs e) {
344      Vehicles.ValueChanged += new EventHandler(Vehicles_ValueChanged);
345      BestKnownSolution = null;
346    }
347    void Vehicles_ValueChanged(object sender, EventArgs e) {
348      BestKnownSolution = null;
349    }
[4620]350    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
351      EvalBestKnownSolution();
352    }
[3938]353    #endregion
354
355    #region Helpers
356    [StorableHook(HookType.AfterDeserialization)]
[4118]357    private void AfterDeserializationHook() {
[4625]358      #region Backwards Compatibility
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
[4118]364      AttachEventHandlers();
365    }
366
[4098]367    private void AttachEventHandlers() {
[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.