Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/GreedyPartitionCrossover.cs @ 17514

Last change on this file since 17514 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.Encodings.LinearLinkageEncoding {
31  [Item("Greedy Partition Crossover", "The Greedy Partition Crossover (GPX) is implemented as described in Ülker, Ö., Özcan, E., Korkmaz, E. E. 2007. Linear linkage encoding in grouping problems: applications on graph coloring and timetabling. In Practice and Theory of Automated Timetabling VI, pp. 347-363. Springer Berlin Heidelberg.")]
32  [StorableClass]
33  public sealed class GreedyPartitionCrossover : LinearLinkageCrossover {
34
35    [StorableConstructor]
36    private GreedyPartitionCrossover(bool deserializing) : base(deserializing) { }
37    private GreedyPartitionCrossover(GreedyPartitionCrossover original, Cloner cloner) : base(original, cloner) { }
38    public GreedyPartitionCrossover() { }
39
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new GreedyPartitionCrossover(this, cloner);
42    }
43
44    public static LinearLinkage Apply(IRandom random, ItemArray<LinearLinkage> parents) {
45      var len = parents[0].Length;
46
47      var child = new LinearLinkage(len);
48      var childGroup = new List<HashSet<int>>();
49      var currentParent = random.Next(parents.Length);
50      var groups = parents.Select(x => x.GetGroups().Select(y => new HashSet<int>(y)).ToList()).ToList();
51      bool remaining;
52      do {
53        var maxGroup = groups[currentParent].Select((v, i) => Tuple.Create(i, v))
54          .MaxItems(x => x.Item2.Count)
55          .SampleRandom(random).Item1;
56        var group = groups[currentParent][maxGroup];
57        groups[currentParent].RemoveAt(maxGroup);
58        childGroup.Add(group);
59
60        remaining = false;
61        for (var p = 0; p < groups.Count; p++) {
62          for (var j = 0; j < groups[p].Count; j++) {
63            foreach (var elem in group) groups[p][j].Remove(elem);
64            if (!remaining && groups[p][j].Count > 0) remaining = true;
65          }
66        }
67
68        currentParent = (currentParent + 1) % parents.Length;
69      } while (remaining);
70
71      child.SetGroups(childGroup);
72      return child;
73    }
74
75    protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
76      return Apply(random, parents);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.