Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Tests/SimulatedBinaryCrossoverTest.cs @ 2994

Last change on this file since 2994 was 2936, checked in by svonolfe, 15 years ago

Added heuristic, local, random convex crossover and added some initial unit tests for all RealVector operators (#890)

File size: 5.6 KB
Line 
1using HeuristicLab.Encodings.RealVector;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Parameters;
6
7namespace HeuristicLab.Encodings.RealVector_33.Tests {
8
9
10  /// <summary>
11  ///This is a test class for SimulatedBinaryCrossoverTest and is intended
12  ///to contain all SimulatedBinaryCrossoverTest Unit Tests
13  ///</summary>
14  [TestClass()]
15  public class SimulatedBinaryCrossoverTest {
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.RealVector-3.3.dll")]
68    public void SimulatedBinaryCrossoverCrossTest() {
69      SimulatedBinaryCrossover_Accessor target = new SimulatedBinaryCrossover_Accessor(new PrivateObject(typeof(SimulatedBinaryCrossover)));
70      ItemArray<DoubleArrayData> 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<DoubleArrayData>(new DoubleArrayData[] { new DoubleArrayData(5), new DoubleArrayData(6), new DoubleArrayData(4) });
76      exceptionFired = false;
77      try {
78        DoubleArrayData 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<DoubleArrayData>(new DoubleArrayData[] { new DoubleArrayData(4) });
88      exceptionFired = false;
89      try {
90        DoubleArrayData 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 SimulatedBinaryCrossoverApplyTest() {
103      TestRandom random = new TestRandom();
104      DoubleArrayData parent1, parent2, expected, actual;
105      DoubleData contiguity;
106      bool exceptionFired;
107      // The following test is not based on published examples
108      random.Reset();
109      random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
110      contiguity = new DoubleData(0.3);
111      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
112      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
113      expected = new DoubleArrayData(new double[] { 1.11032829834638, -0.0145477755417797, 0.3, 0.5, 0.1 });
114      actual = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
115      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
116      // The following test is not based on published examples
117      random.Reset();
118      random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
119      contiguity = new DoubleData(0.3);
120      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
121      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
122      exceptionFired = false;
123      try {
124        actual = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
125      } catch (System.ArgumentException) {
126        exceptionFired = true;
127      }
128      Assert.IsTrue(exceptionFired);
129      // The following test is not based on published examples
130      random.Reset();
131      random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
132      contiguity = new DoubleData(-0.3);  //  contiguity < 0
133      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1});
134      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
135      exceptionFired = false;
136      try {
137        actual = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
138      }
139      catch (System.ArgumentException) {
140        exceptionFired = true;
141      }
142      Assert.IsTrue(exceptionFired);
143    }
144
145    /// <summary>
146    ///A test for SimulatedBinaryCrossover Constructor
147    ///</summary>
148    [TestMethod()]
149    public void SimulatedBinaryCrossoverConstructorTest() {
150      SimulatedBinaryCrossover target = new SimulatedBinaryCrossover();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.