Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/BlendAlphaCrossoverTest.cs @ 3376

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

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

File size: 6.1 KB
Line 
1using HeuristicLab.Encodings.RealVectorEncoding;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7
8namespace HeuristicLab.Encodings.RealVectorEncoding_33.Tests {
9
10
11  /// <summary>
12  ///This is a test class for BlendAlphaCrossoverTest and is intended
13  ///to contain all BlendAlphaCrossoverTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class BlendAlphaCrossoverTest {
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.RealVectorEncoding-3.3.dll")]
69    public void BlendAlphaCrossoverCrossTest() {
70      BlendAlphaCrossover_Accessor target = new BlendAlphaCrossover_Accessor(new PrivateObject(typeof(BlendAlphaCrossover)));
71      ItemArray<RealVector> 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<RealVector>(new RealVector[] { new RealVector(5), new RealVector(6), new RealVector(4) });
77      exceptionFired = false;
78      try {
79        RealVector actual;
80        actual = target.Cross(random, parents);
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<RealVector>(new RealVector[] { new RealVector(4) });
88      exceptionFired = false;
89      try {
90        RealVector 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 BlendAlphaCrossoverApplyTest() {
103      TestRandom random = new TestRandom();
104      RealVector parent1, parent2, expected, actual;
105      DoubleValue alpha;
106      bool exceptionFired;
107      // The following test is not based on published examples
108      random.Reset();
109      random.DoubleNumbers = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
110      alpha = new DoubleValue(0.5);
111      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
112      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
113      expected = new RealVector(new double[] { 0.3, 0.15, 0.3, 0.35, 0.45 });
114      actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
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.25, 0.75, 0.25, 0.75, 0.25 };
119      alpha = new DoubleValue(0.25);
120      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
121      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
122      expected = new RealVector(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
123      actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
124      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
125      // The following test is not based on published examples
126      random.Reset();
127      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
128      alpha = new DoubleValue(-0.25); // negative values for alpha are not allowed
129      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
130      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
131      expected = new RealVector(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
132      exceptionFired = false;
133      try {
134        actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
135      } catch (System.ArgumentException) {
136        exceptionFired = true;
137      }
138      Assert.IsTrue(exceptionFired);
139      // The following test is not based on published examples
140      random.Reset();
141      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25, .75 };
142      alpha = new DoubleValue(0.25);
143      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
144      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
145      expected = new RealVector(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
146      exceptionFired = false;
147      try {
148        actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
149      } catch (System.ArgumentException) {
150        exceptionFired = true;
151      }
152      Assert.IsTrue(exceptionFired);
153    }
154
155    /// <summary>
156    ///A test for BlendAlphaCrossover Constructor
157    ///</summary>
158    [TestMethod()]
159    public void BlendAlphaCrossoverConstructorTest() {
160      BlendAlphaCrossover target = new BlendAlphaCrossover();
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.