Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/GroupCrossover.cs @ 12288

Last change on this file since 12288 was 12288, checked in by abeham, 9 years ago

#2319:

  • Changed encoding to represent linkages in LLE (as opposed to LLE-e)
  • Added GraftManipulator
  • Added repair procedure
  • Improved performance of some crossovers
File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.LinearLinkageEncoding {
30  [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.")]
31  [StorableClass]
32  public sealed class GroupCrossover : LinearLinkageCrossover {
33
34    [StorableConstructor]
35    private GroupCrossover(bool deserializing) : base(deserializing) { }
36    private GroupCrossover(GroupCrossover original, Cloner cloner) : base(original, cloner) { }
37    public GroupCrossover() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new GroupCrossover(this, cloner);
41    }
42
43    public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
44      var length = p1.Length;
45      var child = new LinearLinkage(length);
46      var endNodes = new HashSet<int>();
47      for (var i = 0; i < length; i++) {
48        if ((p1[i] == i && p2[i] == i)
49          || ((p1[i] == i || p2[i] == i) && random.NextDouble() < 0.5)) {
50          child[i] = i;
51          endNodes.Add(i);
52        }
53      }
54      for (var i = 0; i < length; i++) {
55        if (endNodes.Contains(i)) continue;
56        var p1End = endNodes.Contains(p1[i]);
57        var p2End = endNodes.Contains(p2[i]);
58        if ((p1End && p2End) || (!p1End && !p2End)) {
59          child[i] = random.NextDouble() < 0.5 ? p1[i] : p2[i];
60        } else if (p1End) {
61          child[i] = p1[i];
62        } else {
63          child[i] = p2[i];
64        }
65      }
66      child.LinearizeTreeStructures();
67      return child;
68    }
69
70    protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
71      if (parents.Length != 2) throw new InvalidOperationException(Name + ": Can only cross exactly two parents.");
72      return Apply(random, parents[0], parents[1]);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.