1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
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.")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class GraftManipulator : LinearLinkageManipulator {
|
---|
34 |
|
---|
35 | public IValueLookupParameter<IntValue> MaxGroupsParameter {
|
---|
36 | get { return (IValueLookupParameter<IntValue>)Parameters["MaxGroups"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
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)));
|
---|
44 | }
|
---|
45 | public GraftManipulator(int maxGroups)
|
---|
46 | : this() {
|
---|
47 | MaxGroupsParameter.Value = new IntValue(maxGroups);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new GraftManipulator(this, cloner);
|
---|
52 | }
|
---|
53 |
|
---|
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 | }
|
---|
63 |
|
---|
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();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
86 | var maxGroups = MaxGroupsParameter.ActualValue.Value;
|
---|
87 | Apply(random, lle, maxGroups <= 0 ? int.MaxValue : maxGroups);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|