[0513cd]: src / include / graph / Node.h Maximize Restore History

Download this file

Node.h    218 lines (204 with data), 6.7 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
#ifndef NODE_H_
#define NODE_H_
#include <set>
#include <vector>
#include <string>
namespace jags {
class RNG;
class StochasticNode;
class DeterministicNode;
class Graph;
/**
* @short Random variable status of nodes
*
* This enumeration is used by Node#randomVariableStatus to classify
* nodes into one of three categories.
*
* Some nodes are considered as random variables, and among those that
* are, they may be observed or unobserved. Conversely, a node that is
* not a random variable may not be observed.
*/
enum RVStatus {RV_FALSE, RV_TRUE_UNOBSERVED, RV_TRUE_OBSERVED};
/**
* @short Node in a directed acyclic graph
*/
class Node {
std::vector<Node const *> _parents;
std::set<StochasticNode*> *_stoch_children;
std::set<DeterministicNode *> *_dtrm_children;
/* Forbid copying of Node objects */
Node(Node const &orig);
Node &operator=(Node const &rhs);
protected:
std::vector<unsigned int> const &_dim;
const unsigned int _length;
const unsigned int _nchain;
double *_data;
unsigned int _index;
public:
/**
* Constucts a Node with no parents.
* @param dim Dimension of new Node.
* @param nchain Number of chains that the Node will contain data on.
*/
Node(std::vector<unsigned int> const &dim, unsigned int nchain);
/**
* Constructs a node with parents. Each parent must contain data on
* the same number of chains. Subclasses of Node may give specific
* meaning to the ordering of the parents.
*
* @param dim Dimension of new Node.
*
* @param parents vector of parent nodes. A node may not be its own
* parent.
*/
Node(std::vector<unsigned int> const &dim,
std::vector<Node const *> const &parents);
/**
* Destructor.
*/
virtual ~Node();
/**
* Number of chains.
*/
unsigned int nchain() const;
/**
* Vector of parents.
*/
std::vector<Node const *> const &parents() const;
/**
* Draws a random sample from the node's prior distribution.
* @param rng Pointer to random number generator
* @param chain Number of chain from which to draw sample
*/
virtual void randomSample(RNG *rng, unsigned int chain) = 0;
/**
* Calculates a value for the node based on its parents' values.
* @param chain Number of chain from which to draw sample
*/
virtual void deterministicSample(unsigned int chain) = 0;
/**
* Checks whether the parents of the Node have valid values.
*/
virtual bool checkParentValues(unsigned int chain) const = 0;
/**
* Returns the stochastic children of the node
*/
std::set<StochasticNode*> const *stochasticChildren();
/**
* Returns the deterministic children of the node
*/
std::set<DeterministicNode*> const *deterministicChildren();
/**
* Initializes the node for the given chain. The value array of a
* newly constructed Node consists of missing values (denoted by
* the special value JAGS_NA). This function sets the value of
* the node by forward sampling from its parents. If the Node has
* previously had its value set, the function will do nothing and
* return the value true. Initialization will fail if any of the
* parent nodes is uninitialized, and in this case the return
* value is false.
*
* @param chain Index number of chain to initialize.
*
* @returns a logical value indicating success
*/
bool initialize(unsigned int chain);
/**
* Returns the BUGS-language representation of the node, based on the
* names of its parents
*
* @param parents Vector of names of parent nodes
*/
virtual std::string
deparse(std::vector<std::string> const &parents) const = 0;
/**
* Returns the random variable status of a node.
*/
virtual RVStatus randomVariableStatus() const = 0;
/**
* Indicates whether the value of the node is fixed.
*/
virtual bool isFixed() const = 0;
/**
* Sets the value of the node for a given chain
* @param value Array of values to be assigned
* @param length Length of the value array
* @param chain number of chain (starting from zero) to modify
*
* @see SArray#setValue
*/
void setValue(double const *value, unsigned int length, unsigned int chain);
/**
* Indicates whether a node is discrete-valued or not.
* @see SArray#isDiscreteValued
*/
virtual bool isDiscreteValued() const = 0;
/**
* Returns a pointer to the start of the array of values for
* the given chain.
*/
double const *value(unsigned int chain) const;
/**
* Returns the length of the value array
*/
unsigned int length() const;
/**
* Returns the dimensions of the Node
*/
std::vector<unsigned int> const &dim() const;
/**
* Swaps the values in the given chains
*/
void swapValue(unsigned int chain1, unsigned int chain2);
void addChild(StochasticNode *node) const;
void removeChild(StochasticNode *node) const;
void addChild(DeterministicNode *node) const;
void removeChild(DeterministicNode *node) const;
/**
* Sets the index of the node. A Node index should be unique so
* that it can be used in sorting operations. A newly created
* Node has an index of zero.
*
* It is only permitted to set the index if the current index is
* zero. If an attempt to reset the index is made, a logic_error
* is thrown. This provides a runtime guarantee that the index is
* constant.
*
* @param index Value to be used as the Node's index.
*
* @see Graph, Model#insert
*/
void setIndex(unsigned int index);
/**
* Returns the index of a Node. If setIndex has not been called
* then the index is zero.
*/
unsigned int index() const;
};
/**
* Calculate the number of chains of parameter vector. Returns 0
* if the parameters are inconsistent
*/
unsigned int countChains(std::vector<Node const *> const &parameters);
/**
* Comparison function for nodes using the indices of the two nodes.
*
* @return true if node1 has a lower index than node2
*
* A logic_error is thrown if either node has an index of zero, or if
* two distinct nodes have the same index.
*/
bool lt(Node const *node1, Node const *node2);
/**
* @short STL function object for comparing two indexed Nodes
*/
struct ltNode
{
bool operator()(Node const *arg1, Node const *arg2) const
{
return lt(arg1, arg2);
}
};
} /* namespace jags */
#endif /* NODE_H_ */