Skip to content

Commit

Permalink
Explicify only accepted expressions #237
Browse files Browse the repository at this point in the history
Others are flat constraints
  • Loading branch information
glebbelov committed Aug 26, 2024
1 parent c5a6e87 commit 7c675b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
55 changes: 34 additions & 21 deletions include/mp/flat/constr_2_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ class Constraints2Expr {
bool ConvertWithExpressions(
const AlgebraicConstraint<Body, RhsOrRange>& con,
int i,
ConstraintAcceptanceLevel cal) {
ConstraintAcceptanceLevel cal, ExpressionAcceptanceLevel ) {
assert(stage_cvt2expr_>0);
/// Replace \a con by a NLConstraint,
/// if either the ModelAPI does not accept it,
/// or the linear/quadratic terms have expressions
/// (and then they are non-flat.)
if (1==stage_cvt2expr_
&& (ConstraintAcceptanceLevel::Recommended != cal
&& (ConstraintAcceptanceLevel::Recommended != cal
|| HasExpressionArgs(con.GetBody()))) {
ConvertToNLCon(con, i);
return true; // to remove the original \a con
Expand All @@ -112,16 +112,18 @@ class Constraints2Expr {
bool ConvertWithExpressions(
const ConditionalConstraint< AlgebraicConstraint<Body, RhsOrRange> >& con,
int i,
ConstraintAcceptanceLevel ) {
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel eal) {
assert(stage_cvt2expr_>0 && stage_cvt2expr_<=2);
if (1==stage_cvt2expr_) {
if (!con.GetConstraint().GetBody().is_variable()) { // already a variable
ConvertConditionalConLHS(con, i);
return true;
if (ExpressionAcceptanceLevel::NotAccepted != eal) { // going into an expr
if (1==stage_cvt2expr_) {
if (!con.GetConstraint().GetBody().is_variable()) { // already a variable
ConvertConditionalConLHS(con, i);
return true;
}
}
else // if (2==stage_cvt2expr_)
ConsiderExplicifyingExpression(con, i); // this is a func con too
}
else // if (2==stage_cvt2expr_)
ConsiderExplicifyingExpression(con, i); // this is a func con too
return false;
}

Expand All @@ -132,9 +134,12 @@ class Constraints2Expr {
std::enable_if_t<
std::is_base_of_v<FunctionalConstraint, FuncCon>, bool > = true >
bool ConvertWithExpressions(
const FuncCon& con, int i, ConstraintAcceptanceLevel ) {
if (2==stage_cvt2expr_)
ConsiderExplicifyingExpression(con, i);
const FuncCon& con, int i,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel eal) {
if (ExpressionAcceptanceLevel::NotAccepted != eal) { // going into an expr
if (2==stage_cvt2expr_)
ConsiderExplicifyingExpression(con, i);
}
return false; // leave it active
}

Expand All @@ -145,7 +150,7 @@ class Constraints2Expr {
bool ConvertWithExpressions(
const ComplementarityConstraint<Expr>& con,
int i,
ConstraintAcceptanceLevel ) {
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) { // TODO check acc for NLCompl
if (1==stage_cvt2expr_
&& !con.GetExpression().is_variable()) { // already a variable
ConvertComplementarityExpr(con, i);
Expand All @@ -158,14 +163,16 @@ class Constraints2Expr {
/// But check that they are flat?
template <class SubCon>
bool ConvertWithExpressions(
const IndicatorConstraint<SubCon>& , int , ConstraintAcceptanceLevel ) {
const IndicatorConstraint<SubCon>& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}

/// SOS1: do nothing.
/// But check that they are flat?
bool ConvertWithExpressions(
const SOS1Constraint& con, int , ConstraintAcceptanceLevel ) {
const SOS1Constraint& con, int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
if (2==stage_cvt2expr_)
for (int v: con.GetArguments()) {
assert(MPCD( IsProperVar(v) ));
Expand All @@ -176,7 +183,8 @@ class Constraints2Expr {
/// SOS1: do nothing.
/// But check that they are flat?
bool ConvertWithExpressions(
const SOS2Constraint& con, int , ConstraintAcceptanceLevel ) {
const SOS2Constraint& con, int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
if (2==stage_cvt2expr_)
for (int v: con.GetArguments()) {
assert(MPCD( IsProperVar(v) ));
Expand All @@ -186,27 +194,32 @@ class Constraints2Expr {

/// NLConstraint: just produced.
bool ConvertWithExpressions(
const NLConstraint& , int , ConstraintAcceptanceLevel ) {
const NLConstraint& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}
/// NLLogical: just produced.
bool ConvertWithExpressions(
const NLLogical& , int , ConstraintAcceptanceLevel ) {
const NLLogical& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}
/// NLEquivalence: just produced.
bool ConvertWithExpressions(
const NLEquivalence& , int , ConstraintAcceptanceLevel ) {
const NLEquivalence& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}
/// NLImpl: just produced.
bool ConvertWithExpressions(
const NLImpl& , int , ConstraintAcceptanceLevel ) {
const NLImpl& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}
/// NLRimpl: just produced.
bool ConvertWithExpressions(
const NLRimpl& , int , ConstraintAcceptanceLevel ) {
const NLRimpl& , int ,
ConstraintAcceptanceLevel , ExpressionAcceptanceLevel ) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ class ConstraintKeeper final

void DoCvtWithExprs() {
auto cal = GetChosenAcceptanceLevel();
auto eal = GetChosenAcceptanceLevelEXPR();
ForEachActive(
[this, cal](const auto& con, int i) {
return this->GetConverter().ConvertWithExpressions(con, i, cal);
[this, cal, eal](const auto& con, int i) {
return this->GetConverter().ConvertWithExpressions(con, i, cal, eal);
});
}

Expand Down

0 comments on commit 7c675b9

Please sign in to comment.