This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPredictorsMapper.cs
204 lines (191 loc) · 7.82 KB
/
PredictorsMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using RCNet.Extensions;
using System;
using System.Collections.Generic;
namespace RCNet.Neural.Network.SM.Readout
{
/// <summary>
/// Implements the mapper of specific predictors to readout units.
/// </summary>
[Serializable]
public class PredictorsMapper
{
//Attribute properties
/// <summary>
/// The collection of switches generally enabling/disabling the predictors.
/// </summary>
public bool[] PredictorGeneralSwitchCollection { get; private set; }
//Attributes
private readonly Dictionary<string, ReadoutUnitMap> _mapCollection;
private readonly int _numOfAllowedPredictors;
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="numOfPredictors">The total number of predictors.</param>
public PredictorsMapper(int numOfPredictors)
{
PredictorGeneralSwitchCollection = new bool[numOfPredictors];
PredictorGeneralSwitchCollection.Populate(true);
_numOfAllowedPredictors = numOfPredictors;
_mapCollection = new Dictionary<string, ReadoutUnitMap>();
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="predictorGeneralSwitchCollection">The collection of switches generally enabling/disabling the predictors.</param>
public PredictorsMapper(bool[] predictorGeneralSwitchCollection)
{
PredictorGeneralSwitchCollection = (bool[])predictorGeneralSwitchCollection.Clone();
_numOfAllowedPredictors = 0;
for (int i = 0; i < predictorGeneralSwitchCollection.Length; i++)
{
if (predictorGeneralSwitchCollection[i]) ++_numOfAllowedPredictors;
}
if (_numOfAllowedPredictors == 0 || predictorGeneralSwitchCollection.Length == 0)
{
throw new ArgumentException("There is no available predictor", "predictorGeneralSwitchCollection");
}
_mapCollection = new Dictionary<string, ReadoutUnitMap>();
return;
}
/// <summary>
/// Adds the mapping for the specified readout untit.
/// </summary>
/// <param name="readoutUnitName">The name of the readout unit.</param>
/// <param name="map">The collection of switches indicating whether to use a predictor for the readout unit.</param>
public void Add(string readoutUnitName, bool[] map)
{
if (map.Length != PredictorGeneralSwitchCollection.Length)
{
throw new ArgumentException("Incorrect number of switches in the map", "map");
}
if (readoutUnitName.Length == 0)
{
throw new ArgumentException("ReadoutUnit name can not be empty", "readoutUnitName");
}
if (_mapCollection.ContainsKey(readoutUnitName))
{
throw new ArgumentException($"Mapping already contains mapping for ReadoutUnit {readoutUnitName}", "readoutUnitName");
}
//Apply general switches
bool[] localMap = (bool[])map.Clone();
int numOfReadoutUnitAllowedPredictors = 0;
for (int i = 0; i < localMap.Length; i++)
{
if (localMap[i])
{
if (!PredictorGeneralSwitchCollection[i])
{
localMap[i] = false;
}
else
{
++numOfReadoutUnitAllowedPredictors;
}
}
}
if (numOfReadoutUnitAllowedPredictors < 1)
{
throw new ArgumentException("Map contains no allowed predictors", "map");
}
_mapCollection.Add(readoutUnitName, new ReadoutUnitMap(localMap));
return;
}
private double[] CreateVector(double[] predictors, bool[] map, int vectorLength)
{
if (predictors.Length != map.Length)
{
throw new ArgumentException("Incorrect number of predictors", "predictors");
}
double[] vector = new double[vectorLength];
for (int i = 0, vIdx = 0; i < predictors.Length; i++)
{
if (map[i])
{
vector[vIdx] = predictors[i];
++vIdx;
}
}
return vector;
}
/// <summary>
/// Creates an input vector containing specific subset of predictors for the specified readout unit.
/// </summary>
/// <param name="readoutUnitName">ReadoutUnit name</param>
/// <param name="predictors">Available predictors</param>
public double[] CreateVector(string readoutUnitName, double[] predictors)
{
if (predictors.Length != PredictorGeneralSwitchCollection.Length)
{
throw new ArgumentException("Incorrect number of predictors", "predictors");
}
if (_mapCollection.ContainsKey(readoutUnitName))
{
ReadoutUnitMap rum = _mapCollection[readoutUnitName];
return CreateVector(predictors, rum.Map, rum.VectorLength);
}
else
{
return CreateVector(predictors, PredictorGeneralSwitchCollection, _numOfAllowedPredictors);
}
}
/// <summary>
/// Creates an input vector collection where each vector contains the specific subset of predictors for the specified readout unit.
/// </summary>
/// <param name="readoutUnitName">ReadoutUnit name</param>
/// <param name="predictorsCollection">Collection of available predictors</param>
public List<double[]> CreateVectorCollection(string readoutUnitName, IEnumerable<double[]> predictorsCollection)
{
List<double[]> vectorCollection = new List<double[]>();
ReadoutUnitMap rum = null;
if (_mapCollection.ContainsKey(readoutUnitName))
{
rum = _mapCollection[readoutUnitName];
}
foreach (double[] predictors in predictorsCollection)
{
if (rum == null)
{
vectorCollection.Add(CreateVector(predictors, PredictorGeneralSwitchCollection, _numOfAllowedPredictors));
}
else
{
vectorCollection.Add(CreateVector(predictors, rum.Map, rum.VectorLength));
}
}
return vectorCollection;
}
//Inner classes
/// <summary>
/// Implements the map of specific predictors to readout unit.
/// </summary>
[Serializable]
private class ReadoutUnitMap
{
//Attribute properties
/// <summary>
/// The switches indicating whether to use predictor for the readout unit.
/// </summary>
public bool[] Map { get; set; }
/// <summary>
/// The length of the readout unit's input vector.
/// </summary>
public int VectorLength { get; private set; }
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="map">The switches indicating whether to use predictor for the readout unit.</param>
public ReadoutUnitMap(bool[] map)
{
Map = map;
VectorLength = 0;
foreach (bool bSwitch in Map)
{
if (bSwitch) ++VectorLength;
}
return;
}
}//ReadoutUnitMap
}//PredictorsMapper
}//Namespace