[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 |
|
---|
| 22 | using HeuristicLab.Permutation;
|
---|
| 23 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Permutation.Tests {
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | /// <summary>
|
---|
| 30 | ///This is a test class for PartiallyMatchedCrossover and is intended
|
---|
| 31 | ///to contain all PartiallyMatchedCrossover Unit Tests
|
---|
| 32 | ///</summary>
|
---|
| 33 | [TestClass()]
|
---|
| 34 | public class PartiallyMatchedCrossoverTest {
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | private TestContext testContextInstance;
|
---|
| 38 |
|
---|
| 39 | /// <summary>
|
---|
| 40 | ///Gets or sets the test context which provides
|
---|
| 41 | ///information about and functionality for the current test run.
|
---|
| 42 | ///</summary>
|
---|
| 43 | public TestContext TestContext {
|
---|
| 44 | get {
|
---|
| 45 | return testContextInstance;
|
---|
| 46 | }
|
---|
| 47 | set {
|
---|
| 48 | testContextInstance = value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | #region Additional test attributes
|
---|
| 53 | //
|
---|
| 54 | //You can use the following additional attributes as you write your tests:
|
---|
| 55 | //
|
---|
| 56 | //Use ClassInitialize to run code before running the first test in the class
|
---|
| 57 | //[ClassInitialize()]
|
---|
| 58 | //public static void MyClassInitialize(TestContext testContext)
|
---|
| 59 | //{
|
---|
| 60 | //}
|
---|
| 61 | //
|
---|
| 62 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
| 63 | //[ClassCleanup()]
|
---|
| 64 | //public static void MyClassCleanup()
|
---|
| 65 | //{
|
---|
| 66 | //}
|
---|
| 67 | //
|
---|
| 68 | //Use TestInitialize to run code before running each test
|
---|
| 69 | //[TestInitialize()]
|
---|
| 70 | //public void MyTestInitialize()
|
---|
| 71 | //{
|
---|
| 72 | //}
|
---|
| 73 | //
|
---|
| 74 | //Use TestCleanup to run code after each test has run
|
---|
| 75 | //[TestCleanup()]
|
---|
| 76 | //public void MyTestCleanup()
|
---|
| 77 | //{
|
---|
| 78 | //}
|
---|
| 79 | //
|
---|
| 80 | #endregion
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /// <summary>
|
---|
| 84 | ///A test for Cross
|
---|
| 85 | ///</summary>
|
---|
| 86 | [TestMethod()]
|
---|
| 87 | [DeploymentItem("HeuristicLab.Permutation-3.3.dll")]
|
---|
| 88 | public void PartiallyMatchedCrossTest() {
|
---|
| 89 | TestRandom random = new TestRandom();
|
---|
| 90 | PartiallyMatchedCrossover_Accessor target =
|
---|
| 91 | new PartiallyMatchedCrossover_Accessor(new PrivateObject(typeof(PartiallyMatchedCrossover)));
|
---|
| 92 | // perform a test with more than two parents
|
---|
| 93 | random.Reset();
|
---|
| 94 | bool exceptionFired = false;
|
---|
| 95 | try {
|
---|
| 96 | target.Cross(random, new ItemArray<Permutation>(new Permutation[] {
|
---|
| 97 | new Permutation(4), new Permutation(4), new Permutation(4)}));
|
---|
| 98 | }
|
---|
| 99 | catch (System.InvalidOperationException) {
|
---|
| 100 | exceptionFired = true;
|
---|
| 101 | }
|
---|
| 102 | Assert.IsTrue(exceptionFired);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /// <summary>
|
---|
| 106 | ///A test for Apply
|
---|
| 107 | ///</summary>
|
---|
| 108 | [TestMethod()]
|
---|
| 109 | public void PartiallyMatchedCrossoverApplyTest() {
|
---|
| 110 | TestRandom random = new TestRandom();
|
---|
| 111 | Permutation parent1, parent2, expected, actual;
|
---|
| 112 | // The following test is based on an example from Larranaga, 1999. Genetic Algorithms for the Traveling Salesman Problem.
|
---|
| 113 | random.Reset();
|
---|
| 114 | random.IntNumbers = new int[] { 3, 5 };
|
---|
| 115 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
|
---|
| 116 | Assert.IsTrue(parent1.Validate());
|
---|
| 117 | parent2 = new Permutation(new int[] { 2, 6, 4, 0, 5, 7, 1, 3 });
|
---|
| 118 | Assert.IsTrue(parent2.Validate());
|
---|
| 119 | expected = new Permutation(new int[] { 2, 6, 7, 3, 4, 5, 1, 0 });
|
---|
| 120 | Assert.IsTrue(expected.Validate());
|
---|
| 121 | actual = PartiallyMatchedCrossover.Apply(random, parent1, parent2);
|
---|
| 122 | Assert.IsTrue(actual.Validate());
|
---|
| 123 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
[2867] | 124 | // The following test is based on an example from Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 134.
|
---|
| 125 | random.Reset();
|
---|
| 126 | random.IntNumbers = new int[] { 5, 7 };
|
---|
| 127 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
---|
| 128 | Assert.IsTrue(parent1.Validate());
|
---|
| 129 | parent2 = new Permutation(new int[] { 2, 5, 6, 0, 9, 1, 3, 8, 4, 7 });
|
---|
| 130 | Assert.IsTrue(parent2.Validate());
|
---|
| 131 | expected = new Permutation(new int[] { 2, 1, 3, 0, 9, 5, 6, 7, 4, 8 });
|
---|
| 132 | Assert.IsTrue(expected.Validate());
|
---|
| 133 | actual = PartiallyMatchedCrossover.Apply(random, parent1, parent2);
|
---|
| 134 | Assert.IsTrue(actual.Validate());
|
---|
| 135 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 136 |
|
---|
[2854] | 137 | // perform a test when the two permutations are of unequal length
|
---|
| 138 | random.Reset();
|
---|
| 139 | bool exceptionFired = false;
|
---|
| 140 | try {
|
---|
| 141 | PartiallyMatchedCrossover.Apply(random, new Permutation(8), new Permutation(6));
|
---|
| 142 | }
|
---|
| 143 | catch (System.ArgumentException) {
|
---|
| 144 | exceptionFired = true;
|
---|
| 145 | }
|
---|
| 146 | Assert.IsTrue(exceptionFired);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /// <summary>
|
---|
| 150 | ///A test for PartiallyMatchedCrossover Constructor
|
---|
| 151 | ///</summary>
|
---|
| 152 | [TestMethod()]
|
---|
| 153 | public void PartiallyMatchedCrossoverConstructorTest() {
|
---|
| 154 | PartiallyMatchedCrossover target = new PartiallyMatchedCrossover();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|