Minotaur 0.4.1
Docs for developers
CNode.h
Go to the documentation of this file.
1//
2// Minotaur -- It's only 1/2 bull
3//
4// (C)opyright 2008 - 2025 The Minotaur Team.
5//
6
7
15#ifndef MINOTAURCNODE_H
16#define MINOTAURCNODE_H
17
18#include "OpCode.h"
19#include "Types.h"
20
21namespace Minotaur {
22
23 class CNode;
24
26 {
27 bool operator()(const CNode *n1, const CNode *n2) const;
28 };
30 {
31 bool operator()(const CNode *n1, const CNode *n2) const;
32 };
33 typedef std::set<CNode *, CompareCNodes> CNodeSet;
34 typedef std::set<CNode *, CompareCNodesR> CNodeRSet;
35
36 class CQIter2 {
37 public:
38 CNode *node;
39 CQIter2 *next;
40 CQIter2 *prev;
41 };
42
48 class CNode {
49 public:
51 CNode();
52
61 CNode(OpCode op, CNode **children, size_t num_child);
62
72 CNode(OpCode op, CNode *lchild, CNode *rchild);
73
75 ~CNode();
76
78 CNode *clone() const;
79
86 void addPar(CNode *node);
87
96 void changeChild(CNode *out, CNode *in);
97
107 void copyParChild(CNode *out,
108 std::map<const CNode *, CNode *> *nmap) const;
109
120 void eval(const double *x, int *error);
121
134 double evalSingle(double x, int *error) const;
135
141
147 void fwdGrad();
148
150 bool getB() const { return b_; };
151
153 double getG() const { return g_; };
154
156 double getH() const { return h_; };
157
159 size_t getId() const { return id_; };
160
162 size_t getIndex() const { return index_; };
163
165 CNode *getL() const { return l_; };
166
168 double getLb() const { return lb_; };
169
171 CNode **getListL() const { return child_; };
172
174 CNode **getListR() const { return child_ + numChild_; };
175
177 OpCode getOp() const { return op_; };
178 CQIter2 *getParB() const { return parB_; };
179
181 CNode *getR() const { return r_; };
182 int getTempI() const { return ti_; };
183
185 FunctionType getType() const { return fType_; };
186
188 double getUb() const { return ub_; };
189
191 CNode *getUPar() const { return uPar_; };
192
194 const Variable *getV() const { return v_; };
195
197 double getVal() const { return val_; };
198
205 void grad(int *error);
206
208 size_t numChild() const { return numChild_; };
209
211 size_t numPar() const { return numPar_; };
212
218 void propHessSpa();
219
221 void propHessSpa2(CNodeRSet *nset);
222
231 void propBounds(bool *is_inf, int *error);
232
239 void hess(int *error);
240
249 void hess2(CNodeRSet *nset, int *error);
250
256 void setB(bool b) { b_ = b; };
257
264 void setBounds(double lb, double ub)
265 {
266 lb_ = lb;
267 ub_ = ub;
268 };
269
275 void setDouble(double d) { d_ = d; };
276
282 void setG(double g) { g_ = g; };
283
289 void setGi(double gi) { gi_ = gi; };
290
296 void setH(double h) { h_ = h; };
297
303 void setId(size_t i) { id_ = i; };
304
310 void setIndex(size_t i) { index_ = i; };
311
312
318 void setL(CNode *n) { l_ = n; };
319
325 void setOp(OpCode op) { op_ = op; };
326
332 void setR(CNode *n) { r_ = n; };
333
339 void setTempI(int i) { ti_ = i; };
340
346 void setType(FunctionType t);
347
353 void setVal(double v);
354
362 {
363 v_ = v;
364 }; // Changed (showing error while compiling)
365
373 void updateBnd(int *error);
374
376 void write(std::ostream &out) const;
377
379 void writeSubExp(std::ostream &out) const;
380
388 void writeSubNl(std::stringstream &s, int *err) const;
389
390 protected:
391 bool b_;
392 CNode *
394 double d_;
396 double g_;
398 double gi_;
400 double h_;
401 size_t id_;
402 size_t index_;
404 double lb_;
405 size_t numChild_;
406 size_t numPar_;
407 OpCode op_;
411 int ti_;
413 double ub_;
414
415
418 const Variable *v_;
419 double val_;
421
432 void propBounds_(double lb, double ub, bool *is_inf);
433 };
434 typedef std::vector<CNode *> CNodeVector;
435} //namespace Minotaur
436
437
438#endif
Declare the OpCodes used in Minotaur.
Declare important 'types' used in Minotaur.
CNode denotes a node in the computational graph. It stores the op-code, children, parents and other a...
Definition: CNode.h:48
void setType(FunctionType t)
Set the value of function type of the graph rooted at this node.
Definition: CNode.cpp:1670
FunctionType findFType()
Find the type of the function (constant, linear, polynomial, etc.) based on the function type of chil...
Definition: CNode.cpp:534
double val_
Pointer the variable in case it is OpVar.
Definition: CNode.h:419
double gi_
Definition: CNode.h:398
void setB(bool b)
Set the boolean flag.
Definition: CNode.h:256
size_t id_
Value of the hessian.
Definition: CNode.h:401
void write(std::ostream &out) const
Print the function expression at current node only.
Definition: CNode.cpp:1888
size_t numPar_
Number of children.
Definition: CNode.h:406
void setV(VariablePtr v)
Set the Variable at this node (for OpVar nodes)
Definition: CNode.h:361
double g_
Function type of the graph rooted at this node.
Definition: CNode.h:396
CNode ** getListL() const
Definition: CNode.h:171
int ti_
Right child.
Definition: CNode.h:411
void setGi(double gi)
Set the value of the derivative (forward mode gradient)
Definition: CNode.h:289
void setOp(OpCode op)
Set the OpCode.
Definition: CNode.h:325
double getH() const
Definition: CNode.h:156
void hess(int *error)
Reverse mode hessian evaluation. Hessian values are pushed to children nodes.
Definition: CNode.cpp:1082
void propHessSpa()
Find whether child-nodes must participate in evaluation of the current column of the hessian-matrix....
Definition: CNode.cpp:1546
void propBounds(bool *is_inf, int *error)
Deduce and push new bounds to child-nodes based on bounds of this node.
Definition: CNode.cpp:1246
size_t getIndex() const
Definition: CNode.h:162
double h_
Definition: CNode.h:400
size_t numChild() const
Definition: CNode.h:208
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...
Definition: CNode.cpp:1489
const Variable * v_
Definition: CNode.h:418
void setIndex(size_t i)
Set the value of the node index.
Definition: CNode.h:310
OpCode getOp() const
Definition: CNode.h:177
void setL(CNode *n)
Set the pointer to the left-most child.
Definition: CNode.h:318
OpCode op_
Number of parents.
Definition: CNode.h:407
void writeSubExp(std::ostream &out) const
Print the function expression at current node and the sub-tree.
Definition: CNode.cpp:2063
CNode * r_
Pointer to the last parent (parents end)
Definition: CNode.h:410
size_t numChild_
lower bound that a node can achieve
Definition: CNode.h:405
void setTempI(int i)
Set the value of the temporary integer ti_.
Definition: CNode.h:339
bool getB() const
Definition: CNode.h:150
void setR(CNode *n)
Set the pointer to the right-most child.
Definition: CNode.h:332
void setG(double g)
Set the value of the derivative (reverse mode gradient)
Definition: CNode.h:282
void propHessSpa2(CNodeRSet *nset)
A variant of propHessSpa()
Definition: CNode.cpp:1514
CNode * getR() const
Definition: CNode.h:181
void setH(double h)
Set the value of the hessian.
Definition: CNode.h:296
void setBounds(double lb, double ub)
Set lower and upper bounds on the node.
Definition: CNode.h:264
FunctionType fType_
Constant double value in OpNum and OpInt nodes.
Definition: CNode.h:395
double d_
array of size numChild_ + 1. The last pointer is a null
Definition: CNode.h:394
void setId(size_t i)
Set the value of the node ID.
Definition: CNode.h:303
CNode()
Default constructor.
Definition: CNode.cpp:32
void eval(const double *x, int *error)
Evaluate the function at this node.
Definition: CNode.cpp:402
size_t index_
Unique ID of a dependent node. 0 for independent nodes.
Definition: CNode.h:402
void fwdGrad()
Calculate the derivative of the function at this node based on values and gradients of children nodes...
Definition: CNode.cpp:809
const Variable * getV() const
Definition: CNode.h:194
CQIter2 * parE_
Pointer to the first parent (parents begin)
Definition: CNode.h:409
double getLb() const
Definition: CNode.h:168
double getG() const
Definition: CNode.h:153
double evalSingle(double x, int *error) const
Evaluate the value of just this node based on the single value given as an input parameter....
Definition: CNode.cpp:308
void grad(int *error)
Push gradient values to children for reverse mode gradient evaluation.
Definition: CNode.cpp:939
void setVal(double v)
Set the value of function at this node.
Definition: CNode.cpp:1676
CNode * clone() const
Clone the node. Does not copy pointers to parents, children, etc.
Definition: CNode.cpp:200
double ub_
Definition: CNode.h:413
CNode ** getListR() const
Definition: CNode.h:174
~CNode()
Destroy.
Definition: CNode.cpp:153
CNode * getL() const
Definition: CNode.h:165
size_t getId() const
Definition: CNode.h:159
CQIter2 * parB_
Operation code.
Definition: CNode.h:408
void changeChild(CNode *out, CNode *in)
Replace one of the children of a given node by another node.
Definition: CNode.cpp:225
CNode * uPar_
upper bound that a node can achieve
Definition: CNode.h:416
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.
Definition: CNode.cpp:256
CNode * getUPar() const
Definition: CNode.h:191
size_t numPar() const
Definition: CNode.h:211
void hess2(CNodeRSet *nset, int *error)
Reverse mode hessian evaluation with sparsity detection. Hessian values are pushed to children nodes.
Definition: CNode.cpp:1239
double lb_
Left child.
Definition: CNode.h:404
void setDouble(double d)
Set the constant double value (OpNum and OpInt).
Definition: CNode.h:275
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...
Definition: CNode.cpp:1902
void addPar(CNode *node)
Add a parent to a given node (a parent depends on the child in function evaluation).
Definition: CNode.cpp:171
FunctionType getType() const
Definition: CNode.h:185
CNode ** child_
Boolean flag used in finding hessian sparsity.
Definition: CNode.h:393
double getUb() const
Definition: CNode.h:188
double getVal() const
Definition: CNode.h:197
CNode * l_
Unique index of the node.
Definition: CNode.h:403
void updateBnd(int *error)
Find bounds deduced from the bounds of children nodes and update lb_ and ub_.
Definition: CNode.cpp:1684
Definition: CNode.h:36
Definition: Variable.h:31
Definition: ActiveNodeStore.h:20
FunctionType
Different types of functions in Minotaur.
Definition: Types.h:65
Definition: CNode.h:30
Definition: CNode.h:26

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