Changeset 11663 for branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs
- Timestamp:
- 12/05/14 12:25:32 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.