Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/06/15 22:41:24 (9 years ago)
Author:
abeham
Message:

#2319:

  • Changed encoding to represent linkages in LLE (as opposed to LLE-e)
  • Added GraftManipulator
  • Added repair procedure
  • Improved performance of some crossovers
File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/GraftManipulator.cs

    r12286 r12288  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    2829
    2930namespace HeuristicLab.Encodings.LinearLinkageEncoding {
    30   [Item("Merge Group Manipulator", "Performs a maximum of N merge operations on the groups. Groups may be merged multiple times.")]
     31  [Item("Graft Manipulator", "Performs graft mutation as described in Du, J., Korkmaz, E.E., Alhajj, R., and Barker, K. 2004. Novel Clustering Approach that employs Genetic Algorithm with New Representation Scheme and Multiple Objectives. Data Warehousing and Knowledge Discovery, pp. 219-228. Springer Berlin Heidelberg.")]
    3132  [StorableClass]
    32   public sealed class MergeGroupManipulator : LinearLinkageManipulator {
     33  public sealed class GraftManipulator : LinearLinkageManipulator {
    3334
    34     public IValueLookupParameter<IntValue> NParameter {
    35       get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
     35    public IValueLookupParameter<IntValue> MaxGroupsParameter {
     36      get { return (IValueLookupParameter<IntValue>)Parameters["MaxGroups"]; }
    3637    }
    3738
    3839    [StorableConstructor]
    39     private MergeGroupManipulator(bool deserializing) : base(deserializing) { }
    40     private MergeGroupManipulator(MergeGroupManipulator original, Cloner cloner) : base(original, cloner) { }
    41     public MergeGroupManipulator() {
    42       Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of groups to merge.", new IntValue(1)));
     40    private GraftManipulator(bool deserializing) : base(deserializing) { }
     41    private GraftManipulator(GraftManipulator original, Cloner cloner) : base(original, cloner) { }
     42    public GraftManipulator() {
     43      Parameters.Add(new ValueLookupParameter<IntValue>("MaxGroups", "The maximum number of groups. If a value less or equal than 0 is used the number of groups is not limited.", new IntValue(-1)));
    4344    }
    44     public MergeGroupManipulator(int n)
     45    public GraftManipulator(int maxGroups)
    4546      : this() {
    46       NParameter.Value = new IntValue(n);
     47      MaxGroupsParameter.Value = new IntValue(maxGroups);
    4748    }
    4849
    4950    public override IDeepCloneable Clone(Cloner cloner) {
    50       return new MergeGroupManipulator(this, cloner);
     51      return new GraftManipulator(this, cloner);
    5152    }
    5253
    53     public static void Apply(IRandom random, LinearLinkage lle, int n) {
    54       var grouping = lle.GetGroups().ToList();
    55       if (grouping.Count == 1) return; // nothing to merge
     54    public static void Apply(IRandom random, LinearLinkage lle, int maxGroups) {
     55      int tries = lle.Length;
     56      var index = random.Next(lle.Length);
     57      while (tries > 0 && lle[index] == index) {
     58        index = random.Next(lle.Length);
     59        tries--;
     60      }
     61      if (lle[index] != index) Apply(random, lle, maxGroups, index);
     62    }
    5663
    57       for (var i = 0; i < n; i++) {
    58         var g1 = random.Next(grouping.Count);
    59         var g2 = random.Next(grouping.Count);
    60         while (g1 == g2) g2 = random.Next(grouping.Count);
    61         grouping[g1].AddRange(grouping[g2]);
    62         grouping.RemoveAt(g2);
    63         if (grouping.Count == 1) break;
     64    public static void Apply(IRandom random, LinearLinkage lle, int maxGroups, int index) {
     65      var groups = lle.Select((val, idx) => Tuple.Create(idx, val))
     66                      .Where(x => x.Item1 == x.Item2)
     67                      .Select(x => x.Item2).ToList();
     68      var z = groups.Count;
     69
     70      if (random.NextDouble() < 0.5)
     71        lle[index] = index; // divide the cluster into two
     72      else {
     73        var c = random.Next(z);
     74        if (groups[c] > index)
     75          lle[index] = groups[c]; // combine the portion with another class
     76        else {
     77          // combine the other class here
     78          lle[groups[c]] = lle[index];
     79          lle[index] = index;
     80        }
     81        lle.LinearizeTreeStructures();
    6482      }
    65 
    66       lle.SetGroups(grouping);
    6783    }
    6884
    6985    protected override void Manipulate(IRandom random, LinearLinkage lle) {
    70       var N = NParameter.ActualValue.Value;
    71       Apply(random, lle, N);
     86      var maxGroups = MaxGroupsParameter.ActualValue.Value;
     87      Apply(random, lle, maxGroups <= 0 ? int.MaxValue : maxGroups);
    7288    }
    7389  }
Note: See TracChangeset for help on using the changeset viewer.