[12566] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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.Linq;
|
---|
[8172] | 23 | using HeuristicLab.Common;
|
---|
[7128] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[8172] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7128] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.NK {
|
---|
[12569] | 32 | [Item("SortedRandomInteractionsInitializer", "Randomly assigns interactions across all bits and sorts components according to bit affinity.")]
|
---|
[7128] | 33 | [StorableClass]
|
---|
[12582] | 34 | public sealed class SortedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
[8172] | 35 | public IConstrainedValueParameter<IBinaryVectorComparer> ComparerParameter {
|
---|
| 36 | get { return (IConstrainedValueParameter<IBinaryVectorComparer>)Parameters["Comparer"]; }
|
---|
[7128] | 37 | }
|
---|
| 38 | public IBinaryVectorComparer Comparer {
|
---|
| 39 | get { return ComparerParameter.Value; }
|
---|
| 40 | }
|
---|
[8172] | 41 |
|
---|
[7128] | 42 | [StorableConstructor]
|
---|
[12582] | 43 | private SortedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
| 44 | private SortedRandomInteractionsInitializer(SortedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
[7128] | 45 | public SortedRandomInteractionsInitializer() {
|
---|
[12569] | 46 | Parameters.Add(new ConstrainedValueParameter<IBinaryVectorComparer>("Comparer", "Comparison for sorting of component functions."));
|
---|
| 47 | InitializeComparers();
|
---|
[7128] | 48 | }
|
---|
| 49 |
|
---|
[12569] | 50 | private void InitializeComparers() {
|
---|
[7128] | 51 | foreach (var comparer in ApplicationManager.Manager.GetInstances<IBinaryVectorComparer>())
|
---|
| 52 | ComparerParameter.ValidValues.Add(comparer);
|
---|
| 53 | ComparerParameter.Value = ComparerParameter.ValidValues.First(v => v is AverageBitBinaryVectorComparer);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 57 | return new SortedRandomInteractionsInitializer(this, cloner);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
| 61 | BinaryVector[] components = Enumerable.Range(0, nComponents).Select(i => new BinaryVector(length)).ToArray();
|
---|
| 62 | for (int c = 0; c < components.Length; c++) {
|
---|
| 63 | var indices = Enumerable.Range(0, length).ToList();
|
---|
| 64 | if (indices.Count > c) {
|
---|
| 65 | indices.RemoveAt(c);
|
---|
[8172] | 66 | components[c][c] = true;
|
---|
[7128] | 67 | }
|
---|
| 68 | while (indices.Count > nInteractions) {
|
---|
| 69 | indices.RemoveAt(random.Next(indices.Count));
|
---|
| 70 | }
|
---|
| 71 | foreach (var i in indices) {
|
---|
[8172] | 72 | components[c][i] = true;
|
---|
[7128] | 73 | }
|
---|
| 74 | }
|
---|
| 75 | BoolMatrix m = new BoolMatrix(length, nComponents);
|
---|
[8172] | 76 | foreach (var c in components.OrderBy(v => v, Comparer).Select((v, j) => new { v, j })) {
|
---|
| 77 | for (int i = 0; i < c.v.Length; i++) {
|
---|
[7128] | 78 | m[i, c.j] = c.v[i];
|
---|
[8172] | 79 | }
|
---|
[7128] | 80 | }
|
---|
| 81 | return m;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|