#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Problems.QuadraticAssignment.Tests_33 { /// ///This is a test class for the QAP move evaluators /// [TestClass()] public class QAPSwapMoveEvaluatorTest { private TestContext testContextInstance; /// ///Gets or sets the test context which provides ///information about and functionality for the current test run. /// public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion [TestMethod] public void SwapMoveEvaluatorTest() { DoubleMatrix distances = new DoubleMatrix( new double[,] { { 0, 1, 2, 3 }, { 1, 0, 3, 4 }, { 2, 3, 0, 5 }, { 3, 4, 5, 0 } }); DoubleMatrix weights = new DoubleMatrix( new double[,] { { 0, 1, 0, 0 }, { 1, 0, 2, 0 }, { 0, 2, 0, 1 }, { 0, 0, 1, 0 } }); Permutation assignment = new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 2, 3 }); MersenneTwister random = new MersenneTwister(); for (int i = 0; i < 500; i++) { int index1 = random.Next(4); int index2 = random.Next(4); double before = QAPEvaluator.Apply(assignment, weights, distances); Swap2Manipulator.Apply(assignment, index1, index2); double after = QAPEvaluator.Apply(assignment, weights, distances); // evaluate swap back double move = QAPSwapMoveEvaluator.Apply(assignment, new SwapMove(index1, index2, assignment), weights, distances); Assert.IsTrue(move.IsAlmost(before - after)); } } [TestMethod] public void InversionMoveEvaluatorTest() { DoubleMatrix distances = new DoubleMatrix( new double[,] { { 0, 1, 2, 3 }, { 1, 0, 3, 4 }, { 2, 3, 0, 5 }, { 3, 4, 5, 0 } }); DoubleMatrix weights = new DoubleMatrix( new double[,] { { 0, 1, 0, 0 }, { 1, 0, 2, 0 }, { 0, 2, 0, 1 }, { 0, 0, 1, 0 } }); Permutation assignment = new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 2, 3 }); MersenneTwister random = new MersenneTwister(); for (int i = 0; i < 500; i++) { int index1 = random.Next(4); int index2 = random.Next(4); double before = QAPEvaluator.Apply(assignment, weights, distances); // invert InversionManipulator.Apply(assignment, Math.Min(index1, index2), Math.Max(index1, index2)); double after = QAPEvaluator.Apply(assignment, weights, distances); // evaluate invert back double move = QAPInversionMoveEvaluator.Apply(assignment, new InversionMove(index1, index2, assignment), weights, distances); Assert.IsTrue(move.IsAlmost(before - after)); } } [TestMethod] public void TranslocationMoveEvaluatorTest() { DoubleMatrix distances = new DoubleMatrix( new double[,] { { 0, 1, 2, 3 }, { 1, 0, 3, 4 }, { 2, 3, 0, 5 }, { 3, 4, 5, 0 } }); DoubleMatrix weights = new DoubleMatrix( new double[,] { { 0, 1, 0, 0 }, { 1, 0, 2, 0 }, { 0, 2, 0, 1 }, { 0, 0, 1, 0 } }); Permutation assignment = new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 2, 3 }); MersenneTwister random = new MersenneTwister(); for (int i = 0; i < 500; i++) { int index1 = random.Next(assignment.Length - 1); int index2 = random.Next(index1 + 1, assignment.Length); int insertPointLimit = assignment.Length - index2 + index1 - 1; // get insertion point in remaining part int insertPoint; if (insertPointLimit > 0) insertPoint = random.Next(insertPointLimit); else insertPoint = 0; double before = QAPEvaluator.Apply(assignment, weights, distances); // translocate Permutation clone = new Cloner().Clone(assignment); TranslocationManipulator.Apply(assignment, index1, index2, insertPoint); double after = QAPEvaluator.Apply(assignment, weights, distances); // evaluate translocate move double move = QAPTranslocationMoveEvaluator.Apply(clone, new TranslocationMove(index1, index2, insertPoint, assignment), weights, distances); Assert.IsTrue(move.IsAlmost(after - before)); } } } }