Scippy

SCIP

Solving Constraint Integer Programs

expr_value.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_value.c
17  * @ingroup DEFPLUGINS_EXPR
18  * @brief constant value expression handler
19  * @author Stefan Vigerske
20  * @author Benjamin Mueller
21  */
22 
23 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
24 
25 #include <string.h>
26 
27 #include "scip/expr_value.h"
28 
29 #define EXPRHDLR_NAME "val"
30 #define EXPRHDLR_DESC "constant value"
31 #define EXPRHDLR_PRECEDENCE 10000
32 #define EXPRHDLR_HASHKEY SCIPcalcFibHash(36787.0)
33 
34 /*
35  * Data structures
36  */
37 
38 /** expression data */
39 struct SCIP_ExprData
40 {
41  SCIP_Real value; /**< value that expression represents */
42 };
43 
44 /*
45  * Callback methods of expression handler
46  */
47 
48 /** the order of two values is the real order */
49 static
50 SCIP_DECL_EXPRCOMPARE(compareValue)
51 { /*lint --e{715}*/
52  SCIP_Real val1;
53  SCIP_Real val2;
54 
55  assert(SCIPexprGetData(expr1) != NULL);
56  assert(SCIPexprGetData(expr2) != NULL);
57 
58  val1 = SCIPexprGetData(expr1)->value;
59  val2 = SCIPexprGetData(expr2)->value;
60 
61  return val1 < val2 ? -1 : val1 == val2 ? 0 : 1; /*lint !e777*/
62 }
63 
64 /** expression handler copy callback */
65 static
66 SCIP_DECL_EXPRCOPYHDLR(copyhdlrValue)
67 { /*lint --e{715}*/
69 
70  return SCIP_OKAY;
71 }
72 
73 /** expression data copy callback */
74 static
75 SCIP_DECL_EXPRCOPYDATA(copydataValue)
76 { /*lint --e{715}*/
77  assert(targetexprdata != NULL);
78  assert(sourceexpr != NULL);
79 
80  SCIP_CALL( SCIPallocBlockMemory(targetscip, targetexprdata) );
81  (*targetexprdata)->value = SCIPexprGetData(sourceexpr)->value;
82 
83  return SCIP_OKAY;
84 }
85 
86 /** expression data free callback */
87 static
88 SCIP_DECL_EXPRFREEDATA(freedataValue)
89 { /*lint --e{715}*/
90  SCIP_EXPRDATA* exprdata;
91 
92  assert(expr != NULL);
93 
94  exprdata = SCIPexprGetData(expr);
95  assert(exprdata != NULL);
96 
97  SCIPfreeBlockMemory(scip, &exprdata);
98  SCIPexprSetData(expr, NULL);
99 
100  return SCIP_OKAY;
101 }
102 
103 /** expression print callback */
104 static
106 { /*lint --e{715}*/
107  assert(expr != NULL);
108  assert(SCIPexprGetData(expr) != NULL);
109 
110  if( stage == SCIP_EXPRITER_ENTEREXPR )
111  {
112  SCIP_Real v = SCIPexprGetData(expr)->value;
113  if( v < 0.0 && EXPRHDLR_PRECEDENCE <= parentprecedence )
114  {
115  SCIPinfoMessage(scip, file, "(%g)", v);
116  }
117  else
118  {
119  SCIPinfoMessage(scip, file, "%g", v);
120  }
121  }
122 
123  return SCIP_OKAY;
124 }
125 
126 /** expression point evaluation callback */
127 static
129 { /*lint --e{715}*/
130  assert(expr != NULL);
131  assert(SCIPexprGetData(expr) != NULL);
132 
133  *val = SCIPexprGetData(expr)->value;
134 
135  return SCIP_OKAY;
136 }
137 
138 /** expression backward derivative evaluation callback */
139 static
141 { /*lint --e{715}*/
142  /* should never be called since value expressions do not have children */
143  return SCIP_INVALIDCALL;
144 }
145 
146 /** expression forward derivative evaluation callback */
147 static
149 { /*lint --e{715}*/
150  assert(expr != NULL);
151 
152  *dot = 0.0;
153 
154  return SCIP_OKAY;
155 }
156 
157 /** derivative evaluation callback for Hessian directions (backward over forward) */
158 static
160 { /*lint --e{715}*/
161  /* should never be called since value expressions do not have children */
162  return SCIP_INVALIDCALL;
163 }
164 
165 /** expression interval evaluation callback */
166 static
168 { /*lint --e{715}*/
169  assert(expr != NULL);
170  assert(SCIPexprGetData(expr) != NULL);
171 
172  SCIPintervalSet(interval, SCIPexprGetData(expr)->value);
173 
174  return SCIP_OKAY;
175 }
176 
177 /** expression hash callback */
178 static
180 { /*lint --e{715}*/
181  assert(scip != NULL);
182  assert(expr != NULL);
183  assert(SCIPexprGetData(expr) != NULL);
184  assert(SCIPexprGetNChildren(expr) == 0);
185  assert(hashkey != NULL);
186 
187  *hashkey = EXPRHDLR_HASHKEY;
188  *hashkey ^= SCIPcalcFibHash(SCIPexprGetData(expr)->value);
189 
190  return SCIP_OKAY;
191 }
192 
193 /** expression curvature detection callback */
194 static
195 SCIP_DECL_EXPRCURVATURE(curvatureValue)
196 { /*lint --e{715}*/
197  assert(scip != NULL);
198  assert(expr != NULL);
199  assert(success != NULL);
200  assert(SCIPexprGetNChildren(expr) == 0);
201 
202  *success = TRUE;
203 
204  return SCIP_OKAY;
205 }
206 
207 /** expression monotonicity detection callback */
208 static
209 SCIP_DECL_EXPRMONOTONICITY(monotonicityValue)
210 { /*lint --e{715}*/
211  assert(scip != NULL);
212  assert(expr != NULL);
213  assert(result != NULL);
214  assert(SCIPexprGetNChildren(expr) == 0);
215 
216  *result = SCIP_MONOTONE_CONST;
217 
218  return SCIP_OKAY;
219 }
220 
221 /** expression integrality detection callback */
222 static
223 SCIP_DECL_EXPRINTEGRALITY(integralityValue)
224 { /*lint --e{715}*/
225  assert(scip != NULL);
226  assert(expr != NULL);
227  assert(isintegral != NULL);
228  assert(SCIPexprGetData(expr) != NULL);
229 
230  *isintegral = EPSISINT(SCIPexprGetData(expr)->value, 0.0); /*lint !e835 !e666*/
231 
232  return SCIP_OKAY;
233 }
234 
235 /** creates the handler for constant value expression and includes it into SCIP */
237  SCIP* scip /**< SCIP data structure */
238  )
239 {
240  SCIP_EXPRHDLR* exprhdlr;
241 
243  evalValue, NULL) );
244  assert(exprhdlr != NULL);
245 
246  SCIPexprhdlrSetCopyFreeHdlr(exprhdlr, copyhdlrValue, NULL);
247  SCIPexprhdlrSetCopyFreeData(exprhdlr, copydataValue, freedataValue);
248  SCIPexprhdlrSetCompare(exprhdlr, compareValue);
249  SCIPexprhdlrSetPrint(exprhdlr, printValue);
250  SCIPexprhdlrSetIntEval(exprhdlr, intevalValue);
251  SCIPexprhdlrSetHash(exprhdlr, hashValue);
252  SCIPexprhdlrSetDiff(exprhdlr, bwdiffValue, fwdiffValue, bwfwdiffValue);
253  SCIPexprhdlrSetCurvature(exprhdlr, curvatureValue);
254  SCIPexprhdlrSetMonotonicity(exprhdlr, monotonicityValue);
255  SCIPexprhdlrSetIntegrality(exprhdlr, integralityValue);
256 
257  return SCIP_OKAY;
258 }
259 
260 /** creates constant value expression */
262  SCIP* scip, /**< SCIP data structure */
263  SCIP_EXPR** expr, /**< pointer where to store expression */
264  SCIP_Real value, /**< value to be stored */
265  SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
266  void* ownercreatedata /**< data to pass to ownercreate */
267  )
268 {
269  SCIP_EXPRDATA* exprdata;
270 
271  assert(expr != NULL);
272  assert(SCIPisFinite(value));
273 
274  SCIP_CALL( SCIPallocBlockMemory(scip, &exprdata) );
275  exprdata->value = value;
276 
277  SCIP_CALL( SCIPcreateExpr(scip, expr, SCIPgetExprhdlrValue(scip), exprdata, 0, NULL, ownercreate, ownercreatedata) );
278 
279  return SCIP_OKAY;
280 }
281 
282 /* from pub_expr.h */
283 
284 /** gets the value of a constant value expression */
286  SCIP_EXPR* expr /**< sum expression */
287  )
288 {
289  assert(expr != NULL);
290  assert(SCIPexprGetData(expr) != NULL);
291 
292  return SCIPexprGetData(expr)->value;
293 }
SCIP_RETCODE SCIPincludeExprhdlrValue(SCIP *scip)
Definition: expr_value.c:236
static SCIP_DECL_EXPRINTEGRALITY(integralityValue)
Definition: expr_value.c:223
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
static SCIP_DECL_EXPRMONOTONICITY(monotonicityValue)
Definition: expr_value.c:209
static SCIP_DECL_EXPRCOPYHDLR(copyhdlrValue)
Definition: expr_value.c:66
#define EPSISINT(x, eps)
Definition: def.h:214
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
static SCIP_DECL_EXPRFWDIFF(fwdiffValue)
Definition: expr_value.c:148
void SCIPintervalSet(SCIP_INTERVAL *resultant, SCIP_Real value)
void SCIPexprhdlrSetIntegrality(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRINTEGRALITY((*integrality)))
Definition: expr.c:431
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:99
#define SCIP_EXPRITER_ENTEREXPR
Definition: type_expr.h:667
static void printValue(SCIP *scip, FILE *file, SCIP_Real value, FZNNUMBERTYPE type)
Definition: reader_fzn.c:824
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:199
static SCIP_DECL_EXPRBWDIFF(bwdiffValue)
Definition: expr_value.c:140
SCIP_EXPRDATA * SCIPexprGetData(SCIP_EXPR *expr)
Definition: expr.c:3831
static SCIP_DECL_EXPRCOPYDATA(copydataValue)
Definition: expr_value.c:75
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 NULL
Definition: lpi_spx1.cpp:155
#define SCIP_CALL(x)
Definition: def.h:384
SCIP_RETCODE SCIPcreateExprValue(SCIP *scip, SCIP_EXPR **expr, SCIP_Real value, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: expr_value.c:261
#define EXPRHDLR_NAME
Definition: expr_value.c:29
void SCIPexprhdlrSetCopyFreeHdlr(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOPYHDLR((*copyhdlr)), SCIP_DECL_EXPRFREEHDLR((*freehdlr)))
Definition: expr.c:359
static SCIP_DECL_EXPRBWFWDIFF(bwfwdiffValue)
Definition: expr_value.c:159
#define EXPRHDLR_HASHKEY
Definition: expr_value.c:32
SCIP_EXPRHDLR * SCIPgetExprhdlrValue(SCIP *scip)
Definition: scip_expr.c:882
static SCIP_DECL_EXPRHASH(hashValue)
Definition: expr_value.c:179
static SCIP_DECL_EXPRCURVATURE(curvatureValue)
Definition: expr_value.c:195
#define SCIP_DECL_EXPR_OWNERCREATE(x)
Definition: type_expr.h:131
void SCIPexprhdlrSetMonotonicity(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRMONOTONICITY((*monotonicity)))
Definition: expr.c:420
static SCIP_DECL_EXPRFREEDATA(freedataValue)
Definition: expr_value.c:88
#define EXPRHDLR_PRECEDENCE
Definition: expr_value.c:31
void SCIPexprhdlrSetPrint(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRPRINT((*print)))
Definition: expr.c:387
static SCIP_DECL_EXPRCOMPARE(compareValue)
Definition: expr_value.c:50
void SCIPexprhdlrSetCopyFreeData(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOPYDATA((*copydata)), SCIP_DECL_EXPRFREEDATA((*freedata)))
Definition: expr.c:372
constant value expression handler
void SCIPexprhdlrSetCompare(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRCOMPARE((*compare)))
Definition: expr.c:453
static SCIP_DECL_EXPRPRINT(printValue)
Definition: expr_value.c:105
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
#define EXPRHDLR_DESC
Definition: expr_value.c:30
static SCIP_DECL_EXPRINTEVAL(intevalValue)
Definition: expr_value.c:167
#define SCIP_Real
Definition: def.h:177
SCIP_Real SCIPgetValueExprValue(SCIP_EXPR *expr)
Definition: expr_value.c:285
#define SCIPisFinite(x)
Definition: pub_misc.h:1892
unsigned int SCIPcalcFibHash(SCIP_Real v)
Definition: misc.c:10242
SCIPallocBlockMemory(scip, subsol))
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
void SCIPexprhdlrSetIntEval(SCIP_EXPRHDLR *exprhdlr, SCIP_DECL_EXPRINTEVAL((*inteval)))
Definition: expr.c:479
static SCIP_DECL_EXPREVAL(evalValue)
Definition: expr_value.c:128