Scippy

SCIP

Solving Constraint Integer Programs

scip_expr.h
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-2025 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 scip_expr.h
26 * @ingroup PUBLICCOREAPI
27 * @brief public functions to work with algebraic expressions
28 * @author Ksenia Bestuzheva
29 * @author Benjamin Mueller
30 * @author Felipe Serrano
31 * @author Stefan Vigerske
32 */
33
34#ifndef SCIP_SCIP_EXPR_H_
35#define SCIP_SCIP_EXPR_H_
36
37#include "scip/type_scip.h"
38#include "scip/type_expr.h"
39#include "scip/type_misc.h"
40#include "scip/type_message.h"
41
42#ifdef NDEBUG
43#include "scip/struct_scip.h"
44#include "scip/struct_set.h"
45#include "scip/struct_mem.h"
46#include "scip/struct_stat.h"
47#include "scip/set.h"
48#include "scip/expr.h"
49#endif
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/**@addtogroup PublicExprHandlerMethods
56 * @{
57 */
58
59/** creates the handler for an expression handler and includes it into SCIP */
60SCIP_EXPORT
62 SCIP* scip, /**< SCIP data structure */
63 SCIP_EXPRHDLR** exprhdlr, /**< buffer where to store created expression handler */
64 const char* name, /**< name of expression handler (must not be NULL) */
65 const char* desc, /**< description of expression handler (can be NULL) */
66 unsigned int precedence, /**< precedence of expression operation (used for printing) */
67 SCIP_DECL_EXPREVAL((*eval)), /**< point evaluation callback (must not be NULL) */
68 SCIP_EXPRHDLRDATA* data /**< data of expression handler (can be NULL) */
69 );
70
71/** gives expression handlers */
72SCIP_EXPORT
74 SCIP* scip /**< SCIP data structure */
75);
76
77/** gives number of expression handlers */
78SCIP_EXPORT
80 SCIP* scip /**< SCIP data structure */
81);
82
83/** returns an expression handler of a given name (or NULL if not found) */
84SCIP_EXPORT
86 SCIP* scip, /**< SCIP data structure */
87 const char* name /**< name of expression handler */
88 );
89
90/** returns expression handler for variable expressions (or NULL if not included) */
91SCIP_EXPORT
93 SCIP* scip /**< SCIP data structure */
94 );
95
96/** returns expression handler for constant value expressions (or NULL if not included) */
97SCIP_EXPORT
99 SCIP* scip /**< SCIP data structure */
100 );
101
102/** returns expression handler for sum expressions (or NULL if not included) */
103SCIP_EXPORT
105 SCIP* scip /**< SCIP data structure */
106 );
107
108/** returns expression handler for product expressions (or NULL if not included) */
109SCIP_EXPORT
111 SCIP* scip /**< SCIP data structure */
112 );
113
114/** returns expression handler for power expressions (or NULL if not included) */
115SCIP_EXPORT
117 SCIP* scip /**< SCIP data structure */
118 );
119
120#ifdef NDEBUG
121/* If NDEBUG is defined, the function calls are overwritten by defines to reduce the number of function calls and
122 * speed up the algorithms.
123 */
124#define SCIPgetExprhdlrs(scip) (scip)->set->exprhdlrs
125#define SCIPgetNExprhdlrs(scip) (scip)->set->nexprhdlrs
126#define SCIPfindExprhdlr(scip, name) SCIPsetFindExprhdlr((scip)->set, name)
127#define SCIPgetExprhdlrVar(scip) (scip)->set->exprhdlrvar
128#define SCIPgetExprhdlrValue(scip) (scip)->set->exprhdlrval
129#define SCIPgetExprhdlrSum(scip) (scip)->set->exprhdlrsum
130#define SCIPgetExprhdlrProduct(scip) (scip)->set->exprhdlrproduct
131#define SCIPgetExprhdlrPower(scip) (scip)->set->exprhdlrpow
132#endif
133
134/** @} */
135
136/**@addtogroup PublicExprMethods
137 * @{
138 */
139
140/**@name Expressions */
141/**@{ */
142
143/** creates and captures an expression with given expression data and children */
144SCIP_EXPORT
146 SCIP* scip, /**< SCIP data structure */
147 SCIP_EXPR** expr, /**< pointer where to store expression */
148 SCIP_EXPRHDLR* exprhdlr, /**< expression handler */
149 SCIP_EXPRDATA* exprdata, /**< expression data (expression assumes ownership) */
150 int nchildren, /**< number of children */
151 SCIP_EXPR** children, /**< children (can be NULL if nchildren is 0) */
152 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
153 void* ownercreatedata /**< data to pass to ownercreate */
154 );
155
156/** creates and captures an expression with given expression data and up to two children */
157SCIP_EXPORT
159 SCIP* scip, /**< SCIP data structure */
160 SCIP_EXPR** expr, /**< pointer where to store expression */
161 SCIP_EXPRHDLR* exprhdlr, /**< expression handler */
162 SCIP_EXPRDATA* exprdata, /**< expression data */
163 SCIP_EXPR* child1, /**< first child (can be NULL) */
164 SCIP_EXPR* child2, /**< second child (can be NULL) */
165 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
166 void* ownercreatedata /**< data to pass to ownercreate */
167 );
168
169/** creates and captures an expression representing a quadratic function */
170SCIP_EXPORT
172 SCIP* scip, /**< SCIP data structure */
173 SCIP_EXPR** expr, /**< pointer where to store expression */
174 int nlinvars, /**< number of linear terms */
175 SCIP_VAR** linvars, /**< array with variables in linear part */
176 SCIP_Real* lincoefs, /**< array with coefficients of variables in linear part */
177 int nquadterms, /**< number of quadratic terms */
178 SCIP_VAR** quadvars1, /**< array with first variables in quadratic terms */
179 SCIP_VAR** quadvars2, /**< array with second variables in quadratic terms */
180 SCIP_Real* quadcoefs, /**< array with coefficients of quadratic terms */
181 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
182 void* ownercreatedata /**< data to pass to ownercreate */
183 );
184
185/** creates and captures an expression representing a monomial
186 *
187 * @note In deviation from the actual definition of monomials, we also allow for negative and rational exponents.
188 * So this function actually creates an expression for a signomial that has exactly one term.
189 */
190SCIP_EXPORT
192 SCIP* scip, /**< SCIP data structure */
193 SCIP_EXPR** expr, /**< pointer where to store expression */
194 int nfactors, /**< number of factors in monomial */
195 SCIP_VAR** vars, /**< variables in the monomial */
196 SCIP_Real* exponents, /**< exponent in each factor, or NULL if all 1.0 */
197 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
198 void* ownercreatedata /**< data to pass to ownercreate */
199 );
200
201/** appends child to the children list of expr
202 *
203 * @attention Only use if you really know what you are doing. The expression handler of the expression needs to be able to handle an increase in the number of children.
204 */
205SCIP_EXPORT
207 SCIP* scip, /**< SCIP data structure */
208 SCIP_EXPR* expr, /**< expression */
209 SCIP_EXPR* child /**< expression to be appended */
210 );
211
212/** overwrites/replaces a child of an expressions
213 *
214 * The old child is released and the newchild is captured, unless they are the same (=same pointer).
215 */
216SCIP_EXPORT
218 SCIP* scip, /**< SCIP data structure */
219 SCIP_EXPR* expr, /**< expression which is going to replace a child */
220 int childidx, /**< index of child being replaced */
221 SCIP_EXPR* newchild /**< the new child */
222 );
223
224/** remove all children of expr
225 *
226 * @attention Only use if you really know what you are doing. The expression handler of the expression needs to be able to handle the removal of all children.
227 */
228SCIP_EXPORT
230 SCIP* scip, /**< SCIP data structure */
231 SCIP_EXPR* expr /**< expression */
232 );
233
234/** duplicates the given expression and its children */
235SCIP_EXPORT
237 SCIP* scip, /**< SCIP data structure */
238 SCIP_EXPR* expr, /**< original expression */
239 SCIP_EXPR** copyexpr, /**< buffer to store duplicate of expr */
240 SCIP_DECL_EXPR_MAPEXPR((*mapexpr)), /**< expression mapping function, or NULL for creating new expressions */
241 void* mapexprdata, /**< data of expression mapping function */
242 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call on expression copy to create ownerdata */
243 void* ownercreatedata /**< data to pass to ownercreate */
244 );
245
246/** duplicates the given expression, but reuses its children */
247SCIP_EXPORT
249 SCIP* scip, /**< SCIP data structure */
250 SCIP_EXPR* expr, /**< original expression */
251 SCIP_EXPR** copyexpr, /**< buffer to store (shallow) duplicate of expr */
252 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
253 void* ownercreatedata /**< data to pass to ownercreate */
254 );
255
256/** copies an expression including children to use in a (possibly different) SCIP instance */
257SCIP_EXPORT
259 SCIP* sourcescip, /**< source SCIP data structure */
260 SCIP* targetscip, /**< target SCIP data structure */
261 SCIP_EXPR* expr, /**< original expression */
262 SCIP_EXPR** copyexpr, /**< buffer to store duplicate of expr */
263 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call on expression copy to create ownerdata */
264 void* ownercreatedata, /**< data to pass to ownercreate */
265 SCIP_HASHMAP* varmap, /**< a SCIP_HASHMAP mapping variables of the source SCIP to the corresponding
266 * variables of the target SCIP, or NULL */
267 SCIP_HASHMAP* consmap, /**< a hashmap to store the mapping of source constraints to the corresponding
268 * target constraints, or NULL */
269 SCIP_Bool global, /**< create a global or a local copy? */
270 SCIP_Bool* valid /**< pointer to store whether all checked or enforced constraints were validly copied */
271 );
272
273/** creates an expression from a string
274 *
275 * We specify the grammar that defines the syntax of an expression.
276 * Loosely speaking, a `Base` will be any "block", a `Factor` is a `Base` to a power,
277 * a `Term` is a product of `Factors` and an `Expression` is a sum of `Terms`.
278 *
279 * The actual definition:
280 * ```
281 * Expression -> ["+" | "-"] Term { [ ("+" | "-" | "number *") Term | ("number" <varname>) ] }
282 * Term -> Factor { ("*" | "/" ) Factor }
283 * Factor -> Base [ "^" "number" | "^(" "number" ")" ]
284 * Base -> "number" | "<varname>" | "(" Expression ")" | Op "(" OpExpression ")
285 * ```
286 * where `[a|b]` means `a` or `b` or none, `(a|b)` means `a` or `b`, `{a}` means 0 or more `a`.
287 *
288 * Note that `Op` and `OpExpression` are undefined.
289 * `Op` corresponds to the name of an expression handler and `OpExpression` to whatever string the expression handler accepts (through its parse method).
290 */
291SCIP_EXPORT
293 SCIP* scip, /**< SCIP data structure */
294 SCIP_EXPR** expr, /**< pointer to store the expr parsed */
295 const char* exprstr, /**< string with the expr to parse */
296 const char** finalpos, /**< buffer to store the position of exprstr where we finished reading, or NULL if not of interest */
297 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
298 void* ownercreatedata /**< data to pass to ownercreate */
299 );
300
301/** captures an expression (increments usage count) */
302SCIP_EXPORT
303void SCIPcaptureExpr(
304 SCIP_EXPR* expr /**< expression to be captured */
305 );
306
307/** releases an expression (decrements usage count and possibly frees expression) */
308SCIP_EXPORT
310 SCIP* scip, /**< SCIP data structure */
311 SCIP_EXPR** expr /**< pointer to expression to be released */
312 );
313
314/** returns whether an expression is a variable expression */
315SCIP_EXPORT
317 SCIP* scip, /**< SCIP data structure */
318 SCIP_EXPR* expr /**< expression */
319 );
320
321/** returns whether an expression is a value expression */
322SCIP_EXPORT
324 SCIP* scip, /**< SCIP data structure */
325 SCIP_EXPR* expr /**< expression */
326 );
327
328/** returns whether an expression is a sum expression */
329SCIP_EXPORT
331 SCIP* scip, /**< SCIP data structure */
332 SCIP_EXPR* expr /**< expression */
333 );
334
335/** returns whether an expression is a product expression */
336SCIP_EXPORT
338 SCIP* scip, /**< SCIP data structure */
339 SCIP_EXPR* expr /**< expression */
340 );
341
342/** returns whether an expression is a power expression */
343SCIP_EXPORT
345 SCIP* scip, /**< SCIP data structure */
346 SCIP_EXPR* expr /**< expression */
347 );
348
349/** print an expression as info-message */
350SCIP_EXPORT
352 SCIP* scip, /**< SCIP data structure */
353 SCIP_EXPR* expr, /**< expression to be printed */
354 FILE* file /**< file to print to, or NULL for stdout */
355 );
356
357/** initializes printing of expressions in dot format to a give FILE* pointer */
358SCIP_EXPORT
360 SCIP* scip, /**< SCIP data structure */
361 SCIP_EXPRPRINTDATA** printdata, /**< buffer to store dot printing data */
362 FILE* file, /**< file to print to, or NULL for stdout */
363 SCIP_EXPRPRINT_WHAT whattoprint /**< info on what to print for each expression */
364 );
365
366/** initializes printing of expressions in dot format to a file with given filename */
367SCIP_EXPORT
369 SCIP* scip, /**< SCIP data structure */
370 SCIP_EXPRPRINTDATA** printdata, /**< buffer to store dot printing data */
371 const char* filename, /**< name of file to print to */
372 SCIP_EXPRPRINT_WHAT whattoprint /**< info on what to print for each expression */
373 );
374
375/** main part of printing an expression in dot format */
376SCIP_EXPORT
378 SCIP* scip, /**< SCIP data structure */
379 SCIP_EXPRPRINTDATA* printdata, /**< data as initialized by \ref SCIPprintExprDotInit() */
380 SCIP_EXPR* expr /**< expression to be printed */
381 );
382
383/** finishes printing of expressions in dot format */
384SCIP_EXPORT
386 SCIP* scip, /**< SCIP data structure */
387 SCIP_EXPRPRINTDATA** printdata /**< buffer where dot printing data has been stored */
388 );
389
390/** shows a single expression by use of dot and gv
391 *
392 * This function is meant for debugging purposes.
393 * It's signature is kept as simple as possible to make it
394 * easily callable from gdb, for example.
395 *
396 * It prints the expression into a temporary file in dot format, then calls dot to create a postscript file, then calls ghostview (gv) to show the file.
397 * SCIP will hold until ghostscript is closed.
398 */
399SCIP_EXPORT
401 SCIP* scip, /**< SCIP data structure */
402 SCIP_EXPR* expr /**< expression to be printed */
403 );
404
405/** prints structure of an expression a la Maple's dismantle */
406SCIP_EXPORT
408 SCIP* scip, /**< SCIP data structure */
409 FILE* file, /**< file to print to, or NULL for stdout */
410 SCIP_EXPR* expr /**< expression to dismantle */
411 );
412
413/** evaluate an expression in a point
414 *
415 * Iterates over expressions to also evaluate children, if necessary.
416 * Value can be received via SCIPexprGetEvalValue().
417 * If an evaluation error (division by zero, ...) occurs, this value will
418 * be set to SCIP_INVALID.
419 *
420 * If a nonzero \p soltag is passed, then only (sub)expressions are
421 * reevaluated that have a different solution tag. If a soltag of 0
422 * is passed, then subexpressions are always reevaluated.
423 * The tag is stored together with the value and can be received via
424 * SCIPexprGetEvalTag().
425 */
426SCIP_EXPORT
428 SCIP* scip, /**< SCIP data structure */
429 SCIP_EXPR* expr, /**< expression to be evaluated */
430 SCIP_SOL* sol, /**< solution to be evaluated */
431 SCIP_Longint soltag /**< tag that uniquely identifies the solution (with its values), or 0. */
432 );
433
434/** returns a previously unused solution tag for expression evaluation */
435SCIP_EXPORT
437 SCIP* scip /**< SCIP data structure */
438 );
439
440/**@} */
441
442/** @name Differentiation
443 * @anchor SCIP_EXPR_DIFF
444 *
445 * @par Gradients (Automatic differentiation Backward mode)
446 *
447 * Given a function, say, \f$f(s(x,y),t(x,y))\f$ there is a common mnemonic technique to compute its partial derivatives, using a tree diagram.
448 * Suppose we want to compute the partial derivative of \f$f\f$ w.r.t. \f$x\f$.
449 * Write the function as a tree:
450 *
451 * f
452 * |-----|
453 * s t
454 * |--| |--|
455 * x y x y
456 *
457 * The weight of an edge between two nodes represents the partial derivative of the parent w.r.t. the children, e.g.,
458 *
459 * f
460 * |
461 * s
462 *
463 * is \f$ \partial_sf \f$.
464 * The weight of a path is the product of the weight of the edges in the path.
465 * The partial derivative of \f$f\f$ w.r.t. \f$x\f$ is then the sum of the weights of all paths connecting \f$f\f$ with \f$x\f$:
466 * \f[ \frac{\partial f}{\partial x} = \partial_s f \cdot \partial_x s + \partial_t f \cdot \partial_x t. \f]
467 *
468 * We follow this method in order to compute the gradient of an expression (root) at a given point (point).
469 * Note that an expression is a DAG representation of a function, but there is a 1-1 correspondence between paths
470 * in the DAG and path in a tree diagram of a function.
471 * Initially, we set `root->derivative` to 1.0.
472 * Then, traversing the tree in Depth First (see \ref SCIPexpriterInit), for every expr that *has* children,
473 * we store in its i-th child, `child[i]->derivative`, the derivative of expr w.r.t. child evaluated at point multiplied with `expr->derivative`.
474 *
475 * For example:
476 * 1. `f->derivative` = 1.0
477 * 2. `s->derivative` = \f$\partial_s f \,\cdot\f$ `f->derivative` = \f$\partial_s f\f$
478 * 3. `x->derivative` = \f$\partial_x s \,\cdot\f$ `s->derivative` = \f$\partial_x s \cdot \partial_s f\f$
479 *
480 * However, when the child is a variable expressions, we actually need to initialize `child->derivative` to 0.0
481 * and afterwards add, instead of overwrite the computed value.
482 * The complete example would then be:
483 *
484 * 1. `f->derivative` = 1.0, `x->derivative` = 0.0, `y->derivative` = 0.0
485 * 2. `s->derivative` = \f$\partial_s f \,\cdot\f$ `f->derivative` = \f$\partial_s f\f$
486 * 3. `x->derivative` += \f$\partial_x s \,\cdot\f$ `s->derivative` = \f$\partial_x s \cdot \partial_s f\f$
487 * 4. `y->derivative` += \f$\partial_y s \,\cdot\f$ `s->derivative` = \f$\partial_y s \cdot \partial_s f\f$
488 * 5. `t->derivative` = \f$\partial_t f \,\cdot\f$ `f->derivative` = \f$\partial_t f\f$
489 * 6. `x->derivative` += \f$\partial_x t \,\cdot\f$ `t->derivative` = \f$\partial_x t \cdot \partial_t f\f$
490 * 7. `y->derivative` += \f$\partial_y t \,\cdot\f$ `t->derivative` = \f$\partial_y t \cdot \partial_t f\f$
491 *
492 * Note that, to compute this, we only need to know, for each expression, its partial derivatives w.r.t a given child at a point.
493 * This is what the callback `SCIP_DECL_EXPRBWDIFF` should return.
494 * Indeed, from "derivative of expr w.r.t. child evaluated at point multiplied with expr->derivative",
495 * note that at the moment of processing a child, we already know `expr->derivative`, so the only
496 * missing piece of information is "the derivative of expr w.r.t. child evaluated at point".
497 *
498 * An equivalent way of interpreting the procedure is that `expr->derivative` stores the derivative of the root w.r.t. expr.
499 * This way, `x->derivative` and `y->derivative` will contain the partial derivatives of root w.r.t. the variable, that is, the gradient.
500 * Note, however, that this analogy is only correct for leave expressions, since the derivative value of an intermediate expression gets overwritten.
501 *
502 *
503 * \par Hessian (Automatic differentiation Backward on Forward mode)
504 *
505 * Computing the Hessian is more complicated since it is the derivative of the gradient, which is a function with more than one output.
506 * We compute the Hessian by computing "directions" of the Hessian, that is \f$H\cdot u\f$ for different \f$u\f$.
507 * This is easy in general, since it is the gradient of the *scalar* function \f$\nabla f u\f$, that is,
508 * the directional derivative of \f$f\f$ in the direction \f$u\f$: \f$D_u f\f$.
509 *
510 * This is easily computed via the so called forward mode.
511 * Just as `expr->derivative` stores the partial derivative of the root w.r.t. expr,
512 * `expr->dot` stores the directional derivative of expr in the direction \f$u\f$.
513 * Then, by the chain rule, `expr->dot` = \f$\sum_{c:\text{children}} \partial_c \text{expr} \,\cdot\f$ `c->dot`.
514 *
515 * Starting with `x[i]->dot` = \f$u_i\f$, we can compute `expr->dot` for every expression at the same time we evaluate expr.
516 * Computing `expr->dot` is the purpose of the callback `SCIP_DECL_EXPRFWDIFF`.
517 * Obviously, when this callback is called, the "dots" of all children are known
518 * (just like evaluation, where the value of all children are known).
519 *
520 * Once we have this information, we compute the gradient of this function, following the same idea as before.
521 * We define `expr->bardot` to be the directional derivative in direction \f$u\f$ of the partial derivative of the root w.r.t `expr`,
522 * that is \f$D_u (\partial_{\text{expr}} f) = D_u\f$ (`expr->derivative`).
523 *
524 * This way, `x[i]->bardot` = \f$D_u (\partial_{x_i} f) = e_i^T H_f u\f$.
525 * Hence `vars->bardot` contain \f$H_f u\f$.
526 * By the chain rule, product rule, and definition we have
527 * \f{eqnarray*}{
528 * \texttt{expr->bardot} & = & D_u (\partial_{\text{expr}} f) \\
529 * & = & D_u ( \partial_{\text{parent}} f \cdot \partial_{\text{expr}} \text{parent} ) \\
530 * & = & D_u ( \texttt{parent->derivative} \cdot \partial_{\text{expr}} \text{parent} ) \\
531 * & = & \partial_{\text{expr}} \text{parent} \cdot D_u (\texttt{parent->derivative}) + \texttt{parent->derivative} \cdot D_u (\partial_{\text{expr}} \text{parent}) \\
532 * & = & \texttt{parent->bardot} \cdot \partial_{\text{expr}} \text{parent} + \texttt{parent->derivative} \cdot D_u (\partial_{\text{expr}} \text{parent})
533 * \f}
534 *
535 * Note that we have computed `parent->bardot` and `parent->derivative` at this point,
536 * while \f$\partial_{\text{expr}} \text{parent}\f$ is the return of `SCIP_DECL_EXPRBWDIFF`.
537 * Hence the only information we need to compute is \f$D_u (\partial_{\text{expr}} \text{parent})\f$.
538 * This is the purpose of the callback `SCIP_DECL_EXPRBWFWDIFF`.
539 *
540 * @{
541 */
542
543/** evaluates gradient of an expression for a given point
544 *
545 * Initiates an expression walk to also evaluate children, if necessary.
546 * Value can be received from variable expressions via SCIPexprGetDerivative() or via SCIPgetExprPartialDiffNonlinear().
547 * If an error (division by zero, ...) occurs, these functions return SCIP_INVALID.
548 */
549SCIP_EXPORT
551 SCIP* scip, /**< SCIP data structure */
552 SCIP_EXPR* expr, /**< expression to be differentiated */
553 SCIP_SOL* sol, /**< solution to be evaluated (NULL for the current LP solution) */
554 SCIP_Longint soltag /**< tag that uniquely identifies the solution (with its values), or 0. */
555 );
556
557/** evaluates Hessian-vector product of an expression for a given point and direction
558 *
559 * Evaluates children, if necessary.
560 * Value can be received via SCIPgetExprPartialDiffGradientDirNonlinear().
561 * If an error (division by zero, ...) occurs, this value will
562 * be set to SCIP_INVALID.
563 */
564SCIP_EXPORT
566 SCIP* scip, /**< SCIP data structure */
567 SCIP_EXPR* expr, /**< expression to be differentiated */
568 SCIP_SOL* sol, /**< solution to be evaluated (NULL for the current LP solution) */
569 SCIP_Longint soltag, /**< tag that uniquely identifies the solution (with its values), or 0. */
570 SCIP_SOL* direction /**< direction */
571 );
572
573/**@} */ /* end of differentiation methods */
574
575/**@name Expressions
576 * @{
577 */
578
579/** possibly reevaluates the activity of the expression
580 *
581 * Reevaluate activity if currently stored is no longer uptodate (some bound was changed since last evaluation).
582 *
583 * The owner of the expression may overwrite the methods used to evaluate the activity,
584 * including whether the local or global domain of variables is used.
585 * By default (no owner, or owner doesn't overwrite activity evaluation),
586 * the local domain of variables is used.
587 *
588 * @note If expression is set to be integral, then activities are tightened to integral values.
589 * Thus, ensure that the integrality information is valid (if set to TRUE; the default (FALSE) is always ok).
590 */
591SCIP_EXPORT
593 SCIP* scip, /**< SCIP data structure */
594 SCIP_EXPR* expr /**< expression */
595 );
596
597/** compare expressions
598 * @return -1, 0 or 1 if expr1 <, =, > expr2, respectively
599 * @note The given expressions are assumed to be simplified.
600 */
601SCIP_EXPORT
603 SCIP* scip, /**< SCIP data structure */
604 SCIP_EXPR* expr1, /**< first expression */
605 SCIP_EXPR* expr2 /**< second expression */
606 );
607
608/** compute the hash value of an expression */
609SCIP_EXPORT
611 SCIP* scip, /**< SCIP data structure */
612 SCIP_EXPR* expr, /**< expression */
613 unsigned int* hashval /**< pointer to store the hash value */
614 );
615
616/** simplifies an expression
617 *
618 * This is largely inspired by Joel Cohen's
619 * *Computer algebra and symbolic computation: Mathematical methods*,
620 * in particular Chapter 3.
621 * The other fountain of inspiration are the simplifying methods of expr.c in SCIP 7.
622 *
623 * Note: The things to keep in mind when adding simplification rules are the following.
624 * I will be using the product expressions (see expr_product.c) as an example.
625 * There are mainly 3 parts of the simplification process. You need to decide
626 * at which stage the simplification rule makes sense.
627 * 1. Simplify each factor (simplifyFactor()): At this stage we got the children of the product expression.
628 * At this point, each child is simplified when viewed as a stand-alone expression, but not necessarily when viewed as child of a product expression.
629 * Rules like SP2, SP7, etc are enforced at this point.
630 * 2. Multiply the factors (mergeProductExprlist()): At this point rules like SP4, SP5 and SP14 are enforced.
631 * 3. Build the actual simplified product expression (buildSimplifiedProduct()):
632 * At this point rules like SP10, SP11, etc are enforced.
633 *
634 * During steps 1 and 2 do not forget to set the flag `changed` to TRUE when something actually changes.
635 *
636 * \par Definition of simplified expressions
637 *
638 * An expression is simplified if it
639 * - is a value expression
640 * - is a var expression
641 * - is a product expression such that
642 * - SP1: every child is simplified
643 * - SP2: no child is a product
644 * - SP4: no two children are the same expression (those should be multiplied)
645 * - SP5: the children are sorted [commutative rule]
646 * - SP7: no child is a value
647 * - SP8: its coefficient is 1.0 (otherwise should be written as sum)
648 * - SP10: it has at least two children
649 * - TODO?: at most one child is an `abs`
650 * - SP11: no two children are `expr*log(expr)`
651 * (TODO: we could handle more complicated stuff like \f$xy\log(x) \to - y * \mathrm{entropy}(x)\f$, but I am not sure this should happen at the simplification level;
652 * similar for \f$(xy) \log(xy)\f$, which currently simplifies to \f$xy \log(xy)\f$)
653 * - SP12: if it has two children, then neither of them is a sum (expand sums)
654 * - SP12b: if it has at least two children and expandalways is set, then no child is a sum (expand sums always)
655 * - SP13: no child is a sum with a single term
656 * - SP14: at most one child is an `exp`
657 * - is a power expression such that
658 * - POW1: exponent is not 0
659 * - POW2: exponent is not 1
660 * - POW3: its child is not a value
661 * - POW4: its child is simplified
662 * - POW5: if exponent is integer, its child is not a product
663 * - POW5a: if exponent is fractional and distribfracexponent param is enabled, its child is not a product
664 * - POW6: if exponent is integer, its child is not a sum with a single term (\f$(2x)^2 \to 4x^2\f$)
665 * - POW7: if exponent is integer and at most expandmaxeponent param, its child is not a sum (expand sums)
666 * - POW8: its child is not a power unless \f$(x^n)^m\f$ with \f$nm\f$ being integer and \f$n\f$ or \f$m\f$ fractional and \f$n\f$ not being even integer
667 * - POW9: its child is not a sum with a single term with a positive coefficient: \f$(25x)^{0.5} \to 5 x^{0.5}\f$
668 * - POW10: its child is not a binary variable: \f$b^e, e > 0 \to b\f$; \f$b^e, e < 0 \to b := 1\f$
669 * - POW11: its child is not an exponential: \f$\exp(\text{expr})^e \to \exp(e\cdot\text{expr})\f$
670 * - POW12: its child is not an absolute value if the exponent is an even integer: \f$\abs(\text{expr})^e, e \text{ even} \to \text{expr}^e\f$
671 * - is a signedpower expression such that
672 * - SPOW1: exponent is not 0
673 * - SPOW2: exponent is not 1
674 * - SPOW3: its child is not a value
675 * - SPOW4: its child is simplified
676 * - SPOW5: (TODO) do we want to distribute signpowers over products like we do for powers?
677 * - SPOW6: exponent is not an odd integer: (signpow odd expr) -> (pow odd expr)
678 * - SPOW8: if exponent is integer, its child is not a power
679 * - SPOW9: its child is not a sum with a single term: \f$\mathrm{signpow}(25x,0.5) \to 5\mathrm{signpow}(x,0.5)\f$
680 * - SPOW10: its child is not a binary variable: \f$\mathrm{signpow}(b,e), e > 0 \to b\f$; \f$\mathrm{signpow}(b,e), e < 0 \to b := 1\f$
681 * - SPOW11: its child is not an exponential: \f$\mathrm{signpow}(\exp(\text{expr}),e) \to \exp(e\cdot\text{expr})\f$
682 * - TODO: what happens when child is another signed power?
683 * - TODO: if child &ge; 0 -> transform to normal power; if child < 0 -> transform to - normal power
684 *
685 * TODO: Some of these criteria are too restrictive for signed powers; for example, the exponent does not need to be
686 * an integer for signedpower to distribute over a product (SPOW5, SPOW6, SPOW8). Others can also be improved.
687 * - is a sum expression such that
688 * - SS1: every child is simplified
689 * - SS2: no child is a sum
690 * - SS3: no child is a value (values should go in the constant of the sum)
691 * - SS4: no two children are the same expression (those should be summed up)
692 * - SS5: the children are sorted [commutative rule]
693 * - SS6: it has at least one child
694 * - SS7: if it consists of a single child, then either constant is != 0.0 or coef != 1
695 * - SS8: no child has coefficient 0
696 * - SS9: if a child c is a product that has an exponential expression as one of its factors, then the coefficient of c is +/-1.0
697 * - SS10: if a child c is an exponential, then the coefficient of c is +/-1.0
698 * - it is a function with simplified arguments, but not all of them can be values
699 * - TODO? a logarithm doesn't have a product as a child
700 * - TODO? the exponent of an exponential is always 1
701 *
702 * \par Ordering Rules (see SCIPexprCompare())
703 * \anchor EXPR_ORDER
704 * These rules define a total order on *simplified* expressions.
705 * There are two groups of rules, when comparing equal type expressions and different type expressions.
706 *
707 * Equal type expressions:
708 * - OR1: u,v value expressions: u < v &hArr; val(u) < val(v)
709 * - OR2: u,v var expressions: u < v &hArr; `SCIPvarGetIndex(var(u))` < `SCIPvarGetIndex(var(v))`
710 * - OR3: u,v are both sum or product expression: < is a lexicographical order on the terms
711 * - OR4: u,v are both pow: u < v &hArr; base(u) < base(v) or, base(u) = base(v) and expo(u) < expo(v)
712 * - OR5: u,v are \f$u = f(u_1, ..., u_n), v = f(v_1, ..., v_m)\f$: u < v &hArr; For the first k such that \f$u_k \neq v_k\f$, \f$u_k < v_k\f$, or if such a \f$k\f$ doesn't exist, then \f$n < m\f$.
713 *
714 * Different type expressions:
715 * - OR6: u value, v other: u < v always
716 * - OR7: u sum, v var or func: u < v &hArr; u < 0+v;
717 * In other words, if \f$u = \sum_{i=1}^n \alpha_i u_i\f$, then u < v &hArr; \f$u_n\f$ < v or if \f$u_n\f$ = v and \f$\alpha_n\f$ < 1.
718 * - OR8: u product, v pow, sum, var or func: u < v &hArr; u < 1*v;
719 * In other words, if \f$u = \prod_{i=1}^n u_i\f$, then u < v &hArr; \f$u_n\f$ < v.
720 * Note: since this applies only to simplified expressions, the form of the product is correct.
721 * Simplified products do *not* have constant coefficients.
722 * - OR9: u pow, v sum, var or func: u < v &hArr; u < v^1
723 * - OR10: u var, v func: u < v always
724 * - OR11: u func, v other type of func: u < v &hArr; name(type(u)) < name(type(v))
725 * - OR12: none of the rules apply: u < v &hArr; ! v < u
726 *
727 * Examples:
728 * - x < x^2 ?: x is var and x^2 power, so none applies (OR12).
729 * Hence, we try to answer x^2 < x ?: x^2 < x &hArr; x < x or if x = x and 2 < 1 &hArr; 2 < 1 &hArr; False. So x < x^2 is True.
730 * - x < x^-1 --OR12&rarr; ~(x^-1 < x) --OR9&rarr; ~(x^-1 < x^1) --OR4&rarr; ~(x < x or -1 < 1) &rarr; ~True &rarr; False
731 * - x*y < x --OR8&rarr; x*y < 1*x --OR3&rarr; y < x --OR2&rarr; False
732 * - x*y < y --OR8&rarr; x*y < 1*y --OR3&rarr; y < x --OR2&rarr; False
733 *
734 * \par Algorithm
735 *
736 * The recursive version of the algorithm is
737 *
738 * EXPR simplify(expr)
739 * for c in 1..expr->nchildren
740 * expr->children[c] = simplify(expr->children[c])
741 * end
742 * return expr->exprhdlr->simplify(expr)
743 * end
744 *
745 * Important: Whatever is returned by a simplify callback **has** to be simplified.
746 * Also, all children of the given expression **are** already simplified.
747 */
748SCIP_EXPORT
750 SCIP* scip, /**< SCIP data structure */
751 SCIP_EXPR* rootexpr, /**< expression to be simplified */
752 SCIP_EXPR** simplified, /**< buffer to store simplified expression */
753 SCIP_Bool* changed, /**< buffer to store if rootexpr actually changed */
754 SCIP_Bool* infeasible, /**< buffer to store whether infeasibility has been detected */
755 SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), /**< function to call to create ownerdata */
756 void* ownercreatedata /**< data to pass to ownercreate */
757 );
758
759/** retrieves symmetry information from an expression */
760SCIP_EXPORT
762 SCIP* scip, /**< SCIP data structure */
763 SCIP_EXPR* expr, /**< expression from which information needs to be retrieved */
764 SYM_EXPRDATA** symdata /**< buffer to store symmetry data */
765 );
766
767/** replaces common sub-expressions in a given expression graph by using a hash key for each expression
768 *
769 * The algorithm consists of two steps:
770 *
771 * 1. traverse through all given expressions and compute for each of them a (not necessarily unique) hash
772 *
773 * 2. initialize an empty hash table and traverse through all expression; check for each of them if we can find a
774 * structural equivalent expression in the hash table; if yes we replace the expression by the expression inside the
775 * hash table, otherwise we add it to the hash table
776 *
777 * @note the hash keys of the expressions are used for the hashing inside the hash table; to compute if two expressions
778 * (with the same hash) are structurally the same we use the function SCIPexprCompare().
779 */
780SCIP_EXPORT
782 SCIP* scip, /**< SCIP data structure */
783 SCIP_EXPR** exprs, /**< expressions (possibly replaced by equivalent on output) */
784 int nexprs, /**< total number of expressions */
785 SCIP_Bool* replacedroot /**< buffer to store whether any root expression (expression in exprs) was replaced */
786);
787
788/** computes the curvature of a given expression and all its subexpressions
789 *
790 * @note this function also evaluates all subexpressions w.r.t. current variable bounds
791 * @note this function relies on information from the curvature callback of expression handlers only,
792 * consider using function @ref SCIPhasExprCurvature() of the convex-nlhdlr instead, as that uses more information to deduce convexity
793 */
794SCIP_EXPORT
796 SCIP* scip, /**< SCIP data structure */
797 SCIP_EXPR* expr /**< expression */
798 );
799
800/** computes integrality information of a given expression and all its subexpressions
801 *
802 * The integrality information can be accessed via SCIPexprGetIntegrality() and SCIPexprIsIntegral().
803 */
804SCIP_EXPORT
806 SCIP* scip, /**< SCIP data structure */
807 SCIP_EXPR* expr /**< expression */
808 );
809
810/** returns the total number of variable expressions in an expression
811 *
812 * The function counts variable expressions in common sub-expressions only once, but
813 * counts variables appearing in several variable expressions multiple times.
814 */
815SCIP_EXPORT
817 SCIP* scip, /**< SCIP data structure */
818 SCIP_EXPR* expr, /**< expression */
819 int* nvars /**< buffer to store the total number of variables */
820 );
821
822/** returns all variable expressions contained in a given expression
823 *
824 * The array to store all variable expressions needs to be at least of size
825 * the number of unique variable expressions in the expression which is given by SCIPgetExprNVars().
826 *
827 * If every variable is represented by only one variable expression (common subexpression have been removed)
828 * then SCIPgetExprNVars() can be bounded by SCIPgetNTotalVars().
829 * If, in addition, non-active variables have been removed from the expression, e.g., by simplifying,
830 * then SCIPgetExprNVars() can be bounded by SCIPgetNVars().
831 *
832 * @note function captures variable expressions
833 */
834SCIP_EXPORT
836 SCIP* scip, /**< SCIP data structure */
837 SCIP_EXPR* expr, /**< expression */
838 SCIP_EXPR** varexprs, /**< array to store all variable expressions */
839 int* nvarexprs /**< buffer to store the total number of variable expressions */
840 );
841
842/** @} */
843
844/**@name Expression Handler Callbacks
845 * @{
846 */
847
848/** calls the print callback for an expression
849 *
850 * @see SCIP_DECL_EXPRPRINT
851 */
852SCIP_EXPORT
853SCIP_DECL_EXPRPRINT(SCIPcallExprPrint);
854
855/** calls the curvature callback for an expression
856 *
857 * @see SCIP_DECL_EXPRCURVATURE
858 *
859 * Returns unknown curvature if callback not implemented.
860 */
861SCIP_EXPORT
862SCIP_DECL_EXPRCURVATURE(SCIPcallExprCurvature);
863
864/** calls the monotonicity callback for an expression
865 *
866 * @see SCIP_DECL_EXPRMONOTONICITY
867 *
868 * Returns unknown monotonicity if callback not implemented.
869 */
870SCIP_EXPORT
871SCIP_DECL_EXPRMONOTONICITY(SCIPcallExprMonotonicity);
872
873/** calls the eval callback for an expression with given values for children
874 *
875 * Does not iterates over expressions, but requires values for children to be given.
876 * Value is not stored in expression, but returned in `val`.
877 * If an evaluation error (division by zero, ...) occurs, this value will
878 * be set to `SCIP_INVALID`.
879 */
880SCIP_EXPORT
882 SCIP* scip, /**< SCIP data structure */
883 SCIP_EXPR* expr, /**< expression to be evaluated */
884 SCIP_Real* childrenvalues, /**< values for children */
885 SCIP_Real* val /**< buffer to store evaluated value */
886 );
887
888/** calls the eval and fwdiff callback of an expression with given values for children
889 *
890 * Does not iterates over expressions, but requires values for children and direction to be given.
891 *
892 * Value is not stored in expression, but returned in `val`.
893 * If an evaluation error (division by zero, ...) occurs, this value will be set to `SCIP_INVALID`.
894 *
895 * Direction is not stored in expression, but returned in `dot`.
896 * If an differentiation error (division by zero, ...) occurs, this value will be set to `SCIP_INVALID`.
897 */
898SCIP_EXPORT
900 SCIP* scip, /**< SCIP data structure */
901 SCIP_EXPR* expr, /**< expression to be evaluated */
902 SCIP_Real* childrenvalues, /**< values for children */
903 SCIP_Real* direction, /**< direction in which to differentiate */
904 SCIP_Real* val, /**< buffer to store evaluated value */
905 SCIP_Real* dot /**< buffer to store derivative value */
906 );
907
908/** calls the interval evaluation callback for an expression
909 *
910 * @see SCIP_DECL_EXPRINTEVAL
911 *
912 * Returns entire interval if callback not implemented.
913 */
914SCIP_EXPORT
915SCIP_DECL_EXPRINTEVAL(SCIPcallExprInteval);
916
917/** calls the estimate callback for an expression
918 *
919 * @see SCIP_DECL_EXPRESTIMATE
920 *
921 * Returns without success if callback not implemented.
922 */
923SCIP_EXPORT
924SCIP_DECL_EXPRESTIMATE(SCIPcallExprEstimate);
925
926/** calls the initial estimators callback for an expression
927 *
928 * @see SCIP_DECL_EXPRINITESTIMATES
929 *
930 * Returns no estimators if callback not implemented.
931 */
932SCIP_EXPORT
933SCIP_DECL_EXPRINITESTIMATES(SCIPcallExprInitestimates);
934
935/** calls the simplify callback for an expression
936 *
937 * @see SCIP_DECL_EXPRSIMPLIFY
938 *
939 * Returns unmodified expression if simplify callback not implemented.
940 *
941 * Does not simplify descendants (children, etc). Use SCIPsimplifyExpr() for that.
942 */
943SCIP_EXPORT
944SCIP_DECL_EXPRSIMPLIFY(SCIPcallExprSimplify);
945
946/** calls the reverse propagation callback for an expression
947 *
948 * @see SCIP_DECL_EXPRREVERSEPROP
949 *
950 * Returns unmodified `childrenbounds` if reverseprop callback not implemented.
951 */
952SCIP_EXPORT
953SCIP_DECL_EXPRREVERSEPROP(SCIPcallExprReverseprop);
954
955/** calls the symmetry information callback for an expression
956 *
957 * Returns NULL pointer if not implemented.
958 */
959SCIP_EXPORT
960SCIP_DECL_EXPRGETSYMDATA(SCIPcallExprGetSymData);
961
962#ifdef NDEBUG
963#define SCIPappendExprChild(scip, expr, child) SCIPexprAppendChild((scip)->set, (scip)->mem->probmem, expr, child)
964#define SCIPreplaceExprChild(scip, expr, childidx, newchild) SCIPexprReplaceChild((scip)->set, (scip)->stat, (scip)->mem->probmem, expr, childidx, newchild)
965#define SCIPremoveExprChildren(scip, expr) SCIPexprRemoveChildren((scip)->set, (scip)->stat, (scip)->mem->probmem, expr)
966#define SCIPduplicateExpr(scip, expr, copyexpr, mapexpr, mapexprdata, ownercreate, ownercreatedata) SCIPexprCopy((scip)->set, (scip)->stat, (scip)->mem->probmem, (scip)->set, (scip)->stat, (scip)->mem->probmem, expr, copyexpr, mapexpr, mapexprdata, ownercreate, ownercreatedata)
967#define SCIPduplicateExprShallow(scip, expr, copyexpr, ownercreate, ownercreatedata) SCIPexprDuplicateShallow((scip)->set, (scip)->mem->probmem, expr, copyexpr, ownercreate, ownercreatedata)
968#define SCIPcaptureExpr(expr) SCIPexprCapture(expr)
969#define SCIPreleaseExpr(scip, expr) SCIPexprRelease((scip)->set, (scip)->stat, (scip)->mem->probmem, expr)
970#define SCIPisExprVar(scip, expr) SCIPexprIsVar((scip)->set, expr)
971#define SCIPisExprValue(scip, expr) SCIPexprIsValue((scip)->set, expr)
972#define SCIPisExprSum(scip, expr) SCIPexprIsSum((scip)->set, expr)
973#define SCIPisExprProduct(scip, expr) SCIPexprIsProduct((scip)->set, expr)
974#define SCIPisExprPower(scip, expr) SCIPexprIsPower((scip)->set, expr)
975#define SCIPprintExpr(scip, expr, file) SCIPexprPrint((scip)->set, (scip)->stat, (scip)->mem->probmem, (scip)->messagehdlr, file, expr)
976#define SCIPevalExpr(scip, expr, sol, soltag) SCIPexprEval((scip)->set, (scip)->stat, (scip)->mem->probmem, expr, sol, soltag)
977#define SCIPgetExprNewSoltag(scip) (++((scip)->stat->exprlastsoltag))
978#define SCIPevalExprGradient(scip, expr, sol, soltag) SCIPexprEvalGradient((scip)->set, (scip)->stat, (scip)->mem->probmem, expr, sol, soltag)
979#define SCIPevalExprHessianDir(scip, expr, sol, soltag, direction) SCIPexprEvalHessianDir((scip)->set, (scip)->stat, (scip)->mem->probmem, expr, sol, soltag, direction)
980#define SCIPevalExprActivity(scip, expr) SCIPexprEvalActivity((scip)->set, (scip)->stat, (scip)->mem->probmem, expr)
981#define SCIPcompareExpr(scip, expr1, expr2) SCIPexprCompare((scip)->set, expr1, expr2)
982#define SCIPsimplifyExpr(scip, rootexpr, simplified, changed, infeasible, ownercreate, ownercreatedata) SCIPexprSimplify((scip)->set, (scip)->stat, (scip)->mem->probmem, rootexpr, simplified, changed, infeasible, ownercreate, ownercreatedata)
983#define SCIPcallExprCurvature(scip, expr, exprcurvature, success, childcurv) SCIPexprhdlrCurvatureExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, exprcurvature, success, childcurv)
984#define SCIPcallExprMonotonicity(scip, expr, childidx, result) SCIPexprhdlrMonotonicityExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, childidx, result)
985#define SCIPcallExprEval(scip, expr, childrenvalues, val) SCIPexprhdlrEvalExpr(SCIPexprGetHdlr(expr), (scip)->set, (scip)->mem->buffer, expr, val, childrenvalues, NULL)
986#define SCIPcallExprEvalFwdiff(scip, expr, childrenvalues, direction, val, dot) SCIPexprhdlrEvalFwDiffExpr(SCIPexprGetHdlr(expr), (scip)->set, (scip)->mem->buffer, expr, val, dot, childrenvalues, NULL, direction, NULL)
987#define SCIPcallExprInteval(scip, expr, interval, intevalvar, intevalvardata) SCIPexprhdlrIntEvalExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, interval, intevalvar, intevalvardata)
988#define SCIPcallExprEstimate(scip, expr, localbounds, globalbounds, refpoint, overestimate, targetvalue, coefs, constant, islocal, success, branchcand) SCIPexprhdlrEstimateExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, localbounds, globalbounds, refpoint, overestimate, targetvalue, coefs, constant, islocal, success, branchcand)
989#define SCIPcallExprInitestimates(scip, expr, bounds, overestimate, coefs, constant, nreturned) SCIPexprhdlrInitEstimatesExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, bounds, overestimate, coefs, constant, nreturned)
990#define SCIPcallExprSimplify(scip, expr, simplifiedexpr, ownercreate, ownercreatedata) SCIPexprhdlrSimplifyExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, simplifiedexpr, ownercreate, ownercreatedata)
991#define SCIPcallExprReverseprop(scip, expr, bounds, childrenbounds, infeasible) SCIPexprhdlrReversePropExpr(SCIPexprGetHdlr(expr), (scip)->set, expr, bounds, childrenbounds, infeasible)
992#define SCIPcallExprGetSymData(scip, expr, symdata) SCIPexprhdlrGetSymdata(SCIPexprGetHdlr(expr), (scip)->set, expr, symdata)
993#endif
994
995/** @} */
996
997
998/**@name Expression Iterator */
999/**@{ */
1000
1001/** creates an expression iterator */
1002SCIP_EXPORT
1004 SCIP* scip, /**< SCIP data structure */
1005 SCIP_EXPRITER** iterator /**< buffer to store expression iterator */
1006 );
1007
1008/** frees an expression iterator */
1009SCIP_EXPORT
1010void SCIPfreeExpriter(
1011 SCIP_EXPRITER** iterator /**< pointer to the expression iterator */
1012 );
1013
1014#ifdef NDEBUG
1015#define SCIPcreateExpriter(scip, iterator) SCIPexpriterCreate((scip)->stat, (scip)->mem->probmem, iterator)
1016#define SCIPfreeExpriter(iterator) SCIPexpriterFree(iterator)
1017#endif
1018
1019/** @} */
1020
1021
1022/**@name Quadratic Expressions */
1023/**@{ */
1024
1025/** checks whether an expression is quadratic
1026 *
1027 * An expression is quadratic if it is either a square (of some expression), a product (of two expressions),
1028 * or a sum of terms where at least one is a square or a product.
1029 *
1030 * Use SCIPexprGetQuadraticData() to get data about the representation as quadratic.
1031 */
1032SCIP_EXPORT
1034 SCIP* scip, /**< SCIP data structure */
1035 SCIP_EXPR* expr, /**< expression */
1036 SCIP_Bool* isquadratic /**< buffer to store result */
1037 );
1038
1039/** frees information on quadratic representation of an expression
1040 *
1041 * Before doing changes to an expression, it can be useful to call this function.
1042 */
1043SCIP_EXPORT
1045 SCIP* scip, /**< SCIP data structure */
1046 SCIP_EXPR* expr /**< expression */
1047 );
1048
1049/** evaluates quadratic term in a solution
1050 *
1051 * \note This requires that every expression used in the quadratic data is a variable expression.
1052 */
1053SCIP_EXPORT
1055 SCIP* scip, /**< SCIP data structure */
1056 SCIP_EXPR* expr, /**< quadratic expression */
1057 SCIP_SOL* sol /**< solution to evaluate, or NULL for LP solution */
1058 );
1059
1060/** prints quadratic expression */
1061SCIP_EXPORT
1063 SCIP* scip, /**< SCIP data structure */
1064 SCIP_EXPR* expr /**< quadratic expression */
1065 );
1066
1067/** checks the curvature of the quadratic expression
1068 *
1069 * For this, it builds the matrix Q of quadratic coefficients and computes its eigenvalues using LAPACK.
1070 * If Q is
1071 * - semidefinite positive -> curv is set to convex,
1072 * - semidefinite negative -> curv is set to concave,
1073 * - otherwise -> curv is set to unknown.
1074 *
1075 * If `assumevarfixed` is given and some expressions in quadratic terms correspond to variables present in
1076 * this hashmap, then the corresponding rows and columns are ignored in the matrix Q.
1077 */
1078SCIP_EXPORT
1080 SCIP* scip, /**< SCIP data structure */
1081 SCIP_EXPR* expr, /**< quadratic expression */
1082 SCIP_EXPRCURV* curv, /**< pointer to store the curvature of quadratics */
1083 SCIP_HASHMAP* assumevarfixed, /**< hashmap containing variables that should be assumed to be fixed, or NULL */
1084 SCIP_Bool storeeigeninfo /**< whether the eigenvalues and eigenvectors should be stored */
1085 );
1086
1087#ifdef NDEBUG
1088#define SCIPcheckExprQuadratic(scip, expr, isquadratic) SCIPexprCheckQuadratic((scip)->set, (scip)->mem->probmem, expr, isquadratic)
1089#define SCIPfreeExprQuadratic(scip, expr) SCIPexprFreeQuadratic((scip)->mem->probmem, expr)
1090#define SCIPcomputeExprQuadraticCurvature(scip, expr, curv, assumevarfixed, storeeigeninfo) SCIPexprComputeQuadraticCurvature((scip)->set, (scip)->mem->probmem, (scip)->mem->buffer, (scip)->messagehdlr, expr, curv, assumevarfixed, storeeigeninfo)
1091#endif
1092
1093/** @} */
1094
1095/**@name Monomial Expressions */
1096/**@{ */
1097
1098/** returns a monomial representation of a product expression
1099 *
1100 * The array to store all factor expressions needs to be of size the number of
1101 * children in the expression which is given by SCIPexprGetNChildren().
1102 *
1103 * Given a non-trivial monomial expression, the function finds its representation as \f$cx^\alpha\f$, where
1104 * \f$c\f$ is a real coefficient, \f$x\f$ is a vector of auxiliary or original variables (where some entries can
1105 * be NULL is the auxiliary variable has not been created yet), and \f$\alpha\f$ is a real vector of exponents.
1106 *
1107 * A non-trivial monomial is a product of a least two expressions.
1108 */
1109SCIP_EXPORT
1111 SCIP* scip, /**< SCIP data structure */
1112 SCIP_EXPR* expr, /**< expression */
1113 SCIP_Real* coef, /**< coefficient \f$c\f$ */
1114 SCIP_Real* exponents, /**< exponents \f$\alpha\f$ */
1115 SCIP_EXPR** factors /**< factor expressions \f$x\f$ */
1116 );
1117
1118#ifdef NDEBUG
1119#define SCIPgetExprMonomialData(scip, expr, coef, exponents, factors) SCIPexprGetMonomialData((scip)->set, (scip)->mem->probmem, expr, coef, exponents, factors)
1120#endif
1121
1122/** @} */
1123
1124/** @} */
1125
1126#ifdef __cplusplus
1127}
1128#endif
1129
1130#endif /* SCIP_SCIP_EXPR_H_ */
#define SCIP_Longint
Definition: def.h:141
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:156
private functions to work with algebraic expressions
static SCIP_RETCODE eval(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPRINTDATA *exprintdata, const vector< Type > &x, Type &val)
int SCIPgetNExprhdlrs(SCIP *scip)
Definition: scip_expr.c:883
SCIP_EXPRHDLR * SCIPgetExprhdlrProduct(SCIP *scip)
Definition: scip_expr.c:939
SCIP_EXPRHDLR * SCIPgetExprhdlrVar(SCIP *scip)
Definition: scip_expr.c:906
SCIP_EXPRHDLR ** SCIPgetExprhdlrs(SCIP *scip)
Definition: scip_expr.c:872
SCIP_EXPRHDLR * SCIPgetExprhdlrValue(SCIP *scip)
Definition: scip_expr.c:917
SCIP_EXPRHDLR * SCIPgetExprhdlrSum(SCIP *scip)
Definition: scip_expr.c:928
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:847
SCIP_EXPRHDLR * SCIPgetExprhdlrPower(SCIP *scip)
Definition: scip_expr.c:950
SCIP_EXPRHDLR * SCIPfindExprhdlr(SCIP *scip, const char *name)
Definition: scip_expr.c:894
SCIP_DECL_EXPRMONOTONICITY(SCIPcallExprMonotonicity)
Definition: scip_expr.c:2194
SCIP_RETCODE SCIPcreateExprQuadratic(SCIP *scip, SCIP_EXPR **expr, int nlinvars, SCIP_VAR **linvars, SCIP_Real *lincoefs, int nquadterms, SCIP_VAR **quadvars1, SCIP_VAR **quadvars2, SCIP_Real *quadcoefs, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1059
SCIP_RETCODE SCIPcreateExprMonomial(SCIP *scip, SCIP_EXPR **expr, int nfactors, SCIP_VAR **vars, SCIP_Real *exponents, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1167
SCIP_RETCODE SCIPgetSymDataExpr(SCIP *scip, SCIP_EXPR *expr, SYM_EXPRDATA **symdata)
Definition: scip_expr.c:1817
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:1000
SCIP_RETCODE SCIPappendExprChild(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR *child)
Definition: scip_expr.c:1256
SCIP_RETCODE SCIPevalExprHessianDir(SCIP *scip, SCIP_EXPR *expr, SCIP_SOL *sol, SCIP_Longint soltag, SCIP_SOL *direction)
Definition: scip_expr.c:1714
SCIP_RETCODE SCIPevalExpr(SCIP *scip, SCIP_EXPR *expr, SCIP_SOL *sol, SCIP_Longint soltag)
Definition: scip_expr.c:1661
SCIP_DECL_EXPRINTEVAL(SCIPcallExprInteval)
Definition: scip_expr.c:2261
SCIP_RETCODE SCIPprintExprQuadratic(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:2495
SCIP_RETCODE SCIPcomputeExprIntegrality(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:2040
SCIP_DECL_EXPRPRINT(SCIPcallExprPrint)
Definition: scip_expr.c:2164
SCIP_Bool SCIPisExprProduct(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1490
SCIP_RETCODE SCIPevalExprGradient(SCIP *scip, SCIP_EXPR *expr, SCIP_SOL *sol, SCIP_Longint soltag)
Definition: scip_expr.c:1692
SCIP_RETCODE SCIPprintExprDotInit2(SCIP *scip, SCIP_EXPRPRINTDATA **printdata, const char *filename, SCIP_EXPRPRINT_WHAT whattoprint)
Definition: scip_expr.c:1543
SCIP_Longint SCIPgetExprNewSoltag(SCIP *scip)
Definition: scip_expr.c:1677
SCIP_Bool SCIPisExprSum(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1479
SCIP_RETCODE SCIPgetExprMonomialData(SCIP *scip, SCIP_EXPR *expr, SCIP_Real *coef, SCIP_Real *exponents, SCIP_EXPR **factors)
Definition: scip_expr.c:2648
SCIP_RETCODE SCIPgetExprNVars(SCIP *scip, SCIP_EXPR *expr, int *nvars)
Definition: scip_expr.c:2083
SCIP_RETCODE SCIPduplicateExprShallow(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR **copyexpr, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1327
SCIP_RETCODE SCIPreplaceExprChild(SCIP *scip, SCIP_EXPR *expr, int childidx, SCIP_EXPR *newchild)
Definition: scip_expr.c:1274
SCIP_Bool SCIPisExprValue(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1468
SCIP_RETCODE SCIPcreateExpr2(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPRHDLR *exprhdlr, SCIP_EXPRDATA *exprdata, SCIP_EXPR *child1, SCIP_EXPR *child2, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1021
void SCIPfreeExprQuadratic(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:2420
SCIP_RETCODE SCIPprintExprDot(SCIP *scip, SCIP_EXPRPRINTDATA *printdata, SCIP_EXPR *expr)
Definition: scip_expr.c:1559
int SCIPcompareExpr(SCIP *scip, SCIP_EXPR *expr1, SCIP_EXPR *expr2)
Definition: scip_expr.c:1759
SCIP_RETCODE SCIPreleaseExpr(SCIP *scip, SCIP_EXPR **expr)
Definition: scip_expr.c:1443
SCIP_Bool SCIPisExprVar(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1457
SCIP_RETCODE SCIPparseExpr(SCIP *scip, SCIP_EXPR **expr, const char *exprstr, const char **finalpos, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1406
SCIP_RETCODE SCIPhashExpr(SCIP *scip, SCIP_EXPR *expr, unsigned int *hashval)
Definition: scip_expr.c:1771
SCIP_RETCODE SCIPcomputeExprQuadraticCurvature(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPRCURV *curv, SCIP_HASHMAP *assumevarfixed, SCIP_Bool storeeigeninfo)
Definition: scip_expr.c:2611
SCIP_DECL_EXPRGETSYMDATA(SCIPcallExprGetSymData)
Definition: scip_expr.c:2340
SCIP_DECL_EXPRCURVATURE(SCIPcallExprCurvature)
Definition: scip_expr.c:2179
SCIP_RETCODE SCIPcallExprEval(SCIP *scip, SCIP_EXPR *expr, SCIP_Real *childrenvalues, SCIP_Real *val)
Definition: scip_expr.c:2210
SCIP_RETCODE SCIPcreateExpriter(SCIP *scip, SCIP_EXPRITER **iterator)
Definition: scip_expr.c:2362
SCIP_RETCODE SCIPcallExprEvalFwdiff(SCIP *scip, SCIP_EXPR *expr, SCIP_Real *childrenvalues, SCIP_Real *direction, SCIP_Real *val, SCIP_Real *dot)
Definition: scip_expr.c:2237
SCIP_RETCODE SCIPprintExpr(SCIP *scip, SCIP_EXPR *expr, FILE *file)
Definition: scip_expr.c:1512
SCIP_Bool SCIPisExprPower(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1501
SCIP_RETCODE SCIPreplaceCommonSubexpressions(SCIP *scip, SCIP_EXPR **exprs, int nexprs, SCIP_Bool *replacedroot)
Definition: scip_expr.c:1845
SCIP_DECL_EXPRSIMPLIFY(SCIPcallExprSimplify)
Definition: scip_expr.c:2310
SCIP_RETCODE SCIPcheckExprQuadratic(SCIP *scip, SCIP_EXPR *expr, SCIP_Bool *isquadratic)
Definition: scip_expr.c:2402
SCIP_RETCODE SCIPprintExprDotFinal(SCIP *scip, SCIP_EXPRPRINTDATA **printdata)
Definition: scip_expr.c:1573
SCIP_RETCODE SCIPprintExprDotInit(SCIP *scip, SCIP_EXPRPRINTDATA **printdata, FILE *file, SCIP_EXPRPRINT_WHAT whattoprint)
Definition: scip_expr.c:1527
SCIP_DECL_EXPRESTIMATE(SCIPcallExprEstimate)
Definition: scip_expr.c:2276
SCIP_RETCODE SCIPcopyExpr(SCIP *sourcescip, SCIP *targetscip, SCIP_EXPR *expr, SCIP_EXPR **copyexpr, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global, SCIP_Bool *valid)
Definition: scip_expr.c:1344
SCIP_RETCODE SCIPshowExpr(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1595
SCIP_Real SCIPevalExprQuadratic(SCIP *scip, SCIP_EXPR *expr, SCIP_SOL *sol)
Definition: scip_expr.c:2435
SCIP_RETCODE SCIPcomputeExprCurvature(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1960
SCIP_DECL_EXPRREVERSEPROP(SCIPcallExprReverseprop)
Definition: scip_expr.c:2327
SCIP_DECL_EXPRINITESTIMATES(SCIPcallExprInitestimates)
Definition: scip_expr.c:2292
void SCIPfreeExpriter(SCIP_EXPRITER **iterator)
Definition: scip_expr.c:2376
SCIP_RETCODE SCIPduplicateExpr(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR **copyexpr, SCIP_DECL_EXPR_MAPEXPR((*mapexpr)), void *mapexprdata, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1307
void SCIPcaptureExpr(SCIP_EXPR *expr)
Definition: scip_expr.c:1435
SCIP_RETCODE SCIPgetExprVarExprs(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR **varexprs, int *nvarexprs)
Definition: scip_expr.c:2121
SCIP_RETCODE SCIPdismantleExpr(SCIP *scip, FILE *file, SCIP_EXPR *expr)
Definition: scip_expr.c:1634
SCIP_RETCODE SCIPremoveExprChildren(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1293
SCIP_RETCODE SCIPsimplifyExpr(SCIP *scip, SCIP_EXPR *rootexpr, SCIP_EXPR **simplified, SCIP_Bool *changed, SCIP_Bool *infeasible, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: scip_expr.c:1798
SCIP_RETCODE SCIPevalExprActivity(SCIP *scip, SCIP_EXPR *expr)
Definition: scip_expr.c:1742
internal methods for global SCIP settings
datastructures for block memory pools and memory buffers
SCIP main data structure.
datastructures for global SCIP settings
datastructures for problem statistics
type and macro definitions related to algebraic expressions
#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_EXPRCURV
Definition: type_expr.h:61
unsigned int SCIP_EXPRPRINT_WHAT
Definition: type_expr.h:742
#define SCIP_DECL_EXPREVAL(x)
Definition: type_expr.h:428
#define SCIP_DECL_EXPR_MAPEXPR(x)
Definition: type_expr.h:182
struct SCIP_ExprPrintData SCIP_EXPRPRINTDATA
Definition: type_expr.h:743
type definitions for message output methods
type definitions for miscellaneous datastructures
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
type definitions for SCIP's main datastructure