Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/InteractionInitializers/LimitedRandomInteractionsInitializer.cs @ 11316

Last change on this file since 11316 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Common;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Encodings.BinaryVectorEncoding;
10using HeuristicLab.Parameters;
11using HeuristicLab.PluginInfrastructure;
12
13namespace 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}
Note: See TracBrowser for help on using the repository browser.