Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/15 23:38:51 (8 years ago)
Author:
abeham
Message:

#2221:

  • Completely refactored PTSP branch
    • Added two sets of problem instances based on TSPLIB: homogeneous and heterogeneous
    • Implemented missing EvaluateByCoordinates for 1-shift moves
    • Made it clear that move evaluators are for estimated PTSP only
    • Changed parameter realization from a rather strange list of list of ints to a list of bool arrays
    • Reusing code of the 2-opt and 1-shift move evaluators in 2.5 move evaluator
    • Introducing distance calculators to properly handle the case when no distance matrix is given (previous code only worked with distance matrix and without only with euclidean distance in some settings)
    • Fixed several smaller code issues: protected, static, method parameters, copy & paste, interfaces, naming, parameters, serialization hooks, license headers, doc comments, data types
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
     22using System;
    223using HeuristicLab.Common;
    324using HeuristicLab.Core;
     
    627using HeuristicLab.Random;
    728using Microsoft.VisualStudio.TestTools.UnitTesting;
    8 using System.Diagnostics;
    929
    10 namespace HeuristicLab.Problems.PTSP.Tests_3._3 {
     30namespace HeuristicLab.Problems.PTSP.Tests {
    1131  /// <summary>
    1232  ///This is a test class for PTSP move evaluators
     
    1535  public class PTSPMoveEvaluatorTest {
    1636    private const int ProblemSize = 10;
     37    private const int RealizationsSize = 100;
    1738    private static DoubleMatrix coordinates;
    1839    private static DistanceMatrix distances;
    1940    private static Permutation tour;
    2041    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;
    2344
    2445    [ClassInitialize]
     
    2748      coordinates = new DoubleMatrix(ProblemSize, 2);
    2849      distances = new DistanceMatrix(ProblemSize, ProblemSize);
    29       for (int i = 0; i < ProblemSize; i++) {
     50      for (var i = 0; i < ProblemSize; i++) {
    3051        coordinates[i, 0] = random.Next(ProblemSize * 10);
    3152        coordinates[i, 1] = random.Next(ProblemSize * 10);
    3253      }
    33       for (int i = 0; i < ProblemSize - 1; i++) {
    34         for (int j = i + 1; j < ProblemSize; j++) {
     54      for (var i = 0; i < ProblemSize - 1; i++) {
     55        for (var j = i + 1; j < ProblemSize; j++) {
    3556          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)));
    3657          distances[j, i] = distances[i, j];
     
    3859      }
    3960
    40       ProbabilityMatrix = new DoubleArray(ProblemSize);
    41       for (int i = 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();
    4364      }
    4465
    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);
    5070        while (countOnes < 4) { //only generate realizations with at least 4 cities visited
    5171          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++;
    6075          }
    6176        }
    62         countOnes = 0;
    6377        realizations.Add(newRealization);
    6478      }
     
    6882
    6983    [TestMethod]
    70     [TestCategory("Problems.TravelingSalesman")]
     84    [TestCategory("Problems.ProbabilisticTravelingSalesman")]
    7185    [TestProperty("Time", "short")]
    7286    public void InversionMoveEvaluatorTest() {
     87      var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
    7388
    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];
    7594
    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}.
     97The move would invert the tour {1} between values {2} and {3}.",
     98          moveMatrix, tour, tour[move.Index1], tour[move.Index2], Math.Abs(afterMatrix - beforeMatrix)));
    9199
    92100        beforeMatrix = afterMatrix;
     
    95103
    96104    [TestMethod]
    97     [TestCategory("Problems.TravelingSalesman")]
     105    [TestCategory("Problems.ProbabilisticTravelingSalesman")]
    98106    [TestProperty("Time", "short")]
    99107    public void InsertionMoveEvaluatorTest() {
     108      var beforeMatrix = EstimatedProbabilisticTravelingSalesmanProblem.Evaluate(tour, distances, realizations)[0];
    100109
    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];
    102115
    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}.
     118The move would invert the tour {1} between values {2} and {3}.",
     119          moveMatrix, tour, tour[move.Index1], tour[move.Index2], Math.Abs(afterMatrix - beforeMatrix)));
    118120
    119121        beforeMatrix = afterMatrix;
     
    122124
    123125    [TestMethod]
    124     [TestCategory("Problems.TravelingSalesman")]
     126    [TestCategory("Problems.ProbabilisticTravelingSalesman")]
    125127    [TestProperty("Time", "short")]
    126128    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];
    131132
    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));
    135135      }
    136136    }
Note: See TracChangeset for help on using the changeset viewer.