Skip to content

Commit

Permalink
Prepare MOSEK cones #192
Browse files Browse the repository at this point in the history
Some general API extensions towards quadratic cones
  • Loading branch information
glebbelov committed Dec 8, 2022
1 parent 975366e commit 9e3fd74
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
12 changes: 12 additions & 0 deletions include/mp/flat/constr_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ template <class Num, size_t N>
using ParamArray0 = ParamArrayN<int, 0>;
/// Fixed parameter array of 1 double
using DblParamArray1 = ParamArrayN<double, 1>;
/// Variable-length parameter array
using DblParamArray = std::vector<double>;

/// A functional constraint with given arguments
/// and further info as parameters
Expand Down Expand Up @@ -261,6 +263,16 @@ class ConditionalConstraint :
#define DEF_CONDITIONAL_CONSTRAINT_WRAPPER(Name, StaticConName) \
using Name = ConditionalConstraint< StaticConName >

////////////////////////////////////////////////////////////////////////
/// STATIC CONSTRAINTS
/// Workaround: defining as functional constraint (result unused)
/// Could be solved by a mix-in parent
#define DEF_STATIC_CONSTR(Name, Args, Descr) \
DEF_LOGICAL_FUNC_CONSTR(Name, Args, Descr)
#define DEF_STATIC_CONSTR_WITH_PRM(Name, Args, Params, Descr) \
DEF_LOGICAL_FUNC_CONSTR_WITH_PRM(Name, Args, Params, Descr)


} // namespace mp

#endif // BASIC_CONSTR_H
8 changes: 8 additions & 0 deletions include/mp/flat/constr_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ using ComplementarityLinear = ComplementarityConstraint<AffineExpr>;
/// Typedef ComplementarityQuadRange
using ComplementarityQuadratic = ComplementarityConstraint<QuadraticExpr>;

/// Quadratic cone
DEF_STATIC_CONSTR_WITH_PRM( QuadraticConeConstraint, VarArray, DblParamArray,
"Quadratic cone x1 >= sqrt(x2^2 + ...)) with factors "
"applied to the squared arguments, aka MOSEK 10 affine cones");
/// Rotated quadratic cone
DEF_STATIC_CONSTR_WITH_PRM( RotatedQuadraticConeConstraint, VarArray, DblParamArray,
"Rotated quadratic cone x1*x2 >= sqrt(x3^2 + ...)) with factors "
"applied to the squared arguments, aka MOSEK 10 affine cones");

} // namespace mp

Expand Down
2 changes: 2 additions & 0 deletions include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ class ConstraintKeeper : public BasicConstraintKeeper {
if (!cons_[i].IsBridged()) {
try { // Try to convert all but allow failure
ConvertConstraint(cons_[i], i);
} catch (const ConstraintConversionGracefulFailure& ) {
/// nothing
} catch (const ConstraintConversionFailure& ccf) {
GetConverter().AddWarning( ccf.key(), ccf.message() );
}
Expand Down
16 changes: 14 additions & 2 deletions include/mp/flat/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ class FlatConverter :
MP_DISPATCH(Convert(con, i));
}

/// Query if a constraint type
/// is natively accepted by the solver.
/// The parameter is only needed for type.
template <class Con>
ConstraintAcceptanceLevel GetConstraintAcceptance(Con* ) const {
return GET_CONST_CONSTRAINT_KEEPER(Con).GetChosenAcceptanceLevel();
}

/// Query if the constraint type
/// can be converted.
/// This method should not be redefined;
Expand Down Expand Up @@ -738,10 +746,10 @@ class FlatConverter :
bool IfPreproEqBinVar() const
{ return MPCD( CanPreprocess(options_.preprocessEqualityBvar_) ); }

/// Whether we pass quad obj terms
/// Whether we pass quad obj terms to the solver without linearization
bool IfPassQuadObj() const { return options_.passQuadObj_; }

/// Whether we pass quad con terms
/// Whether we pass quad con terms to the solver without linearization
bool IfPassQuadCon() const { return options_.passQuadCon_; }

/// Whether to quadratize pow(..., const_pos_int).
Expand Down Expand Up @@ -882,6 +890,10 @@ class FlatConverter :
ComplementarityLinear, "acc:compl acc:compllin")
STORE_CONSTRAINT_TYPE__NO_MAP(
ComplementarityQuadratic, "acc:complquad")
STORE_CONSTRAINT_TYPE__NO_MAP(
QuadraticConeConstraint, "acc:quadcone")
STORE_CONSTRAINT_TYPE__NO_MAP(
RotatedQuadraticConeConstraint, "acc:rotatedquadcone")


////////////////////// Default map accessors /////////////////////////
Expand Down
38 changes: 35 additions & 3 deletions include/mp/flat/redef/MIP/mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,50 @@ class QCConverter_MIP :
/// Check whether the constraint
/// needs to be converted despite being accepted by ModelAPI.
bool IfNeedsConversion(const ItemType& , int ) {
return !GetMC().IfPassQuadCon();
return IfCvtAcceptsQPCones() ||
!GetMC().IfPassQuadCon();
}

/// Conversion
void Convert(const ItemType& qc, int ) {
LinearizeQPTerms(qc);
void Convert(const ItemType& qc, int i) {
if (IfCvtAcceptsQPCones() && // conex accepted
GetMC().IfPassQuadCon()) // passing quadratic to solver, incl. cones
TryConvertToCone(qc, i);
else
LinearizeQPTerms(qc);
}


protected:
using Base::GetMC;

bool IfCvtAcceptsQPCones() {
return
GetMC().GetConstraintAcceptance((QuadraticConeConstraint*)nullptr) ||
GetMC().GetConstraintAcceptance((RotatedQuadraticConeConstraint*)nullptr);
}

void TryConvertToCone(const ItemType& qc, int ) {
if (!TryQuadCone(qc) && !TryRotatedQuadCone(qc))
throw ConstraintConversionGracefulFailure();
}

bool TryQuadCone(const ItemType& qc) {
if (GetMC().GetConstraintAcceptance(
(QuadraticConeConstraint*)nullptr)) {
return true;
}
return false;
}

bool TryRotatedQuadCone(const ItemType& qc) {
if (GetMC().GetConstraintAcceptance(
(RotatedQuadraticConeConstraint*)nullptr)) {
return true;
}
return false;
}

void LinearizeQPTerms(const ItemType& qc) {
const auto& body = qc.GetBody();
// Copy lin terms
Expand Down
9 changes: 9 additions & 0 deletions include/mp/flat/redef/redef_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BasicItemConverter {
return false;
}

/// Access const ModelConverter
const ModelConverter& GetMC() const { return mdl_cvt_; }
/// Access ModelConverter
ModelConverter& GetMC() { return mdl_cvt_; }

Expand Down Expand Up @@ -127,6 +129,13 @@ class ConstraintConversionFailure {
};


/// Graceful constraint conversion failure - no warnings
class ConstraintConversionGracefulFailure {
public:
ConstraintConversionGracefulFailure() { }
};


} // namespace mp

#endif // REDEF_BASE_H

0 comments on commit 9e3fd74

Please sign in to comment.