Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.NK/3.3/InteractionInitializers/LimitedRandomInteractionsInitializer.cs @ 14429

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

#2526: Updated year of copyrights in license headers

File size: 4.1 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;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.NK {
31  [Item("LimitedRandomInteractionsInitializer", "Randomly assignes interactions across bits in the vicinity of each other respecting the maximum distances if possible.")]
32  [StorableClass]
33  public sealed class LimitedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
34    private class Bounds {
35      public readonly int Min;
36      public readonly int Max;
37      public Bounds(int min, int max) {
38        Min = Math.Min(min, max);
39        Max = Math.Max(min, max);
40      }
41      public int Bounded(int n) {
42        return Math.Max(Min, Math.Min(Max, n));
43      }
44    }
45
46    public IValueParameter<IntValue> MaximumDistanceParameter {
47      get { return (IValueParameter<IntValue>)Parameters["MaximumDistance"]; }
48    }
49    public IValueParameter<DoubleValue> MaximumDistanceRatioParameter {
50      get { return (IValueParameter<DoubleValue>)Parameters["MaximumDistanceRatio"]; }
51    }
52
53    [StorableConstructor]
54    private LimitedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
55    private LimitedRandomInteractionsInitializer(LimitedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
56    public LimitedRandomInteractionsInitializer() {
57      Parameters.Add(new ValueParameter<IntValue>("MaximumDistance", "Maximum distance of interactions in bits or 0 for unlimited"));
58      Parameters.Add(new ValueParameter<DoubleValue>("MaximumDistanceRatio", "Maximum distance of interactions as ratio of the total length or 0 for unlimited"));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new LimitedRandomInteractionsInitializer(this, cloner);
63    }
64
65    private int MaximumDistance(int length, int nInteractions) {
66      int maxBitDist = MaximumDistanceParameter.Value.Value;
67      double maxDistRatio = MaximumDistanceRatioParameter.Value.Value;
68      maxBitDist = Math.Min(
69        maxBitDist == 0 ? length : maxBitDist,
70        maxDistRatio.IsAlmost(0.0) ? length : (int)Math.Round(maxDistRatio * length));
71      if (maxBitDist * 2 < nInteractions)
72        maxBitDist = (int)Math.Ceiling(0.5 * nInteractions);
73      return maxBitDist;
74    }
75
76    public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
77      BoolMatrix m = new BoolMatrix(length, nComponents);
78      int maxBitDistance = MaximumDistance(length, nInteractions);
79      var minBounds = new Bounds(0, length - nInteractions);
80      var maxBounds = new Bounds(nInteractions, length - 1);
81      for (int c = 0; c < m.Columns; c++) {
82        int min = minBounds.Bounded(c - maxBitDistance);
83        int max = maxBounds.Bounded(c + maxBitDistance);
84        var indices = Enumerable.Range(min, max - min).ToList();
85        indices.Remove(c);
86        m[c, c] = true;
87        while (indices.Count > nInteractions) {
88          indices.RemoveAt(random.Next(indices.Count));
89        }
90        foreach (var i in indices) {
91          m[i, c] = true;
92        }
93      }
94      return m;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.