Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Manipulators/SplitGroupManipulator.cs @ 16321

Last change on this file since 16321 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Encodings.LinearLinkageEncoding {
33  [Item("Split Group Manipulator", "Performs a maximum of N split operations on the groups. An already split group may be split again.")]
34  [StorableClass]
35  public sealed class SplitGroupManipulator : LinearLinkageManipulator {
36
37    public IValueLookupParameter<IntValue> NParameter {
38      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
39    }
40
41    [StorableConstructor]
42    private SplitGroupManipulator(bool deserializing) : base(deserializing) { }
43    private SplitGroupManipulator(SplitGroupManipulator original, Cloner cloner) : base(original, cloner) { }
44    public SplitGroupManipulator() {
45      Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of groups to split.", new IntValue(1)));
46    }
47    public SplitGroupManipulator(int n)
48      : this() {
49      NParameter.Value = new IntValue(n);
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new SplitGroupManipulator(this, cloner);
54    }
55
56    public static void Apply(IRandom random, LinearLinkage lle, int n) {
57      var grouping = lle.GetGroups().ToList();
58      var groupsLargerOne = grouping.Select((v, i) => Tuple.Create(i, v))
59                                    .Where(x => x.Item2.Count > 1)
60                                    .ToDictionary(x => x.Item1, x => x.Item2);
61      if (groupsLargerOne.Count == 0) return;
62      var toRemove = new List<int>();
63
64      for (var i = 0; i < n; i++) {
65        var g = groupsLargerOne.Keys.SampleRandom(random);
66        var idx = random.Next(1, groupsLargerOne[g].Count);
67        // shuffle here to avoid a potential bias of grouping smaller and larger numbers together
68        var tmp = groupsLargerOne[g].Shuffle(random);
69        var before = new List<int>();
70        var after = new List<int>();
71        foreach (var t in tmp) {
72          if (idx > 0) before.Add(t);
73          else after.Add(t);
74          idx--;
75        }
76        if (before.Count > 1) groupsLargerOne[grouping.Count] = before;
77        grouping.Add(before);
78        if (after.Count > 1) groupsLargerOne[grouping.Count] = after;
79        grouping.Add(after);
80        toRemove.Add(g);
81        groupsLargerOne.Remove(g);
82        if (groupsLargerOne.Count == 0) break;
83      }
84      foreach (var r in toRemove.OrderByDescending(x => x))
85        grouping.RemoveAt(r);
86
87      lle.SetGroups(grouping);
88    }
89
90    protected override void Manipulate(IRandom random, LinearLinkage lle) {
91      var N = NParameter.ActualValue.Value;
92      Apply(random, lle, N);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.