Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/BlendAlphaBetaCrossoverTest.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: 5.5 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 BlendAlphaBetaCrossoverTest and is intended
13  ///to contain all BlendAlphaBetaCrossoverTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class BlendAlphaBetaCrossoverTest {
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 BlendAlphaBetaCrossoverCrossTest() {
70      BlendAlphaBetaCrossover_Accessor target = new BlendAlphaBetaCrossover_Accessor(new PrivateObject(typeof(BlendAlphaBetaCrossover)));
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 BlendAlphaBetaCrossoverApplyTest() {
103      TestRandom random = new TestRandom();
104      RealVector parent1, parent2, expected, actual;
105      DoubleValue alpha;
106      DoubleValue beta;
107      bool exceptionFired;
108      // The following test is not based on published examples
109      random.Reset();
110      random.DoubleNumbers = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
111      alpha = new DoubleValue(0.5);
112      beta = new DoubleValue(0.5);
113      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
114      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
115      expected = new RealVector(new double[] { 0.3, 0.15, 0.3, 0.35, 0.45 });
116      actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
117      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
118      // The following test is not based on published examples
119      random.Reset();
120      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
121      alpha = new DoubleValue(-0.25); // negative values for alpha are not allowed
122      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
123      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
124      exceptionFired = false;
125      try {
126        actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
127      } catch (System.ArgumentException) {
128        exceptionFired = true;
129      }
130      Assert.IsTrue(exceptionFired);
131      // The following test is not based on published examples
132      random.Reset();
133      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25, .75 };
134      alpha = new DoubleValue(0.25);
135      parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
136      parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
137      exceptionFired = false;
138      try {
139        actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
140      } catch (System.ArgumentException) {
141        exceptionFired = true;
142      }
143      Assert.IsTrue(exceptionFired);
144    }
145
146    /// <summary>
147    ///A test for BlendAlphaBetaCrossover Constructor
148    ///</summary>
149    [TestMethod()]
150    public void BlendAlphaBetaCrossoverConstructorTest() {
151      BlendAlphaBetaCrossover target = new BlendAlphaBetaCrossover();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.