Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2990_VariableImpactBasedFeatureSelection/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Crossovers/GroupCrossover.cs @ 16612

Last change on this file since 16612 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HEAL.Attic;
26
27namespace HeuristicLab.Encodings.LinearLinkageEncoding {
28  [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.")]
29  [StorableType("E3B2FBDD-C923-4FE2-85C8-B2202EEF367F")]
30  public sealed class GroupCrossover : LinearLinkageCrossover {
31
32    [StorableConstructor]
33    private GroupCrossover(StorableConstructorFlag _) : base(_) { }
34    private GroupCrossover(GroupCrossover original, Cloner cloner) : base(original, cloner) { }
35    public GroupCrossover() { }
36
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new GroupCrossover(this, cloner);
39    }
40   
41    public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
42      var length = p1.Length;
43      var lleeP1 = p1.ToEndLinks();
44      var lleeP2 = p2.ToEndLinks();
45      var lleeChild = new int[length];
46      var isTransfered = new bool[length];
47
48      for (var i = p1.Length - 1; i >= 0; i--) {
49        lleeChild[i] = i;
50
51        // Step 1
52        var isEndP1 = p1[i] == i;
53        var isEndP2 = p2[i] == i;
54        if (isEndP1 & isEndP2 || (isEndP1 | isEndP2 && random.NextDouble() < 0.5)) {
55          isTransfered[i] = true;
56          continue;
57        }
58
59        // Step 2
60        var end1 = lleeP1[i];
61        var end2 = lleeP2[i];
62
63        if (isTransfered[end1] & isTransfered[end2]) {
64          var end = random.NextDouble() < 0.5 ? end1 : end2;
65          lleeChild[i] = end;
66        } else if (isTransfered[end1]) {
67          lleeChild[i] = end1;
68        } else if (isTransfered[end2]) {
69          lleeChild[i] = end2;
70        } else {
71          var next = random.NextDouble() < 0.5 ? p1[i] : p2[i];
72          var end = lleeChild[next];
73          lleeChild[i] = end;
74        }
75      }
76      return LinearLinkage.FromEndLinks(lleeChild);
77    }
78
79    protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
80      if (parents.Length != 2) throw new InvalidOperationException(Name + ": Can only cross exactly two parents.");
81      return Apply(random, parents[0], parents[1]);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.