Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/InteractionInitializers/IncreasingBlockSizeInteractionsInitializer.cs @ 11571

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

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

File size: 2.6 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("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}
Note: See TracBrowser for help on using the repository browser.