Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Tests/UniformCrossoverTest.cs @ 3538

Last change on this file since 3538 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 5.7 KB
Line 
1using HeuristicLab.Encodings.BinaryVectorEncoding;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7
8namespace HeuristicLab.Encodings.BinaryVectorEncoding_33.Tests {
9
10
11  /// <summary>
12  ///This is a test class for SinglePointCrossoverTest and is intended
13  ///to contain all SinglePointCrossoverTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class UniformCrossoverTest {
17
18
19    private TestContext testContextInstance;
20
21    /// <summary>
22    ///Gets or sets the test context which provides
23    ///information about and functionality for the current test run.
24    ///</summary>
25    public TestContext TestContext {
26      get {
27        return testContextInstance;
28      }
29      set {
30        testContextInstance = value;
31      }
32    }
33
34    #region Additional test attributes
35    //
36    //You can use the following additional attributes as you write your tests:
37    //
38    //Use ClassInitialize to run code before running the first test in the class
39    //[ClassInitialize()]
40    //public static void MyClassInitialize(TestContext testContext)
41    //{
42    //}
43    //
44    //Use ClassCleanup to run code after all tests in a class have run
45    //[ClassCleanup()]
46    //public static void MyClassCleanup()
47    //{
48    //}
49    //
50    //Use TestInitialize to run code before running each test
51    //[TestInitialize()]
52    //public void MyTestInitialize()
53    //{
54    //}
55    //
56    //Use TestCleanup to run code after each test has run
57    //[TestCleanup()]
58    //public void MyTestCleanup()
59    //{
60    //}
61    //
62    #endregion
63
64    /// <summary>
65    ///A test for Cross
66    ///</summary>
67    [TestMethod()]
68    [DeploymentItem("HeuristicLab.Encodings.BinaryVectorEncoding-3.3.dll")]
69    public void SinglePointCrossoverCrossTest() {
70      UniformCrossover_Accessor target = new UniformCrossover_Accessor(new PrivateObject(typeof(UniformCrossover)));
71      ItemArray<BinaryVector> parents;
72      TestRandom random = new TestRandom();
73      bool exceptionFired;
74      // The following test checks if there is an exception when there are more than 2 parents
75      random.Reset();
76      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(5), new BinaryVector(6), new BinaryVector(4) });
77      exceptionFired = false;
78      try {
79        BinaryVector actual;
80        actual = target.Cross(random, parents);
81      }
82      catch (System.ArgumentException) {
83        exceptionFired = true;
84      }
85      Assert.IsTrue(exceptionFired);
86      // The following test checks if there is an exception when there are less than 2 parents
87      random.Reset();
88      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(4) });
89      exceptionFired = false;
90      try {
91        BinaryVector actual;
92        actual = target.Cross(random, parents);
93      } catch (System.ArgumentException) {
94        exceptionFired = true;
95      }
96      Assert.IsTrue(exceptionFired);
97    }
98
99    /// <summary>
100    ///A test for Apply
101    ///</summary>
102    [TestMethod()]
103    public void SinglePointCrossoverApplyTest() {
104      TestRandom random = new TestRandom();
105      BinaryVector parent1, parent2, expected, actual;
106      bool exceptionFired;
107      // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 49
108      random.Reset();
109      random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
110      parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
111      parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
112      expected = new BinaryVector(new bool[] { false, true, false, false, false, false, false, false, false});
113      actual = UniformCrossover.Apply(random, parent1, parent2);
114      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
115
116      // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 49
117      random.Reset();
118      random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
119      parent2 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
120      parent1 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
121      expected = new BinaryVector(new bool[] { true, false, false, true, true, false, false, false, true });
122      actual = UniformCrossover.Apply(random, parent1, parent2);
123      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
124     
125      // The following test is not based on any published examples
126      random.Reset();
127      random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
128      parent1 = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
129      parent2 = new BinaryVector(new bool[] { false, true, true, false });
130      exceptionFired = false;
131      try {
132        actual = UniformCrossover.Apply(random, parent1, parent2);
133      } catch (System.ArgumentException) {
134        exceptionFired = true;
135      }
136      Assert.IsTrue(exceptionFired);
137    }
138
139    /// <summary>
140    ///A test for SinglePointCrossover Constructor
141    ///</summary>
142    [TestMethod()]
143    public void SinglePointCrossoverConstructorTest() {
144      NPointCrossover target = new NPointCrossover();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.