1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
30 | [Item("Merge Group Manipulator", "Performs a maximum of N merge operations on the groups. An already merged group may be merged again.")]
|
---|
31 | [StorableClass]
|
---|
32 | public sealed class MergeGroupManipulator : LinearLinkageManipulator {
|
---|
33 |
|
---|
34 | public IValueLookupParameter<IntValue> NParameter {
|
---|
35 | get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [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)));
|
---|
43 | }
|
---|
44 | public MergeGroupManipulator(int n)
|
---|
45 | : this() {
|
---|
46 | NParameter.Value = new IntValue(n);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new MergeGroupManipulator(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
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
|
---|
56 |
|
---|
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 | }
|
---|
65 |
|
---|
66 | lle.SetGroups(grouping);
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
70 | var N = NParameter.ActualValue.Value;
|
---|
71 | Apply(random, lle, N);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|