[12566] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16140] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12566] | 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 |
|
---|
| 22 | using System;
|
---|
[7128] | 23 | using System.Linq;
|
---|
[12566] | 24 | using HeuristicLab.Common;
|
---|
[7128] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[12566] | 27 | using HeuristicLab.Parameters;
|
---|
[7128] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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]
|
---|
[12582] | 33 | public sealed class LimitedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
[7128] | 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 | }
|
---|
[12569] | 41 | public int Bounded(int n) {
|
---|
[7128] | 42 | return Math.Max(Min, Math.Min(Max, n));
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[12569] | 46 | public IValueParameter<IntValue> MaximumDistanceParameter {
|
---|
| 47 | get { return (IValueParameter<IntValue>)Parameters["MaximumDistance"]; }
|
---|
[7128] | 48 | }
|
---|
[12569] | 49 | public IValueParameter<DoubleValue> MaximumDistanceRatioParameter {
|
---|
| 50 | get { return (IValueParameter<DoubleValue>)Parameters["MaximumDistanceRatio"]; }
|
---|
[12566] | 51 | }
|
---|
[7128] | 52 |
|
---|
[12582] | 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 |
|
---|
[7128] | 65 | private int MaximumDistance(int length, int nInteractions) {
|
---|
| 66 | int maxBitDist = MaximumDistanceParameter.Value.Value;
|
---|
| 67 | double maxDistRatio = MaximumDistanceRatioParameter.Value.Value;
|
---|
[12566] | 68 | maxBitDist = Math.Min(
|
---|
[7128] | 69 | maxBitDist == 0 ? length : maxBitDist,
|
---|
[12569] | 70 | maxDistRatio.IsAlmost(0.0) ? length : (int)Math.Round(maxDistRatio * length));
|
---|
[7128] | 71 | if (maxBitDist * 2 < nInteractions)
|
---|
| 72 | maxBitDist = (int)Math.Ceiling(0.5 * nInteractions);
|
---|
| 73 | return maxBitDist;
|
---|
[12566] | 74 | }
|
---|
| 75 |
|
---|
[7128] | 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);
|
---|
[12566] | 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++) {
|
---|
[12569] | 82 | int min = minBounds.Bounded(c - maxBitDistance);
|
---|
| 83 | int max = maxBounds.Bounded(c + maxBitDistance);
|
---|
[12566] | 84 | var indices = Enumerable.Range(min, max - min).ToList();
|
---|
[7128] | 85 | indices.Remove(c);
|
---|
[12566] | 86 | m[c, c] = true;
|
---|
[7128] | 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 | }
|
---|
[12566] | 94 | return m;
|
---|
[7128] | 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|