[2854] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
[2906] | 22 | using HeuristicLab.Core;
|
---|
[2836] | 23 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[2930] | 24 | using HeuristicLab.Encodings.Permutation;
|
---|
[2836] | 25 |
|
---|
[2929] | 26 | namespace HeuristicLab.Encodings.Permutation_33.Tests {
|
---|
[2836] | 27 | /// <summary>
|
---|
| 28 | ///This is a test class for OrderCrossoverTest and is intended
|
---|
| 29 | ///to contain all OrderCrossoverTest Unit Tests
|
---|
| 30 | ///</summary>
|
---|
| 31 | [TestClass()]
|
---|
| 32 | public class OrderCrossoverTest {
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | private TestContext testContextInstance;
|
---|
| 36 |
|
---|
| 37 | /// <summary>
|
---|
| 38 | ///Gets or sets the test context which provides
|
---|
| 39 | ///information about and functionality for the current test run.
|
---|
| 40 | ///</summary>
|
---|
| 41 | public TestContext TestContext {
|
---|
| 42 | get {
|
---|
| 43 | return testContextInstance;
|
---|
| 44 | }
|
---|
| 45 | set {
|
---|
| 46 | testContextInstance = value;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | #region Additional test attributes
|
---|
| 51 | //
|
---|
| 52 | //You can use the following additional attributes as you write your tests:
|
---|
| 53 | //
|
---|
| 54 | //Use ClassInitialize to run code before running the first test in the class
|
---|
| 55 | //[ClassInitialize()]
|
---|
| 56 | //public static void MyClassInitialize(TestContext testContext)
|
---|
| 57 | //{
|
---|
| 58 | //}
|
---|
| 59 | //
|
---|
| 60 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
| 61 | //[ClassCleanup()]
|
---|
| 62 | //public static void MyClassCleanup()
|
---|
| 63 | //{
|
---|
| 64 | //}
|
---|
| 65 | //
|
---|
| 66 | //Use TestInitialize to run code before running each test
|
---|
| 67 | //[TestInitialize()]
|
---|
| 68 | //public void MyTestInitialize()
|
---|
| 69 | //{
|
---|
| 70 | //}
|
---|
| 71 | //
|
---|
| 72 | //Use TestCleanup to run code after each test has run
|
---|
| 73 | //[TestCleanup()]
|
---|
| 74 | //public void MyTestCleanup()
|
---|
| 75 | //{
|
---|
| 76 | //}
|
---|
| 77 | //
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /// <summary>
|
---|
| 82 | ///A test for Cross
|
---|
| 83 | ///</summary>
|
---|
| 84 | [TestMethod()]
|
---|
[2906] | 85 | [DeploymentItem("HeuristicLab.Encodings.Permutation-3.3.dll")]
|
---|
[2836] | 86 | public void OrderCrossoverCrossTest() {
|
---|
| 87 | TestRandom random = new TestRandom();
|
---|
[2854] | 88 | OrderCrossover_Accessor target =
|
---|
| 89 | new OrderCrossover_Accessor(new PrivateObject(typeof(OrderCrossover)));
|
---|
[2840] | 90 | // perform a test with more than two parents
|
---|
| 91 | random.Reset();
|
---|
| 92 | bool exceptionFired = false;
|
---|
| 93 | try {
|
---|
[2930] | 94 | target.Cross(random, new ItemArray<Permutation.Permutation>(new Permutation.Permutation[] {
|
---|
| 95 | new Permutation.Permutation(4), new Permutation.Permutation(4), new Permutation.Permutation(4)}));
|
---|
[2840] | 96 | } catch (System.InvalidOperationException) {
|
---|
| 97 | exceptionFired = true;
|
---|
| 98 | }
|
---|
| 99 | Assert.IsTrue(exceptionFired);
|
---|
[2836] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /// <summary>
|
---|
| 103 | ///A test for Apply
|
---|
| 104 | ///</summary>
|
---|
| 105 | [TestMethod()]
|
---|
| 106 | public void OrderCrossoverApplyTest() {
|
---|
| 107 | TestRandom random = new TestRandom();
|
---|
[2930] | 108 | Permutation.Permutation parent1, parent2, expected, actual;
|
---|
[2836] | 109 | // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 55-56
|
---|
| 110 | random.Reset();
|
---|
| 111 | random.IntNumbers = new int[] { 3, 6 };
|
---|
[2930] | 112 | parent1 = new Permutation.Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
[2836] | 113 | Assert.IsTrue(parent1.Validate());
|
---|
[2930] | 114 | parent2 = new Permutation.Permutation(new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 });
|
---|
[2836] | 115 | Assert.IsTrue(parent2.Validate());
|
---|
[2930] | 116 | expected = new Permutation.Permutation(new int[] { 2, 7, 1, 3, 4, 5, 6, 0, 8 });
|
---|
[2836] | 117 | Assert.IsTrue(expected.Validate());
|
---|
| 118 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 119 | Assert.IsTrue(actual.Validate());
|
---|
| 120 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 121 | // The following test is based on an example from Larranaga, P. et al. 1999. Genetic Algorithms for the Travelling Salesman Problem: A Review of Representations and Operators. Artificial Intelligence Review, 13, pp. 129-170.
|
---|
| 122 | random.Reset();
|
---|
| 123 | random.IntNumbers = new int[] { 2, 4 };
|
---|
[2930] | 124 | parent1 = new Permutation.Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
|
---|
[2836] | 125 | Assert.IsTrue(parent1.Validate());
|
---|
[2930] | 126 | parent2 = new Permutation.Permutation(new int[] { 1, 3, 5, 7, 6, 4, 2, 0 });
|
---|
[2836] | 127 | Assert.IsTrue(parent2.Validate());
|
---|
[2930] | 128 | expected = new Permutation.Permutation(new int[] { 7, 6, 2, 3, 4, 0, 1, 5 });
|
---|
[2836] | 129 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 130 | Assert.IsTrue(actual.Validate());
|
---|
| 131 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 132 | // The following test is based on an example from Talbi, E.G. 2009. Metaheuristics - From Design to Implementation. Wiley, p. 218.
|
---|
| 133 | random.Reset();
|
---|
| 134 | random.IntNumbers = new int[] { 2, 5 };
|
---|
[2930] | 135 | parent1 = new Permutation.Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
[2836] | 136 | Assert.IsTrue(parent1.Validate());
|
---|
[2930] | 137 | parent2 = new Permutation.Permutation(new int[] { 7, 3, 0, 4, 8, 2, 5, 1, 6 });
|
---|
[2836] | 138 | Assert.IsTrue(parent2.Validate());
|
---|
[2930] | 139 | expected = new Permutation.Permutation(new int[] { 0, 8, 2, 3, 4, 5, 1, 6, 7 });
|
---|
[2836] | 140 | Assert.IsTrue(expected.Validate());
|
---|
| 141 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 142 | Assert.IsTrue(actual.Validate());
|
---|
| 143 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 144 | // The following test is not based on published examples
|
---|
| 145 | random.Reset();
|
---|
| 146 | random.IntNumbers = new int[] { 0, 5 };
|
---|
[2930] | 147 | parent1 = new Permutation.Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
[2836] | 148 | Assert.IsTrue(parent1.Validate());
|
---|
[2930] | 149 | parent2 = new Permutation.Permutation(new int[] { 5, 3, 4, 0, 9, 8, 2, 7, 1, 6 });
|
---|
[2836] | 150 | Assert.IsTrue(parent2.Validate());
|
---|
[2930] | 151 | expected = new Permutation.Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 5, 0, 9 });
|
---|
[2836] | 152 | Assert.IsTrue(expected.Validate());
|
---|
| 153 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 154 | Assert.IsTrue(actual.Validate());
|
---|
| 155 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 156 | // based on the previous with changed breakpoints
|
---|
| 157 | random.Reset();
|
---|
[2840] | 158 | random.IntNumbers = new int[] { 6, 9 };
|
---|
[2930] | 159 | expected = new Permutation.Permutation(new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
|
---|
[2836] | 160 | Assert.IsTrue(expected.Validate());
|
---|
| 161 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 162 | Assert.IsTrue(actual.Validate());
|
---|
| 163 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 164 | // another one based on the previous with changed breakpoints
|
---|
| 165 | random.Reset();
|
---|
[2840] | 166 | random.IntNumbers = new int[] { 0, 9 };
|
---|
[2930] | 167 | expected = new Permutation.Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
[2836] | 168 | Assert.IsTrue(expected.Validate());
|
---|
| 169 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
| 170 | Assert.IsTrue(actual.Validate());
|
---|
| 171 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
[2867] | 172 |
|
---|
[2840] | 173 | // perform a test when the two permutations are of unequal length
|
---|
| 174 | random.Reset();
|
---|
| 175 | bool exceptionFired = false;
|
---|
| 176 | try {
|
---|
[2930] | 177 | OrderCrossover.Apply(random, new Permutation.Permutation(8), new Permutation.Permutation(6));
|
---|
[2840] | 178 | } catch (System.ArgumentException) {
|
---|
| 179 | exceptionFired = true;
|
---|
| 180 | }
|
---|
| 181 | Assert.IsTrue(exceptionFired);
|
---|
[2836] | 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|