Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11802 was 11666, checked in by bgoldman, 10 years ago

#2282 Evaluation counting, results and graphing, HIFF problem.

File size: 3.7 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    // These are the clusters that should be built using "solutions" and the seed 123
22    private static int[][] correctClusters = new int[][] {
23      new int[] { 4, 5 },
24      new int[] { 2, 3 },
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      MersenneTwister rand = new MersenneTwister();
34      LinkageTree tree = new LinkageTree(Length, rand);
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() {
47      MersenneTwister rand = new MersenneTwister();
48      LinkageTree tree = new LinkageTree(Length, rand);
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() {
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);
71      foreach (var solution in solutions) {
72        tree.Add(solution);
73      }
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));
81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.