Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/PickupDeliveryAnalyzer.cs @ 13042

Last change on this file since 13042 was 8858, checked in by svonolfe, 12 years ago

changed name of the tardiness penalty result (#1955)

File size: 12.7 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.Optimization;
27using HeuristicLab.Analysis;
28using HeuristicLab.Data;
29using HeuristicLab.PDPSimulation.DomainModel;
30using HeuristicLab.Core;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Common;
33using HeuristicLab.Problems.VehicleRouting;
34using HeuristicLab.PDPSimulation.Operators;
35using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
36
37namespace HeuristicLab.PDPSimulation {
38  [Item("PickupDeliveryAnalyzer", "A pickup and delivery analyzer.")]
39  [StorableClass]
40  public class PickupDeliveryAnalyzer: ParameterizedNamedItem {
41    public PickupDeliveryAnalyzer() : base() {
42    }
43    [StorableConstructor]
44    protected PickupDeliveryAnalyzer(bool deserializing) : base(deserializing) { }
45    protected PickupDeliveryAnalyzer(PickupDeliveryAnalyzer original, Cloner cloner)
46      : base(original, cloner) {
47    }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new PickupDeliveryAnalyzer(this, cloner);
50    }
51
52    public virtual void UpdateVisualization(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
53      if (!results.ContainsKey("Visualization"))
54        results.Add(new Result("Visualization", new PickupDeliveryVisualization(simulation)));
55      else
56        (results["Visualization"].Value as PickupDeliveryVisualization).SetState(simulation);
57
58      (results["Visualization"].Value as PickupDeliveryVisualization).Refresh();
59    }
60
61    protected virtual void UpdateOrderResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
62      if (!results.ContainsKey("OrderStatistics")) {
63        DataTable data = new DataTable("OrderStatistics");
64        results.Add(new Result("OrderStatistics", data));
65
66        data.Rows.Add(new DataRow("WaitingOrders"));
67        data.Rows.Add(new DataRow("PickedUpOrders"));
68        data.Rows.Add(new DataRow("DeliveredOrders"));
69        data.Rows.Add(new DataRow("CancelledOrders"));
70
71        results.Add(new Result("AverageQueueLength", new DoubleValue(0)));
72      }
73
74      DataTable orderStatistics = results["OrderStatistics"].Value as DataTable;
75
76      int currentWaiting = 0;
77      int currentPickedUp = 0;
78      int currentDelivered = 0;
79      int currentCancelled = 0;
80
81      foreach (Order order in simulation.Orders) {
82        if (order.OrderState == OrderState.Waiting || order.OrderState == OrderState.PickingUp)
83          currentWaiting++;
84        else if (order.OrderState == OrderState.PickedUp || order.OrderState == OrderState.Delivering)
85          currentPickedUp++;
86        else if (order.OrderState == OrderState.Delivered)
87          currentDelivered++;
88        else if (order.OrderState == OrderState.Cancelled)
89          currentCancelled++;
90      }
91
92      orderStatistics.Rows["WaitingOrders"].Values.Add(currentWaiting);
93      orderStatistics.Rows["PickedUpOrders"].Values.Add(currentPickedUp);
94      orderStatistics.Rows["DeliveredOrders"].Values.Add(currentDelivered);
95      orderStatistics.Rows["CancelledOrders"].Values.Add(currentCancelled);
96
97      DoubleValue queueLength = results["AverageQueueLength"].Value as DoubleValue;
98      queueLength.Value = orderStatistics.Rows["WaitingOrders"].Values.Average();
99    }
100
101    protected virtual void UpdateVehicleResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
102      if (!results.ContainsKey("VehicleStatistics")) {
103        DataTable data = new DataTable("VehicleStatistics");
104        results.Add(new Result("VehicleStatistics", data));
105
106        data.Rows.Add(new DataRow("UsedVehicles"));
107        data.Rows.Add(new DataRow("DrivenDistance"));
108        data.Rows.Add(new DataRow("InvalidActions"));
109        data.Rows.Add(new DataRow("Tardiness"));
110        data.Rows.Add(new DataRow("CapacityUtilization"));
111
112        results.Add(new Result("UsedVehicles", new IntValue()));
113        results.Add(new Result("DrivenDistance", new DoubleValue()));
114        results.Add(new Result("InvalidActions", new DoubleValue()));
115        results.Add(new Result("VehicleTardiness", new DoubleValue()));
116      }
117
118      DataTable vehicleStatistics = results["VehicleStatistics"].Value as DataTable;
119
120      int usedVehicles = simulation.Vehicles.Where(v => v.Distance > 0).Count();
121      double distance = Math.Round(simulation.Vehicles.Sum(v => v.Distance) * 100.0) / 100.0;
122      int invalidActions = simulation.Vehicles.Sum(v => v.InvalidActions);
123      double tardiness = Math.Round(simulation.Vehicles.Sum(v => v.Tardiness) * 100.0) / 100.0;     
124      var used = simulation.Vehicles.Where(v => v.Distance > 0);
125      double capacityUtilization = 0;
126      if(used.Count() > 0)
127        capacityUtilization = Math.Round(used.Average(v => 1.0 - (v.Capacity / v.TotalCapacity)) * 100.0) / 100.0;
128
129      vehicleStatistics.Rows["UsedVehicles"].Values.Add(usedVehicles);
130      vehicleStatistics.Rows["DrivenDistance"].Values.Add(distance);
131      vehicleStatistics.Rows["InvalidActions"].Values.Add(invalidActions);
132      vehicleStatistics.Rows["Tardiness"].Values.Add(tardiness);
133      vehicleStatistics.Rows["CapacityUtilization"].Values.Add(capacityUtilization);
134
135      (results["UsedVehicles"].Value as IntValue).Value = usedVehicles;
136      (results["DrivenDistance"].Value as DoubleValue).Value = distance;
137      (results["VehicleTardiness"].Value as DoubleValue).Value = tardiness;
138    }
139
140    protected virtual void UpdateConstraintViolationResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
141      if (!results.ContainsKey("ConstraintViolations")) {
142        DataTable data = new DataTable("ConstraintViolations");
143        results.Add(new Result("ConstraintViolations", data));
144
145        data.Rows.Add(new DataRow("Overload"));
146        data.Rows.Add(new DataRow("Tardiness"));
147
148        results.Add(new Result("Overload", new DoubleValue()));
149        results.Add(new Result("Tardiness", new DoubleValue()));
150      }
151
152      DataTable violationStatistics = results["ConstraintViolations"].Value as DataTable;
153
154      double overload = Math.Round(simulation.Vehicles.Sum(v => v.Overload) * 100.0) / 100.0;
155      double tardiness = Math.Round(simulation.Orders.Sum(o => o.Tardiness) * 100.0) / 100.0;
156
157      violationStatistics.Rows["Overload"].Values.Add(overload);
158      violationStatistics.Rows["Tardiness"].Values.Add(tardiness);
159
160      (results["Overload"].Value as DoubleValue).Value = overload;
161      (results["Tardiness"].Value as DoubleValue).Value = tardiness;
162    }
163
164    protected virtual void UpdateVehicleHistoryResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
165      if (!results.ContainsKey("VehicleHistory")) {
166        VehicleHistoryVisualization data = new VehicleHistoryVisualization();
167        data.ResultCollectionInterval = simulation.TimeStepParameter.Value.Value;
168        results.Add(new Result("VehicleHistory", data));
169
170        foreach (Vehicle vehicle in simulation.Vehicles) {
171          string name = vehicle.Name;
172          if (string.IsNullOrEmpty(name))
173            name = "Vehicle " + simulation.Vehicles.IndexOf(vehicle);
174
175          data.VehicleHistory.Add(new VehicleHistory(name));
176        }
177      }
178
179      VehicleHistoryVisualization vehicleHistory = results["VehicleHistory"].Value as VehicleHistoryVisualization;
180      foreach (Vehicle vehicle in simulation.Vehicles) {
181        VehicleHistory history = vehicleHistory.VehicleHistory[simulation.Vehicles.IndexOf(vehicle)];
182        history.VehicleState.Add(vehicle.VehicleState);
183        history.VehicleAvailability.Add(vehicle.Availibility);
184        history.ActiveOrders.Add(simulation.Orders.FirstOrDefault(o => o.Id == vehicle.CurrentOrder));
185        history.AssignedOrders.Clear();
186        history.AssignedOrders.AddRange(simulation.Orders.Where(o => o.AssignedVehicle == vehicle.Id));
187      }
188      vehicleHistory.Refresh();
189    }
190
191    protected virtual void UpdateTimeHistogram(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
192      if (!results.ContainsKey("TimeHistogram")) {
193        DataTable data = new DataTable("TimeHistogram");
194        results.Add(new Result("TimeHistogram", data));
195
196        DataRow row = new DataRow("WaitingTime");
197        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
198        data.Rows.Add(row);
199
200        row = new DataRow("TransferTime");
201        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
202        data.Rows.Add(row);
203
204        row = new DataRow("Slack");
205        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
206        data.Rows.Add(row);
207
208        row = new DataRow("LeadTime");
209        row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
210        data.Rows.Add(row);
211
212        results.Add(new Result("LeadTime", new DoubleValue()));
213
214        results.Add(new Result("TotalTardinessPenalty", new DoubleValue()));
215      }
216
217      DataTable timeHistogram = results["TimeHistogram"].Value.Clone() as DataTable;
218
219      //waiting time
220      var values = timeHistogram.Rows["WaitingTime"].Values;
221      values.Clear();
222      foreach (Order order in simulation.Orders.Where(o => o.PickupTime > 0)) {
223        values.Add(order.PickupTime - order.PickupReadyTime);
224      }
225
226      //transfer time
227      values = timeHistogram.Rows["TransferTime"].Values;
228      values.Clear();
229      foreach (Order order in simulation.Orders.Where(o => o.OrderState == OrderState.Delivered)) {
230        values.Add(order.DeliveryTime - order.PickupTime - order.PickupServiceTime);
231      }
232
233      //slack
234      values = timeHistogram.Rows["Slack"].Values;
235      values.Clear();
236      foreach (Order order in simulation.Orders.Where(o => o.PickupTime > 0)) {
237        values.Add(order.PickupDueTime - order.PickupTime);
238      }
239
240      //lead time
241      double leadTime = 0;
242
243      values = timeHistogram.Rows["LeadTime"].Values;
244      values.Clear();
245      foreach (Order order in simulation.Orders.Where(o => o.OrderState == OrderState.Delivered)) {
246        double orderLeadTime = order.DeliveryTime + order.DeliveryServiceTime - order.PickupReadyTime;
247        values.Add(orderLeadTime);
248        leadTime += orderLeadTime;
249      }
250
251      (results["LeadTime"].Value as DoubleValue).Value = leadTime;
252
253      results["TimeHistogram"].Value = timeHistogram;
254
255      double tardiness = 0;
256      foreach(Order o in simulation.Orders) {
257        if(o.Tardiness > 0) {
258          tardiness += Math.Min(1000000, Math.Pow((scenario.TardinessPenaltyParameter.Value.Value * Math.E), o.Tardiness) - 1.0);
259        }
260      }
261      (results["TotalTardinessPenalty"].Value as DoubleValue).Value = tardiness;
262    }
263
264    protected virtual void UpdateSimulationTime(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
265      if (!results.ContainsKey("SimulationTime")) {
266        results.Add(new Result("SimulationTime", new DoubleValue()));
267      }
268
269      (results["SimulationTime"].Value as DoubleValue).Value = simulation.SimulationTime;
270    }
271
272    public virtual void Analyze(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
273      UpdateSimulationTime(simulation, scenario, results);
274      UpdateOrderResults(simulation, scenario, results);
275      UpdateVehicleResults(simulation, scenario, results);
276      UpdateConstraintViolationResults(simulation, scenario, results);
277      UpdateVehicleHistoryResults(simulation, scenario, results);
278      UpdateTimeHistogram(simulation, scenario, results);
279    }
280
281    public virtual void Tick(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
282    }
283  }
284}
Note: See TracBrowser for help on using the repository browser.