Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Creators/MaxGroupSizeLinearLinkageCreator.cs @ 18066

Last change on this file since 18066 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 2.1 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Parameters;
6using HEAL.Attic;
7
8namespace HeuristicLab.Encodings.LinearLinkageEncoding.Creators {
9  [Item("Max Group-size Linear Linkage Creator", "Creates a random linear linkage LLE encoded solution with a given maximum number of items per group.")]
10  [StorableType("5C9DEA79-60CC-44A5-B70E-FC11A588C307")]
11  public class MaxGroupSizeLinearLinkageCreator: LinearLinkageCreator {
12
13    public IValueLookupParameter<IntValue> MaxGroupSizeParameter {
14      get { return (IValueLookupParameter<IntValue>)Parameters["MaxGroupSize"]; }
15    }
16
17    public IntValue MaxGroupSize {
18      get { return MaxGroupSizeParameter.Value; }
19      set { MaxGroupSizeParameter.Value = value; }
20    }
21
22    [StorableConstructor]
23    protected MaxGroupSizeLinearLinkageCreator(StorableConstructorFlag _) : base(_) { }
24    protected MaxGroupSizeLinearLinkageCreator(MaxGroupSizeLinearLinkageCreator original, Cloner cloner) : base(original, cloner) { }
25
26    public MaxGroupSizeLinearLinkageCreator() {
27      Parameters.Add(new ValueLookupParameter<IntValue>("MaxGroupSize", "The maximum number items in every group.", new IntValue(4)));
28    }
29
30    public override IDeepCloneable Clone(Cloner cloner) {
31      return new MaxGroupSizeLinearLinkageCreator(this, cloner);
32    }
33
34    public static LinearLinkage Apply(IRandom random, int length, int maxGroupSize) {
35      var groups = Enumerable.Range(0, length).Select(x => new { Item = x, Group = random.Next(length)})
36        .GroupBy(x => x.Group)
37        .SelectMany(x =>
38           x.Select((y, i) => new {SubGroup = i%maxGroupSize, y.Item})
39            .GroupBy(y=>y.SubGroup)
40            .Select(y=>
41              y.Select(z=>z.Item).ToList()));
42      return LinearLinkage.FromGroups(length, groups);
43    }
44
45    protected override LinearLinkage Create(IRandom random, int length) {
46      var maxGroupSize = MaxGroupSizeParameter.ActualValue.Value;
47      return Apply(random, length, maxGroupSize);
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.