Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12569 was 12569, checked in by ascheibe, 10 years ago

#2306 fixed typos and some cosmetic changes

File size: 3.6 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 assigns interactions across all bits and sorts components according to bit affinity.")]
34  [StorableClass]
35  public class SortedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
36    public IConstrainedValueParameter<IBinaryVectorComparer> ComparerParameter {
37      get { return (IConstrainedValueParameter<IBinaryVectorComparer>)Parameters["Comparer"]; }
38    }
39    public IBinaryVectorComparer Comparer {
40      get { return ComparerParameter.Value; }
41    }
42
43    [StorableConstructor]
44    protected SortedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
45    protected SortedRandomInteractionsInitializer(SortedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
46    public SortedRandomInteractionsInitializer() {
47      Parameters.Add(new ConstrainedValueParameter<IBinaryVectorComparer>("Comparer", "Comparison for sorting of component functions."));
48      InitializeComparers();
49    }
50
51    private void InitializeComparers() {
52      foreach (var comparer in ApplicationManager.Manager.GetInstances<IBinaryVectorComparer>())
53        ComparerParameter.ValidValues.Add(comparer);
54      ComparerParameter.Value = ComparerParameter.ValidValues.First(v => v is AverageBitBinaryVectorComparer);
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new SortedRandomInteractionsInitializer(this, cloner);
59    }
60
61    #region IInteractionInitializer Members
62    public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
63      BinaryVector[] components = Enumerable.Range(0, nComponents).Select(i => new BinaryVector(length)).ToArray();
64      for (int c = 0; c < components.Length; c++) {
65        var indices = Enumerable.Range(0, length).ToList();
66        if (indices.Count > c) {
67          indices.RemoveAt(c);
68          components[c][c] = true;
69        }
70        while (indices.Count > nInteractions) {
71          indices.RemoveAt(random.Next(indices.Count));
72        }
73        foreach (var i in indices) {
74          components[c][i] = true;
75        }
76      }
77      BoolMatrix m = new BoolMatrix(length, nComponents);
78      foreach (var c in components.OrderBy(v => v, Comparer).Select((v, j) => new { v, j })) {
79        for (int i = 0; i < c.v.Length; i++) {
80          m[i, c.j] = c.v[i];
81        }
82      }
83      return m;
84    }
85    #endregion
86  }
87}
Note: See TracBrowser for help on using the repository browser.