Minotaur 0.4.1
Docs for developers
Public Member Functions | List of all members
Minotaur::CGraph Class Reference
Inheritance diagram for Minotaur::CGraph:
Inheritance graph
[legend]
Collaboration diagram for Minotaur::CGraph:
Collaboration graph
[legend]

Public Member Functions

 CGraph ()
 Default constructor.
 
 ~CGraph ()
 Default constructor.
 
void addConst (const double eps, int &err)
 Add a constant to the function. More...
 
NonlinearFunctionPtr clone (int *err) const
 
NonlinearFunctionPtr cloneWithVars (VariableConstIterator, int *) const
 Make a clone using new variables. More...
 
NonlinearFunctionPtr getPersp (VariablePtr z, double eps, int *err) const
 Take perspective of this function with respect to a given variable. More...
 
NonlinearFunctionPtr getPersp (VariablePtr z, double eps, int *err, QuadraticFunctionPtr qf, UInt maxId, VariableGroup nNonzeroVar, double intTol)
 
void computeBounds (double *lb, double *ub, int *error)
 Calculate upper and lower bounds on the function using bounds of the variables. More...
 
double eval (const double *x, int *err)
 Evaluate the function at a given point x. More...
 
void evalGradient (const double *x, double *grad_f, int *error)
 Evaluate and add gradient at a given point. More...
 
void evalHessian (double mult, const double *x, const LTHessStor *stor, double *values, int *error)
 Evaluate and add hessian at a given point. More...
 
void fillHessStor (LTHessStor *stor)
 Fill sparsity of hessian into hessian storage. More...
 
void finalHessStor (const LTHessStor *stor)
 Finalize hessian preparation. More...
 
void fillJac (const double *x, double *values, int *error)
 Evaluate and add gradient at a given point to the jacobian. More...
 
void finalize ()
 
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. More...
 
CNodegetVarNode (VariablePtr v)
 
CNodegetPerspZNode ()
 
FunctionType getType () const
 Return the type of function: polynomial, ... More...
 
std::string getNlString (int *err)
 Return a string in AMPL's .nl format (postfix notation) of this nonlinear function. More...
 
size_t getNumNodes ()
 
size_t getHessNz ()
 
bool ifLinear (LinearFunctionPtr lf, UInt pv, double *consVal)
 
void resetNodeIndex ()
 
const CNodegetOut () const
 Get the output node.
 
VariablePtr getVar (const CNode *cnode) const
 
void getVars (VariableSet *vars)
 Get variables used in this function. More...
 
bool isIdenticalTo (CGraphPtr cg)
 
bool isSumOfSquares () const
 Check if the function is a sum of square terms. More...
 
void multiply (double c)
 Multiply by a constant. More...
 
CNodenewNode (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 nodes of the graph. More...
 
CNodenewNode (OpCode op, CNode **child, size_t n)
 Create a new node with more than two children, and add it to the graph. The children should already be a nodes of the graph. More...
 
CNodenewNode (double d)
 Create a new node with constant real value. It does not have any children. More...
 
CNodenewNode (int i)
 Create a new node with constant integer value. It does not have any children. More...
 
CNodenewNode (VariablePtr v)
 Create a new node denoting an input variable. It does not have any children. More...
 
void prepJac (VarSetConstIter vbeg, VarSetConstIter vend)
 Prepare for evaluating sparse jacobian. More...
 
void removeVar (VariablePtr v, double val)
 Remove a variable v from the function after fixing it to value val. More...
 
void setOut (CNode *node)
 Set the node that should be the output of this graph. This node should already be a part of this graph (created by newNode() function). More...
 
void sqrRoot (int &err)
 Change the nonlinear function to its square-root. More...
 
void subst (VariablePtr out, VariablePtr in, double rat)
 Substitute a variable with another. More...
 
void varBoundMods (double lb, double ub, VarBoundModVector &mods, SolveStatus *status)
 Tighten variables based on function bounds. More...
 
CNodeQ dNodes ()
 
void write (std::ostream &out) const
 Display the nonlinear function. More...
 
- Public Member Functions inherited from Minotaur::NonlinearFunction
 NonlinearFunction ()
 Default constructor.
 
virtual ~NonlinearFunction ()
 Destroy.
 
virtual void addConst (const double d, int &err)
 Add a constant to the function. More...
 
virtual void sqrRoot (int &err)
 Change the nonlinear function to its square-root. More...
 
virtual NonlinearFunctionPtr cloneWithVars (VariableConstIterator vbeg, int *err) const =0
 Make a clone using new variables. More...
 
virtual void computeBounds (double *lb, double *ub, int *error)
 Calculate upper and lower bounds on the function using bounds of the variables. More...
 
virtual double eval (const double *x, int *error)=0
 Evaluate the function at a given point x. More...
 
virtual void evalGradient (const double *x, double *grad_f, int *error)=0
 Evaluate and add gradient at a given point. More...
 
virtual void evalHessian (const double mult, const double *x, const LTHessStor *stor, double *values, int *error)=0
 Evaluate and add hessian at a given point. More...
 
virtual void fillHessStor (LTHessStor *stor)=0
 Fill sparsity of hessian into hessian storage. More...
 
virtual void fillJac (const double *x, double *values, int *error)=0
 Evaluate and add gradient at a given point to the jacobian. More...
 
virtual void finalHessStor (const LTHessStor *stor)=0
 Finalize hessian preparation. More...
 
virtual std::string getNlString (int *err)
 Return a string in AMPL's .nl format (postfix notation) of this nonlinear function. More...
 
virtual double getFixVarOffset (VariablePtr, double)
 If a variable is fixed at a given value and removed, what is the constant (offset) needed to be added. More...
 
virtual FunctionType getType () const
 Return the type of function: polynomial, ... More...
 
virtual void getVars (VariableSet *vars)=0
 Get variables used in this function. More...
 
virtual bool hasVar (ConstVariablePtr v) const
 Check if function contains a variable. More...
 
virtual bool isSumOfSquares () const
 Check if the function is a sum of square terms. More...
 
virtual void multiply (double c)=0
 Multiply by a constant. More...
 
virtual size_t numVars ()
 Return the number of variables in this function.
 
virtual void prepJac (VarSetConstIter vbeg, VarSetConstIter vend)=0
 Prepare for evaluating sparse jacobian. More...
 
virtual void removeVar (VariablePtr, double)
 Remove a variable v from the function after fixing it to value val. More...
 
virtual void subst (VariablePtr, VariablePtr, double)
 Substitute a variable with another. More...
 
virtual NonlinearFunctionPtr getPersp (VariablePtr z, double eps, int *err) const
 Take perspective of this function with respect to a given variable. More...
 
virtual void varBoundMods (double, double, VarBoundModVector &, SolveStatus *)
 Tighten variables based on function bounds. More...
 
virtual VariableSet::iterator varsBegin ()
 First iterator for variables. More...
 
virtual VariableSet::iterator varsEnd ()
 Last iterator for variables. More...
 
virtual void write (std::ostream &out) const
 Display the nonlinear function. More...
 

Additional Inherited Members

- Protected Attributes inherited from Minotaur::NonlinearFunction
VariableSet vars_
 A set of variables used in this function.
 

Member Function Documentation

◆ addConst()

void CGraph::addConst ( const double  d,
int &  err 
)
virtual

Add a constant to the function.

Add a constant to the given nonlinear function. Adding a constant alone usually does not make sense. However, it is useful as a intermediate step. For instance, in writing the perspective: z*f(x/(z+e))

Parameters
[in]dThe value of the constant.
[out]errmust be nonzero if the constant could not be added.

Reimplemented from Minotaur::NonlinearFunction.

◆ cloneWithVars()

NonlinearFunctionPtr CGraph::cloneWithVars ( VariableConstIterator  vbeg,
int *  err 
) const
virtual

Make a clone using new variables.

Parameters
[in]vbegit points to the variable id 0. vbeg+k points to variable id k, where k>=0.
[out]errmust be nonzero if function wasn't cloned.

Implements Minotaur::NonlinearFunction.

◆ computeBounds()

void CGraph::computeBounds ( double *  lb,
double *  ub,
int *  error 
)
virtual

Calculate upper and lower bounds on the function using bounds of the variables.

Parameters
[out]lbPointer that will contain the value of lower bound.
[out]ubPointer that will contain the value of upper bound.
[out]error0 if no error is encountered, nonzero otherwise.

Reimplemented from Minotaur::NonlinearFunction.

◆ eval()

double CGraph::eval ( const double *  x,
int *  error 
)
virtual

Evaluate the function at a given point x.

Parameters
[in]xThe size of the array x must exceed the highest index of the variables used in the function.
[out]*errorIt should be set a positive value if there is error encountered while evaluating. Leave undisturbed otherwise.
Returns
The value of function of x.

Implements Minotaur::NonlinearFunction.

◆ evalGradient()

void CGraph::evalGradient ( const double *  x,
double *  grad_f,
int *  error 
)
virtual

Evaluate and add gradient at a given point.

Parameters
[in]xThe size of the array x must exceed the highest index of the variables used in the function.
[out]grad_fThe values of grad_f are incremented with the gradient of this function at x. The array grad_f is dense.
[out]errorIt should be set a positive value if there is error encountered while evaluating. Leave undisturbed otherwise.

Implements Minotaur::NonlinearFunction.

◆ evalHessian()

void CGraph::evalHessian ( double  mult,
const double *  x,
const LTHessStor stor,
double *  values,
int *  error 
)
virtual

Evaluate and add hessian at a given point.

Parameters
[in]multMultiplier for this objective/constraint function
[in]xThe point where we need the hessian.
[in]storThe Hessian storage information.
[out]valuesThe Hessian values to which we add Hessian of this function.
[out]errorWe set it to nonzero if any errors are encountered. Do not change it otherwise.

Implements Minotaur::NonlinearFunction.

◆ fillHessStor()

void CGraph::fillHessStor ( LTHessStor stor)
virtual

Fill sparsity of hessian into hessian storage.

Parameters
[in,out]storWe add variables into stor->cols

Implements Minotaur::NonlinearFunction.

◆ fillJac()

void CGraph::fillJac ( const double *  x,
double *  values,
int *  error 
)
virtual

Evaluate and add gradient at a given point to the jacobian.

Parameters
[in]xThe size of the array x must exceed the highest index of the variables used in the function.
[out]valuesThe values of jacobian are incremented with the gradient of this function at x. 'values' only contains nonzeros of jacobian. The indices (or offsets) where this nonlinear function should put in the values should be calculated in the prepJac function.
[out]errorIt should be set a zero value if there is error encountered while evaluating. Leave undisturbed otherwise.

Implements Minotaur::NonlinearFunction.

◆ finalHessStor()

void CGraph::finalHessStor ( const LTHessStor stor)
virtual

Finalize hessian preparation.

Parameters
[in]storcontains the sparsity pattern of hessian of lagrangian. The nonlinear function should save offsets or make other preparation to evaluate hessian.

Implements Minotaur::NonlinearFunction.

◆ finalize()

void CGraph::finalize ( )

After adding all the nodes of the graph, finalize is called to create the forward and backward traversal queues, and related book-keeping.

◆ getFixVarOffset()

double CGraph::getFixVarOffset ( VariablePtr  ,
double   
)
virtual

If a variable is fixed at a given value and removed, what is the constant (offset) needed to be added.

param [in] v The variable that is fixed.
param [in] val The value at which v is to be fixed.

Returns
Return the value of the offset.

Reimplemented from Minotaur::NonlinearFunction.

◆ getNlString()

std::string CGraph::getNlString ( int *  err)
virtual

Return a string in AMPL's .nl format (postfix notation) of this nonlinear function.

param [out] err 0 if no errors were encountered.

Returns
Return the string.

Reimplemented from Minotaur::NonlinearFunction.

◆ getPersp()

NonlinearFunctionPtr CGraph::getPersp ( VariablePtr  z,
double  eps,
int *  err 
) const
virtual

Take perspective of this function with respect to a given variable.

Perspective of a given function f(x) with respect to a given variable z is g(x,z) = z.f(x/z)

Parameters
[in]zThe variable for which you take the perspective
[in]epsThe tolerance to tackle function value at 0 value. If the perspective is approximated in the computational graph, the epsilon is added to the z variable. If the perspective uses exact graph, then an approximation in Hessian is used when z < eps.
[out]errmust be nonzero if function wasn't cloned.
Returns
A new nonlinear function with an additional variable that gives the perspective of this function

Reimplemented from Minotaur::NonlinearFunction.

◆ getType()

FunctionType CGraph::getType ( ) const
virtual

Return the type of function: polynomial, ...

Reimplemented from Minotaur::NonlinearFunction.

◆ getVars()

void CGraph::getVars ( VariableSet *  vars)
virtual

Get variables used in this function.

Parameters
[in]varsA set of variable-pointers into which variables are to be inserted.

Implements Minotaur::NonlinearFunction.

◆ isSumOfSquares()

bool CGraph::isSumOfSquares ( ) const
virtual

Check if the function is a sum of square terms.

Returns
True if this function is a sum of squares, False otherwise

Reimplemented from Minotaur::NonlinearFunction.

◆ multiply()

void CGraph::multiply ( double  c)
virtual

Multiply by a constant.

Parameters
[in]cdouble value with which we want to multiply.

Implements Minotaur::NonlinearFunction.

◆ newNode() [1/5]

CNode * CGraph::newNode ( double  d)

Create a new node with constant real value. It does not have any children.

Parameters
[in]dThe value.
Returns
The new node created in this function. Its opcode is OpNum

◆ newNode() [2/5]

CNode * CGraph::newNode ( int  i)

Create a new node with constant integer value. It does not have any children.

Parameters
[in]iThe value.
Returns
The new node created in this function. Its opcode is OpInt

◆ newNode() [3/5]

CNode * CGraph::newNode ( OpCode  op,
CNode **  child,
size_t  n 
)

Create a new node with more than two children, and add it to the graph. The children should already be a nodes of the graph.

Parameters
[in]opOpCode of the new node
[in]childAn array of pointers to children. It should be size 'n'.
[in]nThe size of array 'child'.
Returns
The new node created in this function.

◆ newNode() [4/5]

CNode * CGraph::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 nodes of the graph.

Parameters
[in]opOpCode of the new node
[in]lchildThe left child of the new node. It should never be NULL.
[in]rchildIf the opcode needs two children, e.g. OpPlus, OpMult etc, then rchild should be the right child. For univariate functions like OpSqrt, it should be NULL.
Returns
The new node created in this function.

◆ newNode() [5/5]

CNode * CGraph::newNode ( VariablePtr  v)

Create a new node denoting an input variable. It does not have any children.

The function checks if the variable already exists in the graph. If so, it returns the pointer to that node. Otherwise it creates a new node.

Parameters
[in]vThe variable.
Returns
The new node found or created in this function. Its opcode is OpVar

◆ prepJac()

void CGraph::prepJac ( VarSetConstIter  vbeg,
VarSetConstIter  vend 
)
virtual

Prepare for evaluating sparse jacobian.

All the variables that are in this function can occur in one or more of linear, quadratic and nonlinear functions. All variables that occur in the whole function can be accessed by iterating between vbeg and vend. This function is used to find the offset for variables that occur in the nonlinear function.

Implements Minotaur::NonlinearFunction.

◆ removeVar()

void CGraph::removeVar ( VariablePtr  ,
double   
)
virtual

Remove a variable v from the function after fixing it to value val.

Reimplemented from Minotaur::NonlinearFunction.

◆ setOut()

void CGraph::setOut ( CNode node)

Set the node that should be the output of this graph. This node should already be a part of this graph (created by newNode() function).

Parameters
[in]nodeThe node that is the output.

◆ sqrRoot()

void CGraph::sqrRoot ( int &  err)
virtual

Change the nonlinear function to its square-root.

Parameters
[out]errmust be nonzero if the operation is unsuccessful

Reimplemented from Minotaur::NonlinearFunction.

◆ subst()

void CGraph::subst ( VariablePtr  ,
VariablePtr  ,
double   
)
virtual

Substitute a variable with another.

Reimplemented from Minotaur::NonlinearFunction.

◆ varBoundMods()

void CGraph::varBoundMods ( double  ,
double  ,
VarBoundModVector &  ,
SolveStatus  
)
virtual

Tighten variables based on function bounds.

Given a lower and upper bound on the allowed values of the function, deduce bounds of variables and return the modifications in bounds. The modifications are not applied to the variables.

param [in] lb A lower bound on function values
param [in] ub An upper bound on function values
param [out] mods A vector of modifications deduced by bound propagation/tightening
param [out] status Tells whether there was an error in executing this routine or if the problem is infeasible etc.

Reimplemented from Minotaur::NonlinearFunction.

◆ write()

void CGraph::write ( std::ostream &  out) const
virtual

Display the nonlinear function.

Reimplemented from Minotaur::NonlinearFunction.


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