1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace 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 | private MaxGroupSizeLinearLinkageCreator(bool deserializing) : base(deserializing) { }
|
---|
26 | private 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 | }
|
---|