[12566] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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 |
|
---|
[7128] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[12566] | 24 | using HeuristicLab.Common;
|
---|
[7128] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[12566] | 27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[17097] | 28 | using HEAL.Attic;
|
---|
[7128] | 29 |
|
---|
| 30 | namespace 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.")]
|
---|
[17097] | 32 | [StorableType("E4AAEDCA-6A90-45AE-AFD5-B200A50ACAB8")]
|
---|
[12592] | 33 | public sealed class IncreasingBlockSizeInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
[7128] | 34 | [StorableConstructor]
|
---|
[17097] | 35 | private IncreasingBlockSizeInteractionsInitializer(StorableConstructorFlag _) : base(_) { }
|
---|
[12582] | 36 | private IncreasingBlockSizeInteractionsInitializer(IncreasingBlockSizeInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 37 | public IncreasingBlockSizeInteractionsInitializer() { }
|
---|
[7128] | 38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 39 | return new IncreasingBlockSizeInteractionsInitializer(this, cloner);
|
---|
[12566] | 40 | }
|
---|
[7128] | 41 |
|
---|
| 42 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
| 43 | Dictionary<int, int> bitInteractionCounts = new Dictionary<int, int>();
|
---|
[12566] | 44 | for (int i = 0; i < length; i++) {
|
---|
| 45 | int count = nInteractions * 2 * i / length;
|
---|
[7128] | 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);
|
---|
[12566] | 52 | for (int i = 0; i < nInteractions; i++) {
|
---|
[7128] | 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 | }
|
---|
[12566] | 61 | }
|
---|
[7128] | 62 | }
|
---|
| 63 | components.Add(component);
|
---|
[12566] | 64 | }
|
---|
[7128] | 65 | BoolMatrix m = new BoolMatrix(length, components.Count);
|
---|
[12566] | 66 | foreach (var c in components.Select((v, j) => new { v, j })) {
|
---|
| 67 | for (int i = 0; i < c.v.Length; i++) {
|
---|
[7128] | 68 | m[i, c.j] = c.v[i];
|
---|
[12566] | 69 | }
|
---|
[7128] | 70 | }
|
---|
| 71 | return m;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|