1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 | using HeuristicLab.PluginInfrastructure;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.NK {
|
---|
14 |
|
---|
15 | [Item("IncreasingBlockSizeInteractionsInitializer", "Randomly assignes interactions across all bits but makes sure that different numbers of ineractions are applied to different bits.")]
|
---|
16 | [StorableClass]
|
---|
17 | public class IncreasingBlockSizeInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
18 |
|
---|
19 | [StorableConstructor]
|
---|
20 | protected IncreasingBlockSizeInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
21 | protected IncreasingBlockSizeInteractionsInitializer(IncreasingBlockSizeInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
22 | public IncreasingBlockSizeInteractionsInitializer() {
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
27 | return new IncreasingBlockSizeInteractionsInitializer(this, cloner);
|
---|
28 | }
|
---|
29 |
|
---|
30 | #region IInteractionInitializer Members
|
---|
31 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
32 | Dictionary<int, int> bitInteractionCounts = new Dictionary<int, int>();
|
---|
33 | for (int i = 0; i<length; i++) {
|
---|
34 | int count = nInteractions*2*i/length;
|
---|
35 | if (count > 0)
|
---|
36 | bitInteractionCounts[i] = count;
|
---|
37 | }
|
---|
38 | List<BinaryVector> components = new List<BinaryVector>();
|
---|
39 | while (bitInteractionCounts.Count > 0) {
|
---|
40 | BinaryVector component = new BinaryVector(length);
|
---|
41 | for (int i = 0; i<nInteractions; i++) {
|
---|
42 | while (bitInteractionCounts.Count > 0) {
|
---|
43 | int bit = bitInteractionCounts.ElementAt(random.Next(bitInteractionCounts.Count)).Key;
|
---|
44 | if (bitInteractionCounts[bit]-- <= 0)
|
---|
45 | bitInteractionCounts.Remove(bit);
|
---|
46 | if (!component[bit]) {
|
---|
47 | component[bit] = true;
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | components.Add(component);
|
---|
53 | }
|
---|
54 | BoolMatrix m = new BoolMatrix(length, components.Count);
|
---|
55 | foreach (var c in components.Select((v, j) => new {v, j})) {
|
---|
56 | for (int i = 0; i<c.v.Length; i++) {
|
---|
57 | m[i, c.j] = c.v[i];
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return m;
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 | }
|
---|
64 | }
|
---|