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