Skip to content

Commit

Permalink
MP2NL: reuse explicified exprs #237
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Oct 28, 2024
1 parent 55cd8da commit 093b191
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ class ConstraintKeeper final
if constexpr (ExpressionAcceptanceLevel::NotAccepted
!= Backend::ExpressionInterfaceAcceptanceLevel()) {
assert(!cons_[i].IsBridged());
assert(!cons_[i].IsExprAdded());
cons_[i].MarkExprAdded();
*(typename Backend::Expr*)pexpr =
static_cast<Backend&>(be).AddExpression(cons_[i].GetExpr());
}
Expand Down Expand Up @@ -319,6 +321,11 @@ class ConstraintKeeper final
is_unused_=true;
}

/// Has the expression been added to the backend?
bool IsExprAdded() const { return is_expr_stored_; }
/// Mark as added
void MarkExprAdded() { is_expr_stored_=true; }

/// Get the flat constraint, const &
const Constraint& GetCon() const { return con_.GetFlatConstraint(); }
/// Get the flat constraint &
Expand All @@ -336,6 +343,7 @@ class ConstraintKeeper final
int depth_ = 0;
bool is_bridged_ = false;
bool is_unused_ = false;
bool is_expr_stored_ = false;
};

/// Convert all new constraints of this type
Expand Down
22 changes: 18 additions & 4 deletions include/mp/flat/nl_expr/model_api_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,11 @@ class BasicExprModelAPI
assert(!is_init_expr_retrieved_[i_expr]); // not twice explicified
if (IsVarProper(i_expr)) {
is_init_expr_retrieved_[i_expr] = true; // is being explicified
Expr result;
get_init_expr_(i_expr, &result); // TODO we are not caching them.
return result;
if (!is_init_expr_stored_[i_expr]) {
is_init_expr_stored_[i_expr] = true;
get_init_expr_(i_expr, &init_expr_stored_[i_expr]);
}
return init_expr_stored_[i_expr]; // ...............
}
return GetInitExpression(i_expr); // standard case
}
Expand All @@ -350,6 +352,8 @@ class BasicExprModelAPI
is_expr_stored_.resize(is_var_proper_.size()); // allocate
expr_stored_.resize(is_var_proper_.size());
is_init_expr_retrieved_.resize(is_var_proper_.size());
is_init_expr_stored_.resize(is_var_proper_.size());
init_expr_stored_.resize(is_var_proper_.size());
}

/// Is var proper?
Expand Down Expand Up @@ -378,10 +382,20 @@ class BasicExprModelAPI

private:
std::vector<bool> is_var_proper_;
std::vector<bool> is_expr_stored_; // actual Expr's of the result var or the init expressions
/// actual Expr's of the result vars (for explicified exprds)
/// or the expressions, otherwise
std::vector<bool> is_expr_stored_;
/// Expression cache
std::deque<ExprType> expr_stored_; // to keep iterators valid

/// init exprs: for explicified expressions, whether
/// the actual init expr-s retrieved.
/// Should be cleared by ResetIniExpr...
/// if retrieving repeatedly.
std::vector<bool> is_init_expr_retrieved_;
std::vector<bool> is_init_expr_stored_;
/// Expression cache
std::deque<ExprType> init_expr_stored_; // to keep iterators valid
InitExprGetterType get_init_expr_;
};

Expand Down

0 comments on commit 093b191

Please sign in to comment.