Minotaur 0.4.1
Docs for developers
|
CNode denotes a node in the computational graph. It stores the op-code, children, parents and other auxiliary information to evaluate the function and its derivative, check its properties etc. More...
#include <CNode.h>
Public Member Functions | |
CNode () | |
Default constructor. | |
CNode (OpCode op, CNode **children, size_t num_child) | |
: Construct a node from its children and an opcode More... | |
CNode (OpCode op, CNode *lchild, CNode *rchild) | |
: Construct a node from two children, left and right, and an opcode More... | |
~CNode () | |
Destroy. | |
CNode * | clone () const |
Clone the node. Does not copy pointers to parents, children, etc. | |
void | addPar (CNode *node) |
Add a parent to a given node (a parent depends on the child in function evaluation). More... | |
void | changeChild (CNode *out, CNode *in) |
Replace one of the children of a given node by another node. More... | |
void | copyParChild (CNode *out, std::map< const CNode *, CNode * > *nmap) const |
Make copies of links pointing to parents and children to another node. Used in cloning the CGraph. More... | |
void | eval (const double *x, int *error) |
Evaluate the function at this node. More... | |
double | evalSingle (double x, int *error) const |
Evaluate the value of just this node based on the single value given as an input parameter. Ignores the values at the children nodes. Used by Transformer classes. Applicable only to nodes that require a single input (like OpUMinus, OpSqrt, etc) and not others (like OpSumList, OpPlus, OpMult, etc.) More... | |
FunctionType | findFType () |
Find the type of the function (constant, linear, polynomial, etc.) based on the function type of children. | |
void | fwdGrad () |
Calculate the derivative of the function at this node based on values and gradients of children nodes. Used in forward-mode gradient calculation. | |
bool | getB () const |
double | getG () const |
double | getH () const |
size_t | getId () const |
size_t | getIndex () const |
CNode * | getL () const |
double | getLb () const |
CNode ** | getListL () const |
CNode ** | getListR () const |
OpCode | getOp () const |
CQIter2 * | getParB () const |
CNode * | getR () const |
int | getTempI () const |
FunctionType | getType () const |
double | getUb () const |
CNode * | getUPar () const |
const Variable * | getV () const |
double | getVal () const |
void | grad (int *error) |
Push gradient values to children for reverse mode gradient evaluation. More... | |
size_t | numChild () const |
size_t | numPar () const |
void | propHessSpa () |
Find whether child-nodes must participate in evaluation of the current column of the hessian-matrix. Sets values of internal flags and variables. | |
void | propHessSpa2 (CNodeRSet *nset) |
A variant of propHessSpa() | |
void | propBounds (bool *is_inf, int *error) |
Deduce and push new bounds to child-nodes based on bounds of this node. More... | |
void | hess (int *error) |
Reverse mode hessian evaluation. Hessian values are pushed to children nodes. More... | |
void | hess2 (CNodeRSet *nset, int *error) |
Reverse mode hessian evaluation with sparsity detection. Hessian values are pushed to children nodes. More... | |
void | setB (bool b) |
Set the boolean flag. More... | |
void | setBounds (double lb, double ub) |
Set lower and upper bounds on the node. More... | |
void | setDouble (double d) |
Set the constant double value (OpNum and OpInt). More... | |
void | setG (double g) |
Set the value of the derivative (reverse mode gradient) More... | |
void | setGi (double gi) |
Set the value of the derivative (forward mode gradient) More... | |
void | setH (double h) |
Set the value of the hessian. More... | |
void | setId (size_t i) |
Set the value of the node ID. More... | |
void | setIndex (size_t i) |
Set the value of the node index. More... | |
void | setL (CNode *n) |
Set the pointer to the left-most child. More... | |
void | setOp (OpCode op) |
Set the OpCode. More... | |
void | setR (CNode *n) |
Set the pointer to the right-most child. More... | |
void | setTempI (int i) |
Set the value of the temporary integer ti_. More... | |
void | setType (FunctionType t) |
Set the value of function type of the graph rooted at this node. More... | |
void | setVal (double v) |
Set the value of function at this node. More... | |
void | setV (VariablePtr v) |
Set the Variable at this node (for OpVar nodes) More... | |
void | updateBnd (int *error) |
Find bounds deduced from the bounds of children nodes and update lb_ and ub_. More... | |
void | write (std::ostream &out) const |
Print the function expression at current node only. | |
void | writeSubExp (std::ostream &out) const |
Print the function expression at current node and the sub-tree. | |
void | writeSubNl (std::stringstream &s, int *err) const |
Write the function expression in ASL's text format (reverse polish notation) for the current node and the sub-tree. More... | |
Protected Member Functions | |
void | propBounds_ (double lb, double ub, bool *is_inf) |
Change the current bounds of the function value at this node to new bounds only if the new bounds are tighter. Also checks whether the new bounds are inconsistent. More... | |
Protected Attributes | |
bool | b_ |
CNode ** | child_ |
Boolean flag used in finding hessian sparsity. | |
double | d_ |
array of size numChild_ + 1. The last pointer is a null | |
FunctionType | fType_ |
Constant double value in OpNum and OpInt nodes. | |
double | g_ |
Function type of the graph rooted at this node. | |
double | gi_ |
double | h_ |
size_t | id_ |
Value of the hessian. | |
size_t | index_ |
Unique ID of a dependent node. 0 for independent nodes. | |
CNode * | l_ |
Unique index of the node. | |
double | lb_ |
Left child. | |
size_t | numChild_ |
lower bound that a node can achieve | |
size_t | numPar_ |
Number of children. | |
OpCode | op_ |
Number of parents. | |
CQIter2 * | parB_ |
Operation code. | |
CQIter2 * | parE_ |
Pointer to the first parent (parents begin) | |
CNode * | r_ |
Pointer to the last parent (parents end) | |
int | ti_ |
Right child. | |
double | ub_ |
CNode * | uPar_ |
upper bound that a node can achieve | |
const Variable * | v_ |
double | val_ |
Pointer the variable in case it is OpVar. | |
CNode denotes a node in the computational graph. It stores the op-code, children, parents and other auxiliary information to evaluate the function and its derivative, check its properties etc.
CNode::CNode | ( | OpCode | op, |
CNode ** | children, | ||
size_t | num_child | ||
) |
: Construct a node from its children and an opcode
Constructor with a specific opcode and children.
[in] | op | Opcode of the new CNode |
[in] | children | Pointer to an array of child nodes, can be null |
[in] | Number | of children, can be zero or more |
: Construct a node from two children, left and right, and an opcode
Constructor with a specific opcode and two children.
[in] | op | Opcode of the new CNode |
[in] | lchild | Pointer to the left child node |
[in] | rchild | Pointer to the right child node |
void CNode::addPar | ( | CNode * | node | ) |
Add a parent to a given node (a parent depends on the child in function evaluation).
\parm [in] Pointer to the parent node
Replace one of the children of a given node by another node.
[in] | out | Pointer to the existing child node that will be replaced |
[in] | in | Pointer to the new child node that will replace the existing child. |
Make copies of links pointing to parents and children to another node. Used in cloning the CGraph.
[in,out] | out | The node to which the links must be copied. This is the clone. |
[nmap] | in A map of nodes in of current CGraph to the nodes of new CGraph. |
void CNode::eval | ( | const double * | x, |
int * | error | ||
) |
Evaluate the function at this node.
[in] | x | The point at which the CGraph containing this CNode is being evaluated. x is used only for nodes that have opcode OpVar. For all other nodes, the values of the children are used in evaluating the function. |
[out] | error | Nonzero if some error occurs in evaluation e.g. log of a negative number, etc. |
double CNode::evalSingle | ( | double | x, |
int * | error | ||
) | const |
Evaluate the value of just this node based on the single value given as an input parameter. Ignores the values at the children nodes. Used by Transformer classes. Applicable only to nodes that require a single input (like OpUMinus, OpSqrt, etc) and not others (like OpSumList, OpPlus, OpMult, etc.)
[in] | x | The input argument for the function. |
[out] | error | Nonzero if there is an error in evaluation like some IEEE exception. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
void CNode::grad | ( | int * | error | ) |
Push gradient values to children for reverse mode gradient evaluation.
[out] | error | Nonzero if there is an error in evaluation. |
void CNode::hess | ( | int * | error | ) |
Reverse mode hessian evaluation. Hessian values are pushed to children nodes.
[out] | error | Nonzero if some error is encountered in evaluation. |
void CNode::hess2 | ( | CNodeRSet * | nset, |
int * | error | ||
) |
Reverse mode hessian evaluation with sparsity detection. Hessian values are pushed to children nodes.
[in,out] | nset | Children are added to nset if they participate in Hessian. |
[out] | error | Nonzero if some error is encountered in evaluation. |
|
inline |
|
inline |
void CNode::propBounds | ( | bool * | is_inf, |
int * | error | ||
) |
Deduce and push new bounds to child-nodes based on bounds of this node.
[out] | is_inf | True if it detects inconsistency, i.e. infeasibility |
[out] | error | Nonzero if some error is encountered in evaluation. |
|
protected |
Change the current bounds of the function value at this node to new bounds only if the new bounds are tighter. Also checks whether the new bounds are inconsistent.
Current value of the expression based on the value of the children
[in] | lb | New lower bound |
[in] | ub | New upper bound |
[out] | is_inf | Set to true if lb is more than ub after the update. Otherwise, remains unchanged. |
|
inline |
Set the boolean flag.
[in] | b | The flag. |
|
inline |
Set lower and upper bounds on the node.
[in] | lb | Lower bound. |
[in] | ub | Upper bound. |
|
inline |
Set the constant double value (OpNum and OpInt).
[in] | d | The value. |
|
inline |
Set the value of the derivative (reverse mode gradient)
[in] | g | The value. |
|
inline |
Set the value of the derivative (forward mode gradient)
[in] | gi | The value. |
|
inline |
Set the value of the hessian.
[in] | h | The value. |
|
inline |
Set the value of the node ID.
[in] | i | The value. |
|
inline |
Set the value of the node index.
[in] | i | The value. |
|
inline |
Set the pointer to the left-most child.
[in] | n | The pointer. |
|
inline |
Set the OpCode.
[in] | op | The OpCode value. |
|
inline |
Set the pointer to the right-most child.
[in] | n | The pointer. |
|
inline |
Set the value of the temporary integer ti_.
[in] | ti | The pointer. |
void CNode::setType | ( | FunctionType | t | ) |
Set the value of function type of the graph rooted at this node.
[in] | t | The type. |
|
inline |
Set the Variable at this node (for OpVar nodes)
[in] | v | The variable pointer. |
void CNode::setVal | ( | double | v | ) |
Set the value of function at this node.
[in] | v | The value. |
void CNode::updateBnd | ( | int * | error | ) |
Find bounds deduced from the bounds of children nodes and update lb_ and ub_.
[out] | error | Nonzero if some error is encountered in evaluating the bounds. |
void CNode::writeSubNl | ( | std::stringstream & | s, |
int * | err | ||
) | const |
Write the function expression in ASL's text format (reverse polish notation) for the current node and the sub-tree.
[in] | s | stream to which the expression is appended |
[out] | err | Nonzero if some error is encountered |
|
protected |
Value of the derivative of output w.r.t. this node (reverse mode)
|
protected |
Value of the derivative of this node w.r.t. to a given var (forward mode)
|
protected |
Temporary integer variable to find patterns of sparsity in the hessian.
|
protected |
Unique parent of this node. NULL if the node has multiple parents