Free cookie consent management tool by TermsFeed Policy Generator

source: branches/dataset-ids-2695/HeuristicLab.Problems.NK/3.3/InteractionInitializers/SortedRandomInteractionsInitializer.cs @ 15418

Last change on this file since 15418 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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