#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; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.LinearOrdering { /// /// A base class for operators which evaluate LOP solutions given in Matrix representation. /// [Item("MatrixTriangulationEvaluator", "A base class for operators which evaluate LOP solutions given in Matrix representation.")] [StorableClass] public abstract class MatrixTriangulationEvaluator : LOPEvaluator, IMatrixTriangulationEvaluator { private object locker = new object(); public ILookupParameter PermutationParameter { get { return (ILookupParameter)Parameters["Permutation"]; } } public ILookupParameter MatrixParameter { get { return (ILookupParameter)Parameters["Matrix"]; } } public ILookupParameter BestKnownSolutionParameter { get { return (ILookupParameter)Parameters["BestKnownSolution"]; } } public ILookupParameter BestKnownQualityParameter { get { return (ILookupParameter)Parameters["BestKnownQuality"]; } } [StorableConstructor] protected MatrixTriangulationEvaluator(bool deserializing) : base(deserializing) { } protected MatrixTriangulationEvaluator(MatrixTriangulationEvaluator original, Cloner cloner) : base(original, cloner) { } protected MatrixTriangulationEvaluator() : base() { Parameters.Add(new LookupParameter("Permutation", "The TSP solution given in path representation which should be evaluated.")); Parameters.Add(new LookupParameter("Matrix", "The matrix which contains the distances between the cities.")); Parameters.Add(new LookupParameter("BestKnownSolution", "The currently best known solution.")); Parameters.Add(new LookupParameter("BestKnownQuality", "The currently best known quality.")); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { // BackwardsCompatibility3.3 #region Backwards compatible code (remove with 3.4) LookupParameter oldDistanceMatrixParameter = Parameters["Matrix"] as LookupParameter; if (oldDistanceMatrixParameter != null) { Parameters.Remove(oldDistanceMatrixParameter); Parameters.Add(new LookupParameter("Matrix", "The matrix which contains the distances between the cities.")); MatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName; } #endregion } public static double Apply(MatrixTriangulationEvaluator evaluator, DoubleMatrix matrix, Permutation permut) { double length = evaluator.CalculateUpperTriangle(matrix, permut); return length; } public sealed override IOperation InstrumentedApply() { Permutation p = PermutationParameter.ActualValue; DoubleMatrix c = MatrixParameter.ActualValue; if (c == null) throw new InvalidOperationException("No coordinates were given."); double upperTriangle = CalculateUpperTriangle(c, p); QualityParameter.ActualValue = new DoubleValue(upperTriangle); if (BestKnownQualityParameter.ActualValue == null || BestKnownQualityParameter.ActualValue.Value < upperTriangle) { BestKnownSolutionParameter.ActualValue = p; BestKnownQualityParameter.ActualValue = new DoubleValue(upperTriangle); } return base.InstrumentedApply(); } protected abstract double CalculateUpperTriangle(DoubleMatrix matrix, Permutation tour); } }