Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Crossovers/GreedyPartitionCrossover.cs @ 14783

Last change on this file since 14783 was 14475, checked in by sraggl, 8 years ago

#2666
Updated LinearLinkageEncoding by:

  • Speeding up GroupCrossover and SetGroups and GetGroups
  • making Constructor private adding static initializer Methods
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      var childGroup = new List<HashSet<int>>();
47      var currentParent = random.Next(parents.Length);
48      var groups = parents.Select(x => x.GetGroups().Select(y => new HashSet<int>(y)).ToList()).ToList();
49      bool remaining;
50      do {
51        var maxGroup = groups[currentParent].Select((v, i) => Tuple.Create(i, v))
52          .MaxItems(x => x.Item2.Count)
53          .SampleRandom(random).Item1;
54        var group = groups[currentParent][maxGroup];
55        groups[currentParent].RemoveAt(maxGroup);
56        childGroup.Add(group);
57
58        remaining = false;
59        for (var p = 0; p < groups.Count; p++) {
60          for (var j = 0; j < groups[p].Count; j++) {
61            foreach (var elem in group) groups[p][j].Remove(elem);
62            if (!remaining && groups[p][j].Count > 0) remaining = true;
63          }
64        }
65
66        currentParent = (currentParent + 1) % parents.Length;
67      } while (remaining);
68
69      return LinearLinkage.FromGroups(len, childGroup);
70    }
71
72    protected override LinearLinkage Cross(IRandom random, ItemArray<LinearLinkage> parents) {
73      return Apply(random, parents);
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.