1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Reflection;
|
---|
28 | using HeuristicLab.Analysis;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Optimization.Operators;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
36 | using HeuristicLab.PluginInfrastructure;
|
---|
37 | using HeuristicLab.Problems.Instances;
|
---|
38 | using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
|
---|
39 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
40 | using HeuristicLab.Problems.VehicleRouting.Interpreters;
|
---|
41 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
42 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
43 |
|
---|
44 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
45 | [Item("Vehicle Routing Problem (VRP)", "Represents a Vehicle Routing Problem.")]
|
---|
46 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 110)]
|
---|
47 | [StorableClass]
|
---|
48 | public sealed class VehicleRoutingProblem : Problem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent, IProblemInstanceConsumer<IVRPData> {
|
---|
49 | public string Filename { get; set; }
|
---|
50 |
|
---|
51 | public static new Image StaticItemImage {
|
---|
52 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | #region Parameter Properties
|
---|
56 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
57 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
58 | }
|
---|
59 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
60 | get { return MaximizationParameter; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
63 | get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
64 | }
|
---|
65 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
66 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
67 | }
|
---|
68 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
69 | get { return BestKnownQualityParameter; }
|
---|
70 | }
|
---|
71 | public OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
|
---|
72 | get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
|
---|
73 | }
|
---|
74 | public IConstrainedValueParameter<IVRPCreator> SolutionCreatorParameter {
|
---|
75 | get { return (IConstrainedValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
|
---|
76 | }
|
---|
77 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
78 | get { return SolutionCreatorParameter; }
|
---|
79 | }
|
---|
80 | public IValueParameter<IVRPEvaluator> EvaluatorParameter {
|
---|
81 | get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
|
---|
82 | }
|
---|
83 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
84 | get { return EvaluatorParameter; }
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | #region Properties
|
---|
89 | public IVRPProblemInstance ProblemInstance {
|
---|
90 | get { return ProblemInstanceParameter.Value; }
|
---|
91 | set { ProblemInstanceParameter.Value = value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public VRPSolution BestKnownSolution {
|
---|
95 | get { return BestKnownSolutionParameter.Value; }
|
---|
96 | set { BestKnownSolutionParameter.Value = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public DoubleValue BestKnownQuality {
|
---|
100 | get { return BestKnownQualityParameter.Value; }
|
---|
101 | set { BestKnownQualityParameter.Value = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public ISingleObjectiveEvaluator Evaluator {
|
---|
105 | get { return EvaluatorParameter.Value; }
|
---|
106 | }
|
---|
107 |
|
---|
108 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
109 | get { return this.Evaluator; }
|
---|
110 | }
|
---|
111 |
|
---|
112 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
113 | get { return SolutionCreatorParameter.Value; }
|
---|
114 | }
|
---|
115 | public IVRPCreator SolutionCreator {
|
---|
116 | get { return SolutionCreatorParameter.Value; }
|
---|
117 | set { SolutionCreatorParameter.Value = value; }
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | [StorableConstructor]
|
---|
122 | private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
|
---|
123 | public VehicleRoutingProblem()
|
---|
124 | : base() {
|
---|
125 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
|
---|
126 | Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
127 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
|
---|
128 | Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
|
---|
129 |
|
---|
130 | Parameters.Add(new ConstrainedValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions."));
|
---|
131 | Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions."));
|
---|
132 |
|
---|
133 | EvaluatorParameter.Hidden = true;
|
---|
134 |
|
---|
135 | InitializeRandomVRPInstance();
|
---|
136 | InitializeOperators();
|
---|
137 |
|
---|
138 | AttachEventHandlers();
|
---|
139 | AttachProblemInstanceEventHandlers();
|
---|
140 |
|
---|
141 | EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
145 | cloner.Clone(ProblemInstance);
|
---|
146 | return new VehicleRoutingProblem(this, cloner);
|
---|
147 | }
|
---|
148 |
|
---|
149 | private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
|
---|
150 | : base(original, cloner) {
|
---|
151 | this.AttachEventHandlers();
|
---|
152 | this.AttachProblemInstanceEventHandlers();
|
---|
153 |
|
---|
154 | ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
|
---|
155 | }
|
---|
156 |
|
---|
157 | #region Events
|
---|
158 | public event EventHandler SolutionCreatorChanged;
|
---|
159 | private void OnSolutionCreatorChanged() {
|
---|
160 | EventHandler handler = SolutionCreatorChanged;
|
---|
161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 | public event EventHandler EvaluatorChanged;
|
---|
164 | private void OnEvaluatorChanged() {
|
---|
165 | EventHandler handler = EvaluatorChanged;
|
---|
166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
167 | }
|
---|
168 | #endregion
|
---|
169 |
|
---|
170 | #region Helpers
|
---|
171 | [StorableHook(HookType.AfterDeserialization)]
|
---|
172 | private void AfterDeserialization() {
|
---|
173 | AttachEventHandlers();
|
---|
174 | AttachProblemInstanceEventHandlers();
|
---|
175 |
|
---|
176 | ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
|
---|
177 | }
|
---|
178 |
|
---|
179 | [Storable(Name = "operators", AllowOneWay = true)]
|
---|
180 | private List<IOperator> StorableOperators {
|
---|
181 | set { Operators.AddRange(value); }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void AttachEventHandlers() {
|
---|
185 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
186 | BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
187 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
188 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void AttachProblemInstanceEventHandlers() {
|
---|
192 | if (ProblemInstance != null) {
|
---|
193 | ProblemInstance.EvaluationChanged += new EventHandler(ProblemInstance_EvaluationChanged);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void EvalBestKnownSolution() {
|
---|
198 | if (BestKnownSolution != null) {
|
---|
199 | //call evaluator
|
---|
200 | BestKnownQuality = new DoubleValue(ProblemInstance.Evaluate(BestKnownSolution.Solution).Quality);
|
---|
201 | BestKnownSolution.Quality = BestKnownQuality;
|
---|
202 | } else {
|
---|
203 | BestKnownQuality = null;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
208 | EvalBestKnownSolution();
|
---|
209 | }
|
---|
210 |
|
---|
211 | void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
|
---|
212 | EvalBestKnownSolution();
|
---|
213 | }
|
---|
214 |
|
---|
215 | void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
|
---|
216 | InitializeOperators();
|
---|
217 | AttachProblemInstanceEventHandlers();
|
---|
218 |
|
---|
219 | EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
|
---|
220 |
|
---|
221 | OnSolutionCreatorChanged();
|
---|
222 | OnEvaluatorChanged();
|
---|
223 | OnOperatorsChanged();
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void SetProblemInstance(IVRPProblemInstance instance) {
|
---|
227 | ProblemInstanceParameter.ValueChanged -= new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
228 |
|
---|
229 | ProblemInstance = instance;
|
---|
230 | AttachProblemInstanceEventHandlers();
|
---|
231 |
|
---|
232 | OnSolutionCreatorChanged();
|
---|
233 | OnEvaluatorChanged();
|
---|
234 |
|
---|
235 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
236 | }
|
---|
237 |
|
---|
238 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
239 | OnSolutionCreatorChanged();
|
---|
240 | }
|
---|
241 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
242 | if (ProblemInstance != null)
|
---|
243 | ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
|
---|
244 | OnEvaluatorChanged();
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void InitializeOperators() {
|
---|
248 | var solutionCreatorParameter = SolutionCreatorParameter as ConstrainedValueParameter<IVRPCreator>;
|
---|
249 | solutionCreatorParameter.ValidValues.Clear();
|
---|
250 |
|
---|
251 | Operators.Clear();
|
---|
252 |
|
---|
253 | if (ProblemInstance != null) {
|
---|
254 | Operators.AddRange(
|
---|
255 | ProblemInstance.Operators.Concat(
|
---|
256 | ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
|
---|
257 | Operators.Add(new VRPSimilarityCalculator());
|
---|
258 | Operators.Add(new QualitySimilarityCalculator());
|
---|
259 | Operators.Add(new NoSimilarityCalculator());
|
---|
260 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
261 |
|
---|
262 | IVRPCreator defaultCreator = null;
|
---|
263 | foreach (IVRPCreator creator in Operators.Where(o => o is IVRPCreator)) {
|
---|
264 | solutionCreatorParameter.ValidValues.Add(creator);
|
---|
265 | if (creator is Encodings.Alba.RandomCreator)
|
---|
266 | defaultCreator = creator;
|
---|
267 | }
|
---|
268 | Operators.Add(new AlbaLambdaInterchangeLocalImprovementOperator());
|
---|
269 | if (defaultCreator != null)
|
---|
270 | solutionCreatorParameter.Value = defaultCreator;
|
---|
271 | }
|
---|
272 |
|
---|
273 | ParameterizeOperators();
|
---|
274 | }
|
---|
275 |
|
---|
276 | private void ParameterizeOperators() {
|
---|
277 | foreach (IOperator op in Operators.OfType<IOperator>()) {
|
---|
278 | if (op is IMultiVRPOperator) {
|
---|
279 | (op as IMultiVRPOperator).SetOperators(Operators.OfType<IOperator>());
|
---|
280 | }
|
---|
281 | }
|
---|
282 | if (ProblemInstance != null) {
|
---|
283 | foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
|
---|
284 | op.SolutionParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
|
---|
285 | op.SolutionParameter.Hidden = true;
|
---|
286 | }
|
---|
287 | foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
|
---|
288 | op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
|
---|
289 | op.ParentsParameter.Hidden = true;
|
---|
290 | }
|
---|
291 | foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
292 | op.SolutionVariableName = SolutionCreator.VRPToursParameter.ActualName;
|
---|
293 | op.QualityVariableName = ProblemInstance.SolutionEvaluator.QualityParameter.ActualName;
|
---|
294 | var calc = op as VRPSimilarityCalculator;
|
---|
295 | if (calc != null) calc.ProblemInstance = ProblemInstance;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | #endregion
|
---|
301 |
|
---|
302 | private void InitializeRandomVRPInstance() {
|
---|
303 | System.Random rand = new System.Random();
|
---|
304 |
|
---|
305 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
306 | int cities = 100;
|
---|
307 |
|
---|
308 | problem.Coordinates = new DoubleMatrix(cities + 1, 2);
|
---|
309 | problem.Demand = new DoubleArray(cities + 1);
|
---|
310 | problem.DueTime = new DoubleArray(cities + 1);
|
---|
311 | problem.ReadyTime = new DoubleArray(cities + 1);
|
---|
312 | problem.ServiceTime = new DoubleArray(cities + 1);
|
---|
313 |
|
---|
314 | problem.Vehicles.Value = 100;
|
---|
315 | problem.Capacity.Value = 200;
|
---|
316 |
|
---|
317 | for (int i = 0; i <= cities; i++) {
|
---|
318 | problem.Coordinates[i, 0] = rand.Next(0, 100);
|
---|
319 | problem.Coordinates[i, 1] = rand.Next(0, 100);
|
---|
320 |
|
---|
321 | if (i == 0) {
|
---|
322 | problem.Demand[i] = 0;
|
---|
323 | problem.DueTime[i] = Int16.MaxValue;
|
---|
324 | problem.ReadyTime[i] = 0;
|
---|
325 | problem.ServiceTime[i] = 0;
|
---|
326 | } else {
|
---|
327 | problem.Demand[i] = rand.Next(10, 50);
|
---|
328 | problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i, null)), 1200);
|
---|
329 | problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
|
---|
330 | problem.ServiceTime[i] = 90;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | this.ProblemInstance = problem;
|
---|
335 | }
|
---|
336 |
|
---|
337 | public void ImportSolution(string solutionFileName) {
|
---|
338 | SolutionParser parser = new SolutionParser(solutionFileName);
|
---|
339 | parser.Parse();
|
---|
340 |
|
---|
341 | HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
|
---|
342 |
|
---|
343 | int cities = 0;
|
---|
344 | foreach (List<int> route in parser.Routes) {
|
---|
345 | Tour tour = new Tour();
|
---|
346 | tour.Stops.AddRange(route);
|
---|
347 | cities += tour.Stops.Count;
|
---|
348 |
|
---|
349 | encoding.Tours.Add(tour);
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (cities != ProblemInstance.Coordinates.Rows - 1)
|
---|
353 | throw new IOException("Cannot read file. The optimal solution does not seem to correspond with the problem data");
|
---|
354 | else {
|
---|
355 | VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
|
---|
356 | BestKnownSolutionParameter.Value = solution;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | #region Instance Consuming
|
---|
361 | public void Load(IVRPData data, IVRPDataInterpreter interpreter) {
|
---|
362 | VRPInstanceDescription instance = interpreter.Interpret(data);
|
---|
363 |
|
---|
364 | Name = instance.Name;
|
---|
365 | Description = instance.Description;
|
---|
366 |
|
---|
367 | BestKnownQuality = null;
|
---|
368 | BestKnownSolution = null;
|
---|
369 |
|
---|
370 | if (ProblemInstance != null && instance.ProblemInstance != null &&
|
---|
371 | instance.ProblemInstance.GetType() == ProblemInstance.GetType())
|
---|
372 | SetProblemInstance(instance.ProblemInstance);
|
---|
373 | else
|
---|
374 | ProblemInstance = instance.ProblemInstance;
|
---|
375 |
|
---|
376 | OnReset();
|
---|
377 |
|
---|
378 | if (instance.BestKnownQuality != null) {
|
---|
379 | BestKnownQuality = new DoubleValue((double)instance.BestKnownQuality);
|
---|
380 | }
|
---|
381 |
|
---|
382 | if (instance.BestKnownSolution != null) {
|
---|
383 | VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
|
---|
384 | BestKnownSolution = solution;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | #endregion
|
---|
388 |
|
---|
389 | #region IProblemInstanceConsumer<VRPData> Members
|
---|
390 |
|
---|
391 | public void Load(IVRPData data) {
|
---|
392 | var interpreterDataType = data.GetType();
|
---|
393 | var interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(interpreterDataType);
|
---|
394 |
|
---|
395 | var interpreters = ApplicationManager.Manager.GetTypes(interpreterType);
|
---|
396 |
|
---|
397 | var concreteInterpreter = interpreters.Single(t => GetInterpreterDataType(t) == interpreterDataType);
|
---|
398 |
|
---|
399 | Load(data, (IVRPDataInterpreter)Activator.CreateInstance(concreteInterpreter));
|
---|
400 | }
|
---|
401 |
|
---|
402 | private Type GetInterpreterDataType(Type type) {
|
---|
403 | var parentInterfaces = type.BaseType.GetInterfaces();
|
---|
404 | var interfaces = type.GetInterfaces().Except(parentInterfaces);
|
---|
405 |
|
---|
406 | var interpreterInterface = interfaces.Single(i => typeof(IVRPDataInterpreter).IsAssignableFrom(i));
|
---|
407 | return interpreterInterface.GetGenericArguments()[0];
|
---|
408 | }
|
---|
409 |
|
---|
410 | #endregion
|
---|
411 | }
|
---|
412 | }
|
---|