#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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;
using HeuristicLab.Problems.VehicleRouting.Interfaces;
using HeuristicLab.Problems.VehicleRouting.Variants;
using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
namespace HeuristicLab.Problems.VehicleRouting {
///
/// An operator for analyzing the best solution of Vehicle Routing Problems.
///
[Item("BestTimeWindowedVRPSolutionAnalyzer", "An operator for analyzing the best solution of time windowed Vehicle Routing Problems.")]
[StorableClass]
public sealed class BestTimeWindowedVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ITimeWindowedOperator {
public ILookupParameter ProblemInstanceParameter {
get { return (ILookupParameter)Parameters["ProblemInstance"]; }
}
public ScopeTreeLookupParameter VRPToursParameter {
get { return (ScopeTreeLookupParameter)Parameters["VRPTours"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public ScopeTreeLookupParameter TardinessParameter {
get { return (ScopeTreeLookupParameter)Parameters["Tardiness"]; }
}
public ScopeTreeLookupParameter TravelTimeParameter {
get { return (ScopeTreeLookupParameter)Parameters["TravelTime"]; }
}
public LookupParameter BestSolutionParameter {
get { return (LookupParameter)Parameters["BestSolution"]; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public bool EnabledByDefault {
get { return true; }
}
[StorableConstructor]
private BestTimeWindowedVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
public BestTimeWindowedVRPSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter("ProblemInstance", "The problem instance."));
Parameters.Add(new ScopeTreeLookupParameter("VRPTours", "The VRP tours which should be evaluated."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities 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 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 BestTimeWindowedVRPSolutionAnalyzer(this, cloner);
}
private BestTimeWindowedVRPSolutionAnalyzer(BestTimeWindowedVRPSolutionAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IOperation Apply() {
IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
ItemArray solutions = VRPToursParameter.ActualValue;
ResultCollection results = ResultsParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
ItemArray tardinesses = TardinessParameter.ActualValue;
ItemArray travelTimes = TravelTimeParameter.ActualValue;
int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
VRPSolution solution = BestSolutionParameter.ActualValue;
if (solution != null) {
if (!results.ContainsKey("Best VRP Solution Tardiness")) {
results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
} else {
VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
if (qualities[i].Value <= eval.Quality) {
(results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
(results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
}
}
}
return base.Apply();
}
}
}