Scippy

SCIP

Solving Constraint Integer Programs

expr_varidx.c
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 expr_varidx.c
17  * @ingroup DEFPLUGINS_EXPR
18  * @brief handler for variable index expressions
19  * @author Benjamin Mueller
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/expr_varidx.h"
25 #include "scip/intervalarith.h"
26 #include "scip/pub_expr.h"
27 #include "scip/scip_expr.h"
28 #include "scip/scip_message.h"
29 #include "scip/pub_misc.h"
30 
31 /* fundamental expression handler properties */
32 #define EXPRHDLR_NAME "varidx"
33 #define EXPRHDLR_DESC "expression that represents a variable index (typically used for NLPI)"
34 #define EXPRHDLR_PRECEDENCE 0
35 #define EXPRHDLR_HASHKEY 20201210
36 
37 /*
38  * Callback methods of expression handler
39  */
40 
41 /** expression handler copy callback */
42 static
43 SCIP_DECL_EXPRCOPYHDLR(copyhdlrVaridx)
44 { /*lint --e{715}*/
46 
47  return SCIP_OKAY;
48 }
49 
50 /** expression compare callback */
51 static
52 SCIP_DECL_EXPRCOMPARE(compareVaridx)
53 { /*lint --e{715}*/
54  int idx1, idx2;
55 
56  assert(expr1 != NULL);
57  assert(expr2 != NULL);
58 
59  idx1 = SCIPgetIndexExprVaridx(expr1);
60  idx2 = SCIPgetIndexExprVaridx(expr2);
61 
62  if( idx1 < idx2 )
63  return -1;
64  if( idx1 > idx2 )
65  return 1;
66  return 0;
67 }
68 
69 /** expression data copy callback */
70 static
71 SCIP_DECL_EXPRCOPYDATA(copydataVaridx)
72 { /*lint --e{715}*/
73  assert(targetexprdata != NULL);
74 
75  *targetexprdata = SCIPexprGetData(sourceexpr);
76 
77  return SCIP_OKAY;
78 }
79 
80 /** expression data free callback */
81 static
82 SCIP_DECL_EXPRFREEDATA(freedataVaridx)
83 { /*lint --e{715}*/
84  assert(expr != NULL);
85 
86  SCIPexprSetData(expr, NULL);
87 
88  return SCIP_OKAY;
89 }
90 
91 /** expression print callback */
92 static
93 SCIP_DECL_EXPRPRINT(printVaridx)
94 { /*lint --e{715}*/
95  assert(expr != NULL);
96 
97  if( stage == SCIP_EXPRITER_ENTEREXPR )
98  {
99  SCIPinfoMessage(scip, file, "x%d", SCIPgetIndexExprVaridx(expr));
100  }
101 
102  return SCIP_OKAY;
103 }
104 
105 /** expression (point-) evaluation callback */
106 static
108 { /*lint --e{715}*/
109  assert(expr != NULL);
110 
111  SCIPerrorMessage("method of varidx expression handler not implemented yet\n");
112  SCIPABORT(); /*lint --e{527}*/
113 
114  return SCIP_OKAY;
115 }
116 
117 /** expression backward derivative evaluation callback */
118 static
119 SCIP_DECL_EXPRBWDIFF(bwdiffVaridx)
120 { /*lint --e{715}*/
121  /* this should never happen because variable expressions do not have children */
122  return SCIP_INVALIDCALL;
123 }
124 
125 /** expression forward derivative evaluation callback */
126 static
127 SCIP_DECL_EXPRFWDIFF(fwdiffVaridx)
128 { /*lint --e{715}*/
129  assert(expr != NULL);
130 
131  SCIPerrorMessage("method of varidx expression handler not implemented yet\n");
132  SCIPABORT(); /*lint --e{527}*/
133 
134  return SCIP_OKAY;
135 }
136 
137 /** expression backward-forward derivative evaluation callback */
138 static
139 SCIP_DECL_EXPRBWFWDIFF(bwfwdiffVaridx)
140 { /*lint --e{715}*/
141  /* this should never happen because variable expressions do not have children */
142  return SCIP_INVALIDCALL;
143 }
144 
145 /** varidx hash callback */
146 static
148 { /*lint --e{715}*/
149  assert(expr != NULL);
150  assert(hashkey != NULL);
151 
152  *hashkey = EXPRHDLR_HASHKEY;
154 
155  return SCIP_OKAY;
156 }
157 
158 /** expression curvature detection callback */
159 static
160 SCIP_DECL_EXPRCURVATURE(curvatureVaridx)
161 { /*lint --e{715}*/
162  assert(success != NULL);
163 
164  /* x -> x is linear, convex, and concave */
165  *success = TRUE;
166 
167  return SCIP_OKAY;
168 }
169 
170 /** expression monotonicity detection callback */
171 static
172 SCIP_DECL_EXPRMONOTONICITY(monotonicityVaridx)
173 { /*lint --e{715}*/
174  assert(result != NULL);
175 
176  *result = SCIP_MONOTONE_INC;
177 
178  return SCIP_OKAY;
179 }
180 
181 /** creates the handler for variable index expressions and includes it into SCIP */
183  SCIP* scip /**< SCIP data structure */
184  )
185 {
186  SCIP_EXPRHDLRDATA* exprhdlrdata;
187  SCIP_EXPRHDLR* exprhdlr;
188 
189  /* create expression handler data */
190  exprhdlrdata = NULL;
191 
192  /* include expression handler */
194  exprhdlrdata) );
195  assert(exprhdlr != NULL);
196 
197  SCIPexprhdlrSetCopyFreeHdlr(exprhdlr, copyhdlrVaridx, NULL);
198  SCIPexprhdlrSetCopyFreeData(exprhdlr, copydataVaridx, freedataVaridx);
199  SCIPexprhdlrSetCompare(exprhdlr, compareVaridx);
200  SCIPexprhdlrSetPrint(exprhdlr, printVaridx);
201  SCIPexprhdlrSetHash(exprhdlr, hashVaridx);
202  SCIPexprhdlrSetDiff(exprhdlr, bwdiffVaridx, fwdiffVaridx, bwfwdiffVaridx);
203  SCIPexprhdlrSetCurvature(exprhdlr, curvatureVaridx);
204  SCIPexprhdlrSetMonotonicity(exprhdlr, monotonicityVaridx);
205 
206  return SCIP_OKAY;
207 }
208 
209 /** creates a variable index expression */
211  SCIP* scip, /**< SCIP data structure */
212  SCIP_EXPR** expr, /**< pointer where to store expression */
213  int varidx, /**< variable index to represent */
214  SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
215  void* ownercreatedata /**< data to pass to ownercreate */
216  )
217 {
218  SCIP_EXPRHDLR* exprhdlr;
219  SCIP_EXPRDATA* exprdata;
220 
221  assert(expr != NULL);
222 
223  exprhdlr = SCIPfindExprhdlr(scip, EXPRHDLR_NAME);
224 
225  if( exprhdlr == NULL )
226  {
227  SCIPerrorMessage("could not find %s expression handler -> abort\n", EXPRHDLR_NAME);
228  SCIPABORT();
229  return SCIP_ERROR;
230  }
231 
232  /* create expression data */
233  exprdata = (SCIP_EXPRDATA*)(size_t)varidx; /*lint !e571*/
234 
235  /* create expression */
236  SCIP_CALL( SCIPcreateExpr(scip, expr, exprhdlr, exprdata, 0, NULL, ownercreate, ownercreatedata) );
237 
238  return SCIP_OKAY;
239 }
240 
241 /** indicates whether expression is varidx expression */ /*lint -e{715}*/
243  SCIP* scip, /**< SCIP data structure */
244  SCIP_EXPR* expr /**< expression */
245  )
246 { /*lint --e{715}*/
247  assert(expr != NULL);
248 
249  /* quick inconclusive check first */
250  if( SCIPexprGetNChildren(expr) > 0 )
251  return FALSE;
252 
253  return strcmp(SCIPexprhdlrGetName(SCIPexprGetHdlr(expr)), EXPRHDLR_NAME) == 0;
254 }
255 
256 /** gives the index stored in a varidx expression */
258  SCIP_EXPR* expr /**< varindex expression */
259  )
260 {
261  assert(expr != NULL);
262  assert(strcmp(SCIPexprhdlrGetName(SCIPexprGetHdlr(expr)), EXPRHDLR_NAME) == 0);
263 
264  return (int)(size_t)SCIPexprGetData(expr);
265 }
266 
267 /** sets the index stored in a varidx expression */
269  SCIP_EXPR* expr, /**< varindex expression */
270  int newindex /**< new index */
271  )
272 {
273  assert(expr != NULL);
274  assert(strcmp(SCIPexprhdlrGetName(SCIPexprGetHdlr(expr)), EXPRHDLR_NAME) == 0);
275  assert(newindex >= 0);
276 
277  SCIPexprSetData(expr, (SCIP_EXPRDATA*)(size_t)newindex); /*lint !e571*/
278 }
#define EXPRHDLR_HASHKEY
Definition: expr_varidx.c:35
#define EXPRHDLR_NAME
Definition: expr_varidx.c:32
int SCIPexprGetNChildren(SCIP_EXPR *expr)
Definition: expr.c:3798
void SCIPexprhdlrSetDiff(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRBWDIFF((*bwdiff)), SCIP_DECL_EXPRFWDIFF((*fwdiff)), SCIP_DECL_EXPRBWFWDIFF((*bwfwdiff)))
Definition: expr.c:464
const char * SCIPexprhdlrGetName(SCIP_EXPRHDLR *exprhdlr)
Definition: expr.c:525
static SCIP_DECL_EXPRCOPYDATA(copydataVaridx)
Definition: expr_varidx.c:71
#define FALSE
Definition: def.h:87
struct SCIP_ExprData SCIP_EXPRDATA
Definition: type_expr.h:44
#define TRUE
Definition: def.h:86
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
void SCIPexprhdlrSetHash(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRHASH((*hash)))
Definition: expr.c:442
#define EXPRHDLR_DESC
Definition: expr_varidx.c:33
static SCIP_DECL_EXPRCURVATURE(curvatureVaridx)
Definition: expr_varidx.c:160
static SCIP_DECL_EXPRFWDIFF(fwdiffVaridx)
Definition: expr_varidx.c:127
#define SCIP_EXPRITER_ENTEREXPR
Definition: type_expr.h:667
SCIP_EXPRHDLR * SCIPfindExprhdlr(SCIP *scip, const char *name)
Definition: scip_expr.c:859
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:199
public functions to work with algebraic expressions
SCIP_EXPRDATA * SCIPexprGetData(SCIP_EXPR *expr)
Definition: expr.c:3831
static SCIP_DECL_EXPRCOMPARE(compareVaridx)
Definition: expr_varidx.c:52
handler for variable index expressions
SCIP_RETCODE SCIPcreateExpr(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPRHDLR *exprhdlr, SCIP_EXPRDATA *exprdata, int nchildren, SCIP_EXPR **children, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:964
#define SCIPerrorMessage
Definition: pub_message.h:55
interval arithmetics for provable bounds
static SCIP_DECL_EXPRBWDIFF(bwdiffVaridx)
Definition: expr_varidx.c:119
static SCIP_DECL_EXPRHASH(hashVaridx)
Definition: expr_varidx.c:147
public functions to work with algebraic expressions
SCIP_RETCODE SCIPincludeExprhdlrVaridx(SCIP *scip)
Definition: expr_varidx.c:182
#define NULL
Definition: lpi_spx1.cpp:155
static SCIP_DECL_EXPRBWFWDIFF(bwfwdiffVaridx)
Definition: expr_varidx.c:139
#define SCIP_CALL(x)
Definition: def.h:384
void SCIPexprhdlrSetCopyFreeHdlr(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOPYHDLR((*copyhdlr)), SCIP_DECL_EXPRFREEHDLR((*freehdlr)))
Definition: expr.c:359
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:84
static SCIP_DECL_EXPRFREEDATA(freedataVaridx)
Definition: expr_varidx.c:82
#define SCIP_DECL_EXPR_OWNERCREATE(x)
Definition: type_expr.h:131
void SCIPexprhdlrSetMonotonicity(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRMONOTONICITY((*monotonicity)))
Definition: expr.c:420
void SCIPsetIndexExprVaridx(SCIP_EXPR *expr, int newindex)
Definition: expr_varidx.c:268
struct SCIP_ExprhdlrData SCIP_EXPRHDLRDATA
Definition: type_expr.h:183
SCIP_EXPRHDLR * SCIPexprGetHdlr(SCIP_EXPR *expr)
Definition: expr.c:3821
void SCIPexprhdlrSetPrint(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRPRINT((*print)))
Definition: expr.c:387
void SCIPexprhdlrSetCopyFreeData(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOPYDATA((*copydata)), SCIP_DECL_EXPRFREEDATA((*freedata)))
Definition: expr.c:372
void SCIPexprhdlrSetCompare(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOMPARE((*compare)))
Definition: expr.c:453
void SCIPexprhdlrSetCurvature(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCURVATURE((*curvature)))
Definition: expr.c:409
void SCIPexprSetData(SCIP_EXPR *expr, SCIP_EXPRDATA *exprdata)
Definition: expr.c:3846
static SCIP_DECL_EXPRMONOTONICITY(monotonicityVaridx)
Definition: expr_varidx.c:172
SCIP_Bool SCIPisExprVaridx(SCIP *scip, SCIP_EXPR *expr)
Definition: expr_varidx.c:242
#define SCIP_Real
Definition: def.h:177
public methods for message handling
#define EXPRHDLR_PRECEDENCE
Definition: expr_varidx.c:34
unsigned int SCIPcalcFibHash(SCIP_Real v)
Definition: misc.c:10242
static SCIP_DECL_EXPREVAL(evalVaridx)
Definition: expr_varidx.c:107
static SCIP_DECL_EXPRPRINT(printVaridx)
Definition: expr_varidx.c:93
SCIP_RETCODE SCIPincludeExprhdlr(SCIP *scip, SCIP_EXPRHDLR **exprhdlr, const char *name, const char *desc, unsigned int precedence, SCIP_DECL_EXPREVAL((*eval)), SCIP_EXPRHDLRDATA *data)
Definition: scip_expr.c:814
#define SCIPABORT()
Definition: def.h:356
int SCIPgetIndexExprVaridx(SCIP_EXPR *expr)
Definition: expr_varidx.c:257
SCIP_RETCODE SCIPcreateExprVaridx(SCIP *scip, SCIP_EXPR **expr, int varidx, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: expr_varidx.c:210
static SCIP_DECL_EXPRCOPYHDLR(copyhdlrVaridx)
Definition: expr_varidx.c:43