Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/05/14 12:25:32 (9 years ago)
Author:
bgoldman
Message:

#2282 Reworked Linkage Tree to provide interface for clusters, moved crossover into static class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs

    r11662 r11663  
    6565    private readonly int[][][] occurances;
    6666    private readonly List<int>[] clusters;
    67     private readonly List<int> clusterOrdering;
     67    private List<int> clusterOrdering;
    6868    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) {
    7173      this.length = length;
     74      this.rand = rand;
    7275      occurances = new int[length][][];
    7376
     77      // Create a lower triangular matrix without the diagonal
    7478      for (int i = 1; i < length; i++) {
    7579        occurances[i] = new int[i][];
     
    99103        }
    100104      }
     105      rebuildRequired = true;
    101106    }
    102107
     
    109114        var p = value / total;
    110115        sum += (p * Math.Log(p));
    111 
    112116      }
    113117      return sum;
     
    115119
    116120    private double EntropyDistance(int i, int j) {
     121      // This ensures you are using the lower triangular part of "occurances"
    117122      if (i < j) {
    118123        int temp = i;
     
    139144    }
    140145
    141     public void Rebuild(IRandom rand) {
     146    private void Rebuild() {
    142147      // Keep track of which clusters have not been merged
    143148      var topLevel = Enumerable.Range(0, length).ToList();
     
    236241        if (useful[i]) clusterOrdering.Add(i);
    237242      }
    238       // TODO Shuffle cluster ordering
     243
     244      // Shuffle before sort to ensure ties are broken randomly
    239245      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      }
    245258    }
    246259  }
Note: See TracChangeset for help on using the changeset viewer.