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   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"
28 #include "scip/pub_misc_nonlinear.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  */
SCIPconsNonlinearGetRhs(SCIP * scip,SCIP_CONS * cons,SCIP_Bool * success)36 SCIP_Real SCIPconsNonlinearGetRhs(
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  */
SCIPconsNonlinearGetLhs(SCIP * scip,SCIP_CONS * cons,SCIP_Bool * success)82 SCIP_Real SCIPconsNonlinearGetLhs(
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. */
SCIPconsNonlinearAddLinearCoef(SCIP * scip,SCIP_CONS * cons,SCIP_VAR * var,SCIP_Real val)125 SCIP_RETCODE SCIPconsNonlinearAddLinearCoef(
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 }
164