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, 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 | VehicleRoutingProblem clone = (VehicleRoutingProblem)base.Clone(cloner);
|
---|
125 | clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
126 | clone.AttachEventHandlers();
|
---|
127 | return clone;
|
---|
128 | }
|
---|
129 |
|
---|
130 | #region Events
|
---|
131 | public event EventHandler SolutionCreatorChanged;
|
---|
132 | private void OnSolutionCreatorChanged() {
|
---|
133 | EventHandler handler = SolutionCreatorChanged;
|
---|
134 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
135 | }
|
---|
136 | public event EventHandler EvaluatorChanged;
|
---|
137 | private void OnEvaluatorChanged() {
|
---|
138 | EventHandler handler = EvaluatorChanged;
|
---|
139 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
140 | }
|
---|
141 | public event EventHandler OperatorsChanged;
|
---|
142 | private void OnOperatorsChanged() {
|
---|
143 | EventHandler handler = OperatorsChanged;
|
---|
144 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
145 | }
|
---|
146 | public event EventHandler Reset;
|
---|
147 | private void OnReset() {
|
---|
148 | EventHandler handler = Reset;
|
---|
149 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
150 | }
|
---|
151 | #endregion
|
---|
152 |
|
---|
153 | #region Helpers
|
---|
154 | [StorableHook(HookType.AfterDeserialization)]
|
---|
155 | private void AfterDeserializationHook() {
|
---|
156 | AttachEventHandlers();
|
---|
157 | AttachProblemInstanceEventHandlers();
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void AttachEventHandlers() {
|
---|
161 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void AttachProblemInstanceEventHandlers() {
|
---|
165 | if (Parameters.ContainsKey("Evaluator"))
|
---|
166 | Parameters.Remove("Evaluator");
|
---|
167 |
|
---|
168 | if (Parameters.ContainsKey("SolutionCreator"))
|
---|
169 | Parameters.Remove("SolutionCreator");
|
---|
170 |
|
---|
171 | if (ProblemInstance != null) {
|
---|
172 | ProblemInstance.SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
173 | ProblemInstance.EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
174 | Parameters.Add(ProblemInstance.EvaluatorParameter);
|
---|
175 | Parameters.Add(ProblemInstance.SolutionCreatorParameter);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
|
---|
180 | AttachProblemInstanceEventHandlers();
|
---|
181 | InitializeOperators();
|
---|
182 |
|
---|
183 | OnSolutionCreatorChanged();
|
---|
184 | OnEvaluatorChanged();
|
---|
185 | OnOperatorsChanged();
|
---|
186 | }
|
---|
187 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
188 | ParameterizeSolutionCreator();
|
---|
189 |
|
---|
190 | OnSolutionCreatorChanged();
|
---|
191 | }
|
---|
192 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
193 | OnEvaluatorChanged();
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void InitializeOperators() {
|
---|
197 | operators = new List<IOperator>();
|
---|
198 |
|
---|
199 | if (ProblemInstance != null) {
|
---|
200 | operators.AddRange(
|
---|
201 | ProblemInstance.Operators.Concat(
|
---|
202 | ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
|
---|
203 | }
|
---|
204 |
|
---|
205 | ParameterizeOperators();
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void ParameterizeSolutionCreator() {
|
---|
209 | if (SolutionCreator is IMultiVRPOperator) {
|
---|
210 | (SolutionCreator as IMultiVRPOperator).SetOperators(Operators);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | private void ParameterizeOperators() {
|
---|
215 | foreach (IOperator op in Operators) {
|
---|
216 | if (op is IMultiVRPOperator) {
|
---|
217 | (op as IMultiVRPOperator).SetOperators(Operators);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | public void ImportFromSolomon(string solomonFileName) {
|
---|
224 | SolomonParser parser = new SolomonParser(solomonFileName);
|
---|
225 | parser.Parse();
|
---|
226 |
|
---|
227 | this.Name = parser.ProblemName;
|
---|
228 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
229 |
|
---|
230 | problem.Coordinates = new DoubleMatrix(parser.Coordinates);
|
---|
231 | problem.Vehicles.Value = parser.Vehicles;
|
---|
232 | problem.Capacity.Value = parser.Capacity;
|
---|
233 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
234 | problem.ReadyTime = new DoubleArray(parser.Readytimes);
|
---|
235 | problem.DueTime = new DoubleArray(parser.Duetimes);
|
---|
236 | problem.ServiceTime = new DoubleArray(parser.Servicetimes);
|
---|
237 |
|
---|
238 | this.ProblemInstance = problem;
|
---|
239 |
|
---|
240 | OnReset();
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void ImportFromTSPLib(string tspFileName) {
|
---|
244 | TSPLIBParser parser = new TSPLIBParser(tspFileName);
|
---|
245 | parser.Parse();
|
---|
246 |
|
---|
247 | this.Name = parser.Name;
|
---|
248 | int problemSize = parser.Demands.Length;
|
---|
249 |
|
---|
250 | if (parser.Depot != 1)
|
---|
251 | throw new Exception("Invalid depot specification");
|
---|
252 |
|
---|
253 | if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D)
|
---|
254 | throw new Exception("Invalid weight type");
|
---|
255 |
|
---|
256 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
257 | problem.Coordinates = new DoubleMatrix(parser.Vertices);
|
---|
258 | if (parser.Vehicles != -1)
|
---|
259 | problem.Vehicles.Value = parser.Vehicles;
|
---|
260 | else
|
---|
261 | problem.Vehicles.Value = problemSize - 1;
|
---|
262 | problem.Capacity.Value = parser.Capacity;
|
---|
263 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
264 | problem.ReadyTime = new DoubleArray(problemSize);
|
---|
265 | problem.DueTime = new DoubleArray(problemSize);
|
---|
266 | problem.ServiceTime = new DoubleArray(problemSize);
|
---|
267 |
|
---|
268 | for (int i = 0; i < problemSize; i++) {
|
---|
269 | problem.ReadyTime[i] = 0;
|
---|
270 | problem.DueTime[i] = int.MaxValue;
|
---|
271 | problem.ServiceTime[i] = 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (parser.Distance != -1) {
|
---|
275 | problem.DueTime[0] = parser.Distance;
|
---|
276 | }
|
---|
277 |
|
---|
278 | this.ProblemInstance = problem;
|
---|
279 |
|
---|
280 | OnReset();
|
---|
281 | }
|
---|
282 |
|
---|
283 | public void ImportFromORLib(string orFileName) {
|
---|
284 | ORLIBParser parser = new ORLIBParser(orFileName);
|
---|
285 | parser.Parse();
|
---|
286 |
|
---|
287 | this.Name = parser.Name;
|
---|
288 | int problemSize = parser.Demands.Length;
|
---|
289 |
|
---|
290 | CVRPProblemInstance problem = new CVRPProblemInstance();
|
---|
291 |
|
---|
292 | problem.Coordinates = new DoubleMatrix(parser.Vertices);
|
---|
293 | problem.Vehicles.Value = problemSize - 1;
|
---|
294 | problem.Capacity.Value = parser.Capacity;
|
---|
295 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
296 |
|
---|
297 | this.ProblemInstance = problem;
|
---|
298 |
|
---|
299 | OnReset();
|
---|
300 | }
|
---|
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)), 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 | }
|
---|