Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4435 was 4419, checked in by swagner, 14 years ago

Enabled saving only for some specific items (#1193)

File size: 25.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
35using HeuristicLab.Problems.VehicleRouting.Encodings.General;
36using HeuristicLab.Problems.VehicleRouting.Encodings.Prins;
37
38namespace HeuristicLab.Problems.VehicleRouting {
39  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
40  [Creatable("Problems")]
41  [StorableClass]
42  public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
43    public string Filename { get; set; }
44
45    public override Image ItemImage {
46      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
47    }
48
49    #region Parameter Properties
50    public ValueParameter<BoolValue> MaximizationParameter {
51      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
52    }
53    IParameter ISingleObjectiveProblem.MaximizationParameter {
54      get { return MaximizationParameter; }
55    }
56    public ValueParameter<DoubleMatrix> CoordinatesParameter {
57      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
58    }
59    public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
60      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
61    }
62    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
63      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
64    }
65    public ValueParameter<IntValue> VehiclesParameter {
66      get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
67    }
68    public ValueParameter<DoubleValue> CapacityParameter {
69      get { return (ValueParameter<DoubleValue>)Parameters["Capacity"]; }
70    }
71    public ValueParameter<DoubleArray> DemandParameter {
72      get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
73    }
74    public ValueParameter<DoubleArray> ReadyTimeParameter {
75      get { return (ValueParameter<DoubleArray>)Parameters["ReadyTime"]; }
76    }
77    public ValueParameter<DoubleArray> DueTimeParameter {
78      get { return (ValueParameter<DoubleArray>)Parameters["DueTime"]; }
79    }
80    public ValueParameter<DoubleArray> ServiceTimeParameter {
81      get { return (ValueParameter<DoubleArray>)Parameters["ServiceTime"]; }
82    }
83    ValueParameter<IVRPCreator> SolutionCreatorParameter {
84      get { return (ValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
85    }
86    IParameter IProblem.SolutionCreatorParameter {
87      get { return SolutionCreatorParameter; }
88    }
89    ValueParameter<IVRPEvaluator> EvaluatorParameter {
90      get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
91    }
92    IParameter IProblem.EvaluatorParameter {
93      get { return EvaluatorParameter; }
94    }
95    public IValueParameter<DoubleValue> FleetUsageFactor {
96      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
97    }
98    public IValueParameter<DoubleValue> TimeFactor {
99      get { return (IValueParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
100    }
101    public IValueParameter<DoubleValue> DistanceFactor {
102      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
103    }
104    public IValueParameter<DoubleValue> OverloadPenalty {
105      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
106    }
107    public IValueParameter<DoubleValue> TardinessPenalty {
108      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
109    }
110    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
111      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
112    }
113    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
114      get { return BestKnownQualityParameter; }
115    }
116    #endregion
117
118    #region Properties
119    public DoubleMatrix Coordinates {
120      get { return CoordinatesParameter.Value; }
121      set { CoordinatesParameter.Value = value; }
122    }
123    public DoubleMatrix DistanceMatrix {
124      get { return DistanceMatrixParameter.Value; }
125      set { DistanceMatrixParameter.Value = value; }
126    }
127    public BoolValue UseDistanceMatrix {
128      get { return UseDistanceMatrixParameter.Value; }
129      set { UseDistanceMatrixParameter.Value = value; }
130    }
131    public IntValue Vehicles {
132      get { return VehiclesParameter.Value; }
133      set { VehiclesParameter.Value = value; }
134    }
135    public DoubleValue Capacity {
136      get { return CapacityParameter.Value; }
137      set { CapacityParameter.Value = value; }
138    }
139    public DoubleArray Demand {
140      get { return DemandParameter.Value; }
141      set { DemandParameter.Value = value; }
142    }
143    public DoubleArray ReadyTime {
144      get { return ReadyTimeParameter.Value; }
145      set { ReadyTimeParameter.Value = value; }
146    }
147    public DoubleArray DueTime {
148      get { return DueTimeParameter.Value; }
149      set { DueTimeParameter.Value = value; }
150    }
151    public DoubleArray ServiceTime {
152      get { return ServiceTimeParameter.Value; }
153      set { ServiceTimeParameter.Value = value; }
154    }
155    public DoubleValue BestKnownQuality {
156      get { return BestKnownQualityParameter.Value; }
157      set { BestKnownQualityParameter.Value = value; }
158    }
159    IVRPCreator SolutionCreator {
160      get { return SolutionCreatorParameter.Value; }
161      set { SolutionCreatorParameter.Value = value; }
162    }
163    ISolutionCreator IProblem.SolutionCreator {
164      get { return SolutionCreatorParameter.Value; }
165    }
166    IVRPEvaluator Evaluator {
167      get { return EvaluatorParameter.Value; }
168      set { EvaluatorParameter.Value = value; }
169    }
170    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
171      get { return EvaluatorParameter.Value; }
172    }
173    IEvaluator IProblem.Evaluator {
174      get { return EvaluatorParameter.Value; }
175    }
176    public IEnumerable<IOperator> Operators {
177      get { return operators; }
178    }
179    private BestVRPSolutionAnalyzer BestVRPSolutionAnalyzer {
180      get { return operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }
181    }
182    private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer {
183      get { return operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
184    }
185    #endregion
186
187    [Storable]
188    private List<IOperator> operators;
189
190    [StorableConstructor]
191    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
192    public VehicleRoutingProblem()
193      : base() {
194      IVRPCreator creator = new RandomCreator();
195      IVRPEvaluator evaluator = new VRPEvaluator();
196
197      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
198      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
199      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
200      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
201      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
202      Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
203      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
204      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
205      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
206      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
207      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
208      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
209      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
210      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
211      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
212      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
213
214      Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", creator));
215      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", evaluator));
216
217      creator.VRPToursParameter.ActualName = "VRPTours";
218      evaluator.QualityParameter.ActualName = "VRPQuality";
219
220      InitializeRandomVRPInstance();
221
222      ParameterizeSolutionCreator();
223      ParameterizeEvaluator();
224
225      InitializeOperators();
226      AttachEventHandlers();
227    }
228
229    public override IDeepCloneable Clone(Cloner cloner) {
230      VehicleRoutingProblem clone = (VehicleRoutingProblem)base.Clone(cloner);
231      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
232      clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value;
233      clone.AttachEventHandlers();
234      return clone;
235    }
236
237    #region Events
238    public event EventHandler SolutionCreatorChanged;
239    private void OnSolutionCreatorChanged() {
240      EventHandler handler = SolutionCreatorChanged;
241      if (handler != null) handler(this, EventArgs.Empty);
242    }
243    public event EventHandler EvaluatorChanged;
244    private void OnEvaluatorChanged() {
245      EventHandler handler = EvaluatorChanged;
246      if (handler != null) handler(this, EventArgs.Empty);
247    }
248    public event EventHandler OperatorsChanged;
249    private void OnOperatorsChanged() {
250      EventHandler handler = OperatorsChanged;
251      if (handler != null) handler(this, EventArgs.Empty);
252    }
253    public event EventHandler Reset;
254    private void OnReset() {
255      EventHandler handler = Reset;
256      if (handler != null) handler(this, EventArgs.Empty);
257    }
258    void VehiclesValue_ValueChanged(object sender, EventArgs e) {
259      ParameterizeSolutionCreator();
260    }
261    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
262      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
263      Coordinates.Reset += new EventHandler(Coordinates_Reset);
264      ParameterizeSolutionCreator();
265      ClearDistanceMatrix();
266    }
267    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
268      ClearDistanceMatrix();
269    }
270    private void Coordinates_Reset(object sender, EventArgs e) {
271      ParameterizeSolutionCreator();
272      ClearDistanceMatrix();
273    }
274    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
275      ParameterizeSolutionCreator();
276      ParameterizeEvaluator();
277      ParameterizeAnalyzer();
278      ParameterizeOperators();
279      OnSolutionCreatorChanged();
280    }
281    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
282      ParameterizeEvaluator();
283      ParameterizeAnalyzer();
284      ParameterizeOperators();
285    }
286    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
287      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
288      ParameterizeEvaluator();
289      UpdateMoveEvaluators();
290      ParameterizeAnalyzer();
291      OnEvaluatorChanged();
292    }
293    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
294      ParameterizeAnalyzer();
295    }
296    private void TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
297      string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
298      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
299        op.TranslocationMoveParameter.ActualName = name;
300      }
301    }
302    #endregion
303
304    #region Helpers
305    [StorableHook(HookType.AfterDeserialization)]
306    private void AfterDeserializationHook() {
307      AttachEventHandlers();
308    }
309
310    private void AttachEventHandlers() {
311      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
312      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
313      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
314      Coordinates.Reset += new EventHandler(Coordinates_Reset);
315      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
316      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
317      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
318    }
319    private void InitializeOperators() {
320      operators = new List<IOperator>();
321      operators.Add(new BestVRPSolutionAnalyzer());
322      operators.Add(new BestAverageWorstVRPToursAnalyzer());
323      ParameterizeAnalyzer();
324      operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
325      ParameterizeOperators();
326      UpdateMoveEvaluators();
327      InitializeMoveGenerators();
328    }
329    private void InitializeMoveGenerators() {
330      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>()) {
331        if (op is IMoveGenerator) {
332          op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(TranslocationMoveParameter_ActualNameChanged);
333        }
334      }
335
336    }
337    private void UpdateMoveEvaluators() {
338      ParameterizeOperators();
339      OnOperatorsChanged();
340    }
341    private void ParameterizeSolutionCreator() {
342      SolutionCreator.VehiclesParameter.ActualName = VehiclesParameter.Name;
343      SolutionCreator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
344      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
345      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
346      SolutionCreator.CapacityParameter.ActualName = CapacityParameter.Name;
347      SolutionCreator.DemandParameter.ActualName = DemandParameter.Name;
348      SolutionCreator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
349      SolutionCreator.DueTimeParameter.ActualName = DueTimeParameter.Name;
350      SolutionCreator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
351    }
352    private void ParameterizeEvaluator() {
353      Evaluator.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
354      Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
355      Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
356      Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
357      Evaluator.VehiclesParameter.ActualName = VehiclesParameter.Name;
358      Evaluator.CapacityParameter.ActualName = CapacityParameter.Name;
359      Evaluator.DemandParameter.ActualName = DemandParameter.Name;
360      Evaluator.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
361      Evaluator.DueTimeParameter.ActualName = DueTimeParameter.Name;
362      Evaluator.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
363      Evaluator.FleetUsageFactor.ActualName = FleetUsageFactor.Name;
364      Evaluator.TimeFactor.ActualName = TimeFactor.Name;
365      Evaluator.DistanceFactor.ActualName = DistanceFactor.Name;
366      Evaluator.OverloadPenalty.ActualName = OverloadPenalty.Name;
367      Evaluator.TardinessPenalty.ActualName = TardinessPenalty.Name;
368    }
369    private void ParameterizeAnalyzer() {
370      BestVRPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
371      BestVRPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
372      BestVRPSolutionAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
373      BestVRPSolutionAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
374      BestVRPSolutionAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
375      BestVRPSolutionAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
376      BestVRPSolutionAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
377      BestVRPSolutionAnalyzer.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
378      BestVRPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
379
380      BestAverageWorstVRPToursAnalyzer.DistanceParameter.ActualName = Evaluator.DistanceParameter.ActualName;
381      BestAverageWorstVRPToursAnalyzer.OverloadParameter.ActualName = Evaluator.OverloadParameter.ActualName;
382      BestAverageWorstVRPToursAnalyzer.TardinessParameter.ActualName = Evaluator.TardinessParameter.ActualName;
383      BestAverageWorstVRPToursAnalyzer.TravelTimeParameter.ActualName = Evaluator.TravelTimeParameter.ActualName;
384      BestAverageWorstVRPToursAnalyzer.VehiclesUtilizedParameter.ActualName = Evaluator.VehcilesUtilizedParameter.ActualName;
385      BestAverageWorstVRPToursAnalyzer.ResultsParameter.ActualName = "Results";
386    }
387    private void ParameterizeOperators() {
388      foreach (IVRPOperator op in Operators.OfType<IVRPOperator>()) {
389        if(op.CoordinatesParameter != null) op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
390        if(op.DistanceMatrixParameter != null) op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
391        if(op.UseDistanceMatrixParameter != null) op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
392        if(op.VehiclesParameter != null) op.VehiclesParameter.ActualName = VehiclesParameter.Name;
393        if(op.CapacityParameter != null) op.CapacityParameter.ActualName = CapacityParameter.Name;
394        if(op.DemandParameter != null) op.DemandParameter.ActualName = DemandParameter.Name;
395        if(op.ReadyTimeParameter != null) op.ReadyTimeParameter.ActualName = ReadyTimeParameter.Name;
396        if(op.DueTimeParameter != null) op.DueTimeParameter.ActualName = DueTimeParameter.Name;
397        if(op.ServiceTimeParameter != null) op.ServiceTimeParameter.ActualName = ServiceTimeParameter.Name;
398      }
399     
400      foreach (IVRPMoveOperator op in Operators.OfType<IVRPMoveOperator>()) {
401        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
402      }
403
404      foreach (IPrinsOperator op in Operators.OfType<IPrinsOperator>()) {
405        op.FleetUsageFactor.ActualName = FleetUsageFactor.Name;
406        op.TimeFactor.ActualName = TimeFactor.Name;
407        op.DistanceFactor.ActualName = DistanceFactor.Name;
408        op.OverloadPenalty.ActualName = OverloadPenalty.Name;
409        op.TardinessPenalty.ActualName = TardinessPenalty.Name;
410      }
411
412      foreach (IVRPMoveEvaluator op in Operators.OfType<IVRPMoveEvaluator>()) {
413        op.FleetUsageFactor.ActualName = FleetUsageFactor.Name;
414        op.TimeFactor.ActualName = TimeFactor.Name;
415        op.DistanceFactor.ActualName = DistanceFactor.Name;
416        op.OverloadPenalty.ActualName = OverloadPenalty.Name;
417        op.TardinessPenalty.ActualName = TardinessPenalty.Name;
418        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
419        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
420      }
421      string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IAlbaTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
422      foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>())
423        op.TranslocationMoveParameter.ActualName = translocationMove;
424
425      foreach (IVRPCrossover op in Operators.OfType<IVRPCrossover>()) {
426        op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
427        op.ChildParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
428      }
429
430      foreach (IVRPManipulator op in Operators.OfType<IVRPManipulator>()) {
431        op.VRPToursParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
432      }
433    }
434    private void ClearDistanceMatrix() {
435      DistanceMatrixParameter.Value = null;
436    }
437    #endregion
438
439    public void ImportFromSolomon(string solomonFileName) {
440      SolomonParser parser = new SolomonParser(solomonFileName);
441      parser.Parse();
442
443      this.Name = parser.ProblemName;
444
445      Coordinates = new DoubleMatrix(parser.Coordinates);
446      Vehicles.Value = parser.Vehicles;
447      Capacity.Value = parser.Capacity;
448      Demand = new DoubleArray(parser.Demands);
449      ReadyTime = new DoubleArray(parser.Readytimes);
450      DueTime = new DoubleArray(parser.Duetimes);
451      ServiceTime = new DoubleArray(parser.Servicetimes);
452
453      OnReset();
454    }
455
456    public void ImportFromTSPLib(string tspFileName) {
457      TSPLIBParser parser = new TSPLIBParser(tspFileName);
458      parser.Parse();
459
460      this.Name = parser.Name;
461
462      int problemSize = parser.Demands.Length;
463
464      Coordinates = new DoubleMatrix(parser.Vertices);
465      if (parser.Vehicles != -1)
466        Vehicles.Value = parser.Vehicles;
467      else
468        Vehicles.Value = problemSize - 1;
469      Capacity.Value = parser.Capacity;
470      Demand = new DoubleArray(parser.Demands);
471      ReadyTime = new DoubleArray(problemSize);
472      DueTime = new DoubleArray(problemSize);
473      ServiceTime = new DoubleArray(problemSize);
474
475      for (int i = 0; i < problemSize; i++) {
476        ReadyTime[i] = 0;
477        DueTime[i] = int.MaxValue;
478        ServiceTime[i] = 0;
479      }
480
481      if (parser.Distance != -1) {
482        DueTime[0] = parser.Distance;
483      }
484
485      if (parser.Depot != 1)
486        throw new Exception("Invalid depot specification");
487
488      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
489        throw new Exception("Invalid weight type");
490
491      OnReset();
492    }
493
494    public void ImportFromORLib(string orFileName) {
495      ORLIBParser parser = new ORLIBParser(orFileName);
496      parser.Parse();
497
498      this.Name = parser.Name;
499      int problemSize = parser.Demands.Length;
500
501      Coordinates = new DoubleMatrix(parser.Vertices);
502      Vehicles.Value = problemSize - 1;
503      Capacity.Value = parser.Capacity;
504      Demand = new DoubleArray(parser.Demands);
505      ReadyTime = new DoubleArray(problemSize);
506      DueTime = new DoubleArray(problemSize);
507      ServiceTime = new DoubleArray(problemSize);
508
509      ReadyTime[0] = 0;
510      DueTime[0] = parser.MaxRouteTime;
511      ServiceTime[0] = 0;
512
513      for (int i = 1; i < problemSize; i++) {
514        ReadyTime[i] = 0;
515        DueTime[i] = int.MaxValue;
516        ServiceTime[i] = parser.ServiceTime;
517      }
518
519      OnReset();
520    }
521
522    private void InitializeRandomVRPInstance() {
523      System.Random rand = new System.Random();
524
525      int cities = 100;
526
527      Coordinates = new DoubleMatrix(cities + 1, 2);
528      Demand = new DoubleArray(cities + 1);
529      DueTime = new DoubleArray(cities + 1);
530      ReadyTime = new DoubleArray(cities + 1);
531      ServiceTime = new DoubleArray(cities + 1);
532
533      Vehicles.Value = 100;
534      Capacity.Value = 200;
535
536      for (int i = 0; i <= cities; i++) {
537        Coordinates[i, 0] = rand.Next(0, 100);
538        Coordinates[i, 1] = rand.Next(0, 100);
539
540        if (i == 0) {
541          Demand[i] = 0;
542          DueTime[i] = Int16.MaxValue;
543          ReadyTime[i] = 0;
544          ServiceTime[i] = 0;
545        } else {
546          Demand[i] = rand.Next(10, 50);
547          DueTime[i] = rand.Next((int)Math.Ceiling(VRPUtilities.CalculateDistance(0, i, Coordinates)), 1200);
548          ReadyTime[i] = DueTime[i] - rand.Next(0, 100);
549          ServiceTime[i] = 90;
550        }
551      }
552    }
553  }
554}
Note: See TracBrowser for help on using the repository browser.