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