Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2306 fixed typos and some cosmetic changes

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