Changeset 11663 for branches/Parameter-less Population Pyramid
- Timestamp:
- 12/05/14 12:25:32 (10 years ago)
- Location:
- branches/Parameter-less Population Pyramid
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj
r11656 r11663 100 100 <ItemGroup> 101 101 <Compile Include="AlgorithmBase.cs" /> 102 <Compile Include="LinkageCrossover.cs" /> 102 103 <Compile Include="DeceptiveTrapProblem.cs" /> 103 104 <Compile Include="HillClimber.cs" /> 104 105 <Compile Include="LinkageTree.cs" /> 106 <Compile Include="Population.cs" /> 105 107 <Compile Include="Problems\BinaryVectorProblem.cs" /> 106 108 <Compile Include="Plugin.cs" /> -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs
r11662 r11663 65 65 private readonly int[][][] occurances; 66 66 private readonly List<int>[] clusters; 67 private readonlyList<int> clusterOrdering;67 private List<int> clusterOrdering; 68 68 private readonly int length; 69 70 public LinkageTree(int length) { 69 private readonly IRandom rand; 70 private bool rebuildRequired = false; 71 72 public LinkageTree(int length, IRandom rand) { 71 73 this.length = length; 74 this.rand = rand; 72 75 occurances = new int[length][][]; 73 76 77 // Create a lower triangular matrix without the diagonal 74 78 for (int i = 1; i < length; i++) { 75 79 occurances[i] = new int[i][]; … … 99 103 } 100 104 } 105 rebuildRequired = true; 101 106 } 102 107 … … 109 114 var p = value / total; 110 115 sum += (p * Math.Log(p)); 111 112 116 } 113 117 return sum; … … 115 119 116 120 private double EntropyDistance(int i, int j) { 121 // This ensures you are using the lower triangular part of "occurances" 117 122 if (i < j) { 118 123 int temp = i; … … 139 144 } 140 145 141 p ublic void Rebuild(IRandom rand) {146 private void Rebuild() { 142 147 // Keep track of which clusters have not been merged 143 148 var topLevel = Enumerable.Range(0, length).ToList(); … … 236 241 if (useful[i]) clusterOrdering.Add(i); 237 242 } 238 // TODO Shuffle cluster ordering 243 244 // Shuffle before sort to ensure ties are broken randomly 239 245 clusterOrdering.ShuffleInPlace(rand); 240 clusterOrdering.Sort((i, j) => clusters[i].Count.CompareTo(clusters[j].Count)); 241 } 242 243 public double ImproveUsingTree(bool[] solution, double fitness, BinaryVectorProblem problem) { 244 return fitness; 246 clusterOrdering = clusterOrdering.OrderBy(i => clusters[i].Count).ToList(); 247 } 248 249 public IEnumerable<List<int>> Clusters { 250 get { 251 // Just in time rebuilding 252 if (rebuildRequired) Rebuild(); 253 foreach (var index in clusterOrdering) { 254 // Send out the clusters in the desired order 255 yield return clusters[index]; 256 } 257 } 245 258 } 246 259 } -
branches/Parameter-less Population Pyramid/ParameterlessPopulationPyramid.Test/LinkageTreeTest.cs
r11662 r11663 19 19 }; 20 20 21 // These are the clusters that should be built using "solutions" and the seed 123 21 22 private static int[][] correctClusters = new int[][] { 22 23 new int[] { 2, 3 }, … … 30 31 [TestMethod] 31 32 public void LinkageTreeTestAdd() { 32 LinkageTree tree = new LinkageTree(Length); 33 MersenneTwister rand = new MersenneTwister(); 34 LinkageTree tree = new LinkageTree(Length, rand); 33 35 tree.Add(solutions[0]); 34 36 tree.Add(solutions[1]); … … 43 45 [TestMethod] 44 46 public void LinkageTreeTestEntropyDistance() { 45 LinkageTree tree = new LinkageTree(Length); 47 MersenneTwister rand = new MersenneTwister(); 48 LinkageTree tree = new LinkageTree(Length, rand); 46 49 PrivateObject hidden = new PrivateObject(tree); 47 50 // No information should result in a distance of 0 … … 63 66 [TestMethod] 64 67 public void LinkageTreeTestRebuild() { 65 LinkageTree tree = new LinkageTree(Length); 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); 66 71 foreach (var solution in solutions) { 67 72 tree.Add(solution); 68 73 } 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)); 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)); 79 81 } 80 82 }
Note: See TracChangeset
for help on using the changeset viewer.