Minotaur 0.4.1
Docs for developers
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Minotaur::CNode Class Reference

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>

Collaboration diagram for Minotaur::CNode:
Collaboration graph
[legend]

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.
 
CNodeclone () 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
 
CNodegetL () const
 
double getLb () const
 
CNode ** getListL () const
 
CNode ** getListR () const
 
OpCode getOp () const
 
CQIter2getParB () const
 
CNodegetR () const
 
int getTempI () const
 
FunctionType getType () const
 
double getUb () const
 
CNodegetUPar () const
 
const VariablegetV () 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.
 
CNodel_
 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.
 
CQIter2parB_
 Operation code.
 
CQIter2parE_
 Pointer to the first parent (parents begin)
 
CNoder_
 Pointer to the last parent (parents end)
 
int ti_
 Right child.
 
double ub_
 
CNodeuPar_
 upper bound that a node can achieve
 
const Variablev_
 
double val_
 Pointer the variable in case it is OpVar.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ CNode() [1/2]

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.

Parameters
[in]opOpcode of the new CNode
[in]childrenPointer to an array of child nodes, can be null
[in]Numberof children, can be zero or more

◆ CNode() [2/2]

CNode::CNode ( OpCode  op,
CNode lchild,
CNode rchild 
)

: Construct a node from two children, left and right, and an opcode

Constructor with a specific opcode and two children.

Parameters
[in]opOpcode of the new CNode
[in]lchildPointer to the left child node
[in]rchildPointer to the right child node

Member Function Documentation

◆ addPar()

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

◆ changeChild()

void CNode::changeChild ( CNode out,
CNode in 
)

Replace one of the children of a given node by another node.

Parameters
[in]outPointer to the existing child node that will be replaced
[in]inPointer to the new child node that will replace the existing child.

◆ copyParChild()

void CNode::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.

Parameters
[in,out]outThe 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.

◆ eval()

void CNode::eval ( const double *  x,
int *  error 
)

Evaluate the function at this node.

Parameters
[in]xThe 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]errorNonzero if some error occurs in evaluation e.g. log of a negative number, etc.

◆ evalSingle()

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.)

Parameters
[in]xThe input argument for the function.
[out]errorNonzero if there is an error in evaluation like some IEEE exception.
Returns
The value of the function evaluated.

◆ getB()

bool Minotaur::CNode::getB ( ) const
inline
Returns
The value of the boolean flag.

◆ getG()

double Minotaur::CNode::getG ( ) const
inline
Returns
The value of the gradient value.

◆ getH()

double Minotaur::CNode::getH ( ) const
inline
Returns
The value of the hessian value at this node.

◆ getId()

size_t Minotaur::CNode::getId ( ) const
inline
Returns
The ID

◆ getIndex()

size_t Minotaur::CNode::getIndex ( ) const
inline
Returns
index of the node

◆ getL()

CNode * Minotaur::CNode::getL ( ) const
inline
Returns
The pointer to left child

◆ getLb()

double Minotaur::CNode::getLb ( ) const
inline
Returns
The lower bound

◆ getListL()

CNode ** Minotaur::CNode::getListL ( ) const
inline
Returns
Pointer to the beginning of array of pointers to children

◆ getListR()

CNode ** Minotaur::CNode::getListR ( ) const
inline
Returns
Pointer to the end of array of pointers to children

◆ getOp()

OpCode Minotaur::CNode::getOp ( ) const
inline
Returns
The opcode of this node.

◆ getR()

CNode * Minotaur::CNode::getR ( ) const
inline
Returns
The pointer to right child

◆ getType()

FunctionType Minotaur::CNode::getType ( ) const
inline
Returns
The stored function type (does not evaluate).

◆ getUb()

double Minotaur::CNode::getUb ( ) const
inline
Returns
The upper bound

◆ getUPar()

CNode * Minotaur::CNode::getUPar ( ) const
inline
Returns
The unique parent if any, otherwise return NULL

◆ getV()

const Variable * Minotaur::CNode::getV ( ) const
inline
Returns
Pointer to the variable if the node is OpVar, otherwise NULL

◆ getVal()

double Minotaur::CNode::getVal ( ) const
inline
Returns
The value of function stored at this node (does not evaluate)

◆ grad()

void CNode::grad ( int *  error)

Push gradient values to children for reverse mode gradient evaluation.

Parameters
[out]errorNonzero if there is an error in evaluation.

◆ hess()

void CNode::hess ( int *  error)

Reverse mode hessian evaluation. Hessian values are pushed to children nodes.

Parameters
[out]errorNonzero if some error is encountered in evaluation.

◆ hess2()

void CNode::hess2 ( CNodeRSet *  nset,
int *  error 
)

Reverse mode hessian evaluation with sparsity detection. Hessian values are pushed to children nodes.

Parameters
[in,out]nsetChildren are added to nset if they participate in Hessian.
[out]errorNonzero if some error is encountered in evaluation.

◆ numChild()

size_t Minotaur::CNode::numChild ( ) const
inline
Returns
The number of children nodes.

◆ numPar()

size_t Minotaur::CNode::numPar ( ) const
inline
Returns
The number of parents

◆ propBounds()

void CNode::propBounds ( bool *  is_inf,
int *  error 
)

Deduce and push new bounds to child-nodes based on bounds of this node.

Parameters
[out]is_infTrue if it detects inconsistency, i.e. infeasibility
[out]errorNonzero if some error is encountered in evaluation.

◆ propBounds_()

void CNode::propBounds_ ( double  lb,
double  ub,
bool *  is_inf 
)
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

Parameters
[in]lbNew lower bound
[in]ubNew upper bound
[out]is_infSet to true if lb is more than ub after the update. Otherwise, remains unchanged.

◆ setB()

void Minotaur::CNode::setB ( bool  b)
inline

Set the boolean flag.

Parameters
[in]bThe flag.

◆ setBounds()

void Minotaur::CNode::setBounds ( double  lb,
double  ub 
)
inline

Set lower and upper bounds on the node.

Parameters
[in]lbLower bound.
[in]ubUpper bound.

◆ setDouble()

void Minotaur::CNode::setDouble ( double  d)
inline

Set the constant double value (OpNum and OpInt).

Parameters
[in]dThe value.

◆ setG()

void Minotaur::CNode::setG ( double  g)
inline

Set the value of the derivative (reverse mode gradient)

Parameters
[in]gThe value.

◆ setGi()

void Minotaur::CNode::setGi ( double  gi)
inline

Set the value of the derivative (forward mode gradient)

Parameters
[in]giThe value.

◆ setH()

void Minotaur::CNode::setH ( double  h)
inline

Set the value of the hessian.

Parameters
[in]hThe value.

◆ setId()

void Minotaur::CNode::setId ( size_t  i)
inline

Set the value of the node ID.

Parameters
[in]iThe value.

◆ setIndex()

void Minotaur::CNode::setIndex ( size_t  i)
inline

Set the value of the node index.

Parameters
[in]iThe value.

◆ setL()

void Minotaur::CNode::setL ( CNode n)
inline

Set the pointer to the left-most child.

Parameters
[in]nThe pointer.

◆ setOp()

void Minotaur::CNode::setOp ( OpCode  op)
inline

Set the OpCode.

Parameters
[in]opThe OpCode value.

◆ setR()

void Minotaur::CNode::setR ( CNode n)
inline

Set the pointer to the right-most child.

Parameters
[in]nThe pointer.

◆ setTempI()

void Minotaur::CNode::setTempI ( int  i)
inline

Set the value of the temporary integer ti_.

Parameters
[in]tiThe pointer.

◆ setType()

void CNode::setType ( FunctionType  t)

Set the value of function type of the graph rooted at this node.

Parameters
[in]tThe type.

◆ setV()

void Minotaur::CNode::setV ( VariablePtr  v)
inline

Set the Variable at this node (for OpVar nodes)

Parameters
[in]vThe variable pointer.

◆ setVal()

void CNode::setVal ( double  v)

Set the value of function at this node.

Parameters
[in]vThe value.

◆ updateBnd()

void CNode::updateBnd ( int *  error)

Find bounds deduced from the bounds of children nodes and update lb_ and ub_.

Parameters
[out]errorNonzero if some error is encountered in evaluating the bounds.

◆ writeSubNl()

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.

Parameters
[in]sstream to which the expression is appended
[out]errNonzero if some error is encountered

Member Data Documentation

◆ gi_

double Minotaur::CNode::gi_
protected

Value of the derivative of output w.r.t. this node (reverse mode)

◆ h_

double Minotaur::CNode::h_
protected

Value of the derivative of this node w.r.t. to a given var (forward mode)

◆ ub_

double Minotaur::CNode::ub_
protected

Temporary integer variable to find patterns of sparsity in the hessian.

◆ v_

const Variable* Minotaur::CNode::v_
protected

Unique parent of this node. NULL if the node has multiple parents


The documentation for this class was generated from the following files:

Minotaur source code documented by Doxygen 1.9.4 on Thu Apr 24 2025