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