Scippy

SCIP

Solving Constraint Integer Programs

cons_soc.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-2021 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_soc.c
17  * @ingroup DEFPLUGINS_CONS
18  * @brief some API functions of removed constraint handler for second order cone constraints \f$\sqrt{\gamma + \sum_{i=1}^{n} (\alpha_i\, (x_i + \beta_i))^2} \leq \alpha_{n+1}\, (x_{n+1}+\beta_{n+1})\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_soc.h"
25 #include "scip/cons_nonlinear.h"
26 #include "scip/expr_var.h"
27 #include "scip/expr_pow.h"
28 #include "scip/expr_sum.h"
29 
30 /** creates expression for \f$\sqrt{\gamma + \sum_{i=1}^{n} (\alpha_i\, (x_i + \beta_i))^2} - \alpha_{n+1} x_{n+1}\f$ */
31 static
33  SCIP* scip, /**< SCIP data structure */
34  SCIP_EXPR** expr, /**< buffer to store expression */
35  int nvars, /**< number of variables on left hand side of constraint (n) */
36  SCIP_VAR** vars, /**< array with variables on left hand side (x_i) */
37  SCIP_Real* coefs, /**< array with coefficients of left hand side variables (alpha_i), or NULL if all 1.0 */
38  SCIP_Real* offsets, /**< array with offsets of variables (beta_i), or NULL if all 0.0 */
39  SCIP_Real constant, /**< constant on left hand side (gamma) */
40  SCIP_VAR* rhsvar, /**< variable on right hand side of constraint (x_{n+1}) */
41  SCIP_Real rhscoeff /**< coefficient of variable on right hand side (alpha_{n+1}) */
42  )
43 {
44  SCIP_EXPR* lhssum;
45  SCIP_EXPR* terms[2];
46  SCIP_Real termcoefs[2];
47  int i;
48 
49  assert(expr != NULL);
50  assert(vars != NULL || nvars == 0);
51 
52  SCIP_CALL( SCIPcreateExprSum(scip, &lhssum, 0, NULL, NULL, constant, NULL, NULL) ); /* gamma */
53  for( i = 0; i < nvars; ++i )
54  {
55  SCIP_EXPR* varexpr;
56  SCIP_EXPR* powexpr;
57 
58  SCIP_CALL( SCIPcreateExprVar(scip, &varexpr, vars[i], NULL, NULL) ); /* x_i */
59  if( offsets != NULL && offsets[i] != 0.0 )
60  {
61  SCIP_EXPR* sum;
62  SCIP_CALL( SCIPcreateExprSum(scip, &sum, 1, &varexpr, NULL, offsets[i], NULL, NULL) ); /* x_i + beta_i */
63  SCIP_CALL( SCIPcreateExprPow(scip, &powexpr, sum, 2.0, NULL, NULL) ); /* (x_i + beta_i)^2 */
64  SCIP_CALL( SCIPreleaseExpr(scip, &sum) );
65  }
66  else
67  {
68  SCIP_CALL( SCIPcreateExprPow(scip, &powexpr, varexpr, 2.0, NULL, NULL) ); /* x_i^2 */
69  }
70 
71  SCIP_CALL( SCIPappendExprSumExpr(scip, lhssum, powexpr, coefs != NULL ? coefs[i]*coefs[i] : 1.0) ); /* + alpha_i^2 (x_i + beta_i)^2 */
72  SCIP_CALL( SCIPreleaseExpr(scip, &varexpr) );
73  SCIP_CALL( SCIPreleaseExpr(scip, &powexpr) );
74  }
75 
76  SCIP_CALL( SCIPcreateExprPow(scip, &terms[0], lhssum, 0.5, NULL, NULL) ); /* sqrt(...) */
77  SCIP_CALL( SCIPreleaseExpr(scip, &lhssum) );
78  termcoefs[0] = 1.0;
79 
80  SCIP_CALL( SCIPcreateExprVar(scip, &terms[1], rhsvar, NULL, NULL) ); /* x_{n+1} */
81  termcoefs[1] = -rhscoeff;
82 
83  SCIP_CALL( SCIPcreateExprSum(scip, expr, 2, terms, termcoefs, 0.0, NULL, NULL) ); /* sqrt(...) - alpha_{n+1}x_{n_1} */
84 
85  SCIP_CALL( SCIPreleaseExpr(scip, &terms[1]) );
86  SCIP_CALL( SCIPreleaseExpr(scip, &terms[0]) );
87 
88  return SCIP_OKAY;
89 }
90 
91 /** creates and captures a second order cone nonlinear constraint
92  *
93  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
94  *
95  * @deprecated Use SCIPcreateConsNonlinear() instead
96  */
98  SCIP* scip, /**< SCIP data structure */
99  SCIP_CONS** cons, /**< pointer to hold the created constraint */
100  const char* name, /**< name of constraint */
101  int nvars, /**< number of variables on left hand side of constraint (n) */
102  SCIP_VAR** vars, /**< array with variables on left hand side (x_i) */
103  SCIP_Real* coefs, /**< array with coefficients of left hand side variables (alpha_i), or NULL if all 1.0 */
104  SCIP_Real* offsets, /**< array with offsets of variables (beta_i), or NULL if all 0.0 */
105  SCIP_Real constant, /**< constant on left hand side (gamma) */
106  SCIP_VAR* rhsvar, /**< variable on right hand side of constraint (x_{n+1}) */
107  SCIP_Real rhscoeff, /**< coefficient of variable on right hand side (alpha_{n+1}) */
108  SCIP_Real rhsoffset, /**< offset of variable on right hand side (beta_{n+1}) */
109  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
110  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
111  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
112  * Usually set to TRUE. */
113  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
114  * TRUE for model constraints, FALSE for additional, redundant constraints. */
115  SCIP_Bool check, /**< should the constraint be checked for feasibility?
116  * TRUE for model constraints, FALSE for additional, redundant constraints. */
117  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
118  * Usually set to TRUE. */
119  SCIP_Bool local, /**< is constraint only valid locally?
120  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
121  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
122  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
123  * adds coefficients to this constraint. */
124  SCIP_Bool dynamic, /**< is constraint subject to aging?
125  * Usually set to FALSE. Set to TRUE for own cuts which
126  * are separated as constraints. */
127  SCIP_Bool removable /**< should the relaxation be removed from the LP due to aging or cleanup?
128  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
129  )
130 {
131  SCIP_EXPR* expr;
132 
133  SCIP_CALL( createSOCExpression(scip, &expr, nvars, vars, coefs, offsets, constant, rhsvar, rhscoeff) );
134 
135  SCIP_CALL( SCIPcreateConsNonlinear(scip, cons, name, expr, -SCIPinfinity(scip), rhscoeff * rhsoffset,
136  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable) );
137 
138  SCIP_CALL( SCIPreleaseExpr(scip, &expr) );
139 
140  return SCIP_OKAY;
141 }
142 
143 /** creates and captures a second order cone nonlinear constraint
144  * in its most basic variant, i. e., with all constraint flags set to their default values, which can be set
145  * afterwards using SCIPsetConsFLAGNAME()
146  *
147  * @see SCIPcreateConsSOC() for the default constraint flag configuration
148  *
149  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
150  *
151  * @deprecated Use SCIPcreateConsBasicNonlinear() instead
152  */
154  SCIP* scip, /**< SCIP data structure */
155  SCIP_CONS** cons, /**< pointer to hold the created constraint */
156  const char* name, /**< name of constraint */
157  int nvars, /**< number of variables on left hand side of constraint (n) */
158  SCIP_VAR** vars, /**< array with variables on left hand side (x_i) */
159  SCIP_Real* coefs, /**< array with coefficients of left hand side variables (alpha_i), or NULL if all 1.0 */
160  SCIP_Real* offsets, /**< array with offsets of variables (beta_i), or NULL if all 0.0 */
161  SCIP_Real constant, /**< constant on left hand side (gamma) */
162  SCIP_VAR* rhsvar, /**< variable on right hand side of constraint (x_{n+1}) */
163  SCIP_Real rhscoeff, /**< coefficient of variable on right hand side (alpha_{n+1}) */
164  SCIP_Real rhsoffset /**< offset of variable on right hand side (beta_{n+1}) */
165  )
166 {
167  SCIP_EXPR* expr;
168 
169  SCIP_CALL( createSOCExpression(scip, &expr, nvars, vars, coefs, offsets, constant, rhsvar, rhscoeff) );
170 
171  SCIP_CALL( SCIPcreateConsBasicNonlinear(scip, cons, name, expr, -SCIPinfinity(scip), rhscoeff * rhsoffset) );
172 
173  SCIP_CALL( SCIPreleaseExpr(scip, &expr) );
174 
175  return SCIP_OKAY;
176 }
177 
178 /** Gets the SOC constraint as a nonlinear row representation.
179  *
180  * @deprecated Use SCIPgetNlRowNonlinear() instead.
181  */
183  SCIP* scip, /**< SCIP data structure */
184  SCIP_CONS* cons, /**< constraint */
185  SCIP_NLROW** nlrow /**< pointer to store nonlinear row */
186  )
187 {
188  assert(cons != NULL);
189  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), "nonlinear") == 0);
190 
191  SCIP_CALL( SCIPgetNlRowNonlinear(scip, cons, nlrow) );
192 
193  return SCIP_OKAY;
194 }
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:3160
SCIP_RETCODE SCIPcreateConsBasicSOC(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *coefs, SCIP_Real *offsets, SCIP_Real constant, SCIP_VAR *rhsvar, SCIP_Real rhscoeff, SCIP_Real rhsoffset)
Definition: cons_soc.c:153
static SCIP_RETCODE createSOCExpression(SCIP *scip, SCIP_EXPR **expr, int nvars, SCIP_VAR **vars, SCIP_Real *coefs, SCIP_Real *offsets, SCIP_Real constant, SCIP_VAR *rhsvar, SCIP_Real rhscoeff)
Definition: cons_soc.c:32
SCIP_Real SCIPinfinity(SCIP *scip)
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
some API functions of removed constraint handler for second order cone constraints ...
variable expression handler
SCIP_RETCODE SCIPappendExprSumExpr(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR *child, SCIP_Real childcoef)
Definition: expr_sum.c:1099
SCIP_RETCODE SCIPcreateExprSum(SCIP *scip, SCIP_EXPR **expr, int nchildren, SCIP_EXPR **children, SCIP_Real *coefficients, SCIP_Real constant, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
Definition: expr_sum.c:1062
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4167
SCIP_RETCODE SCIPgetNlRowSOC(SCIP *scip, SCIP_CONS *cons, SCIP_NLROW **nlrow)
Definition: cons_soc.c:182
SCIP_RETCODE SCIPcreateConsBasicNonlinear(SCIP *scip, SCIP_CONS **cons, const char *name, SCIP_EXPR *expr, SCIP_Real lhs, SCIP_Real rhs)
#define NULL
Definition: lpi_spx1.cpp:155
power and signed power expression handlers
#define SCIP_CALL(x)
Definition: def.h:384
#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:8097
#define SCIP_Real
Definition: def.h:177
SCIP_RETCODE SCIPcreateConsNonlinear(SCIP *scip, SCIP_CONS **cons, const char *name, SCIP_EXPR *expr, 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)
sum expression handler
SCIP_RETCODE SCIPcreateConsSOC(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *coefs, SCIP_Real *offsets, SCIP_Real constant, SCIP_VAR *rhsvar, SCIP_Real rhscoeff, SCIP_Real rhsoffset, 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)
Definition: cons_soc.c:97