[01a998]: src / lib / model / NodeArray.cc Maximize Restore History

Download this file

NodeArray.cc    378 lines (332 with data), 10.1 kB

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <config.h>
#include <model/NodeArray.h>
#include <model/Model.h>
#include <graph/ConstantNode.h>
#include <graph/StochasticNode.h>
#include <graph/AggNode.h>
#include <sarray/RangeIterator.h>
#include <graph/NodeError.h>
#include <sarray/SArray.h>
#include <util/nainf.h>
#include <string>
#include <stdexcept>
#include <limits>
using std::pair;
using std::vector;
using std::map;
using std::string;
using std::runtime_error;
using std::logic_error;
using std::set;
using std::numeric_limits;
NodeArray::NodeArray(string const &name, vector<unsigned int> const &dim,
unsigned int nchain)
: _name(name), _range(dim), _nchain(nchain)
{
unsigned int length = _range.length();
_node_pointers = new Node *[length];
_offsets = new unsigned int[length];
for (unsigned int i = 0; i < length; i++) {
_node_pointers[i] = 0;
_offsets[i] = numeric_limits<unsigned int>::max();
}
}
NodeArray::~NodeArray()
{
delete [] _node_pointers;
delete [] _offsets;
}
bool NodeArray::isEmpty(Range const &target_range) const
{
if (!_range.contains(target_range))
throw logic_error("Range error in NodeArray::isEmpty");
for (RangeIterator i(target_range); !i.atEnd(); i.nextLeft()) {
if (_node_pointers[_range.leftOffset(i)] != 0)
return false;
}
return true;
}
void NodeArray::insert(Node *node, Range const &target_range)
{
if (!node) {
throw logic_error(string("Attempt to insert NULL node at ") + name() +
print(target_range));
}
if (node->dim() != target_range.dim(true)) {
throw runtime_error(string("Cannot insert node into ") + name() +
print(target_range) + ". Dimension mismatch");
}
if (!_range.contains(target_range)) {
throw runtime_error(string("Cannot insert node into ") + name() +
print(target_range) + ". Range out of bounds");
}
if (!isEmpty(target_range)) {
throw runtime_error(string("Node ") + name() + print(target_range)
+ " overlaps previously defined nodes");
}
/* Set the _node_pointers array and the offset array */
RangeIterator j(target_range);
for (unsigned int k = 0; !j.atEnd(); j.nextLeft(), ++k)
{
unsigned int offset = _range.leftOffset(j);
_node_pointers[offset] = node;
_offsets[offset] = k;
}
/* Add to the graph */
_member_graph.add(node);
}
Node *NodeArray::find(Range const &target_range) const
{
// Find previously inserted node.
if (!_range.contains(target_range)) {
return 0;
}
//We only need to check the first and last elements. If these
//are correct then everything else must lie in between.
unsigned int start = _range.leftOffset(target_range.lower());
Node *node = _node_pointers[start];
if (!node || _offsets[start] != 0)
return 0;
if (node->dim() != target_range.dim(true))
return 0;
if (node->length() > 1) {
unsigned int end = _range.leftOffset(target_range.upper());
if (_node_pointers[end] != node || _offsets[end] + 1 != node->length()) {
return 0;
}
}
return node;
}
Node *NodeArray::getSubset(Range const &target_range, Model &model)
{
//Check validity of target range
if (!_range.contains(target_range)) {
throw runtime_error(string("Cannot get subset ") + name() +
print(target_range) + ". Range out of bounds");
}
/* If range corresponds to a set node, then return this */
Node *node = find(target_range);
if (node)
return node;
/* If range corresponds to a previously created subset, then return this */
map<Range, Node *>::iterator p = _generated_nodes.find(target_range);
if (p != _generated_nodes.end()) {
return p->second;
}
/* Otherwise create an aggregate node */
vector<Node const *> nodes;
vector<unsigned int> offsets;
for (RangeIterator i(target_range); !i.atEnd(); i.nextLeft()) {
unsigned int offset = _range.leftOffset(i);
if (_node_pointers[offset] == 0) {
return 0;
}
nodes.push_back(_node_pointers[offset]);
offsets.push_back(_offsets[offset]);
}
AggNode *anode = new AggNode(target_range.dim(true), nodes, offsets);
_generated_nodes.insert(pair<Range,Node*>(target_range, anode));
model.addNode(anode);
_member_graph.add(anode);
return anode;
}
void NodeArray::setValue(SArray const &value, unsigned int chain)
{
if (!(_range == value.range())) {
throw runtime_error(string("Dimension mismatch when setting value of node array ") + name());
}
vector<double> const &x = value.value();
unsigned int N = value.length();
//Gather all the nodes for which a data value is supplied
set<Node*> setnodes;
for (unsigned int i = 0; i < _range.length(); ++i) {
if (x[i] != JAGS_NA) {
Node *node = _node_pointers[i];
if (node == 0) {
string msg = "Attempt to set value of undefined node ";
throw runtime_error(msg + name() +
print(value.range().leftIndex(i)));
}
if (node->isRandomVariable()) {
setnodes.insert(node);
}
else {
throw NodeError(node,
"Attempt to set value of non-variable node");
}
}
}
set<Node*>::const_iterator p;
double *node_value = new double[N];
for (p = setnodes.begin(); p != setnodes.end(); ++p) {
//Step through each node
Node *node = *p;
//Get vector of values for this node
for (unsigned int i = 0; i < N; ++i) {
if (_node_pointers[i] == node) {
if (_offsets[i] > node->length()) {
throw logic_error("Invalid offset in NodeArray::setValue");
}
else {
node_value[_offsets[i]] = x[i];
}
}
}
// If there are any missing values, they must all be missing
bool missing = node_value[0] == JAGS_NA;
for (unsigned int j = 1; j < node->length(); ++j) {
if ((node_value[j] == JAGS_NA) != missing) {
delete [] node_value;
throw NodeError(node,"Values supplied for node are partially missing");
}
}
if (!missing) {
node->setValue(node_value, node->length(), chain);
}
}
delete [] node_value;
}
void NodeArray::getValue(SArray &value, unsigned int chain,
bool (*condition)(Node const *)) const
{
if (!(_range == value.range())) {
string msg("Dimension mismatch when getting value of node array ");
msg.append(name());
throw runtime_error(msg);
}
unsigned int array_length = _range.length();
vector<double> array_value(array_length);
for (unsigned int j = 0; j < array_length; ++j) {
Node const *node = _node_pointers[j];
if (node && condition(node)) {
array_value[j] = node->value(chain)[_offsets[j]];
}
else {
array_value[j] = JAGS_NA;
}
}
value.setValue(array_value);
}
void NodeArray::setData(SArray const &value, Model *model)
{
if (!(_range == value.range())) {
throw runtime_error(string("Dimension mismatch when setting value of node array ") + name());
}
vector<double> const &x = value.value();
//Gather all the nodes for which a data value is supplied
for (unsigned int i = 0; i < _range.length(); ++i) {
if (x[i] != JAGS_NA) {
if (_node_pointers[i] == 0) {
//Insert a new constant node
ConstantNode *cnode = new ConstantNode(x[i], _nchain);
model->addNode(cnode);
insert(cnode, _range.leftIndex(i));
}
else {
throw logic_error("Error in NodeArray::setData");
}
}
}
}
string const &NodeArray::name() const
{
return _name;
}
Range const &NodeArray::range() const
{
return _range;
}
bool NodeArray::findActiveIndices(vector<unsigned int> &ind, unsigned int k,
vector<int> const &lower, vector<unsigned int> const &dim) const
{
/*
We pay a heavy computational price for the flexibility of
allowing users to insert multivariate nodes in arbritary
ways into the NodeArray.
Suppose we have an array of dimension [3,4,2,5], the lower index
is [1,2,1,2] and the dimension of the node is [3,2]. Then the
node could be inserted in 5 different ways.
[1:3, 2:3, 1, 2] Active indices (0,1)
[1:3, 2, 1:2, 2] (0,2)
[1:3, 2, 1, 2:3] (0,3)
[1, 2:4, 1:2, 2] (1,2)
[1, 2:4, 1, 2:3] (1,3)
We can't have active indices (2,3) because the node won't fit
*/
if (k == 0)
ind[k] = 0;
else
ind[k] = ind[k-1] + 1;
unsigned int m = ind.size();
unsigned int M = _range.ndim(false);
for (;ind[k] + m <= M + k; ind[k] = ind[k] + 1) {
if (k == m - 1) {
vector<int> upper(lower);
for (unsigned int l = 0; l < m; ++l) {
upper[ind[l]] = upper[ind[l]] + dim[l] - 1;
}
Range test_range(lower, upper);
if (_range.contains(test_range)) {
Node *node = _node_pointers[_range.leftOffset(lower)];
unsigned int j = 0;
bool ok = true;
for (RangeIterator i(test_range); !i.atEnd(); i.nextLeft(), ++j) {
unsigned int offset = _range.leftOffset(i);
if (_node_pointers[offset] != node || _offsets[offset] != j) {
ok = false;
break;
}
}
if (ok)
return true;
}
}
else {
if (findActiveIndices(ind, k+1, lower, dim))
return true;
}
}
return false;
}
Range NodeArray::getRange(Node const *node) const
{
if (!_member_graph.contains(node)) {
return Range();
}
//Look in the generated nodes first
for (map<Range, Node *>::const_iterator p = _generated_nodes.begin();
p != _generated_nodes.end(); ++p)
{
if (node == p->second)
return p->first;
}
/* Find the lower limit of the range. This is easy */
unsigned int ndim = _range.ndim(false);
vector<int> lower(ndim);
unsigned int j = 0;
for (; j < _range.length(); ++j) {
if (_node_pointers[j] == node) {
lower = _range.leftIndex(j);
break;
}
}
if (j == _range.length()) {
return Range();
}
unsigned int m = node->dim().size();
vector<unsigned int> ind(m, 1);
if (findActiveIndices(ind, 0, lower, node->dim())) {
vector<int> upper = lower;
for (unsigned int l = 0; l < m; ++l) {
upper[ind[l]] = upper[ind[l]] + node->dim()[l] - 1;
}
return Range(lower, upper);
}
else {
throw logic_error("Unable to find node range");
}
}
unsigned int NodeArray::nchain() const
{
return _nchain;
}