1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.NK {
|
---|
11 |
|
---|
12 | [Item("RandomInteractionsInitializer", "Randomly assignes interactions across all bits")]
|
---|
13 | [StorableClass]
|
---|
14 | public class RandomInteractionsInitializer : NamedItem, IInteractionInitializer {
|
---|
15 |
|
---|
16 | public override bool CanChangeName { get { return false; } }
|
---|
17 | public override bool CanChangeDescription { get { return false; } }
|
---|
18 |
|
---|
19 |
|
---|
20 | [StorableConstructor]
|
---|
21 | protected RandomInteractionsInitializer(bool serializing) : base(serializing) { }
|
---|
22 | protected RandomInteractionsInitializer(RandomInteractionsInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
23 | public RandomInteractionsInitializer() {
|
---|
24 | name = ItemName;
|
---|
25 | description = ItemDescription;
|
---|
26 | }
|
---|
27 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
28 | return new RandomInteractionsInitializer(this, cloner);
|
---|
29 | }
|
---|
30 |
|
---|
31 | #region IInteractionInitializer Members
|
---|
32 | public BoolMatrix InitializeInterations(int length, int nComponents, int nInteractions, IRandom random) {
|
---|
33 | BoolMatrix m = new BoolMatrix(length, nComponents);
|
---|
34 | for (int c = 0; c<m.Columns; c++) {
|
---|
35 | var indices = Enumerable.Range(0, length).ToList();
|
---|
36 | if (indices.Count > c) {
|
---|
37 | indices.RemoveAt(c);
|
---|
38 | m[c, c] = true;
|
---|
39 | }
|
---|
40 | while (indices.Count > nInteractions) {
|
---|
41 | indices.RemoveAt(random.Next(indices.Count));
|
---|
42 | }
|
---|
43 | foreach (var i in indices) {
|
---|
44 | m[i, c] = true;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | return m;
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 | }
|
---|
51 | }
|
---|