- Timestamp:
- 11/28/15 23:38:51 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP.Tests-3.3/PTSPMoveEvaluatorTest.cs
r12380 r13412 1 using System; 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; 2 23 using HeuristicLab.Common; 3 24 using HeuristicLab.Core; … … 6 27 using HeuristicLab.Random; 7 28 using Microsoft.VisualStudio.TestTools.UnitTesting; 8 using System.Diagnostics;9 29 10 namespace HeuristicLab.Problems.PTSP.Tests _3._3{30 namespace HeuristicLab.Problems.PTSP.Tests { 11 31 /// <summary> 12 32 ///This is a test class for PTSP move evaluators … … 15 35 public class PTSPMoveEvaluatorTest { 16 36 private const int ProblemSize = 10; 37 private const int RealizationsSize = 100; 17 38 private static DoubleMatrix coordinates; 18 39 private static DistanceMatrix distances; 19 40 private static Permutation tour; 20 41 private static MersenneTwister random; 21 private static ItemList< ItemList<IntValue>> realizations;22 private static DoubleArray ProbabilityMatrix;42 private static ItemList<BoolArray> realizations; 43 private static DoubleArray probabilities; 23 44 24 45 [ClassInitialize] … … 27 48 coordinates = new DoubleMatrix(ProblemSize, 2); 28 49 distances = new DistanceMatrix(ProblemSize, ProblemSize); 29 for ( inti = 0; i < ProblemSize; i++) {50 for (var i = 0; i < ProblemSize; i++) { 30 51 coordinates[i, 0] = random.Next(ProblemSize * 10); 31 52 coordinates[i, 1] = random.Next(ProblemSize * 10); 32 53 } 33 for ( inti = 0; i < ProblemSize - 1; i++) {34 for ( intj = i + 1; j < ProblemSize; j++) {54 for (var i = 0; i < ProblemSize - 1; i++) { 55 for (var j = i + 1; j < ProblemSize; j++) { 35 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))); 36 57 distances[j, i] = distances[i, j]; … … 38 59 } 39 60 40 ProbabilityMatrix= new DoubleArray(ProblemSize);41 for ( inti = 0; i < ProblemSize; i++) {42 ProbabilityMatrix[i] = random.NextDouble();61 probabilities = new DoubleArray(ProblemSize); 62 for (var i = 0; i < ProblemSize; i++) { 63 probabilities[i] = random.NextDouble(); 43 64 } 44 65 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>(); 66 realizations = new ItemList<BoolArray>(RealizationsSize); 67 for (var i = 0; i < RealizationsSize; i++) { 68 var countOnes = 0; 69 var newRealization = new BoolArray(ProblemSize); 50 70 while (countOnes < 4) { //only generate realizations with at least 4 cities visited 51 71 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 } 72 for (var j = 0; j < ProblemSize; j++) { 73 newRealization[j] = random.NextDouble() < probabilities[j]; 74 if (newRealization[j]) countOnes++; 60 75 } 61 76 } 62 countOnes = 0;63 77 realizations.Add(newRealization); 64 78 } … … 68 82 69 83 [TestMethod] 70 [TestCategory("Problems. TravelingSalesman")]84 [TestCategory("Problems.ProbabilisticTravelingSalesman")] 71 85 [TestProperty("Time", "short")] 72 86 public void InversionMoveEvaluatorTest() { 87 var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0]; 73 88 74 EstimatedProbabilisticTravelingSalesmanProblem ePTSP = new EstimatedProbabilisticTravelingSalesmanProblem(); 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]; 75 94 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}. 84 The 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())); 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))); 91 99 92 100 beforeMatrix = afterMatrix; … … 95 103 96 104 [TestMethod] 97 [TestCategory("Problems. TravelingSalesman")]105 [TestCategory("Problems.ProbabilisticTravelingSalesman")] 98 106 [TestProperty("Time", "short")] 99 107 public void InsertionMoveEvaluatorTest() { 108 var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0]; 100 109 101 EstimatedProbabilisticTravelingSalesmanProblem ePTSP = new EstimatedProbabilisticTravelingSalesmanProblem(); 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]; 102 115 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}. 111 The 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())); 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))); 118 120 119 121 beforeMatrix = afterMatrix; … … 122 124 123 125 [TestMethod] 124 [TestCategory("Problems. TravelingSalesman")]126 [TestCategory("Problems.ProbabilisticTravelingSalesman")] 125 127 [TestProperty("Time", "short")] 126 128 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]; 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]; 131 132 132 AnalyticalProbabilisticTravelingSalesmanProblem aPTSP = new AnalyticalProbabilisticTravelingSalesmanProblem(); 133 double analytical = aPTSP.EvaluateWithParams(distances, ProbabilityMatrix, tour); 134 Console.WriteLine(Math.Abs(analytical-estimated)); 133 var analytical = AnalyticalProbabilisticTravelingSalesmanProblem.Evaluate(perm, distances, probabilities); 134 Console.WriteLine(Math.Abs(analytical - estimated)); 135 135 } 136 136 }
Note: See TracChangeset
for help on using the changeset viewer.