Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab.Problems.TravelingSalesman-3.3/TSPMoveEvaluatorTest.cs @ 17254

Last change on this file since 17254 was 17254, checked in by abeham, 5 years ago

#2521: Added context lookup parameter

  • Refactored tests
  • Fixed duplicate GUID
File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HeuristicLab.Common;
23using HeuristicLab.Encodings.PermutationEncoding;
24using HeuristicLab.Problems.Instances;
25using HeuristicLab.Random;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Problems.TravelingSalesman.Tests {
29  /// <summary>
30  ///This is a test class for TSP move evaluators
31  ///</summary>
32  [TestClass()]
33  public class TSPMoveEvaluatorTest {
34    private const int ProblemSize = 10;
35    private static double[,] coordinates;
36    private static double[,] distances;
37    private static Permutation tour;
38    private static MersenneTwister random;
39
40    [ClassInitialize]
41    public static void MyClassInitialize(TestContext testContext) {
42      random = new MersenneTwister();
43      coordinates = new double[ProblemSize, 2];
44      for (int i = 0; i < ProblemSize; i++) {
45        coordinates[i, 0] = random.Next(ProblemSize * 10);
46        coordinates[i, 1] = random.Next(ProblemSize * 10);
47      }
48      distances = new double[ProblemSize, ProblemSize];
49      for (int i = 0; i < ProblemSize-1; i++)
50        for (int j = i + 1; j < ProblemSize; j++) {
51          distances[i, j] = DistanceHelper.EuclideanDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
52          distances[j, i] = distances[i, j];
53        }
54      tour = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
55    }
56
57    [TestMethod]
58    [TestCategory("Problems.TravelingSalesman")]
59    [TestProperty("Time", "short")]
60    public void InversionMoveEvaluatorTest() {
61      var euclidTSP = new TSP() { TSPData = new EuclideanTSPData("test", coordinates) };
62      var matrixTSP = new TSP() { TSPData = new MatrixTSPData("test", distances) };
63      double beforeMatrix = matrixTSP.Evaluate(tour);
64      double beforeCoordinates = euclidTSP.Evaluate(tour);
65      Assert.IsTrue(beforeMatrix.IsAlmost(beforeCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
66
67      for (int i = 0; i < 500; i++) {
68        var move = StochasticInversionSingleMoveGenerator.Apply(tour, random);
69
70        double moveMatrix = TSPInversionMoveEvaluator.CalculateTourLengthDelta(matrixTSP.TSPData, tour, move);
71        double moveCoordinates = TSPInversionMoveEvaluator.CalculateTourLengthDelta(euclidTSP.TSPData, tour, move);
72        Assert.IsTrue(moveMatrix.IsAlmost(moveCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
73
74        string failureString = string.Format(@"Inversion move is calculated with quality {0}, but actual difference is {4}.
75The move would invert the tour {1} between values {2} and {3}.", moveMatrix.ToString(), tour.ToString(), tour[move.Index1].ToString(), tour[move.Index2].ToString(), "{0}");
76
77        InversionManipulator.Apply(tour, move.Index1, move.Index2);
78
79        double afterMatrix = matrixTSP.Evaluate(tour);
80        double afterCoordinates = euclidTSP.Evaluate(tour);
81        Assert.IsTrue(afterMatrix.IsAlmost(afterCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
82
83        Assert.IsTrue(moveMatrix.IsAlmost(afterMatrix - beforeMatrix), string.Format(failureString, (afterMatrix - beforeMatrix).ToString()));
84
85        beforeMatrix = afterMatrix;
86        beforeCoordinates = afterCoordinates;
87      }
88    }
89
90    [TestMethod]
91    [TestCategory("Problems.TravelingSalesman")]
92    [TestProperty("Time", "short")]
93    public void TranslocationMoveEvaluatorTest() {
94
95      var euclidTSP = new TSP() { TSPData = new EuclideanTSPData("test", coordinates) };
96      var matrixTSP = new TSP() { TSPData = new MatrixTSPData("test", distances) };
97      double beforeMatrix = matrixTSP.Evaluate(tour);
98      double beforeCoordinates = euclidTSP.Evaluate(tour);
99      Assert.IsTrue(beforeMatrix.IsAlmost(beforeCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
100
101      for (int i = 0; i < 500; i++) {
102        var move = StochasticTranslocationSingleMoveGenerator.Apply(tour, random);
103
104        double moveMatrix = TSPTranslocationMoveEvaluator.CalculateTourLengthDelta(matrixTSP.TSPData, tour, move);
105        double moveCoordinates = TSPTranslocationMoveEvaluator.CalculateTourLengthDelta(euclidTSP.TSPData, tour, move);
106        Assert.IsTrue(moveMatrix.IsAlmost(moveCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
107
108        string failureString = string.Format(@"Translocation move is calculated with quality {0}, but actual difference is {5}.
109The move would move the segment between {1} and {2} in the tour {3} to the new index {4}.", moveMatrix.ToString(), tour[move.Index1].ToString(), tour[move.Index2].ToString(), tour.ToString(), move.Index3.ToString(), "{0}");
110        TranslocationManipulator.Apply(tour, move.Index1, move.Index2, move.Index3);
111
112        double afterMatrix = matrixTSP.Evaluate(tour);
113        double afterCoordinates = euclidTSP.Evaluate(tour);
114        Assert.IsTrue(afterMatrix.IsAlmost(afterCoordinates), "Evaluation differs between using the coordinates and using the distance matrix.");
115
116        Assert.IsTrue(moveMatrix.IsAlmost(afterMatrix - beforeMatrix), string.Format(failureString, (afterMatrix - beforeMatrix).ToString()));
117
118        beforeMatrix = afterMatrix;
119        beforeCoordinates = afterCoordinates;
120      }
121    }
122
123  }
124}
Note: See TracBrowser for help on using the repository browser.