#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.VehicleRouting {
///
/// An operator for analyzing the best solution of Vehicle Routing Problems.
///
[Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
[StorableClass]
public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
public bool EnabledByDefault {
get { return true; }
}
public ScopeTreeLookupParameter VRPToursParameter {
get { return (ScopeTreeLookupParameter)Parameters["VRPTours"]; }
}
public ILookupParameter DistanceMatrixParameter {
get { return (ILookupParameter)Parameters["DistanceMatrix"]; }
}
public ILookupParameter UseDistanceMatrixParameter {
get { return (ILookupParameter)Parameters["UseDistanceMatrix"]; }
}
public LookupParameter CoordinatesParameter {
get { return (LookupParameter)Parameters["Coordinates"]; }
}
public ILookupParameter ReadyTimeParameter {
get { return (ILookupParameter)Parameters["ReadyTime"]; }
}
public ILookupParameter DueTimeParameter {
get { return (ILookupParameter)Parameters["DueTime"]; }
}
public ILookupParameter ServiceTimeParameter {
get { return (ILookupParameter)Parameters["ServiceTime"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public ScopeTreeLookupParameter OverloadParameter {
get { return (ScopeTreeLookupParameter)Parameters["Overload"]; }
}
public ScopeTreeLookupParameter TardinessParameter {
get { return (ScopeTreeLookupParameter)Parameters["Tardiness"]; }
}
public ScopeTreeLookupParameter DistanceParameter {
get { return (ScopeTreeLookupParameter)Parameters["Distance"]; }
}
public ScopeTreeLookupParameter TravelTimeParameter {
get { return (ScopeTreeLookupParameter)Parameters["TravelTime"]; }
}
public ScopeTreeLookupParameter VehiclesUtilizedParameter {
get { return (ScopeTreeLookupParameter)Parameters["VehiclesUtilized"]; }
}
public LookupParameter BestSolutionParameter {
get { return (LookupParameter)Parameters["BestSolution"]; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public LookupParameter BestKnownQualityParameter {
get { return (LookupParameter)Parameters["BestKnownQuality"]; }
}
public LookupParameter BestKnownSolutionParameter {
get { return (LookupParameter)Parameters["BestKnownSolution"]; }
}
[StorableConstructor]
private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public BestVRPSolutionAnalyzer()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("VRPTours", "The VRP tours which should be evaluated."));
Parameters.Add(new LookupParameter("Coordinates", "The x- and y-Coordinates of the cities."));
Parameters.Add(new LookupParameter("DistanceMatrix", "The matrix which contains the distances between the cities."));
Parameters.Add(new LookupParameter("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
Parameters.Add(new LookupParameter("ReadyTime", "The ready time of each customer."));
Parameters.Add(new LookupParameter("DueTime", "The due time of each customer."));
Parameters.Add(new LookupParameter("ServiceTime", "The service time of each customer."));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution of this VRP instance."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the VRP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("Distance", "The distances of the VRP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("Overload", "The overloads of the VRP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
Parameters.Add(new LookupParameter("BestSolution", "The best VRP solution."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best VRP solution should be stored."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BestVRPSolutionAnalyzer(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
// BackwardsCompatibility3.3
#region Backwards compatible code, remove with 3.4
if (!Parameters.ContainsKey("BestKnownQuality")) {
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
}
if (!Parameters.ContainsKey("BestKnownSolution")) {
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution of this VRP instance."));
}
#endregion
}
public override IOperation Apply() {
DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
ItemArray solutions = VRPToursParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
ItemArray overloads = OverloadParameter.ActualValue;
ItemArray tardinesses = TardinessParameter.ActualValue;
ResultCollection results = ResultsParameter.ActualValue;
ItemArray distances = DistanceParameter.ActualValue;
ItemArray travelTimes = TravelTimeParameter.ActualValue;
ItemArray vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
VRPSolution solution = BestSolutionParameter.ActualValue;
if (solution == null) {
solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
BestSolutionParameter.ActualValue = solution;
results.Add(new Result("Best VRP Solution", solution));
results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
} else {
if (qualities[i].Value <= solution.Quality.Value) {
solution.Coordinates = coordinates;
solution.Solution = best.Clone() as IVRPEncoding;
solution.Quality.Value = qualities[i].Value;
solution.Distance.Value = (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
solution.Overload.Value = (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
solution.Tardiness.Value = (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
solution.TravelTime.Value = (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
solution.VehicleUtilization.Value = (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
solution.ReadyTime = ReadyTimeParameter.ActualValue;
solution.DueTime = DueTimeParameter.ActualValue;
solution.ServiceTime = ServiceTimeParameter.ActualValue;
}
}
if (bestKnownQuality == null ||
qualities[i].Value < bestKnownQuality.Value) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
BestKnownSolutionParameter.ActualValue = (IVRPEncoding)best.Clone();
}
return base.Apply();
}
}
}