Scippy

SCIP

Solving Constraint Integer Programs

struct_expr.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file struct_expr.h
17  * @ingroup INTERNALAPI
18  * @brief structure definitions related to algebraic expressions
19  * @author Ksenia Bestuzheva
20  * @author Benjamin Mueller
21  * @author Felipe Serrano
22  * @author Stefan Vigerske
23  */
24 
25 #ifndef SCIP_STRUCT_EXPR_H_
26 #define SCIP_STRUCT_EXPR_H_
27 
28 #include "scip/type_expr.h"
29 #include "scip/type_clock.h"
30 #include "scip/type_stat.h"
31 #include "blockmemshell/memory.h"
32 
33 /** generic data and callback methods of an expression handler */
35 {
36  char* name; /**< expression handler name */
37  char* desc; /**< expression handler description (can be NULL) */
38  SCIP_EXPRHDLRDATA* data; /**< data of handler */
39  unsigned int precedence; /**< precedence of expression operation relative to other expression (used for printing) */
40 
41  /* callbacks */
42  SCIP_DECL_EXPRCOPYHDLR((*copyhdlr)); /**< handler copy callback (can be NULL) */
43  SCIP_DECL_EXPRFREEHDLR((*freehdlr)); /**< handler free callback (can be NULL) */
44  SCIP_DECL_EXPRCOPYDATA((*copydata)); /**< data copy callback, or NULL for expressions that have no data */
45  SCIP_DECL_EXPRFREEDATA((*freedata)); /**< data free callback, or NULL for expressions that have no data or which data does not need to be freed */
46  SCIP_DECL_EXPRSIMPLIFY((*simplify)); /**< simplify callback (can be NULL) */
47  SCIP_DECL_EXPRCOMPARE((*compare)); /**< compare callback (can be NULL) */
48  SCIP_DECL_EXPRPRINT((*print)); /**< print callback (can be NULL) */
49  SCIP_DECL_EXPRPARSE((*parse)); /**< parse callback (can be NULL) */
50  SCIP_DECL_EXPREVAL((*eval)); /**< point evaluation callback (can never be NULL) */
51  SCIP_DECL_EXPRBWDIFF((*bwdiff)); /**< backward derivative evaluation callback (can be NULL) */
52  SCIP_DECL_EXPRFWDIFF((*fwdiff)); /**< forward derivative evaluation callback (can be NULL) */
53  SCIP_DECL_EXPRBWFWDIFF((*bwfwdiff)); /**< backward over forward derivative evaluation callback (can be NULL) */
54  SCIP_DECL_EXPRINTEVAL((*inteval)); /**< interval evaluation callback (can be NULL) */
55  SCIP_DECL_EXPRESTIMATE((*estimate)); /**< estimation callback (can be NULL) */
56  SCIP_DECL_EXPRINITESTIMATES((*initestimates)); /**< initial estimators callback (can be NULL) */
57  SCIP_DECL_EXPRREVERSEPROP((*reverseprop));/**< reverse propagation callback (can be NULL) */
58  SCIP_DECL_EXPRHASH((*hash)); /**< hash callback (can be NULL) */
59  SCIP_DECL_EXPRCURVATURE((*curvature)); /**< curvature detection callback (can be NULL) */
60  SCIP_DECL_EXPRMONOTONICITY((*monotonicity)); /**< monotonicity detection callback (can be NULL) */
61  SCIP_DECL_EXPRINTEGRALITY((*integrality));/**< integrality detection callback (can be NULL) */
62 
63  /* statistics */
64  unsigned int ncreated; /**< number of times expression has been created */
65  SCIP_Longint nestimatecalls; /**< number of times the estimation callback was called */
66  SCIP_Longint nintevalcalls; /**< number of times the interval evaluation callback was called */
67  SCIP_Longint npropcalls; /**< number of times the propagation callback was called */
68  SCIP_Longint ncutoffs; /**< number of cutoffs found so far by this expression handler */
69  SCIP_Longint ndomreds; /**< number of domain reductions found so far by this expression handler */
70  SCIP_Longint nsimplifycalls; /**< number of times the simplification callback was called */
71  SCIP_Longint nsimplified; /**< number of times the simplification callback simplified */
72  SCIP_Longint nbranchscores; /**< number of times branching scores were added by (or for) this expression handler */
73 
74  SCIP_CLOCK* estimatetime; /**< time used for estimation */
75  SCIP_CLOCK* intevaltime; /**< time used for interval evaluation */
76  SCIP_CLOCK* proptime; /**< time used for propagation */
77  SCIP_CLOCK* simplifytime; /**< time used for expression simplification */
78 };
79 
80 /** expression iteration data stored in an expression */
81 struct SCIP_ExprIterData
82 {
83  SCIP_EXPR* parent; /**< parent expression in DFS iteration */
84  int currentchild; /**< child that is currently visited (or will be visited next) by DFS iteration */
85  SCIP_Longint visitedtag; /**< tag to identify whether an expression has been visited already */
86  SCIP_EXPRITER_USERDATA userdata; /**< space for iterator user to store some (temporary) data */
87 };
88 
89 /* private types */
90 typedef struct SCIP_QuadExpr SCIP_QUADEXPR; /**< representation of expression as quadratic */
91 typedef struct SCIP_QuadExpr_QuadTerm SCIP_QUADEXPR_QUADTERM; /**< a single term associated to a quadratic variable */
92 typedef struct SCIP_QuadExpr_BilinTerm SCIP_QUADEXPR_BILINTERM; /**< a single bilinear term */
93 
94 /** an algebraic expression */
95 struct SCIP_Expr
96 {
97  SCIP_EXPRHDLR* exprhdlr; /**< expression type (as pointer to its handler) */
98  SCIP_EXPRDATA* exprdata; /**< expression data */
99 
100  int nchildren; /**< number of children */
101  int childrensize; /**< length of children array */
102  SCIP_EXPR** children; /**< children expressions */
103 
104  int nuses; /**< reference counter */
105  SCIP_EXPRITERDATA iterdata[SCIP_EXPRITER_MAXNACTIVE]; /**< data for expression iterators */
106 
107  /* owner data */
108  SCIP_EXPR_OWNERDATA* ownerdata; /**< data stored by owner of expression */
109  SCIP_DECL_EXPR_OWNERFREE((*ownerfree)); /**< callback for freeing ownerdata */
110  SCIP_DECL_EXPR_OWNERPRINT((*ownerprint)); /**< callback for printing ownerdata */
111  SCIP_DECL_EXPR_OWNEREVALACTIVITY((*ownerevalactivity)); /**< callback for evaluating activity */
112 
113  /* point-evaluation and differentiation*/
114  SCIP_Real evalvalue; /**< value of expression from last evaluation (corresponding to evaltag) */
115  SCIP_Real derivative; /**< partial derivative of a "root path" w.r.t. this expression (see \ref SCIP_EXPR_DIFF "Differentiation methods") */
116  SCIP_Real dot; /**< directional derivative of this expr (see \ref SCIP_EXPR_DIFF "Differentiation methods") */
117  SCIP_Real bardot; /**< directional derivative of derivative of root (strictly speaking, a path) w.r.t this expr (see \ref SCIP_EXPR_DIFF "Differentiation methods") */
118  SCIP_Longint evaltag; /**< tag of point for which the expression has been evaluated last, or 0 */
119  SCIP_Longint difftag; /**< when computing partial derivatives of an expression w.r.t. a variable,
120  * the tag allows us to decide whether the expression depends on the variable;
121  * the tag will be checked in SCIPgetExprPartialDiff() */
122 
123  /* interval-evaluation (activity) */
124  SCIP_INTERVAL activity; /**< activity of expression with respect to variable bounds */
125  SCIP_Longint activitytag; /**< tag of variable bounds for which activity is valid */
126 
127  /* curvature information */
128  SCIP_EXPRCURV curvature; /**< curvature of the expression w.r.t. bounds that have been used in the last curvature detection */
129 
130  /* integrality information */
131  SCIP_Bool isintegral; /**< whether expression value is integral in feasible solutions */
132 
133  /* view expression as quadratic */
134  SCIP_QUADEXPR* quaddata; /**< representation of expression as a quadratic, if checked and being quadratic */
135  SCIP_Bool quadchecked; /**< whether it has been checked whether the expression is quadratic */
136 };
137 
138 /** representation of an expression as quadratic */
140 {
141  SCIP_Real constant; /**< a constant term */
142 
143  int nlinexprs; /**< number of linear terms */
144  SCIP_EXPR** linexprs; /**< expressions of linear terms */
145  SCIP_Real* lincoefs; /**< coefficients of linear terms */
146 
147  int nquadexprs; /**< number of expressions in quadratic terms */
148  SCIP_QUADEXPR_QUADTERM* quadexprterms; /**< array with quadratic expression terms */
149 
150  int nbilinexprterms; /**< number of bilinear expressions terms */
151  SCIP_QUADEXPR_BILINTERM* bilinexprterms; /**< bilinear expression terms array */
152 
153  SCIP_Bool allexprsarevars; /**< whether all arguments (linexprs, quadexprterms[.].expr) are variable expressions */
154 
155  SCIP_EXPRCURV curvature; /**< curvature of the quadratic representation of the expression */
156  SCIP_Bool curvaturechecked; /**< whether curvature has been checked */
157 
158  /* eigen decomposition information */
159  SCIP_Bool eigeninfostored; /**< whether the eigen information is stored */
160  SCIP_Real* eigenvalues; /**< eigenvalues of the Q matrix: size of nquadexprs */
161  SCIP_Real* eigenvectors; /**< eigenvalues of the Q matrix: size of nquadexprs^2 */
162 };
163 
164 /** a quadratic term associated to an expression */
166 {
167  SCIP_EXPR* expr; /**< expression of quadratic term */
168  SCIP_Real lincoef; /**< linear coefficient of expression */
169  SCIP_Real sqrcoef; /**< square coefficient of expression */
170 
171  int nadjbilin; /**< number of bilinear terms this expression is involved in */
172  int adjbilinsize; /**< size of adjacent bilinear terms array */
173  int* adjbilin; /**< indices of associated bilinear terms */
174 
175  SCIP_EXPR* sqrexpr; /**< expression that was found to be the square of expr, or NULL if no square term (sqrcoef==0) */
176 };
177 
178 /** a single bilinear term coef * expr1 * expr2
179  *
180  * except for temporary reasons, we assume that the index of var1 is smaller than the index of var2
181  */
183 {
184  SCIP_EXPR* expr1; /**< first factor of bilinear term */
185  SCIP_EXPR* expr2; /**< second factor of bilinear term */
186  SCIP_Real coef; /**< coefficient of bilinear term */
187  int pos2; /**< position of expr2's quadexprterm in quadexprterms */
188 
189  SCIP_EXPR* prodexpr; /**< expression that was found to be the product of expr1 and expr2 */
190 };
191 
192 /** expression iterator */
194 {
195  BMS_BLKMEM* blkmem; /**< block memory */
196  SCIP_STAT* stat; /**< dynamic problem statistics */
197 
198  SCIP_Bool initialized; /**< whether the iterator has been initialized, that is, is in use */
199  SCIP_EXPRITER_TYPE itertype; /**< type of expression iterator */
200  SCIP_EXPR* curr; /**< current expression of the iterator */
201  int iterindex; /**< index of iterator data in expressions, or -1 if not using iterator data in expressions */
202  SCIP_Longint visitedtag; /**< tag to mark and recognize an expression as visited, or 0 if not avoiding multiple visits */
203 
204  /* data for rtopological mode */
205  SCIP_EXPR** dfsexprs; /**< DFS stack */
206  int* dfsnvisited; /**< number of visited children for each expression in the stack */
207  int dfsnexprs; /**< total number of expression in stack */
208  int dfssize; /**< size of DFS stack */
209 
210  /* data for BFS mode */
211  SCIP_QUEUE* queue; /**< BFS queue */
212 
213  /* data for DFS mode */
214  SCIP_EXPRITER_STAGE dfsstage; /**< current stage */
215  unsigned int stopstages; /**< stages in which to interrupt iterator */
216 };
217 
218 #endif /* SCIP_STRUCT_EXPR_H_ */
static SCIP_RETCODE eval(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPRINTDATA *exprintdata, const vector< Type > &x, Type &val)
SCIP_Bool initialized
Definition: struct_expr.h:198
SCIP_Bool allexprsarevars
Definition: struct_expr.h:153
SCIP_EXPRCURV curvature
Definition: struct_expr.h:155
SCIP_DECL_EXPRINITESTIMATES((*initestimates))
SCIP_EXPR_OWNERDATA * ownerdata
Definition: struct_expr.h:108
SCIP_EXPR ** linexprs
Definition: struct_expr.h:144
unsigned int ncreated
Definition: struct_expr.h:64
SCIP_Longint evaltag
Definition: struct_expr.h:118
SCIP_Real constant
Definition: struct_expr.h:141
SCIP_EXPRCURV curvature
Definition: struct_expr.h:128
SCIP_DECL_EXPRFWDIFF((*fwdiff))
BMS_BLKMEM * blkmem
Definition: struct_expr.h:195
struct SCIP_ExprData SCIP_EXPRDATA
Definition: type_expr.h:44
SCIP_Longint nsimplifycalls
Definition: struct_expr.h:70
SCIP_DECL_EXPRINTEVAL((*inteval))
SCIP_EXPRITER_TYPE
Definition: type_expr.h:687
SCIP_Longint nintevalcalls
Definition: struct_expr.h:66
SCIP_DECL_EXPRINTEGRALITY((*integrality))
struct SCIP_Expr_OwnerData SCIP_EXPR_OWNERDATA
Definition: type_expr.h:68
type definitions for problem statistics
unsigned int precedence
Definition: struct_expr.h:39
int nchildren
Definition: struct_expr.h:100
SCIP_DECL_EXPRFREEHDLR((*freehdlr))
SCIP_Bool isintegral
Definition: struct_expr.h:131
#define SCIP_DECL_EXPR_OWNERFREE(x)
Definition: type_expr.h:83
SCIP_DECL_EXPREVAL((*eval))
SCIP_QUADEXPR_QUADTERM * quadexprterms
Definition: struct_expr.h:148
SCIP_DECL_EXPRPARSE((*parse))
int * dfsnvisited
Definition: struct_expr.h:206
SCIP_DECL_EXPRESTIMATE((*estimate))
SCIP_Longint visitedtag
Definition: struct_expr.h:202
SCIP_CLOCK * estimatetime
Definition: struct_expr.h:74
SCIP_DECL_EXPRREVERSEPROP((*reverseprop))
SCIP_CLOCK * simplifytime
Definition: struct_expr.h:77
void print(const Container &container, const std::string &prefix="", const std::string &suffix="", std::ostream &os=std::cout, bool negate=false, int prec=6)
SCIP_DECL_EXPRPRINT((*print))
SCIP_CLOCK * intevaltime
Definition: struct_expr.h:75
SCIP_INTERVAL activity
Definition: struct_expr.h:124
SCIP_EXPRDATA * exprdata
Definition: struct_expr.h:98
SCIP_Longint npropcalls
Definition: struct_expr.h:67
struct SCIP_ExprIterData SCIP_EXPRITERDATA
Definition: type_expr.h:694
SCIP_Real * lincoefs
Definition: struct_expr.h:145
SCIP_CLOCK * proptime
Definition: struct_expr.h:76
SCIP_EXPRITER_TYPE itertype
Definition: struct_expr.h:199
SCIP_EXPR ** dfsexprs
Definition: struct_expr.h:205
SCIP_Bool eigeninfostored
Definition: struct_expr.h:159
SCIP_DECL_EXPRHASH((*hash))
int childrensize
Definition: struct_expr.h:101
#define SCIP_Bool
Definition: def.h:84
SCIP_Longint nbranchscores
Definition: struct_expr.h:72
SCIP_EXPRCURV
Definition: type_expr.h:48
#define SCIP_DECL_EXPR_OWNEREVALACTIVITY(x)
Definition: type_expr.h:113
SCIP_QUEUE * queue
Definition: struct_expr.h:211
SCIP_Longint nestimatecalls
Definition: struct_expr.h:65
SCIP_EXPRITER_STAGE dfsstage
Definition: struct_expr.h:214
SCIP_EXPR ** children
Definition: struct_expr.h:102
SCIP_EXPR * curr
Definition: struct_expr.h:200
struct SCIP_ExprhdlrData SCIP_EXPRHDLRDATA
Definition: type_expr.h:183
SCIP_Bool curvaturechecked
Definition: struct_expr.h:156
type definitions for clocks and timing issues
SCIP_Longint difftag
Definition: struct_expr.h:119
SCIP_DECL_EXPRCOPYDATA((*copydata))
SCIP_Longint nsimplified
Definition: struct_expr.h:71
SCIP_Real * eigenvectors
Definition: struct_expr.h:161
#define SCIP_EXPRITER_MAXNACTIVE
Definition: type_expr.h:664
SCIP_DECL_EXPRCURVATURE((*curvature))
#define SCIP_DECL_EXPR_OWNERPRINT(x)
Definition: type_expr.h:97
SCIP_Longint ncutoffs
Definition: struct_expr.h:68
SCIP_DECL_EXPRCOMPARE((*compare))
SCIP_EXPRHDLR * exprhdlr
Definition: struct_expr.h:97
SCIP_DECL_EXPRCOPYHDLR((*copyhdlr))
SCIP_QUADEXPR_BILINTERM * bilinexprterms
Definition: struct_expr.h:151
type and macro definitions related to algebraic expressions
SCIP_Real * eigenvalues
Definition: struct_expr.h:160
SCIP_STAT * stat
Definition: struct_expr.h:196
#define SCIP_Real
Definition: def.h:177
SCIP_DECL_EXPRFREEDATA((*freedata))
SCIP_DECL_EXPRBWFWDIFF((*bwfwdiff))
SCIP_Real derivative
Definition: struct_expr.h:115
#define SCIP_Longint
Definition: def.h:162
SCIP_DECL_EXPRMONOTONICITY((*monotonicity))
SCIP_Longint ndomreds
Definition: struct_expr.h:69
unsigned int stopstages
Definition: struct_expr.h:215
SCIP_Real evalvalue
Definition: struct_expr.h:114
SCIP_Real bardot
Definition: struct_expr.h:117
SCIP_QUADEXPR * quaddata
Definition: struct_expr.h:134
SCIP_DECL_EXPRBWDIFF((*bwdiff))
unsigned int SCIP_EXPRITER_STAGE
Definition: type_expr.h:674
struct BMS_BlkMem BMS_BLKMEM
Definition: memory.h:430
SCIP_EXPRHDLRDATA * data
Definition: struct_expr.h:38
SCIP_DECL_EXPRSIMPLIFY((*simplify))
SCIP_Real dot
Definition: struct_expr.h:116
SCIP_Bool quadchecked
Definition: struct_expr.h:135
memory allocation routines
SCIP_Longint activitytag
Definition: struct_expr.h:125