Changeset 11674 for branches/Parameter-less Population Pyramid
- Timestamp:
- 12/09/14 17:00:53 (10 years ago)
- Location:
- branches/Parameter-less Population Pyramid
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs
r11672 r11674 27 27 28 28 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { 29 29 30 30 public class LinkageTree { 31 31 … … 66 66 for (int j = 0; j < i; j++) { 67 67 // Updates the entry of the 4 long array based on the two bits 68 var pattern = (Convert.ToInt32(solution[j]) << 1) + Convert.ToInt32(solution[i]); 68 69 var pattern = (Convert.ToByte(solution[j]) << 1) + Convert.ToByte(solution[i]); 69 70 occurances[i][j][pattern]++; 70 71 } … … 78 79 private static double NegativeEntropy(int[] counts, double total) { 79 80 double sum = 0; 80 for each (var value in counts) {81 if ( value == 0) continue;82 var p = value / total;83 sum += (p * Math.Log(p));81 for (int i = 0; i < counts.Length; i++) { 82 if (counts[i] != 0) { 83 sum += ((counts[i] / total) * Math.Log(counts[i] / total)); 84 } 84 85 } 85 86 return sum; … … 88 89 // Uses the frequency table to calcuate the entropy distance between two indices. 89 90 // In the GECCO paper, calculates Equation 1 91 private int[] bits = new int[4]; 90 92 private double EntropyDistance(int i, int j) { 91 93 // This ensures you are using the lower triangular part of "occurances" … … 96 98 } 97 99 var entry = occurances[i][j]; 98 int[] bits = new int[4];99 100 // extracts the occurrences of the individual bits 100 101 bits[0] = entry[0] + entry[2]; // i zero … … 114 115 } 115 116 117 118 116 119 // Performs O(N^2) clustering based on the method described in: 117 120 // "Optimal implementations of UPGMA and other common clustering algorithms" 118 121 // by I. Gronau and S. Moran 119 122 // In the GECCO paper, Figure 2 is a simplified version of this algorithm. 123 private double[][] distances; 120 124 private void Rebuild() { 125 if (distances == null) { 126 distances = new double[clusters.Length * 2 - 1][]; 127 for (int i = 0; i < distances.Length; i++) 128 distances[i] = new double[clusters.Length * 2 - 1]; 129 } 130 131 121 132 // Keep track of which clusters have not been merged 122 var topLevel = Enumerable.Range(0, length).ToList(); 123 bool[] useful = Enumerable.Repeat(true, clusters.Length).ToArray(); 133 var topLevel = new List<int>(length); 134 for (int i = 0; i < length; i++) 135 topLevel.Add(i); 136 137 bool[] useful = new bool[clusters.Length]; 138 for (int i = 0; i < useful.Length; i++) 139 useful[i] = true; 124 140 125 141 // Store the distances between all clusters 126 double[,] distances = new double[clusters.Length, clusters.Length];127 142 for (int i = 1; i < length; i++) { 128 143 for (int j = 0; j < i; j++) { 129 distances[i ,j] = EntropyDistance(clusters[i][0], clusters[j][0]);144 distances[i][j] = EntropyDistance(clusters[i][0], clusters[j][0]); 130 145 // make it symmetric 131 distances[j , i] = distances[i,j];146 distances[j][i] = distances[i][j]; 132 147 } 133 148 } … … 139 154 for (int index = length; index < clusters.Length; index++) { 140 155 // Shuffle everything not yet in the path 141 topLevel.ShuffleInPlace(rand, end_of_path, topLevel.Count -1);156 topLevel.ShuffleInPlace(rand, end_of_path, topLevel.Count - 1); 142 157 143 158 // if nothing in the path, just add a random usable node … … 151 166 // best_index stores the location of the best thing in the top level 152 167 int best_index = end_of_path; 153 double min_dist = distances[final ,topLevel[best_index]];168 double min_dist = distances[final][topLevel[best_index]]; 154 169 // check all options which might be closer to "final" than "topLevel[best_index]" 155 170 for (int option = end_of_path + 1; option < topLevel.Count; option++) { 156 if (distances[final ,topLevel[option]] < min_dist) {157 min_dist = distances[final ,topLevel[option]];171 if (distances[final][topLevel[option]] < min_dist) { 172 min_dist = distances[final][topLevel[option]]; 158 173 best_index = option; 159 174 } 160 175 } 161 176 // If the current last two in the path are minimally distant 162 if (end_of_path > 1 && min_dist >= distances[final ,topLevel[end_of_path - 2]]) {177 if (end_of_path > 1 && min_dist >= distances[final][topLevel[end_of_path - 2]]) { 163 178 break; 164 179 } … … 173 188 174 189 // Only keep a cluster if the distance between the joining clusters is > zero 175 bool keep = !distances[first ,second].IsAlmost(0.0);190 bool keep = !distances[first][second].IsAlmost(0.0); 176 191 useful[first] = keep; 177 192 useful[second] = keep; … … 191 206 } 192 207 // Use the previous distances to calculate the joined distance 193 double first_distance = distances[first ,x];208 double first_distance = distances[first][x]; 194 209 first_distance *= clusters[first].Count; 195 double second_distance = distances[second ,x];210 double second_distance = distances[second][x]; 196 211 second_distance *= clusters[second].Count; 197 distances[x ,index] = ((first_distance + second_distance)212 distances[x][index] = ((first_distance + second_distance) 198 213 / (clusters[first].Count + clusters[second].Count)); 199 214 // make it symmetric 200 distances[index , x] = distances[x,index];215 distances[index][x] = distances[x][index]; 201 216 i++; 202 217 } -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/DeceptiveStepTrapProblem.cs
r11672 r11674 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 37 36 protected DeceptiveStepTrapProblem(DeceptiveStepTrapProblem original, Cloner cloner) 38 37 : base(original, cloner) { 38 RegisterParameterEvents(); 39 39 } 40 40 public override IDeepCloneable Clone(Cloner cloner) { … … 56 56 : base() { 57 57 Parameters.Add(new FixedValueParameter<IntValue>(StepSizeParameterName, "", new IntValue(2))); 58 RegisterParameterEvents(); 58 59 } 59 60 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterParameterEvents(); 64 } 65 66 private void RegisterParameterEvents() { 67 TrapSizeParameter.Value.ValueChanged += (o, e) => { offset = -1; }; 68 StepSizeParameter.Value.ValueChanged += (o, e) => { offset = -1; }; 69 } 70 71 72 private int offset = -1; 60 73 private int Offset { 61 get { return (TrapSize - StepSize) % StepSize; } 74 get { 75 if (offset == -1) offset = (TrapSize - StepSize) % StepSize; 76 return offset; 77 } 62 78 } 63 79 … … 66 82 } 67 83 68 protected override int Score(bool[] individual, int trapIndex ) {69 int partial = base.Score(individual, trapIndex );84 protected override int Score(bool[] individual, int trapIndex, int trapSize) { 85 int partial = base.Score(individual, trapIndex, trapSize); 70 86 // introduce plateaus using integer division 71 87 return (Offset + partial) / StepSize; -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/DeceptiveTrapProblem.cs
r11672 r11674 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; … … 59 58 60 59 protected virtual int TrapMaximum { 61 get { return TrapSize; 60 get { return TrapSize; } 62 61 } 63 62 … … 69 68 70 69 // In the GECCO paper, calculates Equation 3 71 protected virtual int Score(bool[] individual, int trapIndex ) {70 protected virtual int Score(bool[] individual, int trapIndex, int trapSize) { 72 71 int result = 0; 73 72 // count number of bits in trap set to 1 74 for (int index = trapIndex; index < trapIndex + TrapSize; index++) {73 for (int index = trapIndex; index < trapIndex + trapSize; index++) { 75 74 if (individual[index]) result++; 76 75 } 77 76 78 77 // Make it deceptive 79 if (result < TrapSize) {80 result = TrapSize - result - 1;78 if (result < trapSize) { 79 result = trapSize - result - 1; 81 80 } 82 81 return result; … … 86 85 if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length."); 87 86 int total = 0; 88 for (int i = 0; i < individual.Length; i += TrapSize) { 89 total += Score(individual, i); 87 var trapSize = TrapSize; 88 for (int i = 0; i < individual.Length; i += trapSize) { 89 total += Score(individual, i, trapSize); 90 90 } 91 return (double)(total * TrapSize) / (TrapMaximum * individual.Length);91 return (double)(total * trapSize) / (TrapMaximum * individual.Length); 92 92 } 93 93 } -
branches/Parameter-less Population Pyramid/Parameter-less Population Pyramid.sln
r11656 r11674 2 2 Microsoft Visual Studio Solution File, Format Version 12.00 3 3 # Visual Studio 2013 4 VisualStudioVersion = 12.0. 21005.14 VisualStudioVersion = 12.0.31101.0 5 5 MinimumVisualStudioVersion = 10.0.40219.1 6 6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3", "HeuristicLab.Algorithms.ParameterlessPopulationPyramid\3.3\HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj", "{9319C447-8183-4DBC-8145-0E3CF98084CC}" 7 7 EndProject 8 8 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterlessPopulationPyramid.Test", "ParameterlessPopulationPyramid.Test\ParameterlessPopulationPyramid.Test.csproj", "{0B349086-8EB7-4BC2-8373-44E9CB68B258}" 9 ProjectSection(ProjectDependencies) = postProject 10 {9319C447-8183-4DBC-8145-0E3CF98084CC} = {9319C447-8183-4DBC-8145-0E3CF98084CC} 11 EndProjectSection 9 12 EndProject 10 13 Global
Note: See TracChangeset
for help on using the changeset viewer.