1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Problems.Instances;
|
---|
34 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
35 | using HeuristicLab.Problems.VehicleRouting.Interpreters;
|
---|
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 : Problem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent, IProblemInstanceConsumer<IVRPData> {
|
---|
44 | public string Filename { get; set; }
|
---|
45 |
|
---|
46 | public static new Image StaticItemImage {
|
---|
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 DoubleValue BestKnownQuality {
|
---|
95 | get { return BestKnownQualityParameter.Value; }
|
---|
96 | set { BestKnownQualityParameter.Value = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public ISingleObjectiveEvaluator Evaluator {
|
---|
100 | get { return EvaluatorParameter.Value; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
104 | get { return this.Evaluator; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public ISolutionCreator SolutionCreator {
|
---|
108 | get { return SolutionCreatorParameter.Value; }
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | [StorableConstructor]
|
---|
113 | private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
|
---|
114 | public VehicleRoutingProblem()
|
---|
115 | : base() {
|
---|
116 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
|
---|
117 | Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
118 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
|
---|
119 | Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
|
---|
120 |
|
---|
121 | Parameters.Add(new ConstrainedValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions."));
|
---|
122 | Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions."));
|
---|
123 |
|
---|
124 | EvaluatorParameter.Hidden = true;
|
---|
125 |
|
---|
126 | InitializeRandomVRPInstance();
|
---|
127 | InitializeOperators();
|
---|
128 |
|
---|
129 | AttachEventHandlers();
|
---|
130 | AttachProblemInstanceEventHandlers();
|
---|
131 | }
|
---|
132 |
|
---|
133 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
134 | cloner.Clone(ProblemInstance);
|
---|
135 | return new VehicleRoutingProblem(this, cloner);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
|
---|
139 | : base(original, cloner) {
|
---|
140 | this.AttachEventHandlers();
|
---|
141 | }
|
---|
142 |
|
---|
143 | #region Events
|
---|
144 | public event EventHandler SolutionCreatorChanged;
|
---|
145 | private void OnSolutionCreatorChanged() {
|
---|
146 | EventHandler handler = SolutionCreatorChanged;
|
---|
147 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
148 | }
|
---|
149 | public event EventHandler EvaluatorChanged;
|
---|
150 | private void OnEvaluatorChanged() {
|
---|
151 | EventHandler handler = EvaluatorChanged;
|
---|
152 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | #region Helpers
|
---|
157 | [StorableHook(HookType.AfterDeserialization)]
|
---|
158 | private void AfterDeserialization() {
|
---|
159 | AttachEventHandlers();
|
---|
160 | AttachProblemInstanceEventHandlers();
|
---|
161 | }
|
---|
162 |
|
---|
163 | [Storable(Name = "operators", AllowOneWay = true)]
|
---|
164 | private List<IOperator> StorableOperators {
|
---|
165 | set { Operators.AddRange(value); }
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void AttachEventHandlers() {
|
---|
169 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
170 | BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
171 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
172 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void AttachProblemInstanceEventHandlers() {
|
---|
176 | var solutionCreatorParameter = SolutionCreatorParameter as ConstrainedValueParameter<IVRPCreator>;
|
---|
177 | solutionCreatorParameter.ValidValues.Clear();
|
---|
178 |
|
---|
179 | if (ProblemInstance != null) {
|
---|
180 | EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
|
---|
181 | IVRPCreator defaultCreator = null;
|
---|
182 | foreach (IVRPCreator creator in Operators.Where(o => o is IVRPCreator)) {
|
---|
183 | solutionCreatorParameter.ValidValues.Add(creator);
|
---|
184 | if (creator is Encodings.Alba.RandomCreator)
|
---|
185 | defaultCreator = creator;
|
---|
186 | }
|
---|
187 | if (defaultCreator != null)
|
---|
188 | solutionCreatorParameter.Value = defaultCreator;
|
---|
189 |
|
---|
190 | ProblemInstance.EvaluationChanged += new EventHandler(ProblemInstance_EvaluationChanged);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void EvalBestKnownSolution() {
|
---|
195 | if (BestKnownSolution != null) {
|
---|
196 | //call evaluator
|
---|
197 | BestKnownQuality = new DoubleValue(ProblemInstance.Evaluate(BestKnownSolution.Solution).Quality);
|
---|
198 | BestKnownSolution.Quality = BestKnownQuality;
|
---|
199 | } else {
|
---|
200 | BestKnownQuality = null;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
205 | EvalBestKnownSolution();
|
---|
206 | }
|
---|
207 |
|
---|
208 | void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
|
---|
209 | EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
|
---|
210 | EvalBestKnownSolution();
|
---|
211 | }
|
---|
212 |
|
---|
213 | void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
|
---|
214 | InitializeOperators();
|
---|
215 | AttachProblemInstanceEventHandlers();
|
---|
216 |
|
---|
217 | OnSolutionCreatorChanged();
|
---|
218 | OnEvaluatorChanged();
|
---|
219 | OnOperatorsChanged();
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void SetProblemInstance(IVRPProblemInstance instance) {
|
---|
223 | ProblemInstanceParameter.ValueChanged -= new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
224 |
|
---|
225 | ProblemInstance = instance;
|
---|
226 | AttachProblemInstanceEventHandlers();
|
---|
227 |
|
---|
228 | OnSolutionCreatorChanged();
|
---|
229 | OnEvaluatorChanged();
|
---|
230 |
|
---|
231 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
235 | OnSolutionCreatorChanged();
|
---|
236 | }
|
---|
237 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
238 | if (ProblemInstance != null)
|
---|
239 | ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
|
---|
240 | OnEvaluatorChanged();
|
---|
241 | }
|
---|
242 |
|
---|
243 | private void InitializeOperators() {
|
---|
244 | Operators.Clear();
|
---|
245 |
|
---|
246 | if (ProblemInstance != null) {
|
---|
247 | Operators.AddRange(
|
---|
248 | ProblemInstance.Operators.Concat(
|
---|
249 | ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
|
---|
250 | }
|
---|
251 |
|
---|
252 | ParameterizeOperators();
|
---|
253 | }
|
---|
254 |
|
---|
255 | private void ParameterizeOperators() {
|
---|
256 | foreach (IOperator op in Operators.OfType<IOperator>()) {
|
---|
257 | if (op is IMultiVRPOperator) {
|
---|
258 | (op as IMultiVRPOperator).SetOperators(Operators.OfType<IOperator>());
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 | #endregion
|
---|
263 |
|
---|
264 | private void InitializeRandomVRPInstance() {
|
---|
265 | System.Random rand = new System.Random();
|
---|
266 |
|
---|
267 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
268 | int cities = 100;
|
---|
269 |
|
---|
270 | problem.Coordinates = new DoubleMatrix(cities + 1, 2);
|
---|
271 | problem.Demand = new DoubleArray(cities + 1);
|
---|
272 | problem.DueTime = new DoubleArray(cities + 1);
|
---|
273 | problem.ReadyTime = new DoubleArray(cities + 1);
|
---|
274 | problem.ServiceTime = new DoubleArray(cities + 1);
|
---|
275 |
|
---|
276 | problem.Vehicles.Value = 100;
|
---|
277 | problem.Capacity.Value = 200;
|
---|
278 |
|
---|
279 | for (int i = 0; i <= cities; i++) {
|
---|
280 | problem.Coordinates[i, 0] = rand.Next(0, 100);
|
---|
281 | problem.Coordinates[i, 1] = rand.Next(0, 100);
|
---|
282 |
|
---|
283 | if (i == 0) {
|
---|
284 | problem.Demand[i] = 0;
|
---|
285 | problem.DueTime[i] = Int16.MaxValue;
|
---|
286 | problem.ReadyTime[i] = 0;
|
---|
287 | problem.ServiceTime[i] = 0;
|
---|
288 | } else {
|
---|
289 | problem.Demand[i] = rand.Next(10, 50);
|
---|
290 | problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i, null)), 1200);
|
---|
291 | problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
|
---|
292 | problem.ServiceTime[i] = 90;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | this.ProblemInstance = problem;
|
---|
297 | }
|
---|
298 |
|
---|
299 | public void ImportSolution(string solutionFileName) {
|
---|
300 | SolutionParser parser = new SolutionParser(solutionFileName);
|
---|
301 | parser.Parse();
|
---|
302 |
|
---|
303 | HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
|
---|
304 |
|
---|
305 | int cities = 0;
|
---|
306 | foreach (List<int> route in parser.Routes) {
|
---|
307 | Tour tour = new Tour();
|
---|
308 | tour.Stops.AddRange(route);
|
---|
309 | cities += tour.Stops.Count;
|
---|
310 |
|
---|
311 | encoding.Tours.Add(tour);
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (cities != ProblemInstance.Coordinates.Rows - 1)
|
---|
315 | ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
|
---|
316 | else {
|
---|
317 | VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
|
---|
318 | BestKnownSolutionParameter.Value = solution;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | public void Load(IVRPData data) {
|
---|
323 | Type interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(data.GetType());
|
---|
324 | var interpreters = ApplicationManager.Manager.GetInstances(interpreterType);
|
---|
325 | if (interpreters.Count() > 0) {
|
---|
326 | IVRPDataInterpreter interpreter = interpreters.First() as IVRPDataInterpreter;
|
---|
327 | VRPInstanceDescription instance = interpreter.Interpret(data);
|
---|
328 |
|
---|
329 | Name = instance.Name;
|
---|
330 | Description = instance.Description;
|
---|
331 | if (ProblemInstance != null && instance.ProblemInstance != null &&
|
---|
332 | instance.ProblemInstance.GetType() == ProblemInstance.GetType())
|
---|
333 | SetProblemInstance(instance.ProblemInstance);
|
---|
334 | else
|
---|
335 | ProblemInstance = instance.ProblemInstance;
|
---|
336 |
|
---|
337 | OnReset();
|
---|
338 | BestKnownQuality = null;
|
---|
339 | BestKnownSolution = null;
|
---|
340 |
|
---|
341 | if (instance.BestKnownQuality != null) {
|
---|
342 | BestKnownQuality = new DoubleValue((double)instance.BestKnownQuality);
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (instance.BestKnownSolution != null) {
|
---|
346 | VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
|
---|
347 | BestKnownSolution = solution;
|
---|
348 | }
|
---|
349 | } else {
|
---|
350 | throw new Exception("Cannot find an interpreter for " + data.GetType());
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|