[2906] | 1 | using HeuristicLab.Core;
|
---|
[2871] | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[2930] | 3 | using HeuristicLab.Encodings.Permutation;
|
---|
[2871] | 4 |
|
---|
[2929] | 5 | namespace HeuristicLab.Encodings.Permutation_33.Tests {
|
---|
[2871] | 6 | /// <summary>
|
---|
| 7 | ///This is a test class for CyclicCrossover2Test and is intended
|
---|
| 8 | ///to contain all CyclicCrossover2Test Unit Tests
|
---|
| 9 | ///</summary>
|
---|
| 10 | [TestClass()]
|
---|
| 11 | public class CyclicCrossover2Test {
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | private TestContext testContextInstance;
|
---|
| 15 |
|
---|
| 16 | /// <summary>
|
---|
| 17 | ///Gets or sets the test context which provides
|
---|
| 18 | ///information about and functionality for the current test run.
|
---|
| 19 | ///</summary>
|
---|
| 20 | public TestContext TestContext {
|
---|
| 21 | get {
|
---|
| 22 | return testContextInstance;
|
---|
| 23 | }
|
---|
| 24 | set {
|
---|
| 25 | testContextInstance = value;
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | #region Additional test attributes
|
---|
| 30 | //
|
---|
| 31 | //You can use the following additional attributes as you write your tests:
|
---|
| 32 | //
|
---|
| 33 | //Use ClassInitialize to run code before running the first test in the class
|
---|
| 34 | //[ClassInitialize()]
|
---|
| 35 | //public static void MyClassInitialize(TestContext testContext)
|
---|
| 36 | //{
|
---|
| 37 | //}
|
---|
| 38 | //
|
---|
| 39 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
| 40 | //[ClassCleanup()]
|
---|
| 41 | //public static void MyClassCleanup()
|
---|
| 42 | //{
|
---|
| 43 | //}
|
---|
| 44 | //
|
---|
| 45 | //Use TestInitialize to run code before running each test
|
---|
| 46 | //[TestInitialize()]
|
---|
| 47 | //public void MyTestInitialize()
|
---|
| 48 | //{
|
---|
| 49 | //}
|
---|
| 50 | //
|
---|
| 51 | //Use TestCleanup to run code after each test has run
|
---|
| 52 | //[TestCleanup()]
|
---|
| 53 | //public void MyTestCleanup()
|
---|
| 54 | //{
|
---|
| 55 | //}
|
---|
| 56 | //
|
---|
| 57 | #endregion
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | /// <summary>
|
---|
| 61 | ///A test for Cross
|
---|
| 62 | ///</summary>
|
---|
| 63 | [TestMethod()]
|
---|
[2906] | 64 | [DeploymentItem("HeuristicLab.Encodings.Permutation-3.3.dll")]
|
---|
[2871] | 65 | public void CyclicCrossover2CrossTest() {
|
---|
| 66 | TestRandom random = new TestRandom();
|
---|
| 67 | CyclicCrossover2_Accessor target = new CyclicCrossover2_Accessor(new PrivateObject(typeof(CyclicCrossover2)));
|
---|
| 68 | // perform a test with more than two parents
|
---|
| 69 | random.Reset();
|
---|
| 70 | bool exceptionFired = false;
|
---|
| 71 | try {
|
---|
[2930] | 72 | target.Cross(random, new ItemArray<Permutation.Permutation>(new Permutation.Permutation[] {
|
---|
| 73 | new Permutation.Permutation(4), new Permutation.Permutation(4), new Permutation.Permutation(4)}));
|
---|
[2871] | 74 | } catch (System.InvalidOperationException) {
|
---|
| 75 | exceptionFired = true;
|
---|
| 76 | }
|
---|
| 77 | Assert.IsTrue(exceptionFired);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /// <summary>
|
---|
| 81 | ///A test for Apply
|
---|
| 82 | ///</summary>
|
---|
| 83 | [TestMethod()]
|
---|
| 84 | public void CyclicCrossover2ApplyTest() {
|
---|
| 85 | TestRandom random = new TestRandom();
|
---|
[2930] | 86 | Permutation.Permutation parent1, parent2, expected, actual;
|
---|
[2871] | 87 | // 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. pp. 134.
|
---|
| 88 | random.Reset();
|
---|
| 89 | random.IntNumbers = new int[] { 0 };
|
---|
[2930] | 90 | parent1 = new Permutation.Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
---|
[2871] | 91 | Assert.IsTrue(parent1.Validate());
|
---|
[2930] | 92 | parent2 = new Permutation.Permutation(new int[] { 2, 5, 6, 0, 7, 1, 3, 8, 4, 9 });
|
---|
[2871] | 93 | Assert.IsTrue(parent2.Validate());
|
---|
[2930] | 94 | expected = new Permutation.Permutation(new int[] { 0, 5, 2, 3, 7, 1, 6, 8, 4, 9 });
|
---|
[2871] | 95 | Assert.IsTrue(expected.Validate());
|
---|
| 96 | actual = CyclicCrossover2.Apply(random, parent1, parent2);
|
---|
| 97 | Assert.IsTrue(actual.Validate());
|
---|
| 98 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
| 99 |
|
---|
| 100 | // perform a test when the two permutations are of unequal length
|
---|
| 101 | random.Reset();
|
---|
| 102 | bool exceptionFired = false;
|
---|
| 103 | try {
|
---|
[2930] | 104 | CyclicCrossover.Apply(random, new Permutation.Permutation(8), new Permutation.Permutation(6));
|
---|
[2871] | 105 | } catch (System.ArgumentException) {
|
---|
| 106 | exceptionFired = true;
|
---|
| 107 | }
|
---|
| 108 | Assert.IsTrue(exceptionFired);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|