Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Crossovers/GroupCrossover.cs @ 17886

Last change on this file since 17886 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.1 KB
RevLine 
[12285]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12285]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;
[17097]25using HEAL.Attic;
[12285]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.")]
[17097]29  [StorableType("E3B2FBDD-C923-4FE2-85C8-B2202EEF367F")]
[12285]30  public sealed class GroupCrossover : LinearLinkageCrossover {
31
32    [StorableConstructor]
[17097]33    private GroupCrossover(StorableConstructorFlag _) : base(_) { }
[12285]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    }
[14475]40   
[12285]41    public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
42      var length = p1.Length;
[14475]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;
[12285]57        }
[14475]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;
[12288]70        } else {
[14475]71          var next = random.NextDouble() < 0.5 ? p1[i] : p2[i];
72          var end = lleeChild[next];
73          lleeChild[i] = end;
[12285]74        }
75      }
[14475]76      return LinearLinkage.FromEndLinks(lleeChild);
[12285]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.