Skip to content

Commit

Permalink
Prepare names presolve #209
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Jul 20, 2023
1 parent d71fce0 commit db10b80
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
10 changes: 10 additions & 0 deletions include/mp/flat/redef/std/range_con.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ class RangeCon2Slack :
/// Should not need
}

/// Presolve names
void PresolveNamesEntry(const typename Base::LinkEntry& be) {
SetStr(be, VAR_SLK, GetStr(be, CON_SRC) + "_slk_");
SetStr(be, CON_TARGET, GetStr(be, CON_SRC) + "_equ_");
}
/// Postsolve names: none
void PostsolveNamesEntry(const typename Base::LinkEntry& be) { }


protected:
/// Reverse low/upp basis statuses
Expand All @@ -169,6 +177,8 @@ class RangeCon2Slack :
using Base::SetInt;
using Base::GetDbl;
using Base::SetDbl;
using Base::GetStr;
using Base::SetStr;

using Base::GetNode;

Expand Down
9 changes: 8 additions & 1 deletion include/mp/valcvt-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ using VMapOverElement = ValueMap< std::vector<El> >;
using ValueMapInt = VMapOverElement< int >;
/// Specialize ValueMap storing double's
using ValueMapDbl = VMapOverElement< double >;
/// Specialize ValueMap storing strings
using ValueMapStr = VMapOverElement< std::string >;


/// Group of values or value nodes
Expand Down Expand Up @@ -269,11 +271,15 @@ using MVOverEl = ModelValues< VMapOverElement<El> >;
using ModelValuesInt = MVOverEl< int >;
/// Specialize ModelValues<> for concrete double data
using ModelValuesDbl = MVOverEl< double >;
/// Specialize ModelValues<> for concrete double data
using ModelValuesStr = MVOverEl< std::string >;


/// Declare ValueNode
class ValueNode;

/// Typedef VCString
using VCString = std::string;

/// Macro for a list of pre- / postsolve method definitions
/// in a ValuePresolver or a link.
Expand All @@ -292,7 +298,8 @@ class ValueNode;
PRESOLVE_KIND(Solution, double) \
PRESOLVE_KIND(Basis, int) \
PRESOLVE_KIND(IIS, int) \
PRESOLVE_KIND(LazyUserCutFlags, int)
PRESOLVE_KIND(LazyUserCutFlags, int) \
PRESOLVE_KIND(Names, VCString)
// etc...


Expand Down
11 changes: 11 additions & 0 deletions include/mp/valcvt-link.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ class BasicStaticIndivEntryLink :
void SetDbl(const LinkEntry& be, size_t pos, double i)
{ GetNode(pos).SetDbl(be[pos], i); }

/// Access string value at the node \a pos at the index
/// stored in \a be[pos]
///
/// @param be: a LinkEntry
/// @param pos: node number from 0..NNodes-1
const VCString& GetStr(const LinkEntry& be, size_t pos) const
{ return GetNode(pos).GetStr(be[pos]); }
/// SetStr
void SetStr(const LinkEntry& be, size_t pos, VCString v)
{ GetNode(pos).SetStr(be[pos], std::move(v)); }

private:
NodeList ndl_;
};
Expand Down
75 changes: 69 additions & 6 deletions include/mp/valcvt-node.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class NodeRange {


/// Value node, a node of the conversion graph.
/// Stores arrays of int's and double's
/// Stores arrays of int's, double's, and VCString's
/// corresponding to variables, or a constraint type, or objectives.
/// The data is stored temporarily during a conversion run.
class ValueNode {
Expand All @@ -94,6 +94,7 @@ class ValueNode {
ValueNode(ValueNode&& vn) : pre_(vn.pre_) {
vi_ = std::move(vn.vi_);
vd_ = std::move(vn.vd_);
vStr_ = std::move(vn.vStr_);
sz_ = std::move(vn.sz_);
name_ = std::move(vn.name_);
RegisterMe();
Expand All @@ -103,6 +104,7 @@ class ValueNode {
ValueNode(const ValueNode& vn) : pre_(vn.pre_) {
vi_ = (vn.vi_);
vd_ = (vn.vd_);
vStr_ = (vn.vStr_);
sz_ = (vn.sz_);
name_ = (vn.name_);
RegisterMe();
Expand All @@ -114,7 +116,9 @@ class ValueNode {
/// bool empty(). True when actual values are empty or 0.
/// Has STL syntax.
bool empty() const {
return EmptyOr0(vi_) && EmptyOr0(vd_);
return EmptyOr0(vi_)
&& EmptyOr0(vd_)
&& EmptyOr0(vStr_);
}

/// Declared size (what is being used by links)
Expand Down Expand Up @@ -163,12 +167,24 @@ class ValueNode {
return *this;
}

/// Assign from vector. Always copy
ValueNode& operator=(std::vector<VCString> as)
{
// assert(ad.size() <= size());
vStr_ = std::move(as);
vStr_.resize(Size()); // cut off / complement values
return *this;
}

/// Retrieve whole ArrayRef<int>
operator ArrayRef<int> () const { return vi_; }

/// Retrieve whole ArrayRef<double>
operator ArrayRef<double> () const { return vd_; }

/// Retrieve whole ArrayRef<str>
operator ArrayRef<VCString> () const { return vStr_; }

/// Retrieve vec<T>& - dummy version
template <class T>
std::vector<T>& GetValVec()
Expand All @@ -180,12 +196,15 @@ class ValueNode {
/// Retrieve whole vector<double>&
operator const std::vector<double>& () const { return vd_; }

/// Retrieve whole vector<VCString&>&
operator const std::vector<VCString>& () const { return vStr_; }


/////////////////////// Access individual values ///////////////////////

/// Retrieve T, dummy version
template <class T>
T GetVal(size_t ) const { return {}; }
const T& GetVal(size_t ) const { return {}; }

/// Set T, dummy version
template <class T>
Expand All @@ -194,17 +213,40 @@ class ValueNode {
/// Retrieve int[i]
int GetInt(size_t i) const { assert(i<vi_.size()); return vi_[i]; }

/// Retrieve int[i]
const int& GetIntRef(size_t i) const
{ assert(i<vi_.size()); return vi_[i]; }

/// Set int[i].
/// If existing value non-0, only allow larger value.
void SetInt(size_t i, int v) { SetNum(vi_, i, v); }

/// Retrieve double[i]
double GetDbl(size_t i) const { assert(i<vd_.size()); return vd_[i]; }

/// Retrieve double[i]
const double& GetDblRef(size_t i) const
{ assert(i<vd_.size()); return vd_[i]; }

/// Set double[i].
/// CONFLICT RESOLUTION:
/// If existing value non-0, only allow larger value.
void SetDbl(size_t i, double v) { SetNum(vd_, i, v); }

/// Retrieve string[i]
const VCString& GetStr(size_t i) const
{ assert(i<vStr_.size()); return vStr_[i]; }

/// Set string[i].
/// CONFLICT RESOLUTION:
/// If existing value non-0, only allow larger value???
void SetStr(size_t i, VCString v) {
assert(i<Size()); // index into the originally declared suffix size
if (vStr_.size()<=i) // can happen after CopySrcDest / CopyDestSrc
vStr_.resize(Size());
vStr_[i] = std::move(v);
}

/// GetName
const std::string& GetName() const { return name_; }

Expand All @@ -213,8 +255,9 @@ class ValueNode {

/// Clean up and realloc with current size, fill by 0's
void CleanUpAndRealloc() {
vi_.clear(); vd_.clear();
vi_.clear(); vd_.clear(); vStr_.clear();
vi_.resize(Size()); vd_.resize(Size());
vStr_.resize(Size());
}

protected:
Expand Down Expand Up @@ -254,11 +297,20 @@ class ValueNode {
{ return v; }));
}

/// EmptyOr0 for a vector<str>
static bool EmptyOr0(const std::vector<VCString>& v) {
return (v.end()==std::find_if(v.begin(), v.end(),
[](typename
std::vector<VCString>::value_type v) -> bool
{ return !v.empty(); }));
}

private:
// Move & copy constructors should copy all members!
BasicValuePresolver& pre_;
std::vector<int> vi_;
std::vector<double> vd_;
std::vector<VCString> vStr_;
size_t sz_=0;
std::string name_ = "default_value_node";
};
Expand All @@ -271,17 +323,28 @@ template <>
std::vector<int>& ValueNode::GetValVec<int>() { return vi_; }

template <>
double ValueNode::GetVal<double>(size_t i) const { return GetDbl(i); }
std::vector<VCString>& ValueNode::GetValVec<VCString>() { return vStr_; }

template <>
int ValueNode::GetVal<int>(size_t i) const { return GetInt(i); }
const double& ValueNode::GetVal<double>(size_t i) const { return GetDblRef(i); }

template <>
const int& ValueNode::GetVal<int>(size_t i) const { return GetIntRef(i); }

template <>
const VCString& ValueNode::GetVal<VCString>(size_t i) const
{ return GetStr(i); }

template <>
void ValueNode::SetVal<double>(size_t i, double v) { SetDbl(i, v); }

template <>
void ValueNode::SetVal<int>(size_t i, int v) { SetInt(i, v); }

template <>
void ValueNode::SetVal<VCString>(size_t i, VCString v)
{ SetStr(i, std::move(v)); }


/// Specialize SetValueNodeName() for ValueNode
inline void
Expand Down

0 comments on commit db10b80

Please sign in to comment.