Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added initial version - WIP (#1177)

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