Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/ParameterlessPopulationPyramid.Test/LinkageTreeTest.cs @ 11662

Last change on this file since 11662 was 11662, checked in by mkommend, 10 years ago

#2282: Refactored LinkageTree and adapted unit tests.

File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
5using HeuristicLab.Random;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace 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
21    private static int[][] correctClusters = new int[][] {
22      new int[] { 2, 3 },
23      new int[] { 4, 5 },
24      new int[] { 0, 1 },
25      new int[] { 6, 7, 8 },
26      new int[] { 0, 1, 2, 3},
27      new int[] { 4, 5, 6, 7, 8},
28    };
29
30    [TestMethod]
31    public void LinkageTreeTestAdd() {
32      LinkageTree tree = new LinkageTree(Length);
33      tree.Add(solutions[0]);
34      tree.Add(solutions[1]);
35      PrivateObject hidden = new PrivateObject(tree);
36      int[][][] result = (int[][][])hidden.GetField("occurances");
37      Assert.AreEqual(1, result[1][0][0]); // Positions 0 and 1 had value 00 exactly once
38      Assert.AreEqual(2, result[Length - 1][Length - 2][0]); // Positions 0 and 1 had value 00 exactly once
39      Assert.AreEqual(0, result[Length - 1][Length - 2][1]); // Positions 7 and 8 never had value 10
40      Assert.AreEqual(1, result[1][0][3]); // Positions 0 and 1 had value 11 exactly once
41    }
42
43    [TestMethod]
44    public void LinkageTreeTestEntropyDistance() {
45      LinkageTree tree = new LinkageTree(Length);
46      PrivateObject hidden = new PrivateObject(tree);
47      // No information should result in a distance of 0
48      Assert.AreEqual((double)0, hidden.Invoke("EntropyDistance", new object[] { 0, 1 }));
49      foreach (var solution in solutions) {
50        tree.Add(solution);
51      }
52      // Check that 0 and 1 are closer than 0 and 2
53      var linked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 1 });
54      var unlinked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 2 });
55      Assert.IsTrue(linked < unlinked);
56
57      // Reversing the arguments should not change the result
58      var forward = hidden.Invoke("EntropyDistance", new object[] { Length - 1, Length - 2 });
59      var backward = hidden.Invoke("EntropyDistance", new object[] { Length - 2, Length - 1 });
60      Assert.AreEqual(forward, backward);
61    }
62
63    [TestMethod]
64    public void LinkageTreeTestRebuild() {
65      LinkageTree tree = new LinkageTree(Length);
66      foreach (var solution in solutions) {
67        tree.Add(solution);
68      }
69      MersenneTwister rand = new MersenneTwister(123);
70      tree.Rebuild(rand);
71      PrivateObject hidden = new PrivateObject(tree);
72      var ordering = (List<int>)hidden.GetField("clusterOrdering");
73      var clusters = (List<int>[])hidden.GetField("clusters");
74      int[][] comparable = new int[ordering.Count][];
75      for (int i = 0; i < ordering.Count; i++) {
76        var found = clusters[ordering[i]].ToArray();
77        Array.Sort(found);
78        Assert.IsTrue(correctClusters[i].SequenceEqual(found));
79      }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.