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