Minotaur 0.4.1
Docs for developers
CGraph.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 MINOTAURCGRAPH_H
16#define MINOTAURCGRAPH_H
17
18#include <stack>
19
20#include "Types.h"
21#include "NonlinearFunction.h"
22#include "OpCode.h"
23
24namespace Minotaur {
25
26 class CGraph;
27 class CNode;
28 typedef CGraph *CGraphPtr;
29 typedef std::deque<CNode *> CNodeQ;
30 typedef std::vector<CNode *> CNodeVector;
31 typedef std::map<ConstVariablePtr, CNode *, CompareVariablePtr> VarNodeMap;
32
33 class CGraph : public NonlinearFunction {
34 public:
36 CGraph();
37
39 ~CGraph();
40
41 void addConst(const double eps, int &err);
42
43 NonlinearFunctionPtr clone(int *err) const;
44
45 NonlinearFunctionPtr cloneWithVars(VariableConstIterator, int *) const;
46
47 // base class method.
48 NonlinearFunctionPtr getPersp(VariablePtr z, double eps, int *err) const;
49
50 NonlinearFunctionPtr getPersp(VariablePtr z, double eps, int *err,
51 QuadraticFunctionPtr qf, UInt maxId,
52 VariableGroup nNonzeroVar, double intTol);
53
54 // base class method.
55 void computeBounds(double *lb, double *ub, int *error);
56
57 // Evaluate at a given array.
58 double eval(const double *x, int *err);
59
60 // Evaluate gradient at a given array.
61 void evalGradient(const double *x, double *grad_f, int *error);
62
63 // Evaluate hessian of at a given vector.
64 void evalHessian(double mult, const double *x, const LTHessStor *stor,
65 double *values, int *error);
66
67 // Fill hessian sparsity.
68 void fillHessStor(LTHessStor *stor);
69
70 // Finalize hessian offsets, if needed.
71 void finalHessStor(const LTHessStor *stor);
72
73 // Add gradient values to sparse Jacobian
74 void fillJac(const double *x, double *values, int *error);
75
80 void finalize();
81
82 // base class method.
83 double getFixVarOffset(VariablePtr v, double val);
84
85 // get node corresponding to variable v
86 CNode *getVarNode(VariablePtr v);
87
88 CNode *getPerspZNode() { return zNode_; };
89
90 // get type of function
91 FunctionType getType() const;
92
93 // base method
94 std::string getNlString(int *err);
95
96 size_t getNumNodes();
97
98 size_t getHessNz();
99
100 bool ifLinear(LinearFunctionPtr lf, UInt pv, double *consVal);
101
102 //reset node index every time a node is added or deleted
103 void resetNodeIndex();
104
106 const CNode *getOut() const;
107
108 VariablePtr getVar(const CNode *cnode) const;
109
110 // Get variables that exist in this function.
111 void getVars(VariableSet *vars);
112
113 bool isIdenticalTo(CGraphPtr cg);
114
115 // base class method
116 bool isSumOfSquares() const;
117
118 // multiply by a constant.
119 void multiply(double c);
120
133 CNode *newNode(OpCode op, CNode *lchild, CNode *rchild);
134
145 CNode *newNode(OpCode op, CNode **child, size_t n);
146
154 CNode *newNode(double d);
155
163 CNode *newNode(int i);
164
177
178 // base class method.
179 void prepJac(VarSetConstIter vbeg, VarSetConstIter vend);
180
181 // base class method.
182 void removeVar(VariablePtr v, double val);
183
190 void setOut(CNode *node);
191
192 // base class method.
193 void sqrRoot(int &err);
194
195 // base class method.
196 void subst(VariablePtr out, VariablePtr in, double rat);
197
198 // base class method.
199 void varBoundMods(double lb, double ub, VarBoundModVector &mods,
200 SolveStatus *status);
201
202 // method to return all the dependent nodes of the cgraph.
203 CNodeQ dNodes() { return dq_; };
204
205 // display.
206 void write(std::ostream &out) const;
207
208 private:
210 CNodeVector aNodes_;
211
212 bool changed_;
213
216 CNodeQ dq_;
217
218 UIntVector hInds_;
219 UInt hNnz_;
220 UIntVector hOffs_;
221 UIntVector hStarts_;
222 UIntVector gOffs_;
223
224
226 CNode *oNode_;
227
228 CNode *zNode_;
229
231 VarNodeMap varNode_;
232
234 CNodeQ vq_;
235
236 CGraphPtr clone_(int *err) const;
237
238 void fwdGrad_(CNode *node);
239 void fwdGrad2_(std::stack<CNode *> *st2, CNode *node);
240
241 void fillHessInds_(CNode *node, UIntQ *inds);
242 void fillHessInds2_(CNode *node, UIntQ *inds);
243
246 bool isSOSRec_(CNode *node) const;
247
248 void revHess_(int *error);
249 void revHess2_(std::stack<CNode *> *st2, double mult, UInt vind,
250 double *values, UInt *nz, int *error);
251
258 void grad_(int *error);
259
260 void setupHess_(VariablePtr v, CNode *node,
261 std::set<ConstVariablePair, CompareVariablePair> &vps);
262
263 void simplifyDq_();
264 };
265} //namespace Minotaur
266#endif
Declare abstract base class NonlinearFunction.
Declare the OpCodes used in Minotaur.
Declare important 'types' used in Minotaur.
Definition: CGraph.h:33
bool isSumOfSquares() const
Check if the function is a sum of square terms.
Definition: CGraph.cpp:1164
double getFixVarOffset(VariablePtr v, double val)
If a variable is fixed at a given value and removed, what is the constant (offset) needed to be added...
Definition: CGraph.cpp:724
void computeBounds(double *lb, double *ub, int *error)
Calculate upper and lower bounds on the function using bounds of the variables.
Definition: CGraph.cpp:172
void sqrRoot(int &err)
Change the nonlinear function to its square-root.
Definition: CGraph.cpp:1407
void fillJac(const double *x, double *values, int *error)
Evaluate and add gradient at a given point to the jacobian.
Definition: CGraph.cpp:496
NonlinearFunctionPtr cloneWithVars(VariableConstIterator, int *) const
Make a clone using new variables.
Definition: CGraph.cpp:126
void finalize()
Definition: CGraph.cpp:558
const CNode * getOut() const
Get the output node.
Definition: CGraph.cpp:1067
void write(std::ostream &out) const
Display the nonlinear function.
Definition: CGraph.cpp:1666
void varBoundMods(double lb, double ub, VarBoundModVector &mods, SolveStatus *status)
Tighten variables based on function bounds.
Definition: CGraph.cpp:1624
FunctionType getType() const
Return the type of function: polynomial, ...
Definition: CGraph.cpp:1079
void addConst(const double eps, int &err)
Add a constant to the function.
Definition: CGraph.cpp:59
void subst(VariablePtr out, VariablePtr in, double rat)
Substitute a variable with another.
Definition: CGraph.cpp:1421
void removeVar(VariablePtr v, double val)
Remove a variable v from the function after fixing it to value val.
Definition: CGraph.cpp:1303
void fillHessStor(LTHessStor *stor)
Fill sparsity of hessian into hessian storage.
Definition: CGraph.cpp:411
void setOut(CNode *node)
Set the node that should be the output of this graph. This node should already be a part of this grap...
Definition: CGraph.cpp:1578
void evalHessian(double mult, const double *x, const LTHessStor *stor, double *values, int *error)
Evaluate and add hessian at a given point.
Definition: CGraph.cpp:218
void multiply(double c)
Multiply by a constant.
Definition: CGraph.cpp:1193
std::string getNlString(int *err)
Return a string in AMPL's .nl format (postfix notation) of this nonlinear function.
Definition: CGraph.cpp:736
double eval(const double *x, int *err)
Evaluate the function at a given point x.
Definition: CGraph.cpp:186
void getVars(VariableSet *vars)
Get variables used in this function.
Definition: CGraph.cpp:1120
void finalHessStor(const LTHessStor *stor)
Finalize hessian preparation.
Definition: CGraph.cpp:517
void evalGradient(const double *x, double *grad_f, int *error)
Evaluate and add gradient at a given point.
Definition: CGraph.cpp:202
NonlinearFunctionPtr getPersp(VariablePtr z, double eps, int *err) const
Take perspective of this function with respect to a given variable.
Definition: CGraph.cpp:986
CGraph()
Default constructor.
Definition: CGraph.cpp:31
void prepJac(VarSetConstIter vbeg, VarSetConstIter vend)
Prepare for evaluating sparse jacobian.
Definition: CGraph.cpp:1285
CNode * newNode(OpCode op, CNode *lchild, CNode *rchild)
Create a new node with one or two children, and add it to the graph. The children should already be a...
Definition: CGraph.cpp:1228
~CGraph()
Default constructor.
Definition: CGraph.cpp:47
CNode denotes a node in the computational graph. It stores the op-code, children, parents and other a...
Definition: CNode.h:48
The base class linear function is of the form c'x.
Definition: LinearFunction.h:31
Base class for nonlinear functions.
Definition: NonlinearFunction.h:31
Definition: QuadraticFunction.h:38
Definition: Variable.h:31
Definition: ActiveNodeStore.h:20
std::map< ConstVariablePtr, double, CompareVariablePtr > VariableGroup
Variables should always be constant within a group.
Definition: Types.h:511
std::deque< UInt > UIntQ
Containers for standard types.
Definition: Types.h:33
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
FunctionType
Different types of functions in Minotaur.
Definition: Types.h:65
SolveStatus
Different states an algorithm like branch-and-bound can be in.
Definition: Types.h:158
Definition: HessianOfLag.h:21

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