Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.NK/3.3/InteractionInitializers/IncreasingBlockSizeInteractionsInitializer.cs @ 14923

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

#2526: Updated year of copyrights in license headers

File size: 3.2 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.NK {
31  [Item("IncreasingBlockSizeInteractionsInitializer", "Randomly assignes interactions across all bits but makes sure that different numbers of ineractions are applied to different bits.")]
32  [StorableClass]
33  public sealed class IncreasingBlockSizeInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
34    [StorableConstructor]
35    private IncreasingBlockSizeInteractionsInitializer(bool serializing) : base(serializing) { }
36    private IncreasingBlockSizeInteractionsInitializer(IncreasingBlockSizeInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
37    public IncreasingBlockSizeInteractionsInitializer() { }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new IncreasingBlockSizeInteractionsInitializer(this, cloner);
40    }
41
42    public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
43      Dictionary<int, int> bitInteractionCounts = new Dictionary<int, int>();
44      for (int i = 0; i < length; i++) {
45        int count = nInteractions * 2 * i / length;
46        if (count > 0)
47          bitInteractionCounts[i] = count;
48      }
49      List<BinaryVector> components = new List<BinaryVector>();
50      while (bitInteractionCounts.Count > 0) {
51        BinaryVector component = new BinaryVector(length);
52        for (int i = 0; i < nInteractions; i++) {
53          while (bitInteractionCounts.Count > 0) {
54            int bit = bitInteractionCounts.ElementAt(random.Next(bitInteractionCounts.Count)).Key;
55            if (bitInteractionCounts[bit]-- <= 0)
56              bitInteractionCounts.Remove(bit);
57            if (!component[bit]) {
58              component[bit] = true;
59              break;
60            }
61          }
62        }
63        components.Add(component);
64      }
65      BoolMatrix m = new BoolMatrix(length, components.Count);
66      foreach (var c in components.Select((v, j) => new { v, j })) {
67        for (int i = 0; i < c.v.Length; i++) {
68          m[i, c.j] = c.v[i];
69        }
70      }
71      return m;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.