Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.LinearOrdering/3.3/Evaluators/AwesomeTriangulationEvaluator.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: 2.2 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Encodings.PermutationEncoding;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11
12namespace HeuristicLab.Problems.LinearOrdering
13{ /// <summary>
14  /// An operator which evaluates LOP solutions given in matrix representation.
15  /// </summary>
16    [Item("AwesomeTriangulationEvaluator", "An operator which evaluates LOP solutions given in matrix representation.")]
17    [StorableClass]
18    public sealed class AwesomeTriangulationEvaluator : MatrixTriangulationEvaluator
19    {
20        [StorableConstructor]
21        private AwesomeTriangulationEvaluator(bool deserializing) : base(deserializing) { }
22        private AwesomeTriangulationEvaluator(AwesomeTriangulationEvaluator original, Cloner cloner) : base(original, cloner) { }
23        public AwesomeTriangulationEvaluator() : base() { }
24
25        public override IDeepCloneable Clone(Cloner cloner)
26        {
27            return new AwesomeTriangulationEvaluator(this, cloner);
28        }
29
30        /// <summary>
31        /// Calculates the distance between two points using the rounded Euclidean distance metric.
32        /// </summary>
33        /// <param name="x1">The x-coordinate of point 1.</param>
34        /// <param name="y1">The y-coordinate of point 1.</param>
35        /// <param name="x2">The x-coordinate of point 2.</param>
36        /// <param name="y2">The y-coordinate of point 2.</param>
37        /// <returns>The calculated distance.</returns>
38        protected override double CalculateUpperTriangle(DoubleMatrix m, Permutation p)
39        {
40            int[] permutation = p.ToArray();
41
42            //reduce offset, real arrays start with 0
43            if (!permutation.Contains(0)) { permutation = permutation.Select(v => v - 1).ToArray(); }
44
45            double sum = 0;
46            for (int i = 1; i < m.Columns; i++)
47            {
48                for (int j = 0; j < i; j++)
49                {
50                    sum += m[p[j], p[i]];
51                }
52            }
53
54            return sum;
55        }
56    }
57}
Note: See TracBrowser for help on using the repository browser.