[11939] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11939] | 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 |
|
---|
[11656] | 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
|
---|
[11987] | 24 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[11656] | 25 | using HeuristicLab.Random;
|
---|
| 26 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 27 |
|
---|
| 28 | namespace ParameterlessPopulationPyramid.Test {
|
---|
| 29 | [TestClass]
|
---|
| 30 | public class LinkageTreeTest {
|
---|
| 31 | private static int Length = 9;
|
---|
[11987] | 32 | private static BinaryVector[] solutions = {
|
---|
| 33 | new BinaryVector(new [] { true, true, false, false, false, false, false, false, false }), // 110000000
|
---|
| 34 | new BinaryVector(new [] { false, false, true, true, false, false, false, false, false }), // 001100000
|
---|
| 35 | new BinaryVector(new [] { false, false, false, false, true, true, false, false, false }), // 000011000
|
---|
| 36 | new BinaryVector(new [] { false, false, false, false, false, false, true, true, true }), // 000000111
|
---|
| 37 | new BinaryVector(new [] { true, true, true, true, false, false, false, false, false }), // 111100000
|
---|
| 38 | new BinaryVector(new [] { true, true, true, true, true, true, true, true, true }), // 111111111
|
---|
[11656] | 39 | };
|
---|
| 40 |
|
---|
[11663] | 41 | // These are the clusters that should be built using "solutions" and the seed 123
|
---|
[11987] | 42 | private static int[][] correctClusters = {
|
---|
[11666] | 43 | new int[] { 4, 5 },
|
---|
[11656] | 44 | new int[] { 2, 3 },
|
---|
| 45 | new int[] { 0, 1 },
|
---|
| 46 | new int[] { 6, 7, 8 },
|
---|
| 47 | new int[] { 0, 1, 2, 3},
|
---|
| 48 | new int[] { 4, 5, 6, 7, 8},
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | [TestMethod]
|
---|
[11939] | 52 | [TestProperty("Time", "short")]
|
---|
| 53 | [TestCategory("Algorithms.ParameterlessPopulationPyramid")]
|
---|
[11656] | 54 | public void LinkageTreeTestAdd() {
|
---|
[11663] | 55 | MersenneTwister rand = new MersenneTwister();
|
---|
| 56 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 57 | tree.Add(solutions[0]);
|
---|
| 58 | tree.Add(solutions[1]);
|
---|
| 59 | PrivateObject hidden = new PrivateObject(tree);
|
---|
| 60 | int[][][] result = (int[][][])hidden.GetField("occurances");
|
---|
| 61 | Assert.AreEqual(1, result[1][0][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
| 62 | Assert.AreEqual(2, result[Length - 1][Length - 2][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
| 63 | Assert.AreEqual(0, result[Length - 1][Length - 2][1]); // Positions 7 and 8 never had value 10
|
---|
| 64 | Assert.AreEqual(1, result[1][0][3]); // Positions 0 and 1 had value 11 exactly once
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | [TestMethod]
|
---|
[11939] | 68 | [TestProperty("Time", "short")]
|
---|
| 69 | [TestCategory("Algorithms.ParameterlessPopulationPyramid")]
|
---|
[11656] | 70 | public void LinkageTreeTestEntropyDistance() {
|
---|
[11663] | 71 | MersenneTwister rand = new MersenneTwister();
|
---|
| 72 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 73 | PrivateObject hidden = new PrivateObject(tree);
|
---|
| 74 | // No information should result in a distance of 0
|
---|
| 75 | Assert.AreEqual((double)0, hidden.Invoke("EntropyDistance", new object[] { 0, 1 }));
|
---|
| 76 | foreach (var solution in solutions) {
|
---|
| 77 | tree.Add(solution);
|
---|
| 78 | }
|
---|
| 79 | // Check that 0 and 1 are closer than 0 and 2
|
---|
| 80 | var linked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 1 });
|
---|
| 81 | var unlinked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 2 });
|
---|
| 82 | Assert.IsTrue(linked < unlinked);
|
---|
| 83 |
|
---|
| 84 | // Reversing the arguments should not change the result
|
---|
| 85 | var forward = hidden.Invoke("EntropyDistance", new object[] { Length - 1, Length - 2 });
|
---|
| 86 | var backward = hidden.Invoke("EntropyDistance", new object[] { Length - 2, Length - 1 });
|
---|
| 87 | Assert.AreEqual(forward, backward);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | [TestMethod]
|
---|
[11939] | 91 | [TestProperty("Time", "short")]
|
---|
| 92 | [TestCategory("Algorithms.ParameterlessPopulationPyramid")]
|
---|
[11656] | 93 | public void LinkageTreeTestRebuild() {
|
---|
[11663] | 94 | // The seed matters as equal sized clusters can appear in any order
|
---|
| 95 | MersenneTwister rand = new MersenneTwister(123);
|
---|
| 96 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 97 | foreach (var solution in solutions) {
|
---|
| 98 | tree.Add(solution);
|
---|
| 99 | }
|
---|
[11663] | 100 |
|
---|
| 101 | // Check if the clusters created contain the expected variables.
|
---|
| 102 | var found = tree.Clusters.ToArray();
|
---|
| 103 | Assert.AreEqual(correctClusters.Length, found.Length);
|
---|
| 104 | for (int i = 0; i < found.Length; i++) {
|
---|
| 105 | found[i].Sort();
|
---|
| 106 | Assert.IsTrue(found[i].SequenceEqual(correctClusters[i]), string.Format("Failed On Cluster {0}", i));
|
---|
[11662] | 107 | }
|
---|
[11656] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|