Scippy

SCIP

Solving Constraint Integer Programs

misc_nonlinear.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-2020 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 misc_linear.c
17  * @brief miscellaneous methods for linear constraints
18  * @author Stephen J. Maher
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 
26 #include "scip/def.h"
27 #include "scip/scip.h"
29 #include "scip/scipdefplugins.h"
30 
31 
32 /** returns the right-hand side of an arbitrary SCIP constraint that can be represented as a single nonlinear constraint
33  *
34  * @note The success pointer indicates if the individual contraint handler was able to return the involved values
35  */
37  SCIP* scip, /**< SCIP data structure */
38  SCIP_CONS* cons, /**< constraint for which right-hand side is queried */
39  SCIP_Bool* success /**< pointer to store whether a valid right-hand side was returned */
40  )
41 {
42  SCIP_CONSHDLR* conshdlr;
43  const char* conshdlrname;
44  SCIP_Real rhs;
45 
46  assert(scip != NULL);
47  assert(cons != NULL);
48  assert(success != NULL);
49 
50  conshdlr = SCIPconsGetHdlr(cons);
51  assert(conshdlr != NULL);
52  conshdlrname = SCIPconshdlrGetName(conshdlr);
53 
54  *success = TRUE;
55  rhs = SCIP_INVALID;
56 
57  if( strcmp(conshdlrname, "nonlinear") == 0 )
58  {
59  rhs = SCIPgetRhsNonlinear(scip, cons);
60  }
61  else if( strcmp(conshdlrname, "quadratic") == 0 )
62  {
63  rhs = SCIPgetRhsQuadratic(scip, cons);
64  }
65  else if( strcmp(conshdlrname, "abspower") == 0 )
66  {
67  rhs = SCIPgetRhsAbspower(scip, cons);
68  }
69  else
70  {
71  SCIPwarningMessage(scip, "Cannot return rhs for constraint of type <%s>\n", conshdlrname);
72  *success = FALSE;
73  }
74 
75  return rhs;
76 }
77 
78 /** returns the left-hand side of an arbitrary SCIP constraint that can be represented as a single nonlinear constraint
79  *
80  * @note The success pointer indicates if the individual contraint handler was able to return the involved values
81  */
83  SCIP* scip, /**< SCIP data structure */
84  SCIP_CONS* cons, /**< constraint to get left-hand side for */
85  SCIP_Bool* success /**< pointer to store whether a valid left-hand side was returned */
86  )
87 {
88  SCIP_CONSHDLR* conshdlr;
89  const char* conshdlrname;
90  SCIP_Real lhs;
91 
92  assert(scip != NULL);
93  assert(cons != NULL);
94  assert(success != NULL);
95 
96  conshdlr = SCIPconsGetHdlr(cons);
97  assert(conshdlr != NULL);
98  conshdlrname = SCIPconshdlrGetName(conshdlr);
99 
100  *success = TRUE;
101  lhs = SCIP_INVALID;
102 
103  if( strcmp(conshdlrname, "nonlinear") == 0 )
104  {
105  lhs = SCIPgetLhsNonlinear(scip, cons);
106  }
107  else if( strcmp(conshdlrname, "quadratic") == 0 )
108  {
109  lhs = SCIPgetLhsQuadratic(scip, cons);
110  }
111  else if( strcmp(conshdlrname, "abspower") == 0 )
112  {
113  lhs = SCIPgetLhsAbspower(scip, cons);
114  }
115  else
116  {
117  SCIPwarningMessage(scip, "Cannot return lhs for constraint of type <%s>\n", conshdlrname);
118  *success = FALSE;
119  }
120 
121  return lhs;
122 }
123 
124 /** adds the given variable to the input constraint. */
126  SCIP* scip, /**< SCIP data structure */
127  SCIP_CONS* cons, /**< constraint for which row is queried */
128  SCIP_VAR* var, /**< variable of the constraint entry */
129  SCIP_Real val /**< the coefficient of the constraint entry */
130  )
131 {
132  SCIP_CONSHDLR* conshdlr;
133  const char* conshdlrname;
134 
135  assert(scip != NULL);
136  assert(cons != NULL);
137  assert(var != NULL);
138 
139  conshdlr = SCIPconsGetHdlr(cons);
140  assert(conshdlr != NULL);
141  conshdlrname = SCIPconshdlrGetName(conshdlr);
142 
143  if( strcmp(conshdlrname, "nonlinear") == 0 )
144  {
145  SCIP_CALL( SCIPaddLinearVarNonlinear(scip, cons, var, val) );
146  }
147  else if( strcmp(conshdlrname, "quadratic") == 0 )
148  {
149  SCIP_CALL( SCIPaddLinearVarQuadratic(scip, cons, var, val) );
150  }
151  else if( strcmp(conshdlrname, "abspower") == 0 )
152  {
153  SCIPerrorMessage("Sorry, can't add coefficient for constraint of type <%s>\n", conshdlrname);
154  return SCIP_ERROR;
155  }
156  else
157  {
158  SCIPerrorMessage("Sorry, can't add coefficient for constraint of type <%s>\n", conshdlrname);
159  return SCIP_ERROR;
160  }
161 
162  return SCIP_OKAY;
163 }
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:123
SCIP_Real SCIPgetRhsAbspower(SCIP *scip, SCIP_CONS *cons)
#define FALSE
Definition: def.h:73
SCIP_Real SCIPgetLhsQuadratic(SCIP *scip, SCIP_CONS *cons)
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_Real SCIPgetRhsNonlinear(SCIP *scip, SCIP_CONS *cons)
SCIP_Real SCIPconsNonlinearGetLhs(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *success)
SCIP_Real SCIPconsNonlinearGetRhs(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *success)
#define SCIPerrorMessage
Definition: pub_message.h:55
#define NULL
Definition: lpi_spx1.cpp:155
#define SCIP_CALL(x)
Definition: def.h:364
#define SCIP_Bool
Definition: def.h:70
SCIP_Real SCIPgetLhsNonlinear(SCIP *scip, SCIP_CONS *cons)
internal miscellaneous methods for nonlinear constraints
SCIP_RETCODE SCIPaddLinearVarQuadratic(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
#define SCIP_Real
Definition: def.h:163
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:8097
SCIP_Real SCIPgetRhsQuadratic(SCIP *scip, SCIP_CONS *cons)
#define SCIP_INVALID
Definition: def.h:183
SCIP_RETCODE SCIPconsNonlinearAddLinearCoef(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4167
SCIP_Real SCIPgetLhsAbspower(SCIP *scip, SCIP_CONS *cons)
common defines and data types used in all packages of SCIP
default SCIP plugins
SCIP_RETCODE SCIPaddLinearVarNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
SCIP callable library.