Skip to content

Commit

Permalink
ACCEPT_EXPRESSION() #237
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Apr 25, 2024
1 parent 3819651 commit 74c394f
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 126 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,14 @@ add_prefix(MP_FLAT_REDEF_MIP_HEADERS include/mp/flat/redef/MIP/
piecewise_linear.h
power_const.h sos2.h)

add_prefix(MP_EXPR_HEADERS include/mp/expr/
add_prefix(MP_FLAT_EXPR_HEADERS include/mp/flat/expr/
model_api_base.h)


set(MP_ALL_HEADERS ${MP_HEADERS} ${MP_FLAT_HEADERS}
${MP_FLAT_REDEF_HEADERS}
${MP_FLAT_REDEF_STD_HEADERS} ${MP_FLAT_REDEF_MIP_HEADERS}
${MP_EXPR_HEADERS}
${MP_FLAT_EXPR_HEADERS}
)


Expand Down
4 changes: 2 additions & 2 deletions include/mp/flat/constr_algebraic.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class AlgebraicConstraint :
public BasicConstraint, public Body, public RhsOrRange {
public:
/// Constraint type name
static const std::string& GetTypeName() {
static const char* GetTypeName() {
static std::string name {
std::string("AlgebraicConstraint< ") +
Body::GetTypeName() + ", " +
RhsOrRange::GetTypeName() + " >" };
return name;
return name.c_str();
}

/// Is logical?
Expand Down
30 changes: 25 additions & 5 deletions include/mp/flat/constr_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "mp/common.h"

#include "mp/flat/context.h"
#include "mp/arrayref.h"

namespace mp {

Expand Down Expand Up @@ -70,6 +69,25 @@ class BasicConstraint {
};


/// Wrap (functional) constraint to represent it as an expression.
///
/// (Solver)ModelAPI should only inspect it via the
/// BasicExprModelAPI<>::GetExpr... methods.
template <class Con>
class ExprWrapper {
Con con_flat_;
public:
/// Construct
ExprWrapper(Con c) : con_flat_(std::move(c)) { }
/// Constraint type
using FlatConType = Con;
/// Get const & (con)
const Con& GetFlatConstraint() const { return con_flat_; }
/// Get & (con)
Con& GetFlatConstraint() { return con_flat_; }
};


/// A special constraint 'var=...', which defines a result variable
class FunctionalConstraint : public BasicConstraint {
int result_var_=-1; // defined var is optional
Expand Down Expand Up @@ -439,14 +457,15 @@ inline void WriteJSON(JW jw,


////////////////////////////////////////////////////////////////////////
/// Args is the argument type, e.g., array of variables, or an expression
/// Params is the parameter type, e.g., array of numbers. Can be empty
/// Args is the argument type, e.g., array of variables, or an expression.
/// Params is the parameter type, e.g., array of numbers. Can be empty.
#define DEF_CUSTOM_FUNC_CONSTR_WITH_PRM(Name, Args, Params, NumLogic, Descr) \
struct Name ## Id { \
static constexpr const char* description() { return Descr; } \
static constexpr const char* GetTypeName() { return #Name; } \
}; \
using Name = CustomFunctionalConstraint<Args, Params, NumLogic, Name ## Id>
using Name ## Constraint = CustomFunctionalConstraint<Args, Params, NumLogic, Name ## Id>; \
using Name ## Expression = ExprWrapper< Name ## Constraint >

/// Custom numeric constraint without fixed parameters
#define DEF_NUMERIC_FUNC_CONSTR(Name, Args, Descr) \
Expand All @@ -466,7 +485,8 @@ inline void WriteJSON(JW jw,

/// A wrapper on a static constraint making it conditional
#define DEF_CONDITIONAL_CONSTRAINT_WRAPPER(Name, StaticConName) \
using Name = ConditionalConstraint< StaticConName >
using Name ## Constraint = ConditionalConstraint< StaticConName >; \
using Name ## Expression = ExprWrapper< Name ## Constraint >

////////////////////////////////////////////////////////////////////////
/// STATIC CONSTRAINTS
Expand Down
114 changes: 61 additions & 53 deletions include/mp/flat/constr_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,169 +13,169 @@ namespace mp {


////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( MaxConstraint, VarArray,
DEF_NUMERIC_FUNC_CONSTR( Max, VarArray,
"r = max(v1, v2, ..., vn)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( MinConstraint, VarArray,
DEF_NUMERIC_FUNC_CONSTR( Min, VarArray,
"r = min(v1, v2, ..., vn)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AbsConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Abs, VarArray1,
"r = abs(v)");

////////////////////////////////////////////////////////////////////////
DEF_LOGICAL_FUNC_CONSTR( AndConstraint, VarArray,
DEF_LOGICAL_FUNC_CONSTR( And, VarArray,
"r = forall({vi})");

////////////////////////////////////////////////////////////////////////
DEF_LOGICAL_FUNC_CONSTR( OrConstraint, VarArray,
DEF_LOGICAL_FUNC_CONSTR( Or, VarArray,
"r = exists({vi})");


////////////////////////////////////////////////////////////////////////
DEF_LOGICAL_FUNC_CONSTR( NotConstraint, VarArray1,
DEF_LOGICAL_FUNC_CONSTR( Not, VarArray1,
"r = !v");

////////////////////////////////////////////////////////////////////////
/// \brief DivConstraint
DEF_NUMERIC_FUNC_CONSTR( DivConstraint, VarArray2,
/// \brief DivConstraint and DivExpression
DEF_NUMERIC_FUNC_CONSTR( Div, VarArray2,
"r = v1 / v2 and v2!=0");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( IfThenConstraint, VarArrayN<3>,
DEF_NUMERIC_FUNC_CONSTR( IfThen, VarArrayN<3>,
"Expr-valued: if (cond) then (expr1) else (expr2)");

////////////////////////////////////////////////////////////////////////
DEF_LOGICAL_FUNC_CONSTR( ImplicationConstraint, VarArrayN<3>,
DEF_LOGICAL_FUNC_CONSTR( Implication, VarArrayN<3>,
"Logic-valued: if (cond) then (con1) else (con2)");

////////////////////////////////////////////////////////////////////////
DEF_LOGICAL_FUNC_CONSTR( AllDiffConstraint, VarArray,
DEF_LOGICAL_FUNC_CONSTR( AllDiff, VarArray,
"alldiff({})");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( NumberofConstConstraint,
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( NumberofConst,
VarArray, DblParamArray1,
"numberof_const(k, {x0...xn})");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( NumberofVarConstraint, VarArray,
DEF_NUMERIC_FUNC_CONSTR( NumberofVar, VarArray,
"numberof_var(x0, {x1...xn})");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( CountConstraint, VarArray,
DEF_NUMERIC_FUNC_CONSTR( Count, VarArray,
"count({x0...xn})");


//////////////////// NONLINEAR FUNCTIONS //////////////////////
////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( ExpConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Exp, VarArray1,
"r = exp(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( ExpAConstraint,
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( ExpA,
VarArray1, DblParamArray1, "r = a**v");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( LogConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Log, VarArray1,
"r = log(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( LogAConstraint,
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( LogA,
VarArray1, DblParamArray1, "r = log(v)/log(a)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( PowConstraint,
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( Pow,
VarArray1, DblParamArray1, "r = v ** a");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( SinConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Sin, VarArray1,
"r = sin(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( CosConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Cos, VarArray1,
"r = cos(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( TanConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Tan, VarArray1,
"r = tan(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AsinConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Asin, VarArray1,
"r = asin(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AcosConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Acos, VarArray1,
"r = acos(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AtanConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Atan, VarArray1,
"r = atan(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( SinhConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Sinh, VarArray1,
"r = sinh(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( CoshConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Cosh, VarArray1,
"r = cosh(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( TanhConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Tanh, VarArray1,
"r = tanh(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AsinhConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Asinh, VarArray1,
"r = asinh(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AcoshConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Acosh, VarArray1,
"r = acosh(v)");

////////////////////////////////////////////////////////////////////////
DEF_NUMERIC_FUNC_CONSTR( AtanhConstraint, VarArray1,
DEF_NUMERIC_FUNC_CONSTR( Atanh, VarArray1,
"r = atanh(v)");


/// Not using: var1 != var2.
/// Represented by Not { Eq0Constraint... }
////////////////////////////////////////////////////////////////////////
// DEF_LOGICAL_FUNC_CONSTR( NEConstraint__unused, VarArray2,
// DEF_LOGICAL_FUNC_CONSTR( NE__unused, VarArray2,
// "r = (v1 != v2)");


////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinConLT, LinConLT);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinLT, LinConLT);
using CondLinConLT = CondLinLTConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinConLE, LinConLE);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinLE, LinConLE);
using CondLinConLE = CondLinLEConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinConEQ, LinConEQ);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinEQ, LinConEQ);
using CondLinConEQ = CondLinEQConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinConGE, LinConGE);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinGE, LinConGE);
using CondLinConGE = CondLinGEConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinConGT, LinConGT);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondLinGT, LinConGT);
using CondLinConGT = CondLinGTConstraint;

////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadConLT, QuadConLT);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadLT, QuadConLT);
using CondQuadConLT = CondQuadLTConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadConLE, QuadConLE);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadLE, QuadConLE);
using CondQuadConLE = CondQuadLEConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadConEQ, QuadConEQ);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadEQ, QuadConEQ);
using CondQuadConEQ = CondQuadEQConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadConGE, QuadConGE);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadGE, QuadConGE);
using CondQuadConGE = CondQuadGEConstraint;
////////////////////////////////////////////////////////////////////////
DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadConGT, QuadConGT);

DEF_CONDITIONAL_CONSTRAINT_WRAPPER(CondQuadGT, QuadConGT);
using CondQuadConGT = CondQuadGTConstraint;


////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -215,6 +215,10 @@ class LinearFunctionalConstraint :
}
};


/// Typedef LinExpression
using LinExpression = ExprWrapper<LinearFunctionalConstraint>;

/// Write LFC without name.
template <class Writer>
inline void WriteModelItem(Writer& wrt,
Expand Down Expand Up @@ -283,6 +287,10 @@ class QuadraticFunctionalConstraint :
}
};


/// Typedef LinExpression
using QuadExpression = ExprWrapper<QuadraticFunctionalConstraint>;

/// Write LFC without name.
template <class Writer>
inline void WriteModelItem(Writer& wrt,
Expand Down Expand Up @@ -405,7 +413,7 @@ class PLConParams {


/// Define PLConstraint
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( PLConstraint,
DEF_NUMERIC_FUNC_CONSTR_WITH_PRM( PL,
VarArray1, PLConParams, "r = piecewise_linear(x)");

/// Write flat expr/obj/con parameters:
Expand Down
Loading

0 comments on commit 74c394f

Please sign in to comment.