[8670] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Analysis;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.PDPSimulation.DomainModel;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Common;
|
---|
| 33 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 34 | using HeuristicLab.PDPSimulation.Operators;
|
---|
| 35 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
| 36 |
|
---|
| 37 | namespace 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 |
|
---|
| 111 | results.Add(new Result("UsedVehicles", new IntValue()));
|
---|
| 112 | results.Add(new Result("DrivenDistance", new DoubleValue()));
|
---|
| 113 | results.Add(new Result("InvalidActions", new DoubleValue()));
|
---|
| 114 | results.Add(new Result("VehicleTardiness", new DoubleValue()));
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | DataTable vehicleStatistics = results["VehicleStatistics"].Value as DataTable;
|
---|
| 118 |
|
---|
| 119 | int usedVehicles = simulation.Vehicles.Where(v => v.Distance > 0).Count();
|
---|
| 120 | double distance = Math.Round(simulation.Vehicles.Sum(v => v.Distance) * 100.0) / 100.0;
|
---|
| 121 | int invalidActions = simulation.Vehicles.Sum(v => v.InvalidActions);
|
---|
| 122 | double tardiness = Math.Round(simulation.Vehicles.Sum(v => v.Tardiness) * 100.0) / 100.0;
|
---|
| 123 |
|
---|
| 124 | vehicleStatistics.Rows["UsedVehicles"].Values.Add(usedVehicles);
|
---|
| 125 | vehicleStatistics.Rows["DrivenDistance"].Values.Add(distance);
|
---|
| 126 | vehicleStatistics.Rows["InvalidActions"].Values.Add(invalidActions);
|
---|
| 127 | vehicleStatistics.Rows["Tardiness"].Values.Add(tardiness);
|
---|
| 128 |
|
---|
| 129 | (results["UsedVehicles"].Value as IntValue).Value = usedVehicles;
|
---|
| 130 | (results["DrivenDistance"].Value as DoubleValue).Value = distance;
|
---|
| 131 | (results["VehicleTardiness"].Value as DoubleValue).Value = tardiness;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | protected virtual void UpdateConstraintViolationResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
|
---|
| 135 | if (!results.ContainsKey("ConstraintViolations")) {
|
---|
| 136 | DataTable data = new DataTable("ConstraintViolations");
|
---|
| 137 | results.Add(new Result("ConstraintViolations", data));
|
---|
| 138 |
|
---|
| 139 | data.Rows.Add(new DataRow("Overload"));
|
---|
| 140 | data.Rows.Add(new DataRow("Tardiness"));
|
---|
| 141 |
|
---|
| 142 | results.Add(new Result("Overload", new DoubleValue()));
|
---|
| 143 | results.Add(new Result("Tardiness", new DoubleValue()));
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | DataTable violationStatistics = results["ConstraintViolations"].Value as DataTable;
|
---|
| 147 |
|
---|
| 148 | double overload = Math.Round(simulation.Vehicles.Sum(v => v.Overload) * 100.0) / 100.0;
|
---|
| 149 | double tardiness = Math.Round(simulation.Orders.Sum(o => o.Tardiness) * 100.0) / 100.0;
|
---|
| 150 |
|
---|
| 151 | violationStatistics.Rows["Overload"].Values.Add(overload);
|
---|
| 152 | violationStatistics.Rows["Tardiness"].Values.Add(tardiness);
|
---|
| 153 |
|
---|
| 154 | (results["Overload"].Value as DoubleValue).Value = overload;
|
---|
| 155 | (results["Tardiness"].Value as DoubleValue).Value = tardiness;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | protected virtual void UpdateVehicleHistoryResults(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
|
---|
| 159 | if (!results.ContainsKey("VehicleHistory")) {
|
---|
| 160 | VehicleHistoryVisualization data = new VehicleHistoryVisualization();
|
---|
| 161 | data.ResultCollectionInterval = simulation.TimeStepParameter.Value.Value;
|
---|
| 162 | results.Add(new Result("VehicleHistory", data));
|
---|
| 163 |
|
---|
| 164 | foreach (Vehicle vehicle in simulation.Vehicles) {
|
---|
| 165 | string name = vehicle.Name;
|
---|
| 166 | if (string.IsNullOrEmpty(name))
|
---|
| 167 | name = "Vehicle " + simulation.Vehicles.IndexOf(vehicle);
|
---|
| 168 |
|
---|
| 169 | data.VehicleHistory.Add(new VehicleHistory(name));
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | VehicleHistoryVisualization vehicleHistory = results["VehicleHistory"].Value as VehicleHistoryVisualization;
|
---|
| 174 | foreach (Vehicle vehicle in simulation.Vehicles) {
|
---|
| 175 | VehicleHistory history = vehicleHistory.VehicleHistory[simulation.Vehicles.IndexOf(vehicle)];
|
---|
| 176 | history.VehicleState.Add(vehicle.VehicleState);
|
---|
| 177 | history.VehicleAvailability.Add(vehicle.Availibility);
|
---|
| 178 | history.ActiveOrders.Add(simulation.Orders.FirstOrDefault(o => o.Id == vehicle.CurrentOrder));
|
---|
| 179 | history.AssignedOrders.Clear();
|
---|
| 180 | history.AssignedOrders.AddRange(simulation.Orders.Where(o => o.AssignedVehicle == vehicle.Id));
|
---|
| 181 | }
|
---|
| 182 | vehicleHistory.Refresh();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | public virtual void Analyze(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
|
---|
| 186 | UpdateOrderResults(simulation, scenario, results);
|
---|
| 187 | UpdateVehicleResults(simulation, scenario, results);
|
---|
| 188 | UpdateConstraintViolationResults(simulation, scenario, results);
|
---|
| 189 | UpdateVehicleHistoryResults(simulation, scenario, results);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | public virtual void Tick(PickupDeliverySimulation simulation, PickupDeliveryScenario scenario, ResultCollection results) {
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|