Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Creators/MaxGroupSizeLinearLinkageCreator.cs @ 16189

Last change on this file since 16189 was 14479, checked in by sraggl, 8 years ago

#2666
Fixed privacy issue indicated by tests

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