Skip to content

Commit

Permalink
Hash PLConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Oct 31, 2024
1 parent ad2c60f commit 2532278
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/mp/flat/constr_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ struct PLPoints {
0.0 :
(y_[i1]-y_[i0]) / (x_[i1]-x_[i0]);
}

/// operator==
bool operator==(const PLPoints& plp) const
{ return x_==plp.x_ && y_==plp.y_; }
};


Expand All @@ -433,6 +437,9 @@ class PLConParams {
plp_ = pls_;
return plp_;
}
/// operator==
bool operator==(const PLConParams& plcp) const
{ return GetPLPoints() == plcp.GetPLPoints(); }

private:
PLSlopes pls_;
Expand Down
31 changes: 30 additions & 1 deletion include/mp/flat/constr_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace std {
/// Specialize std::hash<> for various expressions and constraints.
/// Remember we also need operator== for [std::reference_wrapper<> of]
/// the compared types. Operator=='s are class memebrs, or
/// defined in ``constraint_keeper.h``.
/// defined in ``constr_keeper.h``.

/// Partially specialize std::hash<> for CustomFunctionalConstraint<>
///
Expand Down Expand Up @@ -80,6 +80,35 @@ struct hash< mp::ArrayRef<Element> >
};


/// Specialize std::hash<> for mp::PLPoints
template <>
struct hash< mp::PLPoints >
{
size_t operator()(
const mp::PLPoints& plp) const
{
mp::HashStreamer hs;
hs.Add(std::hash< std::vector<double> >{}(plp.x_));
hs.Add(std::hash< std::vector<double> >{}(plp.y_));
return hs.FinalizeHashValue();
}
};


/// Specialize std::hash<> for mp::PLConParams
template <>
struct hash< mp::PLConParams >
{
size_t operator()(
const mp::PLConParams& plp) const
{
mp::HashStreamer hs;
hs.Add(std::hash<mp::PLPoints>{}(plp.GetPLPoints()));
return hs.FinalizeHashValue();
}
};


/// Partially specialize std::hash<> for ConditionalConstraint<>
///
/// Assumes that std::hash<> is specialized
Expand Down
2 changes: 1 addition & 1 deletion include/mp/flat/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ class FlatConverter :
IndicatorConstraintQuadEQ, "acc:indquadeq")
STORE_CONSTRAINT_TYPE__NO_MAP(
IndicatorConstraintQuadGE, "acc:indquadge")
STORE_CONSTRAINT_TYPE__NO_MAP(PLConstraint,
STORE_CONSTRAINT_TYPE__WITH_MAP(PLConstraint,
"acc:pl acc:pwl acc:piecewise")
STORE_CONSTRAINT_TYPE__NO_MAP(SOS1Constraint, "acc:sos1")
STORE_CONSTRAINT_TYPE__NO_MAP(SOS2Constraint, "acc:sos2")
Expand Down

0 comments on commit 2532278

Please sign in to comment.