Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/Tests/CyclicCrossover2Test.cs @ 2879

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

readded references to operator descriptions and updated documentation
added OrderCrossover2 to implement the behavior described in our book (this was the behavior of the former OrderCrossover)
added CyclicCrossover2 to implement the behavior described in our book (this was the behavior of the former CyclicCrossover)
implemented unit tests for the new variants
#889

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