Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP.Tests-3.3/PTSPMoveEvaluatorTest.cs @ 12701

Last change on this file since 12701 was 12380, checked in by apolidur, 10 years ago

#2221: Small refactoring and code cleaning

File size: 6.0 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.PermutationEncoding;
6using HeuristicLab.Random;
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8using System.Diagnostics;
9
10namespace HeuristicLab.Problems.PTSP.Tests_3._3 {
11  /// <summary>
12  ///This is a test class for PTSP move evaluators
13  ///</summary>
14  [TestClass()]
15  public class PTSPMoveEvaluatorTest {
16    private const int ProblemSize = 10;
17    private static DoubleMatrix coordinates;
18    private static DistanceMatrix distances;
19    private static Permutation tour;
20    private static MersenneTwister random;
21    private static ItemList<ItemList<IntValue>> realizations;
22    private static DoubleArray ProbabilityMatrix;
23
24    [ClassInitialize]
25    public static void MyClassInitialize(TestContext testContext) {
26      random = new MersenneTwister();
27      coordinates = new DoubleMatrix(ProblemSize, 2);
28      distances = new DistanceMatrix(ProblemSize, ProblemSize);
29      for (int i = 0; i < ProblemSize; i++) {
30        coordinates[i, 0] = random.Next(ProblemSize * 10);
31        coordinates[i, 1] = random.Next(ProblemSize * 10);
32      }
33      for (int i = 0; i < ProblemSize - 1; i++) {
34        for (int j = i + 1; j < ProblemSize; j++) {
35          distances[i, j] = Math.Round(Math.Sqrt(Math.Pow(coordinates[i, 0] - coordinates[j, 0], 2) + Math.Pow(coordinates[i, 1] - coordinates[j, 1], 2)));
36          distances[j, i] = distances[i, j];
37        }
38      }
39
40      ProbabilityMatrix = new DoubleArray(ProblemSize);
41      for (int i = 0; i < ProblemSize; i++) {
42        ProbabilityMatrix[i] = random.NextDouble();
43      }
44
45      int numRealizations = 100;
46      int countOnes = 0;
47      realizations = new ItemList<ItemList<IntValue>>(numRealizations);
48      for (int i = 0; i < numRealizations; i++) {
49        ItemList<IntValue> newRealization = new ItemList<IntValue>();
50        while (countOnes < 4) { //only generate realizations with at least 4 cities visited
51          countOnes = 0;
52          newRealization.Clear();
53          for (int j = 0; j < ProblemSize; j++) {
54            if (ProbabilityMatrix[j] > random.NextDouble()) {
55              newRealization.Add(new IntValue(1));
56              countOnes++;
57            } else {
58              newRealization.Add(new IntValue(0));
59            }
60          }
61        }
62        countOnes = 0;
63        realizations.Add(newRealization);
64      }
65
66      tour = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
67    }
68
69    [TestMethod]
70    [TestCategory("Problems.TravelingSalesman")]
71    [TestProperty("Time", "short")]
72    public void InversionMoveEvaluatorTest() {
73
74      EstimatedProbabilisticTravelingSalesmanProblem ePTSP = new EstimatedProbabilisticTravelingSalesmanProblem();
75
76      double beforeMatrix = ePTSP.EvaluateWithParams(distances, ProbabilityMatrix, realizations, tour)[0];
77
78      for (int i = 0; i < 500; i++) {
79        var move = StochasticInversionSingleMoveGenerator.Apply(tour, random);
80
81        double moveMatrix = PTSPEstimatedInversionEvaluator.EvaluateByDistanceMatrix(tour, move, distances,realizations);
82
83        string failureString = string.Format(@"Inversion move is calculated with quality {0}, but actual difference is {4}.
84The move would invert the tour {1} between values {2} and {3}.", moveMatrix.ToString(), tour.ToString(), tour[move.Index1].ToString(), tour[move.Index2].ToString(), "{0}");
85
86        InversionManipulator.Apply(tour, move.Index1, move.Index2);
87
88        double afterMatrix = ePTSP.EvaluateWithParams(distances, ProbabilityMatrix, realizations, tour)[0];
89
90        Assert.IsTrue(Math.Abs(moveMatrix).IsAlmost(Math.Abs(afterMatrix - beforeMatrix)), string.Format(failureString, (Math.Abs(afterMatrix - beforeMatrix)).ToString()));
91
92        beforeMatrix = afterMatrix;
93      }
94    }
95
96    [TestMethod]
97    [TestCategory("Problems.TravelingSalesman")]
98    [TestProperty("Time", "short")]
99    public void InsertionMoveEvaluatorTest() {
100
101      EstimatedProbabilisticTravelingSalesmanProblem ePTSP = new EstimatedProbabilisticTravelingSalesmanProblem();
102
103      double beforeMatrix = ePTSP.EvaluateWithParams(distances, ProbabilityMatrix, realizations, tour)[0];
104
105      for (int i = 0; i < 500; i++) {
106        var move = StochasticTranslocationSingleMoveGenerator.Apply(tour, random);
107
108        double moveMatrix = PTSPEstimatedInsertionEvaluator.EvaluateByDistanceMatrix(tour, move, distances, realizations);
109
110        string failureString = string.Format(@"Inversion move is calculated with quality {0}, but actual difference is {4}.
111The move would invert the tour {1} between values {2} and {3}.", moveMatrix.ToString(), tour.ToString(), tour[move.Index1].ToString(), tour[move.Index2].ToString(), "{0}");
112
113        TranslocationManipulator.Apply(tour, move.Index1, move.Index1, move.Index3);
114
115        double afterMatrix = ePTSP.EvaluateWithParams(distances, ProbabilityMatrix, realizations, tour)[0];
116
117        Assert.IsTrue(Math.Abs(moveMatrix).IsAlmost(Math.Abs(afterMatrix - beforeMatrix)), string.Format(failureString, (Math.Abs(afterMatrix - beforeMatrix)).ToString()));
118
119        beforeMatrix = afterMatrix;
120      }
121    }
122
123    [TestMethod]
124    [TestCategory("Problems.TravelingSalesman")]
125    [TestProperty("Time", "short")]
126    public void AnalyticalTest() {
127      for (int i = 0; i < 10; i++) {
128        tour = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
129        EstimatedProbabilisticTravelingSalesmanProblem ePTSP = new EstimatedProbabilisticTravelingSalesmanProblem();
130        double estimated = ePTSP.EvaluateWithParams(distances, ProbabilityMatrix, realizations, tour)[0];
131
132        AnalyticalProbabilisticTravelingSalesmanProblem aPTSP = new AnalyticalProbabilisticTravelingSalesmanProblem();
133        double analytical = aPTSP.EvaluateWithParams(distances, ProbabilityMatrix, tour);
134        Console.WriteLine(Math.Abs(analytical-estimated));
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.