[12285] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12285] | 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 |
|
---|
[12286] | 22 | using System;
|
---|
[12285] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[17097] | 29 | using HEAL.Attic;
|
---|
[12285] | 30 | using HeuristicLab.Random;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
[12396] | 33 | [Item("Split Group Manipulator", "Performs a maximum of N split operations on the groups. An already split group may be split again.")]
|
---|
[17097] | 34 | [StorableType("789781B8-0047-4880-ACAF-077A934AD320")]
|
---|
[12285] | 35 | public sealed class SplitGroupManipulator : LinearLinkageManipulator {
|
---|
| 36 |
|
---|
| 37 | public IValueLookupParameter<IntValue> NParameter {
|
---|
| 38 | get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [StorableConstructor]
|
---|
[17097] | 42 | private SplitGroupManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
[12285] | 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 |
|
---|
[12286] | 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>();
|
---|
[12285] | 63 |
|
---|
[12286] | 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--;
|
---|
[12285] | 75 | }
|
---|
[12286] | 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;
|
---|
[12285] | 83 | }
|
---|
[12286] | 84 | foreach (var r in toRemove.OrderByDescending(x => x))
|
---|
| 85 | grouping.RemoveAt(r);
|
---|
| 86 |
|
---|
[12285] | 87 | lle.SetGroups(grouping);
|
---|
| 88 | }
|
---|
[12286] | 89 |
|
---|
| 90 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
| 91 | var N = NParameter.ActualValue.Value;
|
---|
| 92 | Apply(random, lle, N);
|
---|
| 93 | }
|
---|
[12285] | 94 | }
|
---|
| 95 | }
|
---|