Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3/LinkageTreeTest.cs @ 18123

Last change on this file since 18123 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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