Changeset 12288 for branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/GraftManipulator.cs
- Timestamp:
- 04/06/15 22:41:24 (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/GraftManipulator.cs
r12286 r12288 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 28 29 29 30 namespace 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.")] 31 32 [StorableClass] 32 public sealed class MergeGroupManipulator : LinearLinkageManipulator {33 public sealed class GraftManipulator : LinearLinkageManipulator { 33 34 34 public IValueLookupParameter<IntValue> NParameter {35 get { return (IValueLookupParameter<IntValue>)Parameters[" N"]; }35 public IValueLookupParameter<IntValue> MaxGroupsParameter { 36 get { return (IValueLookupParameter<IntValue>)Parameters["MaxGroups"]; } 36 37 } 37 38 38 39 [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))); 43 44 } 44 public MergeGroupManipulator(int n)45 public GraftManipulator(int maxGroups) 45 46 : this() { 46 NParameter.Value = new IntValue(n);47 MaxGroupsParameter.Value = new IntValue(maxGroups); 47 48 } 48 49 49 50 public override IDeepCloneable Clone(Cloner cloner) { 50 return new MergeGroupManipulator(this, cloner);51 return new GraftManipulator(this, cloner); 51 52 } 52 53 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 } 56 63 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(); 64 82 } 65 66 lle.SetGroups(grouping);67 83 } 68 84 69 85 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); 72 88 } 73 89 }
Note: See TracChangeset
for help on using the changeset viewer.