#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.TravelingSalesman {
///
/// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
///
[Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
[StorableClass]
public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
public bool EnabledByDefault {
get { return true; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
public LookupParameter CoordinatesParameter {
get { return (LookupParameter)Parameters["Coordinates"]; }
}
public ScopeTreeLookupParameter PermutationParameter {
get { return (ScopeTreeLookupParameter)Parameters["Permutation"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
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 BestTSPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
private BestTSPSolutionAnalyzer(BestTSPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new BestTSPSolutionAnalyzer(this, cloner);
}
public BestTSPSolutionAnalyzer()
: 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 TSP solutions given in path representation from which the best solution should be analyzed."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the TSP solutions which should be analyzed."));
Parameters.Add(new LookupParameter("BestSolution", "The best TSP solution."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best TSP solution should be stored."));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution of this TSP instance."));
MaximizationParameter.Hidden = true;
CoordinatesParameter.Hidden = true;
PermutationParameter.Hidden = true;
QualityParameter.Hidden = true;
BestSolutionParameter.Hidden = true;
ResultsParameter.Hidden = true;
BestKnownQualityParameter.Hidden = true;
BestKnownSolutionParameter.Hidden = true;
}
public override IOperation Apply() {
DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
ItemArray permutations = PermutationParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
ResultCollection results = ResultsParameter.ActualValue;
bool max = MaximizationParameter.ActualValue.Value;
DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
int i = -1;
if (!max)
i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
else i = 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();
}
PathTSPTour tour = BestSolutionParameter.ActualValue;
if (tour == null) {
tour = new PathTSPTour(coordinates, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
BestSolutionParameter.ActualValue = tour;
results.Add(new Result("Best TSP 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();
}
}
}