From d5b26c0d2d5912a85c0a62f3a075ef6d6f9f15a6 Mon Sep 17 00:00:00 2001 From: Gleb Belov Date: Fri, 3 May 2024 22:25:36 +1000 Subject: [PATCH] Marking: prepare for new expressions #237 --- include/mp/flat/constr_2_expr.h | 2 ++ include/mp/flat/converter_model.h | 11 +++++++---- include/mp/utils-vec.h | 12 ++++++++++++ solvers/scipmp/scipmpmodelapi.h | 3 ++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/mp/flat/constr_2_expr.h b/include/mp/flat/constr_2_expr.h index d587b8485..a9536795d 100644 --- a/include/mp/flat/constr_2_expr.h +++ b/include/mp/flat/constr_2_expr.h @@ -61,6 +61,8 @@ class Constraints2Expr { protected: /// Algebraic cons: no marking (when NLConstraint accepted?) + /// @todo Do we need to consider NLConstraint / NLObjective at this step? + /// Are they added during marking? template void DoMarkArgsAsVars( // needs to appear before the most generic template const AlgebraicConstraint& , int ) { } diff --git a/include/mp/flat/converter_model.h b/include/mp/flat/converter_model.h index 3ba5da29d..804bbece8 100644 --- a/include/mp/flat/converter_model.h +++ b/include/mp/flat/converter_model.h @@ -7,6 +7,8 @@ #include #include +#include "mp/utils-vec.h" + #include "mp/flat/obj_std.h" #include "mp/flat/constr_std.h" #include "mp/flat/constr_keeper.h" @@ -235,18 +237,19 @@ class FlatModel } /// Mark as an explicit result variable + /// @todo AutoExpand fills 'false' for new elements... void MarkAsResultVar(int v) { - var_result_.at(v) = true; + AutoExpand(var_result_, v) = true; } /// Mark as a proper expression void MarkAsExpression(int v) { - var_result_.at(v) = false; + AutoExpand(var_result_, v) = false; } /// Is the variable an explicit result var? - bool IsResultVar(int v) const { - return var_result_.at(v); + bool IsResultVar(int v) { + return AutoExpand(var_result_, v); } ///////////////////////////// OBJECTIVES //////////////////////////// diff --git a/include/mp/utils-vec.h b/include/mp/utils-vec.h index 05e7f6e96..7db6e875f 100644 --- a/include/mp/utils-vec.h +++ b/include/mp/utils-vec.h @@ -4,6 +4,18 @@ namespace mp { +/// Grow vector capacity by a factor if needed. +/// @return reference to the element at \a i. +template +auto AutoExpand(Vec& vec, typename Vec::size_type i) { + if (vec.size()<=i) { + if (vec.capacity()<=i) + vec.reserve(((i+1)*13)/10); + vec.resize(i+1); + } + return vec[i]; +} + } // namespace mp diff --git a/solvers/scipmp/scipmpmodelapi.h b/solvers/scipmp/scipmpmodelapi.h index 6f5144428..53c94e9af 100644 --- a/solvers/scipmp/scipmpmodelapi.h +++ b/solvers/scipmp/scipmpmodelapi.h @@ -66,8 +66,9 @@ class ScipModelAPI : /// For each expression, /// say ACCEPT_EXPRESSION(Recommended) /// or ACCEPT_EXPRESSION(AcceptedButNotRecommended). + /// This can be user-configured via options 'acc:exp' etc. ACCEPT_EXPRESSION(ExpExpression, Recommended) - void AddExpression(const ExpExpression& ); + SCIP_EXPR* AddExpression(const ExpExpression& ); /// The linear range constraint, if fully supported with basis info etc. ACCEPT_CONSTRAINT(LinConRange, Recommended, CG_Linear)