Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs @ 4408

Last change on this file since 4408 was 4365, checked in by svonolfe, 14 years ago

Worked on VRP operators (WIP) (#1177)

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