Skip to content

Commit

Permalink
MP2NL: Fix GetLinPart() #237
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Nov 13, 2024
1 parent 39d9c12 commit 777e0e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 17 additions & 9 deletions include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class ConstraintKeeper final
{
cons_.emplace_back( d, std::move(args)... );
ExportConstraint(cons_.size()-1, cons_.back());
// fmt::MemoryWriter wrt;
// WriteCon2JSON(wrt, cons_.size()-1, cons_.back());
// printf("%s\n", wrt.c_str());
return cons_.size()-1;
}

Expand Down Expand Up @@ -464,19 +467,24 @@ class ConstraintKeeper final
void ExportConstraint(int i_con, const Container& cnt) {
if (GetLogger()) {
fmt::MemoryWriter wrt;
{
MiniJSONWriter jw(wrt);
jw["CON_TYPE"] = GetShortTypeName();
jw["index"] = i_con;
if (*cnt.GetCon().name())
jw["name"] = cnt.GetCon().name();
jw["depth"] = cnt.GetDepth();
WriteJSON(jw["data"], cnt.GetCon());
}
WriteCon2JSON(wrt, i_con, cnt);
wrt.write("\n"); // EOL
GetLogger()->Append(wrt);
}
}

/// Write constraint in JSON format
void WriteCon2JSON(
fmt::MemoryWriter& wrt, int i_con, const Container& cnt) {
MiniJSONWriter jw(wrt);
jw["CON_TYPE"] = GetShortTypeName();
jw["index"] = i_con;
if (*cnt.GetCon().name())
jw["name"] = cnt.GetCon().name();
jw["depth"] = cnt.GetDepth();
WriteJSON(jw["data"], cnt.GetCon());
}

/// Export constraint status.
/// This is called in the end,
/// so printing the readable form.
Expand Down
10 changes: 6 additions & 4 deletions solvers/mp2nl/mp2nlmodelapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ void MP2NLModelAPI::MergeItemSparsity(
} else { // proper expr or variable
std::unordered_map<int, double> linp_ext;
auto lp = GetLinPart(info);
for (auto i=lp.size(); i--; )
for (auto i=lp.size(); i--; ) {
linp_ext[lp.vars()[i]] += lp.coefs()[i];
}
if (expr.IsVariable()) { // @todo should have been inlined
linp_ext[expr.GetVarIndex()] += 1.0; // Merge 1 var
} else {
Expand Down Expand Up @@ -348,8 +349,8 @@ template <int sense>
inline
MP2NLModelAPI::LinPartRefOrStorage
GetLPRoS(const NLBaseAssign<sense>& nla) {
std::vector<double> c{1, nla.coef(0)}; // @todo: SmallVec
std::vector<int> v{1, nla.var(0)};
std::vector<double> c{{nla.coef(0)}}; // @todo: SmallVec
std::vector<int> v{{nla.var(0)}};
return {std::move(c), std::move(v)}; // std::move to pass storage
}

Expand Down Expand Up @@ -770,9 +771,10 @@ void MP2NLModelAPI::FeedExtLinPart(
const auto& lp_ext = item.GetExtLinPart();
if (lp_ext.size()) {
auto svw = svwf.MakeVectorWriter(lp_ext.size());
for (int j=0; j<lp_ext.size(); ++j)
for (int j=0; j<lp_ext.size(); ++j) {
svw.Write(GetNewVarIndex( lp_ext.vars()[j]), // new ordering
lp_ext.coefs()[j]);
}
}
}

Expand Down

0 comments on commit 777e0e0

Please sign in to comment.