#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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.Encodings.PermutationEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.PTSP {
///
/// An operator for analyzing the best solution of probabilistic traveling salesman problems given in path representation.
///
[Item("BestPTSPSolutionAnalyzer", "An operator for analyzing the best solution of Probabilistic Traveling Salesman Problems given in path representation using city coordinates.")]
[StorableClass]
public sealed class BestPTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
public bool EnabledByDefault {
get { return true; }
}
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter CoordinatesParameter {
get { return (ILookupParameter)Parameters["Coordinates"]; }
}
public IScopeTreeLookupParameter PermutationParameter {
get { return (IScopeTreeLookupParameter)Parameters["Permutation"]; }
}
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["Quality"]; }
}
public ILookupParameter ProbabilitiesParameter {
get { return (ILookupParameter)Parameters["Probabilities"]; }
}
public ILookupParameter BestSolutionParameter {
get { return (ILookupParameter)Parameters["BestSolution"]; }
}
public IValueLookupParameter ResultsParameter {
get { return (IValueLookupParameter)Parameters["Results"]; }
}
public ILookupParameter BestKnownQualityParameter {
get { return (ILookupParameter)Parameters["BestKnownQuality"]; }
}
public ILookupParameter BestKnownSolutionParameter {
get { return (ILookupParameter)Parameters["BestKnownSolution"]; }
}
[StorableConstructor]
private BestPTSPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
private BestPTSPSolutionAnalyzer(BestPTSPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new BestPTSPSolutionAnalyzer(this, cloner);
}
public BestPTSPSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter("Maximization", "True if the problem is a maximization problem."));
Parameters.Add(new LookupParameter("Coordinates", "The x- and y-Coordinates of the cities."));
Parameters.Add(new ScopeTreeLookupParameter("Permutation", "The PTSP solutions given in path representation from which the best solution should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the PTSP solutions which should be analyzed."));
Parameters.Add(new LookupParameter("Probabilities", "This list describes for each city the probability of appearing in a realized instance."));
Parameters.Add(new LookupParameter("BestSolution", "The best PTSP solution."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best PTSP solution should be stored."));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this PTSP instance."));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution of this PTSP instance."));
}
public override IOperation Apply() {
var coordinates = CoordinatesParameter.ActualValue;
var permutations = PermutationParameter.ActualValue;
var qualities = QualityParameter.ActualValue;
var probabilities = ProbabilitiesParameter.ActualValue;
var results = ResultsParameter.ActualValue;
var max = MaximizationParameter.ActualValue.Value;
var bestKnownQuality = BestKnownQualityParameter.ActualValue;
var i = !max ? qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index
: qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
if (bestKnownQuality == null ||
max && qualities[i].Value > bestKnownQuality.Value ||
!max && qualities[i].Value < bestKnownQuality.Value) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
}
var tour = BestSolutionParameter.ActualValue;
if (tour == null) {
tour = new PathPTSPTour(coordinates, probabilities, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
BestSolutionParameter.ActualValue = tour;
results.Add(new Result("Best PTSP Solution", tour));
} else {
if (max && tour.Quality.Value < qualities[i].Value ||
!max && tour.Quality.Value > qualities[i].Value) {
tour.Coordinates = coordinates;
tour.Permutation = (Permutation)permutations[i].Clone();
tour.Quality.Value = qualities[i].Value;
}
}
return base.Apply();
}
}
}