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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
29 | [Item("Group Crossover", "The Group Crossover is implemented as described in Korkmaz, E.E. 2010. Multi-objective Genetic Algorithms for grouping problems. Applied Intelligence 33(2), pp. 179-192.")]
|
---|
30 | [StorableClass]
|
---|
31 | public sealed class GroupCrossover : LinearLinkageCrossover {
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | private GroupCrossover(bool deserializing) : base(deserializing) { }
|
---|
35 | private GroupCrossover(GroupCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
36 | public GroupCrossover() { }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new GroupCrossover(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
|
---|
43 | var length = p1.Length;
|
---|
44 | var child = new LinearLinkage(length);
|
---|
45 | var endNodes = new HashSet<int>();
|
---|
46 | for (var i = 0; i < length; i++) {
|
---|
47 | if ((p1[i] == i && p2[i] == i)
|
---|
48 | || ((p1[i] == i || p2[i] == i) && random.NextDouble() < 0.5)) {
|
---|
49 | child[i] = i;
|
---|
50 | endNodes.Add(i);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | for (var i = 0; i < length; i++) {
|
---|
54 | if (endNodes.Contains(i)) continue;
|
---|
55 | var p1End = endNodes.Contains(p1[i]);
|
---|
56 | var p2End = endNodes.Contains(p2[i]);
|
---|
57 | if ((p1End && p2End) || (!p1End && !p2End)) {
|
---|
58 | child[i] = random.NextDouble() < 0.5 ? p1[i] : p2[i];
|
---|
59 | } else if (p1End) {
|
---|
60 | child[i] = p1[i];
|
---|
61 | } else {
|
---|
62 | child[i] = p2[i];
|
---|
63 | }
|
---|
64 | }
|
---|
65 | child.LinearizeTreeStructures();
|
---|
66 | return child;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
|
---|
70 | if (parents.Length != 2) throw new InvalidOperationException(Name + ": Can only cross exactly two parents.");
|
---|
71 | return Apply(random, parents[0], parents[1]);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|