1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
8 |
|
---|
9 | namespace ParameterlessPopulationPyramid.Test {
|
---|
10 | [TestClass]
|
---|
11 | public class LinkageTreeTest {
|
---|
12 | private static int Length = 9;
|
---|
13 | private static bool[][] solutions = new bool[][] {
|
---|
14 | new bool[] { true, true, false, false, false, false, false, false, false }, // 110000000
|
---|
15 | new bool[] { false, false, true, true, false, false, false, false, false }, // 001100000
|
---|
16 | new bool[] { false, false, false, false, true, true, false, false, false }, // 000011000
|
---|
17 | new bool[] { false, false, false, false, false, false, true, true, true }, // 000000111
|
---|
18 | new bool[] { true, true, true, true, false, false, false, false, false }, // 111100000
|
---|
19 | new bool[] { true, true, true, true, true, true, true, true, true }, // 111111111
|
---|
20 | };
|
---|
21 |
|
---|
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() {
|
---|
33 | LinkageTree tree = new LinkageTree(Length);
|
---|
34 | tree.Add(solutions[0]);
|
---|
35 | tree.Add(solutions[1]);
|
---|
36 | PrivateObject hidden = new PrivateObject(tree);
|
---|
37 | int[][][] result = (int[][][])hidden.GetField("occurances");
|
---|
38 | Assert.AreEqual(1, result[1][0][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
39 | Assert.AreEqual(2, result[Length - 1][Length - 2][0]); // Positions 0 and 1 had value 00 exactly once
|
---|
40 | Assert.AreEqual(0, result[Length - 1][Length - 2][1]); // Positions 7 and 8 never had value 10
|
---|
41 | Assert.AreEqual(1, result[1][0][3]); // Positions 0 and 1 had value 11 exactly once
|
---|
42 | }
|
---|
43 |
|
---|
44 | [TestMethod]
|
---|
45 | public void LinkageTreeTestEntropyDistance() {
|
---|
46 | LinkageTree tree = new LinkageTree(Length);
|
---|
47 | PrivateObject hidden = new PrivateObject(tree);
|
---|
48 | // No information should result in a distance of 0
|
---|
49 | Assert.AreEqual((double)0, hidden.Invoke("EntropyDistance", new object[] { 0, 1 }));
|
---|
50 | foreach (var solution in solutions) {
|
---|
51 | tree.Add(solution);
|
---|
52 | }
|
---|
53 | // Check that 0 and 1 are closer than 0 and 2
|
---|
54 | var linked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 1 });
|
---|
55 | var unlinked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 2 });
|
---|
56 | Assert.IsTrue(linked < unlinked);
|
---|
57 |
|
---|
58 | // Reversing the arguments should not change the result
|
---|
59 | var forward = hidden.Invoke("EntropyDistance", new object[] { Length - 1, Length - 2 });
|
---|
60 | var backward = hidden.Invoke("EntropyDistance", new object[] { Length - 2, Length - 1 });
|
---|
61 | Assert.AreEqual(forward, backward);
|
---|
62 | }
|
---|
63 |
|
---|
64 | [TestMethod]
|
---|
65 | public void LinkageTreeTestRebuild() {
|
---|
66 | LinkageTree tree = new LinkageTree(Length);
|
---|
67 | foreach (var solution in solutions) {
|
---|
68 | tree.Add(solution);
|
---|
69 | }
|
---|
70 | MersenneTwister rand = new MersenneTwister(123);
|
---|
71 | tree.Rebuild(rand);
|
---|
72 | PrivateObject hidden = new PrivateObject(tree);
|
---|
73 | var ordering = (List<int>)hidden.GetField("cluster_ordering");
|
---|
74 | var clusters = (List<int>[])hidden.GetField("clusters");
|
---|
75 | int[][] comparable = new int[ordering.Count][];
|
---|
76 | for (int i=0; i < ordering.Count; i++) {
|
---|
77 | var found = clusters[ordering[i]].ToArray();
|
---|
78 | Array.Sort(found);
|
---|
79 | Assert.IsTrue(correctClusters[i].SequenceEqual(found));
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|