[11656] | 1 | using System;
|
---|
[11662] | 2 | using System.Collections.Generic;
|
---|
[11656] | 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
|
---|
| 5 | using HeuristicLab.Random;
|
---|
| 6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 7 |
|
---|
| 8 | namespace ParameterlessPopulationPyramid.Test {
|
---|
| 9 | [TestClass]
|
---|
| 10 | public class LinkageTreeTest {
|
---|
| 11 | private static int Length = 9;
|
---|
| 12 | private static bool[][] solutions = new bool[][] {
|
---|
| 13 | new bool[] { true, true, false, false, false, false, false, false, false }, // 110000000
|
---|
| 14 | new bool[] { false, false, true, true, false, false, false, false, false }, // 001100000
|
---|
| 15 | new bool[] { false, false, false, false, true, true, false, false, false }, // 000011000
|
---|
| 16 | new bool[] { false, false, false, false, false, false, true, true, true }, // 000000111
|
---|
| 17 | new bool[] { true, true, true, true, false, false, false, false, false }, // 111100000
|
---|
| 18 | new bool[] { true, true, true, true, true, true, true, true, true }, // 111111111
|
---|
| 19 | };
|
---|
| 20 |
|
---|
[11663] | 21 | // These are the clusters that should be built using "solutions" and the seed 123
|
---|
[11656] | 22 | private static int[][] correctClusters = new int[][] {
|
---|
| 23 | new int[] { 2, 3 },
|
---|
| 24 | new int[] { 4, 5 },
|
---|
| 25 | new int[] { 0, 1 },
|
---|
| 26 | new int[] { 6, 7, 8 },
|
---|
| 27 | new int[] { 0, 1, 2, 3},
|
---|
| 28 | new int[] { 4, 5, 6, 7, 8},
|
---|
| 29 | };
|
---|
| 30 |
|
---|
| 31 | [TestMethod]
|
---|
| 32 | public void LinkageTreeTestAdd() {
|
---|
[11663] | 33 | MersenneTwister rand = new MersenneTwister();
|
---|
| 34 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 35 | tree.Add(solutions[0]);
|
---|
| 36 | tree.Add(solutions[1]);
|
---|
| 37 | PrivateObject hidden = new PrivateObject(tree);
|
---|
| 38 | int[][][] result = (int[][][])hidden.GetField("occurances");
|
---|
| 39 | Assert.AreEqual(1, result[1][0][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
| 40 | Assert.AreEqual(2, result[Length - 1][Length - 2][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
| 41 | Assert.AreEqual(0, result[Length - 1][Length - 2][1]); // Positions 7 and 8 never had value 10
|
---|
| 42 | Assert.AreEqual(1, result[1][0][3]); // Positions 0 and 1 had value 11 exactly once
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [TestMethod]
|
---|
| 46 | public void LinkageTreeTestEntropyDistance() {
|
---|
[11663] | 47 | MersenneTwister rand = new MersenneTwister();
|
---|
| 48 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 49 | PrivateObject hidden = new PrivateObject(tree);
|
---|
| 50 | // No information should result in a distance of 0
|
---|
| 51 | Assert.AreEqual((double)0, hidden.Invoke("EntropyDistance", new object[] { 0, 1 }));
|
---|
| 52 | foreach (var solution in solutions) {
|
---|
| 53 | tree.Add(solution);
|
---|
| 54 | }
|
---|
| 55 | // Check that 0 and 1 are closer than 0 and 2
|
---|
| 56 | var linked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 1 });
|
---|
| 57 | var unlinked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 2 });
|
---|
| 58 | Assert.IsTrue(linked < unlinked);
|
---|
| 59 |
|
---|
| 60 | // Reversing the arguments should not change the result
|
---|
| 61 | var forward = hidden.Invoke("EntropyDistance", new object[] { Length - 1, Length - 2 });
|
---|
| 62 | var backward = hidden.Invoke("EntropyDistance", new object[] { Length - 2, Length - 1 });
|
---|
| 63 | Assert.AreEqual(forward, backward);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | [TestMethod]
|
---|
| 67 | public void LinkageTreeTestRebuild() {
|
---|
[11663] | 68 | // The seed matters as equal sized clusters can appear in any order
|
---|
| 69 | MersenneTwister rand = new MersenneTwister(123);
|
---|
| 70 | LinkageTree tree = new LinkageTree(Length, rand);
|
---|
[11656] | 71 | foreach (var solution in solutions) {
|
---|
| 72 | tree.Add(solution);
|
---|
| 73 | }
|
---|
[11663] | 74 |
|
---|
| 75 | // Check if the clusters created contain the expected variables.
|
---|
| 76 | var found = tree.Clusters.ToArray();
|
---|
| 77 | Assert.AreEqual(correctClusters.Length, found.Length);
|
---|
| 78 | for (int i = 0; i < found.Length; i++) {
|
---|
| 79 | found[i].Sort();
|
---|
| 80 | Assert.IsTrue(found[i].SequenceEqual(correctClusters[i]), string.Format("Failed On Cluster {0}", i));
|
---|
[11662] | 81 | }
|
---|
[11656] | 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|