Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/InteractionInitializers/SortedRandomInteractionsInitializer.cs @ 7403

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

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

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