#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.PDPSimulation.SimulationCore;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Common;
using HeuristicLab.Parameters;
using HeuristicLab.Data;
using HeuristicLab.PDPSimulation.DomainModel;
using HeuristicLab.PDPSimulation.DistanceMeasures;
using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
using HeuristicLab.PDPSimulation.Operators;
namespace HeuristicLab.PDPSimulation {
[Item("PickupDeliveryScenario", "A pickup and delivery scenario.")]
[StorableClass]
public sealed class PickupDeliveryScenario: Scenario {
public event EventHandler ScenarioChanged;
public ValueParameter DistanceMeasureParameter {
get { return (ValueParameter)Parameters["DistanceMeasure"]; }
}
public ValueParameter AllowDiversionParameter {
get { return (ValueParameter)Parameters["AllowDiversion"]; }
}
public ValueParameter DepotCoordinatesParameter {
get { return (ValueParameter)Parameters["DepotCoordinates"]; }
}
public ValueParameter VehicleCapacitiesParameter {
get { return (ValueParameter)Parameters["VehicleCapacities"]; }
}
public ValueParameter VehicleReadyTimesParameter {
get { return (ValueParameter)Parameters["VehicleReadyTimes"]; }
}
public ValueParameter VehicleDueTimesParameter {
get { return (ValueParameter)Parameters["VehicleDueTimes"]; }
}
public OptionalValueParameter OrderGeneratorParameter {
get { return (OptionalValueParameter)Parameters["PickupDeliveryOrderGenerator"]; }
}
public ValueParameter EvaluatorParameter {
get { return (ValueParameter)Parameters["Evaluator"]; }
}
public IValueParameter DistanceFactorParameter {
get { return (IValueParameter)Parameters["DistanceFactor"]; }
}
public IValueParameter FleetUsageFactorParameter {
get { return (IValueParameter)Parameters["FleetUsageFactor"]; }
}
public IValueParameter OverloadPenaltyParameter {
get { return (IValueParameter)Parameters["OverloadPenalty"]; }
}
public IValueParameter TardinessPenaltyParameter {
get { return (IValueParameter)Parameters["TardinessPenalty"]; }
}
public IValueParameter PickupViolationPenaltyParameter {
get { return (IValueParameter)Parameters["PickupViolationPenalty"]; }
}
public IValueParameter RelocateBackToDepotParameter {
get { return (IValueParameter)Parameters["RelocateBackToDepot"]; }
}
public DistanceMeasure DistanceMeasure {
get {
return DistanceMeasureParameter.Value;
}
set {
DistanceMeasureParameter.Value = value;
}
}
public PickupDeliveryOrderGenerator OrderGenerator {
get {
return OrderGeneratorParameter.Value;
}
set {
OrderGeneratorParameter.Value = value;
}
}
public MDCVRPTWEvaluator Evaluator {
get {
return EvaluatorParameter.Value;
}
set {
EvaluatorParameter.Value = value;
}
}
public bool AllowDiversion {
get {
return AllowDiversionParameter.Value.Value &&
DistanceMeasure.DiversionSupported;
}
}
public bool RelocateBackToDepot {
get {
return RelocateBackToDepotParameter.Value.Value;
}
}
public PickupDeliveryScenario() : base() {
Parameters.Add(new ValueParameter("DistanceMeasure", new EuclideanDistanceMeasure()));
Parameters.Add(new ValueParameter("AllowDiversion", new BoolValue(false)));
Parameters.Add(new ValueParameter("DepotCoordinates", new DoubleMatrix()));
Parameters.Add(new ValueParameter("RelocateBackToDepot", new BoolValue(true)));
Parameters.Add(new ValueParameter("VehicleCapacities", new DoubleArray()));
Parameters.Add(new ValueParameter("VehicleReadyTimes", new DoubleArray()));
Parameters.Add(new ValueParameter("VehicleDueTimes", new DoubleArray()));
Parameters.Add(new ValueParameter("DistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
Parameters.Add(new ValueParameter("FleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(3000)));
Parameters.Add(new ValueParameter("OverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(10000)));
Parameters.Add(new ValueParameter("TardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(10000)));
Parameters.Add(new ValueParameter("PickupViolationPenalty", "The pickup violation penalty considered in the evaluation.", new DoubleValue(1000000)));
Parameters.Add(new OptionalValueParameter("PickupDeliveryOrderGenerator"));
Parameters.Add(new ValueParameter("Evaluator", new DynPDPEvaluator()));
DepotCoordinatesParameter.GetsCollected = false;
VehicleCapacitiesParameter.GetsCollected = false;
VehicleReadyTimesParameter.GetsCollected = false;
VehicleDueTimesParameter.GetsCollected = false;
OrderGeneratorParameter.GetsCollected = false;
DistanceMeasureParameter.GetsCollected = false;
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey("Evaluator")) {
Parameters.Add(new ValueParameter("Evaluator", new DynPDPEvaluator()));
}
}
public IEnumerable GetOrders(double timeStart, double timeEnd) {
return OrderGeneratorParameter.Value.GetOrders(timeStart, timeEnd);
}
public bool MoreOrders(double time) {
return OrderGeneratorParameter.Value.MoreOrders(time);
}
public double OrderRangeX() {
return OrderGeneratorParameter.Value.RangeX();
}
public double OrderRangeY() {
return OrderGeneratorParameter.Value.RangeY();
}
private void OnScenarioChanged() {
if (ScenarioChanged != null)
ScenarioChanged(this, null);
}
private void ImportFromDynPDP(DynPDPParser parser) {
parser.Parse();
int vehicles = parser.Vehicles.Length;
DoubleMatrix depotCoordinates = new DoubleMatrix(vehicles, 2);
DoubleArray vehicleCapacities = new DoubleArray(vehicles);
DoubleArray vehicleReadyTimes = new DoubleArray(vehicles);
DoubleArray vehicleDueTimes = new DoubleArray(vehicles);
for (int i = 0; i < vehicles; i++) {
DynPDPParser.Vehicle vehicle = parser.Vehicles[i];
depotCoordinates[i, 0] = vehicle.xCoord;
depotCoordinates[i, 1] = vehicle.yCoord;
vehicleCapacities[i] = vehicle.capacity;
vehicleReadyTimes[i] = vehicle.readyTime;
vehicleDueTimes[i] = vehicle.dueTime;
}
DepotCoordinatesParameter.Value = depotCoordinates;
VehicleCapacitiesParameter.Value = vehicleCapacities;
VehicleReadyTimesParameter.Value = vehicleReadyTimes;
VehicleDueTimesParameter.Value = vehicleDueTimes;
if (parser.DistanceMatrix.useDistanceMatrix) {
DistanceMatrixMeasure dm = new DistanceMatrixMeasure();
dm.PointMapping = new IntMatrix(parser.DistanceMatrix.coordinates);
dm.DistanceMatrix = new DoubleMatrix(parser.DistanceMatrix.matrix);
DistanceMeasure = dm;
}
PickupDeliveryPredefinedOrderGenerator orderGen = new PickupDeliveryPredefinedOrderGenerator();
int orders = parser.Orders.Length;
DoubleMatrix orderMatrix = new DoubleMatrix(orders, 12,
new string[] { "Time revealed", "P X coordinate", "P Y coordinate", "D X coordinate", "D Y coordinate",
"Demand", "P service time", "P ready time", "P due time", "D service time", "D ready time", "D due time"});
for (int i = 0; i < orders; i++) {
DynPDPParser.Order order = parser.Orders[i];
orderMatrix[i, 0] = order.revealedTime;
orderMatrix[i, 1] = order.pickupXCoord;
orderMatrix[i, 2] = order.pickupYCoord;
orderMatrix[i, 3] = order.deliveryXCoord;
orderMatrix[i, 4] = order.deliveryYCoord;
orderMatrix[i, 5] = order.demand;
orderMatrix[i, 6] = order.pickupServiceTime;
orderMatrix[i, 7] = order.pickupReadyTime;
orderMatrix[i, 8] = order.pickupDueTime;
orderMatrix[i, 9] = order.deliveryServiceTime;
orderMatrix[i, 10] = order.deliveryReadyTime;
orderMatrix[i, 11] = order.deliveryDueTime;
}
orderGen.OrdersParameter.Value = orderMatrix;
OrderGeneratorParameter.Value = orderGen;
OnScenarioChanged();
}
public void ImportFromDynPDP(string fileName) {
DefaultDynPDPParser parser = new DefaultDynPDPParser(fileName);
ImportFromDynPDP(parser);
}
public void ImportFromDynTSPLib(string fileName) {
TSPLibDynPDPParser parser = new TSPLibDynPDPParser(fileName);
ImportFromDynPDP(parser);
}
[StorableConstructor]
private PickupDeliveryScenario(bool deserializing) : base(deserializing) { }
private PickupDeliveryScenario(PickupDeliveryScenario original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PickupDeliveryScenario(this, cloner);
}
}
}