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