Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3/LinkageTreeTest.cs @ 11939

Last change on this file since 11939 was 11939, checked in by mkommend, 9 years ago

#2282: Moved PPP into the trunk.

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