1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
29 | [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.")]
|
---|
30 | [StorableType("B4806B37-B641-4750-B41F-FF5614156038")]
|
---|
31 | public sealed class GraftManipulator : LinearLinkageManipulator {
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | private GraftManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
35 | private GraftManipulator(GraftManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
36 | public GraftManipulator() { }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new GraftManipulator(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static void Apply(IRandom random, LinearLinkage lle) {
|
---|
43 | int tries = lle.Length;
|
---|
44 | var index = random.Next(lle.Length);
|
---|
45 | while (tries > 0 && lle[index] == index) {
|
---|
46 | index = random.Next(lle.Length);
|
---|
47 | tries--;
|
---|
48 | }
|
---|
49 | if (lle[index] != index) Apply(random, lle, index);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static void Apply(IRandom random, LinearLinkage lle, int index) {
|
---|
53 | var groups = lle.Select((val, idx) => Tuple.Create(idx, val))
|
---|
54 | .Where(x => x.Item1 == x.Item2)
|
---|
55 | .Select(x => x.Item2).ToList();
|
---|
56 | var z = groups.Count;
|
---|
57 |
|
---|
58 | if (random.NextDouble() < 0.5)
|
---|
59 | lle[index] = index; // divide the cluster into two
|
---|
60 | else {
|
---|
61 | var c = random.Next(z);
|
---|
62 | if (groups[c] > index)
|
---|
63 | lle[index] = groups[c]; // combine the portion with another class
|
---|
64 | else {
|
---|
65 | // combine the other class here
|
---|
66 | lle[groups[c]] = lle[index];
|
---|
67 | lle[index] = index;
|
---|
68 | }
|
---|
69 | lle.LinearizeTreeStructures();
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
74 | Apply(random, lle);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|