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