From 093b191dbcf54b0860ac55b9705e5d9c1c6ec05c Mon Sep 17 00:00:00 2001 From: Gleb Belov Date: Mon, 28 Oct 2024 12:25:09 +1100 Subject: [PATCH] MP2NL: reuse explicified exprs #237 --- include/mp/flat/constr_keeper.h | 8 ++++++++ include/mp/flat/nl_expr/model_api_base.h | 22 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/mp/flat/constr_keeper.h b/include/mp/flat/constr_keeper.h index ce2e32b0a..19c4a96f3 100644 --- a/include/mp/flat/constr_keeper.h +++ b/include/mp/flat/constr_keeper.h @@ -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(be).AddExpression(cons_[i].GetExpr()); } @@ -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 & @@ -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 diff --git a/include/mp/flat/nl_expr/model_api_base.h b/include/mp/flat/nl_expr/model_api_base.h index ad911c36e..be7edd6ab 100644 --- a/include/mp/flat/nl_expr/model_api_base.h +++ b/include/mp/flat/nl_expr/model_api_base.h @@ -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 } @@ -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? @@ -378,10 +382,20 @@ class BasicExprModelAPI private: std::vector is_var_proper_; - std::vector 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 is_expr_stored_; /// Expression cache std::deque 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 is_init_expr_retrieved_; + std::vector is_init_expr_stored_; + /// Expression cache + std::deque init_expr_stored_; // to keep iterators valid InitExprGetterType get_init_expr_; };