Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/PickupDeliveryScenario.cs @ 11055

Last change on this file since 11055 was 8839, checked in by svonolfe, 12 years ago

Adapted the parser to read the distance matrix (#1955)

File size: 10.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
25using System.Text;
26using HeuristicLab.PDPSimulation.SimulationCore;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Common;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
32using HeuristicLab.PDPSimulation.DomainModel;
33using HeuristicLab.PDPSimulation.DistanceMeasures;
34using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
35using HeuristicLab.PDPSimulation.Operators;
36
37namespace HeuristicLab.PDPSimulation {
38  [Item("PickupDeliveryScenario", "A pickup and delivery scenario.")]
39  [StorableClass]
40  public sealed class PickupDeliveryScenario: Scenario {
41    public event EventHandler ScenarioChanged;
42
43    public ValueParameter<DistanceMeasure> DistanceMeasureParameter {
44      get { return (ValueParameter<DistanceMeasure>)Parameters["DistanceMeasure"]; }
45    }
46
47    public ValueParameter<BoolValue> AllowDiversionParameter {
48      get { return (ValueParameter<BoolValue>)Parameters["AllowDiversion"]; }
49    }
50   
51    public ValueParameter<DoubleMatrix> DepotCoordinatesParameter {
52      get { return (ValueParameter<DoubleMatrix>)Parameters["DepotCoordinates"]; }
53    }
54
55    public ValueParameter<DoubleArray> VehicleCapacitiesParameter {
56      get { return (ValueParameter<DoubleArray>)Parameters["VehicleCapacities"]; }
57    }
58
59    public ValueParameter<DoubleArray> VehicleReadyTimesParameter {
60      get { return (ValueParameter<DoubleArray>)Parameters["VehicleReadyTimes"]; }
61    }
62
63    public ValueParameter<DoubleArray> VehicleDueTimesParameter {
64      get { return (ValueParameter<DoubleArray>)Parameters["VehicleDueTimes"]; }
65    }
66   
67    public OptionalValueParameter<PickupDeliveryOrderGenerator> OrderGeneratorParameter {
68      get { return (OptionalValueParameter<PickupDeliveryOrderGenerator>)Parameters["PickupDeliveryOrderGenerator"]; }
69    }
70
71    public ValueParameter<MDCVRPTWEvaluator> EvaluatorParameter {
72      get { return (ValueParameter<MDCVRPTWEvaluator>)Parameters["Evaluator"]; }
73    }
74
75    public IValueParameter<DoubleValue> DistanceFactorParameter {
76      get { return (IValueParameter<DoubleValue>)Parameters["DistanceFactor"]; }
77    }
78
79    public IValueParameter<DoubleValue> FleetUsageFactorParameter {
80      get { return (IValueParameter<DoubleValue>)Parameters["FleetUsageFactor"]; }
81    }
82
83    public IValueParameter<DoubleValue> OverloadPenaltyParameter {
84      get { return (IValueParameter<DoubleValue>)Parameters["OverloadPenalty"]; }
85    }
86
87    public IValueParameter<DoubleValue> TardinessPenaltyParameter {
88      get { return (IValueParameter<DoubleValue>)Parameters["TardinessPenalty"]; }
89    }
90
91    public IValueParameter<DoubleValue> PickupViolationPenaltyParameter {
92      get { return (IValueParameter<DoubleValue>)Parameters["PickupViolationPenalty"]; }
93    }
94
95    public IValueParameter<BoolValue> RelocateBackToDepotParameter {
96      get { return (IValueParameter<BoolValue>)Parameters["RelocateBackToDepot"]; }
97    }
98
99    public DistanceMeasure DistanceMeasure {
100      get {
101        return DistanceMeasureParameter.Value;
102      }
103      set {
104        DistanceMeasureParameter.Value = value;
105      }
106    }
107
108    public PickupDeliveryOrderGenerator OrderGenerator {
109      get {
110        return OrderGeneratorParameter.Value;
111      }
112      set {
113        OrderGeneratorParameter.Value = value;
114      }
115    }
116
117    public MDCVRPTWEvaluator Evaluator {
118      get {
119        return EvaluatorParameter.Value;
120      }
121
122      set {
123        EvaluatorParameter.Value = value;
124      }
125    }
126
127    public bool AllowDiversion {
128      get {
129        return AllowDiversionParameter.Value.Value &&
130          DistanceMeasure.DiversionSupported;
131      }
132    }
133
134    public bool RelocateBackToDepot {
135      get {
136        return RelocateBackToDepotParameter.Value.Value;
137      }
138    }
139
140    public PickupDeliveryScenario() : base() {
141      Parameters.Add(new ValueParameter<DistanceMeasure>("DistanceMeasure", new EuclideanDistanceMeasure()));
142      Parameters.Add(new ValueParameter<BoolValue>("AllowDiversion", new BoolValue(false)));
143      Parameters.Add(new ValueParameter<DoubleMatrix>("DepotCoordinates", new DoubleMatrix()));
144      Parameters.Add(new ValueParameter<BoolValue>("RelocateBackToDepot", new BoolValue(true)));
145      Parameters.Add(new ValueParameter<DoubleArray>("VehicleCapacities", new DoubleArray()));
146      Parameters.Add(new ValueParameter<DoubleArray>("VehicleReadyTimes", new DoubleArray()));
147      Parameters.Add(new ValueParameter<DoubleArray>("VehicleDueTimes", new DoubleArray()));
148      Parameters.Add(new ValueParameter<DoubleValue>("DistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
149      Parameters.Add(new ValueParameter<DoubleValue>("FleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(3000)));
150      Parameters.Add(new ValueParameter<DoubleValue>("OverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(10000)));
151      Parameters.Add(new ValueParameter<DoubleValue>("TardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(10000)));
152      Parameters.Add(new ValueParameter<DoubleValue>("PickupViolationPenalty", "The pickup violation penalty considered in the evaluation.", new DoubleValue(1000000)));
153      Parameters.Add(new OptionalValueParameter<PickupDeliveryOrderGenerator>("PickupDeliveryOrderGenerator"));
154      Parameters.Add(new ValueParameter<MDCVRPTWEvaluator>("Evaluator", new DynPDPEvaluator()));
155
156      DepotCoordinatesParameter.GetsCollected = false;
157      VehicleCapacitiesParameter.GetsCollected = false;
158      VehicleReadyTimesParameter.GetsCollected = false;
159      VehicleDueTimesParameter.GetsCollected = false;
160      OrderGeneratorParameter.GetsCollected = false;
161      DistanceMeasureParameter.GetsCollected = false;
162    }
163
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      if (!Parameters.ContainsKey("Evaluator")) {
167        Parameters.Add(new ValueParameter<MDCVRPTWEvaluator>("Evaluator", new DynPDPEvaluator()));
168      }
169    }
170
171    public IEnumerable<Order> GetOrders(double timeStart, double timeEnd) {
172      return OrderGeneratorParameter.Value.GetOrders(timeStart, timeEnd);
173    }
174
175    public bool MoreOrders(double time) {
176      return OrderGeneratorParameter.Value.MoreOrders(time);
177    }
178
179    public double OrderRangeX() {
180      return OrderGeneratorParameter.Value.RangeX();
181    }
182
183    public double OrderRangeY() {
184      return OrderGeneratorParameter.Value.RangeY();
185    }
186
187    private void OnScenarioChanged() {
188      if (ScenarioChanged != null)
189        ScenarioChanged(this, null);
190    }
191
192    private void ImportFromDynPDP(DynPDPParser parser) {
193      parser.Parse();
194
195      int vehicles = parser.Vehicles.Length;
196      DoubleMatrix depotCoordinates = new DoubleMatrix(vehicles, 2);
197      DoubleArray vehicleCapacities = new DoubleArray(vehicles);
198      DoubleArray vehicleReadyTimes = new DoubleArray(vehicles);
199      DoubleArray vehicleDueTimes = new DoubleArray(vehicles);
200
201      for (int i = 0; i < vehicles; i++) {
202        DynPDPParser.Vehicle vehicle = parser.Vehicles[i];
203        depotCoordinates[i, 0] = vehicle.xCoord;
204        depotCoordinates[i, 1] = vehicle.yCoord;
205        vehicleCapacities[i] = vehicle.capacity;
206        vehicleReadyTimes[i] = vehicle.readyTime;
207        vehicleDueTimes[i] = vehicle.dueTime;
208      }
209      DepotCoordinatesParameter.Value = depotCoordinates;
210      VehicleCapacitiesParameter.Value = vehicleCapacities;
211      VehicleReadyTimesParameter.Value = vehicleReadyTimes;
212      VehicleDueTimesParameter.Value = vehicleDueTimes;
213
214      if (parser.DistanceMatrix.useDistanceMatrix) {
215        DistanceMatrixMeasure dm = new DistanceMatrixMeasure();
216
217        dm.PointMapping = new IntMatrix(parser.DistanceMatrix.coordinates);
218        dm.DistanceMatrix = new DoubleMatrix(parser.DistanceMatrix.matrix);
219
220        DistanceMeasure = dm;
221      }
222
223      PickupDeliveryPredefinedOrderGenerator orderGen = new PickupDeliveryPredefinedOrderGenerator();
224      int orders = parser.Orders.Length;
225
226      DoubleMatrix orderMatrix = new DoubleMatrix(orders, 12,
227        new string[] { "Time revealed", "P X coordinate", "P Y coordinate", "D X coordinate", "D Y coordinate",
228        "Demand", "P service time", "P ready time", "P due time", "D service time", "D ready time", "D due time"});
229      for (int i = 0; i < orders; i++) {
230        DynPDPParser.Order order = parser.Orders[i];
231
232        orderMatrix[i, 0] = order.revealedTime;
233        orderMatrix[i, 1] = order.pickupXCoord;
234        orderMatrix[i, 2] = order.pickupYCoord;
235        orderMatrix[i, 3] = order.deliveryXCoord;
236        orderMatrix[i, 4] = order.deliveryYCoord;
237        orderMatrix[i, 5] = order.demand;
238        orderMatrix[i, 6] = order.pickupServiceTime;
239        orderMatrix[i, 7] = order.pickupReadyTime;
240        orderMatrix[i, 8] = order.pickupDueTime;
241        orderMatrix[i, 9] = order.deliveryServiceTime;
242        orderMatrix[i, 10] = order.deliveryReadyTime;
243        orderMatrix[i, 11] = order.deliveryDueTime;
244      }
245      orderGen.OrdersParameter.Value = orderMatrix;
246      OrderGeneratorParameter.Value = orderGen;
247
248      OnScenarioChanged();
249    }
250
251    public void ImportFromDynPDP(string fileName) {
252      DefaultDynPDPParser parser = new DefaultDynPDPParser(fileName);
253      ImportFromDynPDP(parser);
254    }
255
256    public void ImportFromDynTSPLib(string fileName) {
257      TSPLibDynPDPParser parser = new TSPLibDynPDPParser(fileName);
258      ImportFromDynPDP(parser);
259    }
260
261    [StorableConstructor]
262    private PickupDeliveryScenario(bool deserializing) : base(deserializing) { }
263    private PickupDeliveryScenario(PickupDeliveryScenario original, Cloner cloner)
264      : base(original, cloner) {
265    }
266    public override IDeepCloneable Clone(Cloner cloner) {
267      return new PickupDeliveryScenario(this, cloner);
268    }
269  }
270}
Note: See TracBrowser for help on using the repository browser.