1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Parameters;
|
---|
6 | using HEAL.Attic;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|