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("SortedRandomInteractionsInitializer", "Randomly assigned interactions across all bits and sorts components according to bit affinity.")]
|
---|
16 | [StorableClass]
|
---|
17 | public class SortedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
18 |
|
---|
19 | public ConstrainedValueParameter<IBinaryVectorComparer> ComparerParameter {
|
---|
20 | get { return (ConstrainedValueParameter<IBinaryVectorComparer>)Parameters["Comparer"]; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public IBinaryVectorComparer Comparer {
|
---|
24 | get { return ComparerParameter.Value; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | [StorableConstructor]
|
---|
28 | protected SortedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
29 | protected SortedRandomInteractionsInitializer(SortedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
30 | public SortedRandomInteractionsInitializer() {
|
---|
31 | AddComparerParameter();
|
---|
32 | }
|
---|
33 |
|
---|
34 | private void AddComparerParameter() {
|
---|
35 | Parameters.Add(new ConstrainedValueParameter<IBinaryVectorComparer>("Comparer", "Comparison for sorting of component functions"));
|
---|
36 | foreach (var comparer in ApplicationManager.Manager.GetInstances<IBinaryVectorComparer>())
|
---|
37 | ComparerParameter.ValidValues.Add(comparer);
|
---|
38 | ComparerParameter.Value = ComparerParameter.ValidValues.First(v => v is AverageBitBinaryVectorComparer);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new SortedRandomInteractionsInitializer(this, cloner);
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableHook(HookType.AfterDeserialization)]
|
---|
46 | private void AfterDeserialization() {
|
---|
47 | try {
|
---|
48 | IBinaryVectorComparer comparer = Comparer;
|
---|
49 | } catch {
|
---|
50 | Parameters.Remove("Comparer");
|
---|
51 | AddComparerParameter();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | #region IInteractionInitializer Members
|
---|
56 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
57 | BinaryVector[] components = Enumerable.Range(0, nComponents).Select(i => new BinaryVector(length)).ToArray();
|
---|
58 | for (int c = 0; c < components.Length; c++) {
|
---|
59 | var indices = Enumerable.Range(0, length).ToList();
|
---|
60 | if (indices.Count > c) {
|
---|
61 | indices.RemoveAt(c);
|
---|
62 | components[c][c] = true;
|
---|
63 | }
|
---|
64 | while (indices.Count > nInteractions) {
|
---|
65 | indices.RemoveAt(random.Next(indices.Count));
|
---|
66 | }
|
---|
67 | foreach (var i in indices) {
|
---|
68 | components[c][i] = true;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | BoolMatrix m = new BoolMatrix(length, nComponents);
|
---|
72 | foreach (var c in components.OrderBy(v => v, Comparer).Select((v, j) => new {v, j})) {
|
---|
73 | for (int i = 0; i<c.v.Length; i++) {
|
---|
74 | m[i, c.j] = c.v[i];
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return m;
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 | }
|
---|
81 | }
|
---|