1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Random;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.PTSP.Tests {
|
---|
31 | /// <summary>
|
---|
32 | ///This is a test class for PTSP move evaluators
|
---|
33 | ///</summary>
|
---|
34 | [TestClass()]
|
---|
35 | public class PTSPMoveEvaluatorTest {
|
---|
36 | private const int ProblemSize = 10;
|
---|
37 | private const int RealizationsSize = 100;
|
---|
38 | private static DoubleMatrix coordinates;
|
---|
39 | private static DistanceMatrix distances;
|
---|
40 | private static Permutation tour;
|
---|
41 | private static MersenneTwister random;
|
---|
42 | private static ItemList<BoolArray> realizations;
|
---|
43 | private static DoubleArray probabilities;
|
---|
44 |
|
---|
45 | [ClassInitialize]
|
---|
46 | public static void MyClassInitialize(TestContext testContext) {
|
---|
47 | random = new MersenneTwister();
|
---|
48 | coordinates = new DoubleMatrix(ProblemSize, 2);
|
---|
49 | distances = new DistanceMatrix(ProblemSize, ProblemSize);
|
---|
50 | for (var i = 0; i < ProblemSize; i++) {
|
---|
51 | coordinates[i, 0] = random.Next(ProblemSize * 10);
|
---|
52 | coordinates[i, 1] = random.Next(ProblemSize * 10);
|
---|
53 | }
|
---|
54 | for (var i = 0; i < ProblemSize - 1; i++) {
|
---|
55 | for (var j = i + 1; j < ProblemSize; j++) {
|
---|
56 | 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)));
|
---|
57 | distances[j, i] = distances[i, j];
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | probabilities = new DoubleArray(ProblemSize);
|
---|
62 | for (var i = 0; i < ProblemSize; i++) {
|
---|
63 | probabilities[i] = random.NextDouble();
|
---|
64 | }
|
---|
65 |
|
---|
66 | realizations = new ItemList<BoolArray>(RealizationsSize);
|
---|
67 | for (var i = 0; i < RealizationsSize; i++) {
|
---|
68 | var countOnes = 0;
|
---|
69 | var newRealization = new BoolArray(ProblemSize);
|
---|
70 | while (countOnes < 4) { //only generate realizations with at least 4 cities visited
|
---|
71 | countOnes = 0;
|
---|
72 | for (var j = 0; j < ProblemSize; j++) {
|
---|
73 | newRealization[j] = random.NextDouble() < probabilities[j];
|
---|
74 | if (newRealization[j]) countOnes++;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | realizations.Add(newRealization);
|
---|
78 | }
|
---|
79 |
|
---|
80 | tour = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
|
---|
81 | }
|
---|
82 |
|
---|
83 | [TestMethod]
|
---|
84 | [TestCategory("Problems.ProbabilisticTravelingSalesman")]
|
---|
85 | [TestProperty("Time", "short")]
|
---|
86 | public void InversionMoveEvaluatorTest() {
|
---|
87 | var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
|
---|
88 |
|
---|
89 | for (var i = 0; i < 500; i++) {
|
---|
90 | var move = StochasticInversionSingleMoveGenerator.Apply(tour, random);
|
---|
91 | var moveMatrix = PTSPEstimatedInversionMoveEvaluator.EvaluateByDistanceMatrix(tour, move, distances, realizations);
|
---|
92 | InversionManipulator.Apply(tour, move.Index1, move.Index2);
|
---|
93 | var afterMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
|
---|
94 |
|
---|
95 | Assert.IsTrue(Math.Abs(moveMatrix).IsAlmost(Math.Abs(afterMatrix - beforeMatrix)),
|
---|
96 | string.Format(@"Inversion move is calculated with quality {0}, but actual difference is {4}.
|
---|
97 | The move would invert the tour {1} between values {2} and {3}.",
|
---|
98 | moveMatrix, tour, tour[move.Index1], tour[move.Index2], Math.Abs(afterMatrix - beforeMatrix)));
|
---|
99 |
|
---|
100 | beforeMatrix = afterMatrix;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [TestMethod]
|
---|
105 | [TestCategory("Problems.ProbabilisticTravelingSalesman")]
|
---|
106 | [TestProperty("Time", "short")]
|
---|
107 | public void InsertionMoveEvaluatorTest() {
|
---|
108 | var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
|
---|
109 |
|
---|
110 | for (var i = 0; i < 500; i++) {
|
---|
111 | var move = StochasticTranslocationSingleMoveGenerator.Apply(tour, random);
|
---|
112 | var moveMatrix = PTSPEstimatedInsertionMoveEvaluator.EvaluateByDistanceMatrix(tour, move, distances, realizations);
|
---|
113 | TranslocationManipulator.Apply(tour, move.Index1, move.Index1, move.Index3);
|
---|
114 | var afterMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
|
---|
115 |
|
---|
116 | Assert.IsTrue(Math.Abs(moveMatrix).IsAlmost(Math.Abs(afterMatrix - beforeMatrix)),
|
---|
117 | string.Format(@"Insertion move is calculated with quality {0}, but actual difference is {4}.
|
---|
118 | The move would invert the tour {1} between values {2} and {3}.",
|
---|
119 | moveMatrix, tour, tour[move.Index1], tour[move.Index2], Math.Abs(afterMatrix - beforeMatrix)));
|
---|
120 |
|
---|
121 | beforeMatrix = afterMatrix;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | [TestMethod]
|
---|
126 | [TestCategory("Problems.ProbabilisticTravelingSalesman")]
|
---|
127 | [TestProperty("Time", "short")]
|
---|
128 | public void AnalyticalTest() {
|
---|
129 | for (var i = 0; i < 10; i++) {
|
---|
130 | var perm = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
|
---|
131 | var estimated = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(perm, distances, realizations)[0];
|
---|
132 |
|
---|
133 | var analytical = AnalyticalProbabilisticTravelingSalesmanProblem.Evaluate(perm, distances, probabilities);
|
---|
134 | Console.WriteLine(Math.Abs(analytical - estimated));
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|