Skip to content

Commit

Permalink
SCIP: release expressions #237
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Aug 26, 2024
1 parent 7c675b9 commit 98e97c0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/mp/flat/constr_2_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Constraints2Expr {
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel eal) {
assert(stage_cvt2expr_>0 && stage_cvt2expr_<=2);
if (ExpressionAcceptanceLevel::NotAccepted != eal) { // going into an expr
if (1==stage_cvt2expr_) {
if (1==stage_cvt2expr_) { // otherwise it's a flat con
if (!con.GetConstraint().GetBody().is_variable()) { // already a variable
ConvertConditionalConLHS(con, i);
return true;
Expand Down
10 changes: 10 additions & 0 deletions include/mp/flat/nl_expr/model_api_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,21 @@ class BasicExprModelAPI
double GetConstTerm(const QuadExpression& qe) const
{ return qe.GetFlatConstraint().GetQuadExpr().constant_term(); }

/// Get number of arguments
template <class FlatExpression>
int GetNumArguments(const FlatExpression& fe)
{ return GetInitExpression(fe.GetFlatConstraint().GetArguments().size()); }

/// Get argument expression [\a i]
template <class FlatExpression>
Expr GetArgExpression(const FlatExpression& fe, int i)
{ return GetInitExpression(fe.GetFlatConstraint().GetArguments().at(i)); }

/// Get number of parameters
template <class FlatExpression>
int GetNumParameters(const FlatExpression& fe)
{ return fe.GetFlatConstraint().GetParameters().size(); }

/// Get expression parameter [\a i]
template <class FlatExpression>
double GetParameter(const FlatExpression& fe, int i)
Expand Down
5 changes: 4 additions & 1 deletion solvers/scipmp/scipmpcommon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ SCIP_DECL_PROBDELORIG(probdataDelOrigNl)
SCIP_CCALL( SCIPreleaseVar(scip, &(*probdata)->vars[i]) );
}

for (auto& e: (*probdata)->exprs)
SCIP_CCALL( SCIPreleaseExpr(scip, &e) );

if ((*probdata)->dummyexpr)
SCIP_CCALL( SCIPreleaseExpr(scip, &(*probdata)->dummyexpr) );

SCIPfreeBlockMemoryArray(scip, &(*probdata)->vars, (*probdata)->nvars);

SCIPsetMessagehdlrQuiet(scip, true); // Spurious warnings and failure in Debug build

// https://github.com/scipopt/scip/issues/71
SCIPfreeMemory(scip, probdata);

return SCIP_OKAY;
Expand Down
8 changes: 5 additions & 3 deletions solvers/scipmp/scipmpcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ struct SCIP_ProbData
int i = 0; /**< shows free slot of linear constraints */
int nlinconss = 0; /**< number of linear constraints */

std::vector<SCIP_CONS*> nlconss; /**< NL linear constraints in the order given by AMPL */
std::vector<SCIP_CONS*> nlconss; /**< NL linear constraints in the order given by AMPL */

std::vector<SCIP_EXPR*> exprs; /**< expressions*/

SCIP_EXPR* dummyexpr {nullptr};
};
Expand Down Expand Up @@ -80,9 +82,9 @@ class ScipCommon :
// solver API does not return a valid errorcode. In this mock driver, we define it
// ourselves, normally this constant would be defined in the solver's API.
#define SCIP_RETCODE_OK 1
#define SCIP_CCALL( call ) do { if (int e = (call) != SCIP_RETCODE_OK) \
#define SCIP_CCALL( scip__call_ ) do { if (int err__code_ = (scip__call_) != SCIP_RETCODE_OK) \
throw std::runtime_error( \
fmt::format(" Call failed: '{}' with code {}", #call, e )); } while (0)
fmt::format(" Call failed: '{}' with code {}", #scip__call_, err__code_ )); } while (0)

} // namespace mp

Expand Down
8 changes: 8 additions & 0 deletions solvers/scipmp/scipmpmodelapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const AbsExpression &abse) {
SCIP_EXPR* result_expr;
SCIP_CCALL( SCIPcreateExprAbs(getSCIP(), &result_expr,
GetArgExpression(abse, 0), NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down Expand Up @@ -361,6 +362,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const LinExpression &le) {
SCIP_CCALL( SCIPcreateExprSum(getSCIP(), &sumexpr,
terms.size(), terms.data(), coefs.data(),
GetConstTerm(le), NULL, NULL) );
getPROBDATA()->exprs.push_back(sumexpr);
return sumexpr;
}

Expand Down Expand Up @@ -389,6 +391,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const QuadExpression &qe) {
int i_quad = i + GetLinSize(qe);
SCIP_CCALL( SCIPreleaseExpr(getSCIP(), &terms[i_quad]) );
}
getPROBDATA()->exprs.push_back(sumexpr);
return sumexpr;
}

Expand Down Expand Up @@ -460,6 +463,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const ExpExpression &ee) {
SCIP_EXPR* result_expr;
SCIP_CCALL( SCIPcreateExprExp(getSCIP(), &result_expr,
GetArgExpression(ee, 0), NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down Expand Up @@ -495,6 +499,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const LogExpression &ee) {
SCIP_EXPR* result_expr;
SCIP_CCALL( SCIPcreateExprLog(getSCIP(), &result_expr,
GetArgExpression(ee, 0), NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down Expand Up @@ -532,6 +537,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const PowExpression &ee) {
GetArgExpression(ee, 0),
GetParameter(ee, 0),
NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down Expand Up @@ -567,6 +573,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const SinExpression &ee) {
SCIP_EXPR* result_expr;
SCIP_CCALL( SCIPcreateExprSin(getSCIP(), &result_expr,
GetArgExpression(ee, 0), NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down Expand Up @@ -602,6 +609,7 @@ SCIP_EXPR* ScipModelAPI::AddExpression(const CosExpression &ee) {
SCIP_EXPR* result_expr;
SCIP_CCALL( SCIPcreateExprCos(getSCIP(), &result_expr,
GetArgExpression(ee, 0), NULL, NULL) );
getPROBDATA()->exprs.push_back(result_expr);
return result_expr;
}

Expand Down

0 comments on commit 98e97c0

Please sign in to comment.