Skip to content

Commit

Permalink
MP2NL: Div expression, QP terms as expression #237
Browse files Browse the repository at this point in the history
Don't linearize quadratics with acc:_expr=1 because we assume any nonlinear solver to accept quadratics; put objective's QP terms into the expression
  • Loading branch information
glebbelov committed Oct 30, 2024
1 parent 0b32144 commit b54b78e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/mp/flat/redef/MIP/mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class QCConverter_MIP :
return !GetMC().IfPassQuadCon();
}

/// Skip conversion?
bool IfDelayConversion(const ItemType& , int ) {
return
GetMC().IfPassQuadCon()
|| GetMC().IfWantNLOutput(); // Assume QuadExpr accepted
}

/// Conversion
void Convert(const ItemType& qc, int ) {
LinearizeQPTerms(qc);
Expand Down
5 changes: 5 additions & 0 deletions solvers/mp2nl/mp2nlmodelapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ MP2NL_Expr MP2NLModelAPI::AddExpression(const SinExpression &expr)
MP2NL_Expr MP2NLModelAPI::AddExpression(const CosExpression &expr)
{ return AddExpression(expr, ExpressionTypeID::ID_Cos); }

MP2NL_Expr MP2NLModelAPI::AddExpression(const DivExpression &expr)
{ return AddExpression(expr, ExpressionTypeID::ID_Div); }


void MP2NLModelAPI::FinishProblemModificationPhase() { }

Expand Down Expand Up @@ -752,6 +755,8 @@ void MP2NLModelAPI::FeedOpcode(Expr expr, ExprWriter& ew) {
HANDLE_OPCODE_CASE_1_ARG(Sin, SIN, FdArgs)
HANDLE_OPCODE_CASE_1_ARG(Cos, COS, FdArgs)

HANDLE_OPCODE_CASE_2_ARG(Div, DIV, FdArgs)

default:
MP_RAISE("MP2NL: unknown expression type");
}
Expand Down
13 changes: 10 additions & 3 deletions solvers/mp2nl/mp2nlmodelapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ class MP2NLModelAPI
void AddVariables(const VarArrayDef& vad);
void SetLinearObjective( int iobj, const LinearObjective& lo );
/// Whether accepting quadratic objectives:
/// 0 - no, 1 - convex, 2 - nonconvex
static int AcceptsQuadObj() { return 2; }
/// 0 - no, 1 - convex, 2 - nonconvex.
/// Return 0 because the QP terms go into expression
static int AcceptsQuadObj() { return 0; }
void SetQuadraticObjective(int iobj, const QuadraticObjective& qo);
/// Whether accepts NLObjective.
static int AcceptsNLObj() { return 1; }
Expand Down Expand Up @@ -414,6 +415,9 @@ class MP2NLModelAPI
ACCEPT_EXPRESSION(CosExpression, Recommended)
Expr AddExpression(const CosExpression& );

ACCEPT_EXPRESSION(DivExpression, Recommended)
Expr AddExpression(const DivExpression& );

// TODO Div; PowVarVar;


Expand Down Expand Up @@ -1110,7 +1114,9 @@ class MP2NLModelAPI
ID_Log,
ID_Pow,
ID_Sin,
ID_Cos
ID_Cos,

ID_Div
};


Expand Down Expand Up @@ -1287,6 +1293,7 @@ class MP2NLModelAPI
CREATE_EXPRESSION_DISPATCHER(Sin)
CREATE_EXPRESSION_DISPATCHER(Cos)

CREATE_EXPRESSION_DISPATCHER(Div)

/// Constraint/objective/expression info.
/// We rely on the pointers staying valid.
Expand Down

0 comments on commit b54b78e

Please sign in to comment.