Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v9-minor'
Browse files Browse the repository at this point in the history
  • Loading branch information
scip-ci committed Dec 28, 2024
2 parents 67bda84 + 30a0fa9 commit bb0ac50
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Known bugs
Features
--------

- simplify expressions of nonlinear constraints in MPS and LP writing to increase chance that they are recognized as quadratic

Performance improvements
------------------------

Expand Down
30 changes: 28 additions & 2 deletions src/scip/reader_lp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3888,25 +3888,51 @@ SCIP_RETCODE SCIPwriteLp(
else if( strcmp(conshdlrname, "nonlinear") == 0 )
{
SCIP_Bool isquadratic;
SCIP_EXPR* expr;

/* check whether there is a quadratic representation of the nonlinear constraint */
SCIP_CALL( SCIPcheckQuadraticNonlinear(scip, cons, &isquadratic) );
if( !isquadratic )
{
/* simplify expr and check again whether there is a quadratic representation */
SCIP_EXPR* exprcopy;
SCIP_Bool changed;
SCIP_Bool infeasible;

SCIP_CALL( SCIPduplicateExpr(scip, SCIPgetExprNonlinear(cons), &exprcopy, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPsimplifyExpr(scip, exprcopy, &expr, &changed, &infeasible, NULL, NULL) );
SCIP_CALL( SCIPreleaseExpr(scip, &exprcopy) );
if( changed && !infeasible )
{
SCIP_CALL( SCIPcheckExprQuadratic(scip, expr, &isquadratic) );
isquadratic &= SCIPexprAreQuadraticExprsVariables(expr);
}
}
else
{
expr = SCIPgetExprNonlinear(cons);
}

/* we cannot handle nonlinear constraint that are not quadratically representable */
if( !isquadratic )
{
SCIPwarningMessage(scip, "constraint handler <%s> cannot print constraint\n", SCIPconshdlrGetName(SCIPconsGetHdlr(cons)));
SCIPwarningMessage(scip, "nonlinear constraint <%s> not recognized as quadratic: cannot print as LP\n", SCIPconsGetName(cons));
SCIPinfoMessage(scip, file, "\\ ");
SCIP_CALL( SCIPprintCons(scip, cons, file) );
SCIPinfoMessage(scip, file, ";\n");
}
else
{
SCIP_CALL( printQuadraticCons(scip, file, consname, NULL, NULL, 0, SCIPgetExprNonlinear(cons),
SCIP_CALL( printQuadraticCons(scip, file, consname, NULL, NULL, 0, expr,
SCIPgetLhsNonlinear(cons), SCIPgetRhsNonlinear(cons), transformed) );

consExpr[nConsExpr++] = cons;
}

if( expr != SCIPgetExprNonlinear(cons) )
{
SCIP_CALL( SCIPreleaseExpr(scip, &expr) );
}
}
else if( strcmp(conshdlrname, "and") == 0 )
{
Expand Down
44 changes: 37 additions & 7 deletions src/scip/reader_mps.c
Original file line number Diff line number Diff line change
Expand Up @@ -3900,6 +3900,7 @@ SCIP_RETCODE SCIPwriteMps(
SCIP_CONS** consSOS1;
SCIP_CONS** consSOS2;
SCIP_CONS** consQuadratic;
SCIP_EXPR** exprQuadratic; /* expressions to quadratic constraints */
int nConsIndicator;
int nConsSOS1;
int nConsSOS2;
Expand Down Expand Up @@ -3966,6 +3967,7 @@ SCIP_RETCODE SCIPwriteMps(
SCIP_CALL( SCIPallocBufferArray(scip, &consSOS1, nconss) );
SCIP_CALL( SCIPallocBufferArray(scip, &consSOS2, nconss) );
SCIP_CALL( SCIPallocBufferArray(scip, &consQuadratic, nconss) );
SCIP_CALL( SCIPallocBufferArray(scip, &exprQuadratic, nconss) );
SCIP_CALL( SCIPallocBufferArray(scip, &consIndicator, nconss) );

/* nfixedvars counts all variables with status SCIP_VARSTATUS_FIXED, SCIP_VARSTATUS_AGGREGATED, SCIP_VARSTATUS_MULTAGGR, but not SCIP_VARSTATUS_NEGATED */
Expand Down Expand Up @@ -4293,20 +4295,42 @@ SCIP_RETCODE SCIPwriteMps(
int j;

/* check if it is a quadratic constraint */
SCIP_CALL( SCIPcheckQuadraticNonlinear(scip, cons, &isquadratic) );
expr = SCIPgetExprNonlinear(cons);
SCIP_CALL( SCIPcheckExprQuadratic(scip, expr, &isquadratic) );
if( !isquadratic || !SCIPexprAreQuadraticExprsVariables(expr) )
{
SCIP_EXPR* exprcopy;
SCIP_Bool changed;
SCIP_Bool infeasible;

SCIP_CALL( SCIPduplicateExpr(scip, expr, &exprcopy, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPsimplifyExpr(scip, exprcopy, &expr, &changed, &infeasible, NULL, NULL) );
SCIP_CALL( SCIPreleaseExpr(scip, &exprcopy) );
/* the corresponding releaseExpr is in the writing of the QCMATRIX sections at the end */
if( changed && !infeasible )
{
SCIP_CALL( SCIPcheckExprQuadratic(scip, expr, &isquadratic) );
isquadratic &= SCIPexprAreQuadraticExprsVariables(expr);
}
}

if( !isquadratic )
{
/* unknown constraint type; mark this with SCIPinfinity(scip) */
rhss[c] = SCIPinfinity(scip);

SCIPwarningMessage(scip, "constraint handler <%s> cannot print requested format\n", conshdlrname );
SCIPwarningMessage(scip, "nonlinear constraint <%s> not recognized as quadratic: cannot print as MPS\n", SCIPconsGetName(cons));
if( expr != SCIPgetExprNonlinear(cons) )
{
SCIP_CALL( SCIPreleaseExpr(scip, &expr) );
}
continue;
}

/* store constraint */
consQuadratic[nConsQuadratic++] = cons;

expr = SCIPgetExprNonlinear(cons);
/* store constraint and corresponding expression (may be the simplified one) */
consQuadratic[nConsQuadratic] = cons;
exprQuadratic[nConsQuadratic] = expr;
++nConsQuadratic;

/* collect linear coefficients of quadratic part */
SCIPexprGetQuadraticData(expr, &constant, &nlinexprs, &linexprs, &lincoefs, &nquadexprs, NULL, NULL,
Expand Down Expand Up @@ -4752,7 +4776,7 @@ SCIP_RETCODE SCIPwriteMps(
SCIP_EXPR* expr;

cons = consQuadratic[c];
expr = SCIPgetExprNonlinear(cons);
expr = exprQuadratic[c];

SCIPexprGetQuadraticData(expr, NULL, NULL, NULL, NULL, &nconsvars, &nbilin, NULL, NULL);

Expand Down Expand Up @@ -4828,6 +4852,11 @@ SCIP_RETCODE SCIPwriteMps(
printRecord(scip, file, varname, valuestr, maxnamelen);
SCIPinfoMessage(scip, file, "\n");
}

if( expr != SCIPgetExprNonlinear(cons) )
{
SCIP_CALL( SCIPreleaseExpr(scip, &expr) );
}
}

SCIPfreeBufferArray(scip, &namestr);
Expand Down Expand Up @@ -4906,6 +4935,7 @@ SCIP_RETCODE SCIPwriteMps(

/* free buffer arrays for SOS1, SOS2, and quadratic */
SCIPfreeBufferArray(scip, &consIndicator);
SCIPfreeBufferArray(scip, &exprQuadratic);
SCIPfreeBufferArray(scip, &consQuadratic);
SCIPfreeBufferArray(scip, &consSOS2);
SCIPfreeBufferArray(scip, &consSOS1);
Expand Down

0 comments on commit bb0ac50

Please sign in to comment.