Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Tests/OrderCrossover2Test.cs @ 3231

Last change on this file since 3231 was 3231, checked in by abeham, 14 years ago

Added a permutation type property specifying whether it's a relative (directed or undirected) or absolute permutation #889

File size: 4.0 KB
Line 
1using HeuristicLab.Core;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Encodings.PermutationEncoding;
4
5namespace HeuristicLab.Encodings.PermutationEncoding_33.Tests {
6    /// <summary>
7    ///This is a test class for OrderCrossover2Test and is intended
8    ///to contain all OrderCrossover2Test Unit Tests
9    ///</summary>
10  [TestClass()]
11  public class OrderCrossover2Test {
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()]
64    [DeploymentItem("HeuristicLab.Encodings.PermutationEncoding-3.3.dll")]
65    public void OrderCrossover2CrossTest() {
66      TestRandom random = new TestRandom();
67      OrderCrossover2_Accessor target = new OrderCrossover2_Accessor(new PrivateObject(typeof(OrderCrossover2)));
68      random.Reset();
69      bool exceptionFired = false;
70      try {
71        target.Cross(random, new ItemArray<Permutation>(new Permutation[] {
72          new Permutation(PermutationTypes.RelativeUndirected, 4), new Permutation(PermutationTypes.RelativeUndirected, 4), new Permutation(PermutationTypes.RelativeUndirected, 4)}));
73      } catch (System.InvalidOperationException) {
74        exceptionFired = true;
75      }
76      Assert.IsTrue(exceptionFired);
77    }
78
79    /// <summary>
80    ///A test for Apply
81    ///</summary>
82    [TestMethod()]
83    public void OrderCrossover2ApplyTest() {
84      TestRandom random = new TestRandom();
85      Permutation parent1, parent2, expected, actual;
86      // 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. 135.
87      random.Reset();
88      random.IntNumbers = new int[] { 5, 7 };
89      parent1 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
90      Assert.IsTrue(parent1.Validate());
91      parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 5, 6, 0, 9, 1, 3, 8, 4, 7 });
92      Assert.IsTrue(parent2.Validate());
93      expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 0, 9, 1, 3, 5, 6, 7, 8, 4 });
94      Assert.IsTrue(expected.Validate());
95      actual = OrderCrossover2.Apply(random, parent1, parent2);
96      Assert.IsTrue(actual.Validate());
97      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
98
99      // perform a test when the two permutations are of unequal length
100      random.Reset();
101      bool exceptionFired = false;
102      try {
103        OrderCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
104      } catch (System.ArgumentException) {
105        exceptionFired = true;
106      }
107      Assert.IsTrue(exceptionFired);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.