[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[14353] | 22 | using HeuristicLab.Core;
|
---|
[6891] | 23 | using HeuristicLab.Tests;
|
---|
[3062] | 24 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 25 |
|
---|
[9764] | 26 | namespace HeuristicLab.Encodings.BinaryVectorEncoding.Tests {
|
---|
[3062] | 27 | /// <summary>
|
---|
| 28 | ///This is a test class for SinglePointCrossoverTest and is intended
|
---|
| 29 | ///to contain all SinglePointCrossoverTest Unit Tests
|
---|
| 30 | ///</summary>
|
---|
| 31 | [TestClass()]
|
---|
| 32 | public class UniformCrossoverTest {
|
---|
| 33 | /// <summary>
|
---|
[14353] | 34 | ///A test for Cross
|
---|
| 35 | ///</summary>
|
---|
| 36 | [TestMethod]
|
---|
| 37 | [TestCategory("Encodings.BinaryVector")]
|
---|
| 38 | [TestProperty("Time", "short")]
|
---|
| 39 | public void SinglePointCrossoverCrossTest() {
|
---|
| 40 | var target = new PrivateObject(typeof(UniformCrossover));
|
---|
| 41 | ItemArray<BinaryVector> parents;
|
---|
| 42 | TestRandom random = new TestRandom();
|
---|
| 43 | bool exceptionFired;
|
---|
| 44 | // The following test checks if there is an exception when there are more than 2 parents
|
---|
| 45 | random.Reset();
|
---|
| 46 | parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(5), new BinaryVector(6), new BinaryVector(4) });
|
---|
| 47 | exceptionFired = false;
|
---|
| 48 | try {
|
---|
| 49 | BinaryVector actual;
|
---|
| 50 | actual = (BinaryVector)target.Invoke("Cross", random, parents);
|
---|
| 51 | }
|
---|
| 52 | catch (System.ArgumentException) {
|
---|
| 53 | exceptionFired = true;
|
---|
| 54 | }
|
---|
| 55 | Assert.IsTrue(exceptionFired);
|
---|
| 56 | // The following test checks if there is an exception when there are less than 2 parents
|
---|
| 57 | random.Reset();
|
---|
| 58 | parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(4) });
|
---|
| 59 | exceptionFired = false;
|
---|
| 60 | try {
|
---|
| 61 | BinaryVector actual;
|
---|
| 62 | actual = (BinaryVector)target.Invoke("Cross", random, parents);
|
---|
| 63 | }
|
---|
| 64 | catch (System.ArgumentException) {
|
---|
| 65 | exceptionFired = true;
|
---|
| 66 | }
|
---|
| 67 | Assert.IsTrue(exceptionFired);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /// <summary>
|
---|
[3062] | 71 | ///A test for Apply
|
---|
| 72 | ///</summary>
|
---|
[9765] | 73 | [TestMethod]
|
---|
[9777] | 74 | [TestCategory("Encodings.BinaryVector")]
|
---|
| 75 | [TestProperty("Time", "short")]
|
---|
[3062] | 76 | public void SinglePointCrossoverApplyTest() {
|
---|
| 77 | TestRandom random = new TestRandom();
|
---|
| 78 | BinaryVector parent1, parent2, expected, actual;
|
---|
| 79 | bool exceptionFired;
|
---|
| 80 | // 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
|
---|
| 81 | random.Reset();
|
---|
| 82 | random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
|
---|
| 83 | parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
|
---|
| 84 | parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
|
---|
[4068] | 85 | expected = new BinaryVector(new bool[] { false, true, false, false, false, false, false, false, false });
|
---|
[3062] | 86 | actual = UniformCrossover.Apply(random, parent1, parent2);
|
---|
| 87 | Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
|
---|
| 88 |
|
---|
| 89 | // 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
|
---|
| 90 | random.Reset();
|
---|
| 91 | random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
|
---|
| 92 | parent2 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
|
---|
| 93 | parent1 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
|
---|
| 94 | expected = new BinaryVector(new bool[] { true, false, false, true, true, false, false, false, true });
|
---|
| 95 | actual = UniformCrossover.Apply(random, parent1, parent2);
|
---|
| 96 | Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
|
---|
[4068] | 97 |
|
---|
[3062] | 98 | // The following test is not based on any published examples
|
---|
| 99 | random.Reset();
|
---|
| 100 | random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
|
---|
| 101 | parent1 = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
|
---|
| 102 | parent2 = new BinaryVector(new bool[] { false, true, true, false });
|
---|
| 103 | exceptionFired = false;
|
---|
| 104 | try {
|
---|
| 105 | actual = UniformCrossover.Apply(random, parent1, parent2);
|
---|
[13235] | 106 | }
|
---|
| 107 | catch (System.ArgumentException) {
|
---|
[3062] | 108 | exceptionFired = true;
|
---|
| 109 | }
|
---|
| 110 | Assert.IsTrue(exceptionFired);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|