Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12566 was 12566, checked in by ascheibe, 9 years ago

#2306

  • removed unused usings
  • renamed plugin.cs file
  • fixed prebuild event
  • fixed plugin dependencies
  • added license headers
  • updated assemblyinfo
File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Problems.NK {
32
33  [Item("SortedRandomInteractionsInitializer", "Randomly assigned interactions across all bits and sorts components according to bit affinity.")]
34  [StorableClass]
35  public class SortedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
36
37    public IConstrainedValueParameter<IBinaryVectorComparer> ComparerParameter {
38      get { return (IConstrainedValueParameter<IBinaryVectorComparer>)Parameters["Comparer"]; }
39    }
40
41    public IBinaryVectorComparer Comparer {
42      get { return ComparerParameter.Value; }
43    }
44
45    [StorableConstructor]
46    protected SortedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
47    protected SortedRandomInteractionsInitializer(SortedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
48    public SortedRandomInteractionsInitializer() {
49      AddComparerParameter();
50    }
51
52    private void AddComparerParameter() {
53      Parameters.Add(new ConstrainedValueParameter<IBinaryVectorComparer>("Comparer", "Comparison for sorting of component functions"));
54      foreach (var comparer in ApplicationManager.Manager.GetInstances<IBinaryVectorComparer>())
55        ComparerParameter.ValidValues.Add(comparer);
56      ComparerParameter.Value = ComparerParameter.ValidValues.First(v => v is AverageBitBinaryVectorComparer);
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new SortedRandomInteractionsInitializer(this, cloner);
61    }
62
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() {
65      try {
66        IBinaryVectorComparer comparer = Comparer;
67      }
68      catch {
69        Parameters.Remove("Comparer");
70        AddComparerParameter();
71      }
72    }
73
74    #region IInteractionInitializer Members
75    public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
76      BinaryVector[] components = Enumerable.Range(0, nComponents).Select(i => new BinaryVector(length)).ToArray();
77      for (int c = 0; c < components.Length; c++) {
78        var indices = Enumerable.Range(0, length).ToList();
79        if (indices.Count > c) {
80          indices.RemoveAt(c);
81          components[c][c] = true;
82        }
83        while (indices.Count > nInteractions) {
84          indices.RemoveAt(random.Next(indices.Count));
85        }
86        foreach (var i in indices) {
87          components[c][i] = true;
88        }
89      }
90      BoolMatrix m = new BoolMatrix(length, nComponents);
91      foreach (var c in components.OrderBy(v => v, Comparer).Select((v, j) => new { v, j })) {
92        for (int i = 0; i < c.v.Length; i++) {
93          m[i, c.j] = c.v[i];
94        }
95      }
96      return m;
97    }
98    #endregion
99  }
100}
Note: See TracBrowser for help on using the repository browser.