[12566] | 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 |
|
---|
| 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 {
|
---|
| 32 |
|
---|
| 33 | [Item("SortedRandomInteractionsInitializer", "Randomly assigned interactions across all bits and sorts components according to bit affinity.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class SortedRandomInteractionsInitializer : ParameterizedNamedItem, IInteractionInitializer {
|
---|
| 36 |
|
---|
[8172] | 37 | public IConstrainedValueParameter<IBinaryVectorComparer> ComparerParameter {
|
---|
| 38 | get { return (IConstrainedValueParameter<IBinaryVectorComparer>)Parameters["Comparer"]; }
|
---|
[7128] | 39 | }
|
---|
| 40 |
|
---|
| 41 | public IBinaryVectorComparer Comparer {
|
---|
| 42 | get { return ComparerParameter.Value; }
|
---|
| 43 | }
|
---|
[8172] | 44 |
|
---|
[7128] | 45 | [StorableConstructor]
|
---|
| 46 | protected SortedRandomInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
| 47 | protected SortedRandomInteractionsInitializer(SortedRandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 48 | public SortedRandomInteractionsInitializer() {
|
---|
| 49 | AddComparerParameter();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | private void AddComparerParameter() {
|
---|
| 53 | Parameters.Add(new ConstrainedValueParameter<IBinaryVectorComparer>("Comparer", "Comparison for sorting of component functions"));
|
---|
| 54 | foreach (var comparer in ApplicationManager.Manager.GetInstances<IBinaryVectorComparer>())
|
---|
| 55 | ComparerParameter.ValidValues.Add(comparer);
|
---|
| 56 | ComparerParameter.Value = ComparerParameter.ValidValues.First(v => v is AverageBitBinaryVectorComparer);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | return new SortedRandomInteractionsInitializer(this, cloner);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | try {
|
---|
| 66 | IBinaryVectorComparer comparer = Comparer;
|
---|
[8172] | 67 | }
|
---|
| 68 | catch {
|
---|
[7128] | 69 | Parameters.Remove("Comparer");
|
---|
| 70 | AddComparerParameter();
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | #region IInteractionInitializer Members
|
---|
| 75 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
| 76 | BinaryVector[] components = Enumerable.Range(0, nComponents).Select(i => new BinaryVector(length)).ToArray();
|
---|
| 77 | for (int c = 0; c < components.Length; c++) {
|
---|
| 78 | var indices = Enumerable.Range(0, length).ToList();
|
---|
| 79 | if (indices.Count > c) {
|
---|
| 80 | indices.RemoveAt(c);
|
---|
[8172] | 81 | components[c][c] = true;
|
---|
[7128] | 82 | }
|
---|
| 83 | while (indices.Count > nInteractions) {
|
---|
| 84 | indices.RemoveAt(random.Next(indices.Count));
|
---|
| 85 | }
|
---|
| 86 | foreach (var i in indices) {
|
---|
[8172] | 87 | components[c][i] = true;
|
---|
[7128] | 88 | }
|
---|
| 89 | }
|
---|
| 90 | BoolMatrix m = new BoolMatrix(length, nComponents);
|
---|
[8172] | 91 | foreach (var c in components.OrderBy(v => v, Comparer).Select((v, j) => new { v, j })) {
|
---|
| 92 | for (int i = 0; i < c.v.Length; i++) {
|
---|
[7128] | 93 | m[i, c.j] = c.v[i];
|
---|
[8172] | 94 | }
|
---|
[7128] | 95 | }
|
---|
| 96 | return m;
|
---|
| 97 | }
|
---|
| 98 | #endregion
|
---|
| 99 | }
|
---|
| 100 | }
|
---|