Scippy

SCIP

Solving Constraint Integer Programs

cons_quadratic.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 scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_quadratic.c
17  * @ingroup DEFPLUGINS_CONS
18  * @brief some API functions of removed constraint handler for quadratic constraints \f$\textrm{lhs} \leq \sum_{i,j} a_{i,j} x_i x_j + \sum_i b_i x_i \leq \textrm{rhs}\f$
19  * @author Stefan Vigerske
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/cons_nonlinear.h"
25 #include "scip/cons_quadratic.h"
26 #include "scip/expr_var.h"
27 #include "scip/expr_pow.h"
28 #include "scip/expr_product.h"
29 
30 /** Creates and captures a quadratic nonlinear constraint.
31  *
32  * The constraint should be given in the form
33  * \f[
34  * \ell \leq \sum_{i=1}^n b_i x_i + \sum_{j=1}^m a_j y_j z_j \leq u,
35  * \f]
36  * where \f$x_i = y_j = z_k\f$ is possible.
37  *
38  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
39  *
40  * @deprecated Use SCIPcreateConsQuadraticNonlinear() instead.
41  */
43  SCIP* scip, /**< SCIP data structure */
44  SCIP_CONS** cons, /**< pointer to hold the created constraint */
45  const char* name, /**< name of constraint */
46  int nlinvars, /**< number of linear terms (n) */
47  SCIP_VAR** linvars, /**< variables in linear part (x_i) or NULL if nlinvars == 0 */
48  SCIP_Real* lincoefs, /**< coefficients of variables in linear part (b_i) or NULL if nlinvars == 0 */
49  int nquadterms, /**< number of quadratic terms (m) */
50  SCIP_VAR** quadvars1, /**< array with first variables in quadratic terms (y_j) or NULL if nquadterms == 0 */
51  SCIP_VAR** quadvars2, /**< array with second variables in quadratic terms (z_j) or NULL if nquadterms == 0 */
52  SCIP_Real* quadcoeffs, /**< array with coefficients of quadratic terms (a_j) or NULL if nquadterms == 0 */
53  SCIP_Real lhs, /**< left hand side of quadratic equation (l) */
54  SCIP_Real rhs, /**< right hand side of quadratic equation (u) */
55  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
56  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
57  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
58  * Usually set to TRUE. */
59  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
60  * TRUE for model constraints, FALSE for additional, redundant constraints. */
61  SCIP_Bool check, /**< should the constraint be checked for feasibility?
62  * TRUE for model constraints, FALSE for additional, redundant constraints. */
63  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
64  * Usually set to TRUE. */
65  SCIP_Bool local, /**< is constraint only valid locally?
66  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
67  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
68  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
69  * adds coefficients to this constraint. */
70  SCIP_Bool dynamic, /**< is constraint subject to aging?
71  * Usually set to FALSE. Set to TRUE for own cuts which
72  * are separated as constraints. */
73  SCIP_Bool removable /**< should the relaxation be removed from the LP due to aging or cleanup?
74  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
75  )
76 {
77  SCIP_CALL( SCIPcreateConsQuadraticNonlinear(scip, cons, name, nlinvars, linvars, lincoefs, nquadterms, quadvars1, quadvars2, quadcoeffs, lhs, rhs,
78  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable) );
79 
80  return SCIP_OKAY;
81 }
82 
83 /** creates and captures a quadratic nonlinear constraint
84  * in its most basic variant, i.e., with all constraint flags set to their default values, which can be set
85  * afterwards using SCIPsetConsFLAGNAME()
86  *
87  * The constraint should be given in the form
88  * \f[
89  * \ell \leq \sum_{i=1}^n b_i x_i + \sum_{j=1}^m a_j y_jz_j \leq u,
90  * \f]
91  * where \f$x_i = y_j = z_k\f$ is possible.
92  *
93  * @see SCIPcreateConsQuadratic() for the default constraint flag configuration
94  *
95  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
96  *
97  * @deprecated Use SCIPcreateConsBasicQuadraticNonlinear instead.
98  */
100  SCIP* scip, /**< SCIP data structure */
101  SCIP_CONS** cons, /**< pointer to hold the created constraint */
102  const char* name, /**< name of constraint */
103  int nlinvars, /**< number of linear terms (n) */
104  SCIP_VAR** linvars, /**< array with variables in linear part (x_i) */
105  SCIP_Real* lincoefs, /**< array with coefficients of variables in linear part (b_i) */
106  int nquadterms, /**< number of quadratic terms (m) */
107  SCIP_VAR** quadvars1, /**< array with first variables in quadratic terms (y_j) */
108  SCIP_VAR** quadvars2, /**< array with second variables in quadratic terms (z_j) */
109  SCIP_Real* quadcoefs, /**< array with coefficients of quadratic terms (a_j) */
110  SCIP_Real lhs, /**< left hand side of quadratic equation (ell) */
111  SCIP_Real rhs /**< right hand side of quadratic equation (u) */
112  )
113 {
114  SCIP_CALL( SCIPcreateConsBasicQuadraticNonlinear(scip, cons, name, nlinvars, linvars, lincoefs, nquadterms, quadvars1, quadvars2, quadcoefs, lhs, rhs) );
115 
116  return SCIP_OKAY;
117 }
118 
119 /** Adds a constant to the constraint function, that is, subtracts a constant from both sides
120  *
121  * @deprecated Use SCIPchgLhsNonlinear() and SCIPchgRhsNonlinear() instead.
122  */
124  SCIP* scip, /**< SCIP data structure */
125  SCIP_CONS* cons, /**< constraint */
126  SCIP_Real constant /**< constant to subtract from both sides */
127  )
128 {
129  SCIP_Real side;
130 
131  assert(cons != NULL);
132  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
133 
134  side = SCIPgetLhsNonlinear(cons);
135  if( !SCIPisInfinity(scip, -side) )
136  {
137  SCIP_CALL_ABORT( SCIPchgLhsNonlinear(scip, cons, side-constant) );
138  }
139 
140  side = SCIPgetRhsNonlinear(cons);
141  if( !SCIPisInfinity(scip, side) )
142  {
143  SCIP_CALL_ABORT( SCIPchgRhsNonlinear(scip, cons, side-constant) );
144  }
145 }
146 
147 /** Adds a linear variable with coefficient to a quadratic constraint.
148  *
149  * @deprecated Use SCIPaddLinearVarNonlinear() instead.
150  */
152  SCIP* scip, /**< SCIP data structure */
153  SCIP_CONS* cons, /**< constraint */
154  SCIP_VAR* var, /**< variable */
155  SCIP_Real coef /**< coefficient of variable */
156  )
157 {
158  assert(cons != NULL);
159  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
160 
161  SCIP_CALL( SCIPaddLinearVarNonlinear(scip, cons, var, coef) );
162 
163  return SCIP_OKAY;
164 }
165 
166 /** Adds a quadratic variable with linear and square coefficient to a quadratic constraint.
167  *
168  * @deprecated Use SCIPaddLinearVarNonlinear() and SCIPaddExprNonlinear() instead.
169  */
171  SCIP* scip, /**< SCIP data structure */
172  SCIP_CONS* cons, /**< constraint */
173  SCIP_VAR* var, /**< variable */
174  SCIP_Real lincoef, /**< linear coefficient of variable */
175  SCIP_Real sqrcoef /**< square coefficient of variable */
176  )
177 {
178  assert(cons != NULL);
179  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
180 
181  if( lincoef != 0.0 )
182  {
183  SCIP_CALL( SCIPaddLinearVarNonlinear(scip, cons, var, lincoef) );
184  }
185 
186  if( sqrcoef != 0.0 )
187  {
188  SCIP_EXPR* varexpr;
189  SCIP_EXPR* sqrexpr;
190 
191  SCIP_CALL( SCIPcreateExprVar(scip, &varexpr, var, NULL, NULL) );
192  SCIP_CALL( SCIPcreateExprPow(scip, &sqrexpr, varexpr, 2.0, NULL, NULL) );
193 
194  SCIP_CALL( SCIPaddExprNonlinear(scip, cons, sqrexpr, sqrcoef) );
195 
196  SCIP_CALL( SCIPreleaseExpr(scip, &sqrexpr) );
197  SCIP_CALL( SCIPreleaseExpr(scip, &varexpr) );
198  }
199 
200  return SCIP_OKAY;
201 }
202 
203 /** Adds a linear coefficient for a quadratic variable.
204  *
205  * Variable will be added with square coefficient 0.0 if not existing yet.
206  *
207  * @deprecated Use SCIPaddLinearVarNonlinear() instead.
208  */
210  SCIP* scip, /**< SCIP data structure */
211  SCIP_CONS* cons, /**< constraint */
212  SCIP_VAR* var, /**< variable */
213  SCIP_Real coef /**< value to add to linear coefficient of variable */
214  )
215 {
216  assert(cons != NULL);
217  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
218 
219  SCIP_CALL( SCIPaddLinearVarNonlinear(scip, cons, var, coef) );
220 
221  return SCIP_OKAY;
222 }
223 
224 /** Adds a square coefficient for a quadratic variable.
225  *
226  * Variable will be added with linear coefficient 0.0 if not existing yet.
227  *
228  * @deprecated Use SCIPaddExprNonlinear() instead.
229  */
231  SCIP* scip, /**< SCIP data structure */
232  SCIP_CONS* cons, /**< constraint */
233  SCIP_VAR* var, /**< variable */
234  SCIP_Real coef /**< value to add to square coefficient of variable */
235  )
236 {
237  SCIP_EXPR* varexpr;
238  SCIP_EXPR* sqrexpr;
239 
240  assert(cons != NULL);
241  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
242 
243  SCIP_CALL( SCIPcreateExprVar(scip, &varexpr, var, NULL, NULL) );
244  SCIP_CALL( SCIPcreateExprPow(scip, &sqrexpr, varexpr, 2.0, NULL, NULL) );
245 
246  SCIP_CALL( SCIPaddExprNonlinear(scip, cons, sqrexpr, coef) );
247 
248  SCIP_CALL( SCIPreleaseExpr(scip, &sqrexpr) );
249  SCIP_CALL( SCIPreleaseExpr(scip, &varexpr) );
250 
251  return SCIP_OKAY;
252 }
253 
254 /** Adds a bilinear term to a quadratic constraint.
255  *
256  * Variables will be added with linear and square coefficient 0.0 if not existing yet.
257  * If variables are equal, only the square coefficient of the variable is updated.
258  *
259  * @deprecated Use SCIPaddExprNonlinear() instead.
260  */
262  SCIP* scip, /**< SCIP data structure */
263  SCIP_CONS* cons, /**< constraint */
264  SCIP_VAR* var1, /**< first variable */
265  SCIP_VAR* var2, /**< second variable */
266  SCIP_Real coef /**< coefficient of bilinear term */
267  )
268 {
269  SCIP_EXPR* varexprs[2];
270  SCIP_EXPR* prodexpr;
271 
272  assert(cons != NULL);
273  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
274 
275  SCIP_CALL( SCIPcreateExprVar(scip, &varexprs[0], var1, NULL, NULL) );
276  SCIP_CALL( SCIPcreateExprVar(scip, &varexprs[1], var2, NULL, NULL) );
277  SCIP_CALL( SCIPcreateExprProduct(scip, &prodexpr, 2, varexprs, 1.0, NULL, NULL) );
278 
279  SCIP_CALL( SCIPaddExprNonlinear(scip, cons, prodexpr, coef) );
280 
281  SCIP_CALL( SCIPreleaseExpr(scip, &prodexpr) );
282  SCIP_CALL( SCIPreleaseExpr(scip, &varexprs[1]) );
283  SCIP_CALL( SCIPreleaseExpr(scip, &varexprs[0]) );
284 
285  return SCIP_OKAY;
286 }
287 
288 /** Gets the quadratic constraint as a nonlinear row representation.
289  *
290  * @deprecated Use SCIPgetNlRowNonlinear() instead.
291  */
293  SCIP* scip, /**< SCIP data structure */
294  SCIP_CONS* cons, /**< constraint */
295  SCIP_NLROW** nlrow /**< pointer to store nonlinear row */
296  )
297 {
298  assert(cons != NULL);
299  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
300 
301  SCIP_CALL( SCIPgetNlRowNonlinear(scip, cons, nlrow) );
302 
303  return SCIP_OKAY;
304 }
305 
306 /** sets the left hand side of a quadratic constraint
307  *
308  * @note This method may only be called during problem creation stage for an original constraint.
309  *
310  * @deprecated Use SCIPchgLhsNonlinear() instead.
311  */
313  SCIP* scip, /**< SCIP data structure */
314  SCIP_CONS* cons, /**< constraint data */
315  SCIP_Real lhs /**< new left hand side */
316  )
317 {
318  assert(cons != NULL);
319  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
320 
321  SCIP_CALL( SCIPchgLhsNonlinear(scip, cons, lhs) );
322 
323  return SCIP_OKAY;
324 }
325 
326 /** sets the right hand side of a quadratic constraint
327  *
328  * @note This method may only be called during problem creation stage for an original constraint.
329  *
330  * @deprecated Use SCIPchgRhsNonlinear() instead.
331  */
333  SCIP* scip, /**< SCIP data structure */
334  SCIP_CONS* cons, /**< constraint data */
335  SCIP_Real rhs /**< new right hand side */
336  )
337 {
338  assert(cons != NULL);
339  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
340 
341  SCIP_CALL( SCIPchgRhsNonlinear(scip, cons, rhs) );
342 
343  return SCIP_OKAY;
344 }
SCIP_RETCODE SCIPaddQuadVarQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real lincoef, SCIP_Real sqrcoef)
SCIP_RETCODE SCIPcreateExprPow(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_Real exponent, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: expr_pow.c:3166
void SCIPaddConstantQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_Real constant)
SCIP_RETCODE SCIPaddSquareCoefQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
SCIP_RETCODE SCIPchgRhsQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
SCIP_Real SCIPgetRhsNonlinear(SCIP_CONS *cons)
SCIP_RETCODE SCIPchgRhsNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
SCIP_RETCODE SCIPgetNlRowNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_NLROW **nlrow)
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_RETCODE SCIPcreateExprVar(SCIP *scip, SCIP_EXPR **expr, SCIP_VAR *var, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: expr_var.c:377
variable expression handler
SCIP_RETCODE SCIPcreateConsBasicQuadratic(SCIP *scip, SCIP_CONS **cons, const char *name, int nlinvars, SCIP_VAR **linvars, SCIP_Real *lincoefs, int nquadterms, SCIP_VAR **quadvars1, SCIP_VAR **quadvars2, SCIP_Real *quadcoefs, SCIP_Real lhs, SCIP_Real rhs)
SCIP_RETCODE SCIPaddLinearVarQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
SCIP_RETCODE SCIPcreateConsQuadratic(SCIP *scip, SCIP_CONS **cons, const char *name, int nlinvars, SCIP_VAR **linvars, SCIP_Real *lincoefs, int nquadterms, SCIP_VAR **quadvars1, SCIP_VAR **quadvars2, SCIP_Real *quadcoeffs, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable)
SCIP_RETCODE SCIPchgLhsQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
SCIP_RETCODE SCIPcreateConsBasicQuadraticNonlinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nlinvars, SCIP_VAR **linvars, SCIP_Real *lincoefs, int nquadterms, SCIP_VAR **quadvars1, SCIP_VAR **quadvars2, SCIP_Real *quadcoefs, SCIP_Real lhs, SCIP_Real rhs)
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4175
SCIP_RETCODE SCIPcreateConsQuadraticNonlinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nlinvars, SCIP_VAR **linvars, SCIP_Real *lincoefs, int nquadterms, SCIP_VAR **quadvars1, SCIP_VAR **quadvars2, SCIP_Real *quadcoefs, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable)
some API functions of removed constraint handler for quadratic constraints
#define NULL
Definition: lpi_spx1.cpp:155
SCIP_RETCODE SCIPcreateExprProduct(SCIP *scip, SCIP_EXPR **expr, int nchildren, SCIP_EXPR **children, SCIP_Real coefficient, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
power and signed power expression handlers
SCIP_RETCODE SCIPaddExprNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_EXPR *expr, SCIP_Real coef)
SCIP_RETCODE SCIPgetNlRowQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_NLROW **nlrow)
#define SCIP_CALL(x)
Definition: def.h:384
SCIP_RETCODE SCIPaddLinearVarNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
SCIP_RETCODE SCIPaddBilinTermQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var1, SCIP_VAR *var2, SCIP_Real coef)
#define SCIP_Bool
Definition: def.h:84
SCIP_RETCODE SCIPreleaseExpr(SCIP *scip, SCIP_EXPR **expr)
Definition: scip_expr.c:1407
constraint handler for nonlinear constraints specified by algebraic expressions
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:8105
SCIP_RETCODE SCIPchgLhsNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
product expression handler
#define SCIP_Real
Definition: def.h:177
SCIP_RETCODE SCIPaddQuadVarLinearCoefQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
#define SCIP_CALL_ABORT(x)
Definition: def.h:363
SCIP_Real SCIPgetLhsNonlinear(SCIP_CONS *cons)