[3053] | 1 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[2936] | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Data;
|
---|
| 5 | using HeuristicLab.Parameters;
|
---|
| 6 |
|
---|
[3053] | 7 | namespace HeuristicLab.Encodings.RealVectorEncoding_33.Tests {
|
---|
[2936] | 8 |
|
---|
| 9 |
|
---|
| 10 | /// <summary>
|
---|
| 11 | ///This is a test class for BlendAlphaBetaCrossoverTest and is intended
|
---|
| 12 | ///to contain all BlendAlphaBetaCrossoverTest Unit Tests
|
---|
| 13 | ///</summary>
|
---|
| 14 | [TestClass()]
|
---|
| 15 | public class BlendAlphaBetaCrossoverTest {
|
---|
| 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()]
|
---|
[3053] | 67 | [DeploymentItem("HeuristicLab.Encodings.RealVectorEncoding-3.3.dll")]
|
---|
[2936] | 68 | public void BlendAlphaBetaCrossoverCrossTest() {
|
---|
| 69 | BlendAlphaBetaCrossover_Accessor target = new BlendAlphaBetaCrossover_Accessor(new PrivateObject(typeof(BlendAlphaBetaCrossover)));
|
---|
[3060] | 70 | ItemArray<RealVector> parents;
|
---|
[2936] | 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();
|
---|
[3060] | 75 | parents = new ItemArray<RealVector>(new RealVector[] { new RealVector(5), new RealVector(6), new RealVector(4) });
|
---|
[2936] | 76 | exceptionFired = false;
|
---|
| 77 | try {
|
---|
[3060] | 78 | RealVector actual;
|
---|
[2936] | 79 | actual = target.Cross(random, parents);
|
---|
| 80 | } catch (System.ArgumentException) {
|
---|
| 81 | exceptionFired = true;
|
---|
| 82 | }
|
---|
| 83 | Assert.IsTrue(exceptionFired);
|
---|
| 84 | // The following test checks if there is an exception when there are less than 2 parents
|
---|
| 85 | random.Reset();
|
---|
[3060] | 86 | parents = new ItemArray<RealVector>(new RealVector[] { new RealVector(4) });
|
---|
[2936] | 87 | exceptionFired = false;
|
---|
| 88 | try {
|
---|
[3060] | 89 | RealVector actual;
|
---|
[2936] | 90 | actual = target.Cross(random, parents);
|
---|
| 91 | } catch (System.ArgumentException) {
|
---|
| 92 | exceptionFired = true;
|
---|
| 93 | }
|
---|
| 94 | Assert.IsTrue(exceptionFired);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /// <summary>
|
---|
| 98 | ///A test for Apply
|
---|
| 99 | ///</summary>
|
---|
| 100 | [TestMethod()]
|
---|
| 101 | public void BlendAlphaBetaCrossoverApplyTest() {
|
---|
| 102 | TestRandom random = new TestRandom();
|
---|
[3060] | 103 | RealVector parent1, parent2, expected, actual;
|
---|
[3048] | 104 | DoubleValue alpha;
|
---|
| 105 | DoubleValue beta;
|
---|
[2936] | 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 };
|
---|
[3048] | 110 | alpha = new DoubleValue(0.5);
|
---|
| 111 | beta = new DoubleValue(0.5);
|
---|
[3060] | 112 | parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
|
---|
| 113 | parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
|
---|
| 114 | expected = new RealVector(new double[] { 0.3, 0.15, 0.3, 0.35, 0.45 });
|
---|
[2936] | 115 | actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
|
---|
[3061] | 116 | Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
|
---|
[2936] | 117 | // The following test is not based on published examples
|
---|
| 118 | random.Reset();
|
---|
| 119 | random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
|
---|
[3048] | 120 | alpha = new DoubleValue(-0.25); // negative values for alpha are not allowed
|
---|
[3060] | 121 | parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
|
---|
| 122 | parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
|
---|
[2936] | 123 | exceptionFired = false;
|
---|
| 124 | try {
|
---|
| 125 | actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
|
---|
| 126 | } catch (System.ArgumentException) {
|
---|
| 127 | exceptionFired = true;
|
---|
| 128 | }
|
---|
| 129 | Assert.IsTrue(exceptionFired);
|
---|
| 130 | // The following test is not based on published examples
|
---|
| 131 | random.Reset();
|
---|
| 132 | random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25, .75 };
|
---|
[3048] | 133 | alpha = new DoubleValue(0.25);
|
---|
[3060] | 134 | parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
|
---|
| 135 | parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
|
---|
[2936] | 136 | exceptionFired = false;
|
---|
| 137 | try {
|
---|
| 138 | actual = BlendAlphaBetaCrossover.Apply(random, parent1, parent2, alpha, beta);
|
---|
| 139 | } catch (System.ArgumentException) {
|
---|
| 140 | exceptionFired = true;
|
---|
| 141 | }
|
---|
| 142 | Assert.IsTrue(exceptionFired);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /// <summary>
|
---|
| 146 | ///A test for BlendAlphaBetaCrossover Constructor
|
---|
| 147 | ///</summary>
|
---|
| 148 | [TestMethod()]
|
---|
| 149 | public void BlendAlphaBetaCrossoverConstructorTest() {
|
---|
| 150 | BlendAlphaBetaCrossover target = new BlendAlphaBetaCrossover();
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|