Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Crossovers/GroupCrossover.cs @ 17709

Last change on this file since 17709 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace 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 lleeP1 = p1.ToEndLinks();
45      var lleeP2 = p2.ToEndLinks();
46      var lleeChild = new int[length];
47      var isTransfered = new bool[length];
48
49      for (var i = p1.Length - 1; i >= 0; i--) {
50        lleeChild[i] = i;
51
52        // Step 1
53        var isEndP1 = p1[i] == i;
54        var isEndP2 = p2[i] == i;
55        if (isEndP1 & isEndP2 || (isEndP1 | isEndP2 && random.NextDouble() < 0.5)) {
56          isTransfered[i] = true;
57          continue;
58        }
59
60        // Step 2
61        var end1 = lleeP1[i];
62        var end2 = lleeP2[i];
63
64        if (isTransfered[end1] & isTransfered[end2]) {
65          var end = random.NextDouble() < 0.5 ? end1 : end2;
66          lleeChild[i] = end;
67        } else if (isTransfered[end1]) {
68          lleeChild[i] = end1;
69        } else if (isTransfered[end2]) {
70          lleeChild[i] = end2;
71        } else {
72          var next = random.NextDouble() < 0.5 ? p1[i] : p2[i];
73          var end = lleeChild[next];
74          lleeChild[i] = end;
75        }
76      }
77      return LinearLinkage.FromEndLinks(lleeChild);
78    }
79
80    protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
81      if (parents.Length != 2) throw new InvalidOperationException(Name + ": Can only cross exactly two parents.");
82      return Apply(random, parents[0], parents[1]);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.