[12483] | 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("LimitedRandomInteractionsInitializer", "Randomly assignes interactions across bits in the vicinity of each other respecting the maximum distances if possible.")]
|
---|
| 16 | [StorableClass]
|
---|
| 17 | public class LimitedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
| 18 |
|
---|
| 19 | private class Bounds {
|
---|
| 20 | public readonly int Min;
|
---|
| 21 | public readonly int Max;
|
---|
| 22 | public Bounds(int min, int max) {
|
---|
| 23 | Min = Math.Min(min, max);
|
---|
| 24 | Max = Math.Max(min, max);
|
---|
| 25 | }
|
---|
| 26 | public int bounded(int n) {
|
---|
| 27 | return Math.Max(Min, Math.Min(Max, n));
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public ValueParameter<IntValue> MaximumDistanceParameter {
|
---|
| 32 | get { return (ValueParameter<IntValue>)Parameters["MaximumDistance"]; }
|
---|
| 33 | }
|
---|
| 34 | public ValueParameter<DoubleValue> MaximumDistanceRatioParameter {
|
---|
| 35 | get { return (ValueParameter<DoubleValue>)Parameters["MaximumDistanceRatio"]; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | private int MaximumDistance(int length, int nInteractions) {
|
---|
| 39 | int maxBitDist = MaximumDistanceParameter.Value.Value;
|
---|
| 40 | double maxDistRatio = MaximumDistanceRatioParameter.Value.Value;
|
---|
| 41 | maxBitDist = Math.Min(
|
---|
| 42 | maxBitDist == 0 ? length : maxBitDist,
|
---|
| 43 | maxDistRatio == 0 ? length : (int)Math.Round(maxDistRatio*length));
|
---|
| 44 | if (maxBitDist * 2 < nInteractions)
|
---|
| 45 | maxBitDist = (int)Math.Ceiling(0.5 * nInteractions);
|
---|
| 46 | return maxBitDist;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | [StorableConstructor]
|
---|
| 50 | protected LimitedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
| 51 | protected LimitedRandomInteractionsInitializer(LimitedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 52 | public LimitedRandomInteractionsInitializer() {
|
---|
| 53 | Parameters.Add(new ValueParameter<IntValue>("MaximumDistance", "Maximum distance of interactions in bits or 0 for unlimited"));
|
---|
| 54 | Parameters.Add(new ValueParameter<DoubleValue>("MaximumDistanceRatio", "Maximum distance of interactions as ratio of the total length or 0 for unlimited"));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 58 | return new LimitedRandomInteractionsInitializer(this, cloner);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | #region IInteractionInitializer Members
|
---|
| 62 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
| 63 | BoolMatrix m = new BoolMatrix(length, nComponents);
|
---|
| 64 | int maxBitDistance = MaximumDistance(length, nInteractions);
|
---|
| 65 | var minBounds = new Bounds(0, length-nInteractions);
|
---|
| 66 | var maxBounds = new Bounds(nInteractions, length-1);
|
---|
| 67 | for (int c = 0; c<m.Columns; c++) {
|
---|
| 68 | int min = minBounds.bounded(c-maxBitDistance);
|
---|
| 69 | int max = maxBounds.bounded(c+maxBitDistance);
|
---|
| 70 | var indices = Enumerable.Range(min, max-min).ToList();
|
---|
| 71 | indices.Remove(c);
|
---|
| 72 | m[c, c] = true;
|
---|
| 73 | while (indices.Count > nInteractions) {
|
---|
| 74 | indices.RemoveAt(random.Next(indices.Count));
|
---|
| 75 | }
|
---|
| 76 | foreach (var i in indices) {
|
---|
| 77 | m[i, c] = true;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | return m;
|
---|
| 81 | }
|
---|
| 82 | #endregion
|
---|
| 83 | }
|
---|
| 84 | }
|
---|