[11656] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[11662] | 25 | using HeuristicLab.Common;
|
---|
[11656] | 26 | using HeuristicLab.Core;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
| 29 | public static class ListExtensions {
|
---|
| 30 | public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB) {
|
---|
| 31 | T tmp = list[indexA];
|
---|
| 32 | list[indexA] = list[indexB];
|
---|
| 33 | list[indexB] = tmp;
|
---|
| 34 | return list;
|
---|
| 35 | }
|
---|
| 36 | public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random) {
|
---|
[11666] | 37 | return list.ShuffleInPlace(random, 0, list.Count - 1);
|
---|
[11656] | 38 | }
|
---|
| 39 | public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int maxIndex) {
|
---|
| 40 | return list.ShuffleInPlace(random, 0, maxIndex);
|
---|
| 41 | }
|
---|
| 42 | public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int minIndex, int maxIndex) {
|
---|
[11666] | 43 | for (int i = maxIndex; i > minIndex; i--) {
|
---|
| 44 | int swapIndex = random.Next(minIndex, i + 1);
|
---|
[11656] | 45 | list.Swap(i, swapIndex);
|
---|
| 46 | }
|
---|
| 47 | return list;
|
---|
| 48 | }
|
---|
[11662] | 49 |
|
---|
[11666] | 50 | public static IEnumerable<T> ShuffleList<T>(this IList<T> source, IRandom random) {
|
---|
[11662] | 51 | for (int i = source.Count - 1; i > 0; i--) {
|
---|
| 52 | // Swap element "i" with a random earlier element (including itself)
|
---|
| 53 | int swapIndex = random.Next(i + 1);
|
---|
[11667] | 54 | source.Swap(i, swapIndex);
|
---|
| 55 | yield return source[i];
|
---|
[11662] | 56 | }
|
---|
| 57 | yield return source[0];
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[11656] | 60 | }
|
---|
| 61 | public class LinkageTree {
|
---|
| 62 |
|
---|
[11662] | 63 | private readonly int[][][] occurances;
|
---|
| 64 | private readonly List<int>[] clusters;
|
---|
[11663] | 65 | private List<int> clusterOrdering;
|
---|
[11662] | 66 | private readonly int length;
|
---|
[11663] | 67 | private readonly IRandom rand;
|
---|
| 68 | private bool rebuildRequired = false;
|
---|
[11656] | 69 |
|
---|
[11663] | 70 | public LinkageTree(int length, IRandom rand) {
|
---|
[11662] | 71 | this.length = length;
|
---|
[11663] | 72 | this.rand = rand;
|
---|
[11662] | 73 | occurances = new int[length][][];
|
---|
[11656] | 74 |
|
---|
[11663] | 75 | // Create a lower triangular matrix without the diagonal
|
---|
[11662] | 76 | for (int i = 1; i < length; i++) {
|
---|
[11656] | 77 | occurances[i] = new int[i][];
|
---|
| 78 | for (int j = 0; j < i; j++) {
|
---|
| 79 | occurances[i][j] = new int[4];
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[11662] | 82 | clusters = new List<int>[2 * length - 1];
|
---|
[11656] | 83 | for (int i = 0; i < clusters.Length; i++) {
|
---|
| 84 | clusters[i] = new List<int>();
|
---|
| 85 | }
|
---|
[11662] | 86 | clusterOrdering = new List<int>();
|
---|
[11656] | 87 |
|
---|
| 88 | // first "length" clusters just contain a single gene
|
---|
[11662] | 89 | for (int i = 0; i < length; i++) {
|
---|
[11656] | 90 | clusters[i].Add(i);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public void Add(bool[] solution) {
|
---|
[11662] | 95 | if (solution.Length != length) throw new ArgumentException("The individual has not the correct length.");
|
---|
[11656] | 96 | for (int i = 1; i < solution.Length; i++) {
|
---|
| 97 | for (int j = 0; j < i; j++) {
|
---|
| 98 | // Updates the entry of the 4 long array based on the two bits
|
---|
| 99 | var pattern = (Convert.ToInt32(solution[j]) << 1) + Convert.ToInt32(solution[i]);
|
---|
| 100 | occurances[i][j][pattern]++;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[11663] | 103 | rebuildRequired = true;
|
---|
[11656] | 104 | }
|
---|
| 105 |
|
---|
| 106 | // While "total" always has an integer value, it is a double to reduce
|
---|
| 107 | // how often type casts are needed to prevent integer divison
|
---|
| 108 | private static double NegativeEntropy(int[] counts, double total) {
|
---|
| 109 | double sum = 0;
|
---|
| 110 | foreach (var value in counts) {
|
---|
[11662] | 111 | if (value == 0) continue;
|
---|
| 112 | var p = value / total;
|
---|
| 113 | sum += (p * Math.Log(p));
|
---|
[11656] | 114 | }
|
---|
| 115 | return sum;
|
---|
| 116 | }
|
---|
[11662] | 117 |
|
---|
[11656] | 118 | private double EntropyDistance(int i, int j) {
|
---|
[11663] | 119 | // This ensures you are using the lower triangular part of "occurances"
|
---|
[11656] | 120 | if (i < j) {
|
---|
| 121 | int temp = i;
|
---|
| 122 | i = j;
|
---|
| 123 | j = temp;
|
---|
| 124 | }
|
---|
| 125 | var entry = occurances[i][j];
|
---|
| 126 | int[] bits = new int[4];
|
---|
| 127 | // extracts the occurrences of the individual bits
|
---|
| 128 | bits[0] = entry[0] + entry[2]; // i zero
|
---|
| 129 | bits[1] = entry[1] + entry[3]; // i one
|
---|
| 130 | bits[2] = entry[0] + entry[1]; // j zero
|
---|
| 131 | bits[3] = entry[2] + entry[3]; // j one
|
---|
| 132 | double total = bits[0] + bits[1];
|
---|
| 133 | // entropy of the two bits on their own
|
---|
| 134 | double separate = NegativeEntropy(bits, total);
|
---|
| 135 | // entropy of the two bits as a single unit
|
---|
| 136 | double together = NegativeEntropy(entry, total);
|
---|
| 137 | // If together there is 0 entropy, the distance is zero
|
---|
[11662] | 138 | if (together.IsAlmost(0)) {
|
---|
| 139 | return 0.0;
|
---|
[11656] | 140 | }
|
---|
[11662] | 141 | return 2 - (separate / together);
|
---|
[11656] | 142 | }
|
---|
| 143 |
|
---|
[11663] | 144 | private void Rebuild() {
|
---|
[11656] | 145 | // Keep track of which clusters have not been merged
|
---|
[11662] | 146 | var topLevel = Enumerable.Range(0, length).ToList();
|
---|
| 147 | bool[] useful = Enumerable.Repeat(true, clusters.Length).ToArray();
|
---|
[11656] | 148 |
|
---|
| 149 | // Store the distances between all clusters
|
---|
| 150 | double[,] distances = new double[clusters.Length, clusters.Length];
|
---|
[11662] | 151 | for (int i = 1; i < length; i++) {
|
---|
[11656] | 152 | for (int j = 0; j < i; j++) {
|
---|
| 153 | distances[i, j] = EntropyDistance(clusters[i][0], clusters[j][0]);
|
---|
| 154 | // make it symmetric
|
---|
| 155 | distances[j, i] = distances[i, j];
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | // Each iteration we add some amount to the path, and remove the last
|
---|
| 159 | // two elements. This keeps track of how much of usable is in the path.
|
---|
| 160 | int end_of_path = 0;
|
---|
| 161 |
|
---|
| 162 | // build all clusters of size greater than 1
|
---|
[11662] | 163 | for (int index = length; index < clusters.Length; index++) {
|
---|
[11656] | 164 | // Shuffle everything not yet in the path
|
---|
[11666] | 165 | topLevel.ShuffleInPlace(rand, end_of_path, topLevel.Count-1);
|
---|
[11656] | 166 |
|
---|
| 167 | // if nothing in the path, just add a random usable node
|
---|
| 168 | if (end_of_path == 0) {
|
---|
| 169 | end_of_path = 1;
|
---|
| 170 | }
|
---|
| 171 | while (end_of_path < topLevel.Count) {
|
---|
| 172 | // last node in the path
|
---|
| 173 | int final = topLevel[end_of_path - 1];
|
---|
| 174 |
|
---|
| 175 | // best_index stores the location of the best thing in the top level
|
---|
| 176 | int best_index = end_of_path;
|
---|
| 177 | double min_dist = distances[final, topLevel[best_index]];
|
---|
| 178 | // check all options which might be closer to "final" than "topLevel[best_index]"
|
---|
| 179 | for (int option = end_of_path + 1; option < topLevel.Count; option++) {
|
---|
| 180 | if (distances[final, topLevel[option]] < min_dist) {
|
---|
| 181 | min_dist = distances[final, topLevel[option]];
|
---|
| 182 | best_index = option;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | // If the current last two in the path are minimally distant
|
---|
| 186 | if (end_of_path > 1 && min_dist >= distances[final, topLevel[end_of_path - 2]]) {
|
---|
| 187 | break;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | // move the best to the end of the path
|
---|
| 191 | topLevel.Swap(end_of_path, best_index);
|
---|
| 192 | end_of_path++;
|
---|
| 193 |
|
---|
| 194 | }
|
---|
| 195 | // Last two elements in the path are the clusters to join
|
---|
| 196 | int first = topLevel[end_of_path - 2];
|
---|
| 197 | int second = topLevel[end_of_path - 1];
|
---|
| 198 |
|
---|
| 199 | // Only keep a cluster if the distance between the joining clusters is > zero
|
---|
[11662] | 200 | bool keep = !distances[first, second].IsAlmost(0.0);
|
---|
[11656] | 201 | useful[first] = keep;
|
---|
| 202 | useful[second] = keep;
|
---|
| 203 |
|
---|
| 204 | // create the new cluster
|
---|
| 205 | clusters[index] = clusters[first].Concat(clusters[second]).ToList();
|
---|
| 206 | // Calculate distances from all clusters to the newly created cluster
|
---|
| 207 | int i = 0;
|
---|
| 208 | int end = topLevel.Count - 1;
|
---|
| 209 | while (i <= end) {
|
---|
| 210 | int x = topLevel[i];
|
---|
| 211 | // Moves 'first' and 'second' to after "end" in topLevel
|
---|
| 212 | if (x == first || x == second) {
|
---|
| 213 | topLevel.Swap(i, end);
|
---|
| 214 | end--;
|
---|
| 215 | continue;
|
---|
| 216 | }
|
---|
| 217 | // Use the previous distances to calculate the joined distance
|
---|
| 218 | double first_distance = distances[first, x];
|
---|
| 219 | first_distance *= clusters[first].Count;
|
---|
| 220 | double second_distance = distances[second, x];
|
---|
| 221 | second_distance *= clusters[second].Count;
|
---|
| 222 | distances[x, index] = ((first_distance + second_distance)
|
---|
| 223 | / (clusters[first].Count + clusters[second].Count));
|
---|
| 224 | // make it symmetric
|
---|
| 225 | distances[index, x] = distances[x, index];
|
---|
| 226 | i++;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | // Remove first and second from the path
|
---|
| 230 | end_of_path -= 2;
|
---|
| 231 | topLevel.RemoveAt(topLevel.Count - 1);
|
---|
| 232 | topLevel[topLevel.Count - 1] = index;
|
---|
| 233 | }
|
---|
| 234 | // Extract the useful clusters
|
---|
[11662] | 235 | clusterOrdering.Clear();
|
---|
[11656] | 236 | // Add all useful clusters. The last one is never useful.
|
---|
| 237 | for (int i = 0; i < useful.Length - 1; i++) {
|
---|
[11662] | 238 | if (useful[i]) clusterOrdering.Add(i);
|
---|
[11656] | 239 | }
|
---|
[11663] | 240 |
|
---|
| 241 | // Shuffle before sort to ensure ties are broken randomly
|
---|
[11662] | 242 | clusterOrdering.ShuffleInPlace(rand);
|
---|
[11663] | 243 | clusterOrdering = clusterOrdering.OrderBy(i => clusters[i].Count).ToList();
|
---|
[11656] | 244 | }
|
---|
| 245 |
|
---|
[11663] | 246 | public IEnumerable<List<int>> Clusters {
|
---|
| 247 | get {
|
---|
| 248 | // Just in time rebuilding
|
---|
| 249 | if (rebuildRequired) Rebuild();
|
---|
| 250 | foreach (var index in clusterOrdering) {
|
---|
| 251 | // Send out the clusters in the desired order
|
---|
| 252 | yield return clusters[index];
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
[11656] | 255 | }
|
---|
| 256 | }
|
---|
[11663] | 257 | } |
---|