Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs @ 4752

Last change on this file since 4752 was 4752, checked in by svonolfe, 14 years ago

Merged relevant changes from the trunk into the branch (cloning,...) (#1177)

File size: 11.9 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.Interfaces;
35using HeuristicLab.Problems.VehicleRouting.Parsers;
36using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
37using HeuristicLab.Problems.VehicleRouting.Variants;
38
39namespace HeuristicLab.Problems.VehicleRouting {
40  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
41  [Creatable("Problems")]
42  [StorableClass]
43  public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
44    public string Filename { get; set; }
45   
46    public override Image ItemImage {
47      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
48    }
49
50    #region Parameter Properties
51    public ValueParameter<BoolValue> MaximizationParameter {
52      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
53    }
54    IParameter ISingleObjectiveProblem.MaximizationParameter {
55      get { return MaximizationParameter; }
56    }
57    public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
58      get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
59    }
60    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
61      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
62    }
63    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
64      get { return BestKnownQualityParameter; }
65    }
66    public IValueParameter<IVRPCreator> SolutionCreatorParameter {
67      get { return (IValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
68    }
69    IParameter IProblem.SolutionCreatorParameter {
70      get { return SolutionCreatorParameter; }
71    }
72    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
73      get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
74    }
75    IParameter IProblem.EvaluatorParameter {
76      get { return EvaluatorParameter; }
77    }
78    #endregion
79
80    #region Properties
81    public IVRPProblemInstance ProblemInstance {
82      get { return ProblemInstanceParameter.Value; }
83      set { ProblemInstanceParameter.Value = value; }
84    }
85
86    public ISingleObjectiveEvaluator Evaluator {
87      get { return ProblemInstance.EvaluatorParameter.Value; }
88    }
89
90    IEvaluator IProblem.Evaluator {
91      get { return this.Evaluator; }
92    }
93
94    public ISolutionCreator SolutionCreator {
95      get { return ProblemInstance.SolutionCreatorParameter.Value; }
96    }
97
98    [Storable]
99    private List<IOperator> operators;
100
101    public IEnumerable<IOperator> Operators {
102      get { return operators; }
103    }
104    #endregion
105
106    [StorableConstructor]
107    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
108    public VehicleRoutingProblem()
109      : base() {
110      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
111      Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
112      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
113
114      operators = new List<IOperator>();
115
116      InitializeRandomVRPInstance();
117      InitializeOperators();
118
119      AttachEventHandlers();
120      AttachProblemInstanceEventHandlers();
121    }
122
123    public override IDeepCloneable Clone(Cloner cloner) {
124      return new VehicleRoutingProblem(this, cloner);
125    }
126
127    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
128      : base(original, cloner) {
129      this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
130      this.AttachEventHandlers();
131    }
132
133    #region Events
134    public event EventHandler SolutionCreatorChanged;
135    private void OnSolutionCreatorChanged() {
136      EventHandler handler = SolutionCreatorChanged;
137      if (handler != null) handler(this, EventArgs.Empty);
138    }
139    public event EventHandler EvaluatorChanged;
140    private void OnEvaluatorChanged() {
141      EventHandler handler = EvaluatorChanged;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144    public event EventHandler OperatorsChanged;
145    private void OnOperatorsChanged() {
146      EventHandler handler = OperatorsChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149    public event EventHandler Reset;
150    private void OnReset() {
151      EventHandler handler = Reset;
152      if (handler != null) handler(this, EventArgs.Empty);
153    } 
154    #endregion
155
156    #region Helpers
157    [StorableHook(HookType.AfterDeserialization)]
158    private void AfterDeserializationHook() {
159      AttachEventHandlers();
160      AttachProblemInstanceEventHandlers();
161    }
162
163    private void AttachEventHandlers() {
164      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
165    }
166
167    private void AttachProblemInstanceEventHandlers() {
168      if (Parameters.ContainsKey("Evaluator"))
169        Parameters.Remove("Evaluator");
170
171      if (Parameters.ContainsKey("SolutionCreator"))
172        Parameters.Remove("SolutionCreator");
173
174      if (ProblemInstance != null) {
175        ProblemInstance.SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
176        ProblemInstance.EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
177        Parameters.Add(ProblemInstance.EvaluatorParameter);
178        Parameters.Add(ProblemInstance.SolutionCreatorParameter);
179      }
180    }
181
182    void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
183      AttachProblemInstanceEventHandlers();
184      InitializeOperators();
185
186      OnSolutionCreatorChanged();
187      OnEvaluatorChanged();
188      OnOperatorsChanged();
189    }
190    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
191      ParameterizeSolutionCreator();
192     
193      OnSolutionCreatorChanged();
194    }
195    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
196      OnEvaluatorChanged();
197    }
198
199    private void InitializeOperators() {
200      operators = new List<IOperator>();
201
202      if (ProblemInstance != null) {
203        operators.AddRange(
204        ProblemInstance.Operators.Concat(
205          ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
206      }
207
208      ParameterizeOperators();
209    }
210
211    private void ParameterizeSolutionCreator() {
212      if (SolutionCreator is IMultiVRPOperator) {
213        (SolutionCreator as IMultiVRPOperator).SetOperators(Operators);
214      }
215    }
216
217    private void ParameterizeOperators() {
218      foreach (IOperator op in Operators) {
219        if (op is IMultiVRPOperator) {
220          (op as IMultiVRPOperator).SetOperators(Operators);
221        }
222      }
223    }
224    #endregion
225
226    public void ImportFromSolomon(string solomonFileName) {
227      SolomonParser parser = new SolomonParser(solomonFileName);
228      parser.Parse();
229
230      this.Name = parser.ProblemName;
231      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
232     
233      problem.Coordinates = new DoubleMatrix(parser.Coordinates);
234      problem.Vehicles.Value = parser.Vehicles;
235      problem.Capacity.Value = parser.Capacity;
236      problem.Demand = new DoubleArray(parser.Demands);
237      problem.ReadyTime = new DoubleArray(parser.Readytimes);
238      problem.DueTime = new DoubleArray(parser.Duetimes);
239      problem.ServiceTime = new DoubleArray(parser.Servicetimes);
240
241      this.ProblemInstance = problem;
242
243      OnReset();
244    }
245
246    public void ImportFromTSPLib(string tspFileName) {
247      TSPLIBParser parser = new TSPLIBParser(tspFileName);
248      parser.Parse();
249
250      this.Name = parser.Name;
251      int problemSize = parser.Demands.Length;
252
253      if (parser.Depot != 1)
254        throw new Exception("Invalid depot specification");
255
256      if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
257        throw new Exception("Invalid weight type");
258
259      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
260      problem.Coordinates = new DoubleMatrix(parser.Vertices);
261      if (parser.Vehicles != -1)
262        problem.Vehicles.Value = parser.Vehicles;
263      else
264        problem.Vehicles.Value = problemSize - 1;
265      problem.Capacity.Value = parser.Capacity;
266      problem.Demand = new DoubleArray(parser.Demands);
267      problem.ReadyTime = new DoubleArray(problemSize);
268      problem.DueTime = new DoubleArray(problemSize);
269      problem.ServiceTime = new DoubleArray(problemSize);
270
271      for (int i = 0; i < problemSize; i++) {
272        problem.ReadyTime[i] = 0;
273        problem.DueTime[i] = int.MaxValue;
274        problem.ServiceTime[i] = 0;
275      }
276
277      if (parser.Distance != -1) {
278        problem.DueTime[0] = parser.Distance;
279      }
280
281      this.ProblemInstance = problem;
282
283      OnReset();
284    }
285
286    public void ImportFromORLib(string orFileName) {
287      ORLIBParser parser = new ORLIBParser(orFileName);
288      parser.Parse();
289
290      this.Name = parser.Name;
291      int problemSize = parser.Demands.Length;
292
293      CVRPProblemInstance problem = new CVRPProblemInstance();
294
295      problem.Coordinates = new DoubleMatrix(parser.Vertices);
296      problem.Vehicles.Value = problemSize - 1;
297      problem.Capacity.Value = parser.Capacity;
298      problem.Demand = new DoubleArray(parser.Demands);
299
300      this.ProblemInstance = problem;
301
302      OnReset();
303    }
304
305    private void InitializeRandomVRPInstance() {
306      System.Random rand = new System.Random();
307
308      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
309      int cities = 100;
310
311      problem.Coordinates = new DoubleMatrix(cities + 1, 2);
312      problem.Demand = new DoubleArray(cities + 1);
313      problem.DueTime = new DoubleArray(cities + 1);
314      problem.ReadyTime = new DoubleArray(cities + 1);
315      problem.ServiceTime = new DoubleArray(cities + 1);
316
317      problem.Vehicles.Value = 100;
318      problem.Capacity.Value = 200;
319
320      for (int i = 0; i <= cities; i++) {
321        problem.Coordinates[i, 0] = rand.Next(0, 100);
322        problem.Coordinates[i, 1] = rand.Next(0, 100);
323
324        if (i == 0) {
325          problem.Demand[i] = 0;
326          problem.DueTime[i] = Int16.MaxValue;
327          problem.ReadyTime[i] = 0;
328          problem.ServiceTime[i] = 0;
329        } else {
330          problem.Demand[i] = rand.Next(10, 50);
331          problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i)), 1200);
332          problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
333          problem.ServiceTime[i] = 90;
334        }
335      }
336
337      this.ProblemInstance = problem;
338    }
339  }
340}
Note: See TracBrowser for help on using the repository browser.