Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.LinearOrdering/3.3/Evaluators/MatrixTriangulationEvaluator.cs @ 15521

Last change on this file since 15521 was 15521, checked in by fholzing, 6 years ago

#2864: First commit of new branch of Permutation based benchmark problems.

File size: 4.9 KB
RevLine 
[15521]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.LinearOrdering
31{
32    /// <summary>
33    /// A base class for operators which evaluate LOP solutions given in Matrix representation.
34    /// </summary>
35    [Item("MatrixTriangulationEvaluator", "A base class for operators which evaluate LOP solutions given in Matrix representation.")]
36    [StorableClass]
37    public abstract class MatrixTriangulationEvaluator : LOPEvaluator, IMatrixTriangulationEvaluator
38    {
39        private object locker = new object();
40
41        public ILookupParameter<Permutation> PermutationParameter { get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; } }
42        public ILookupParameter<DoubleMatrix> MatrixParameter { get { return (ILookupParameter<DoubleMatrix>)Parameters["Matrix"]; } }
43        public ILookupParameter<Permutation> BestKnownSolutionParameter { get { return (ILookupParameter<Permutation>)Parameters["BestKnownSolution"]; } }
44        public ILookupParameter<DoubleValue> BestKnownQualityParameter { get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } }
45        [StorableConstructor]
46        protected MatrixTriangulationEvaluator(bool deserializing) : base(deserializing) { }
47        protected MatrixTriangulationEvaluator(MatrixTriangulationEvaluator original, Cloner cloner) : base(original, cloner) { }
48        protected MatrixTriangulationEvaluator()
49          : base()
50        {
51            Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
52            Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The matrix which contains the distances between the cities."));
53            Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The currently best known solution."));
54            Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The currently best known quality."));
55        }
56
57        [StorableHook(HookType.AfterDeserialization)]
58        private void AfterDeserialization()
59        {
60            // BackwardsCompatibility3.3
61            #region Backwards compatible code (remove with 3.4)
62            LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["Matrix"] as LookupParameter<DoubleMatrix>;
63            if (oldDistanceMatrixParameter != null)
64            {
65                Parameters.Remove(oldDistanceMatrixParameter);
66                Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The matrix which contains the distances between the cities."));
67                MatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
68            }
69            #endregion
70        }
71
72        public static double Apply(MatrixTriangulationEvaluator evaluator, DoubleMatrix matrix, Permutation permut)
73        {
74            double length = evaluator.CalculateUpperTriangle(matrix, permut);
75            return length;
76        }
77
78        public sealed override IOperation InstrumentedApply()
79        {
80            Permutation p = PermutationParameter.ActualValue;
81            DoubleMatrix c = MatrixParameter.ActualValue;
82            if (c == null) throw new InvalidOperationException("No coordinates were given.");
83
84            double upperTriangle = CalculateUpperTriangle(c, p);
85            QualityParameter.ActualValue = new DoubleValue(upperTriangle);
86
87            if (BestKnownQualityParameter.ActualValue == null || BestKnownQualityParameter.ActualValue.Value < upperTriangle)
88            {
89                BestKnownSolutionParameter.ActualValue = p;
90                BestKnownQualityParameter.ActualValue = new DoubleValue(upperTriangle);
91            }
92            return base.InstrumentedApply();
93        }
94
95        protected abstract double CalculateUpperTriangle(DoubleMatrix matrix, Permutation tour);
96    }
97}
Note: See TracBrowser for help on using the repository browser.