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