From 777e0e01e0feb1ed73d3023d29e2951a7d7d5040 Mon Sep 17 00:00:00 2001 From: Gleb Belov Date: Wed, 13 Nov 2024 17:25:30 +1100 Subject: [PATCH] MP2NL: Fix GetLinPart() #237 --- include/mp/flat/constr_keeper.h | 26 +++++++++++++++++--------- solvers/mp2nl/mp2nlmodelapi.cc | 10 ++++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/mp/flat/constr_keeper.h b/include/mp/flat/constr_keeper.h index 1d7e5c112..98357eb63 100644 --- a/include/mp/flat/constr_keeper.h +++ b/include/mp/flat/constr_keeper.h @@ -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; } @@ -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. diff --git a/solvers/mp2nl/mp2nlmodelapi.cc b/solvers/mp2nl/mp2nlmodelapi.cc index d592a0ff6..cb6511909 100644 --- a/solvers/mp2nl/mp2nlmodelapi.cc +++ b/solvers/mp2nl/mp2nlmodelapi.cc @@ -314,8 +314,9 @@ void MP2NLModelAPI::MergeItemSparsity( } else { // proper expr or variable std::unordered_map 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 { @@ -348,8 +349,8 @@ template inline MP2NLModelAPI::LinPartRefOrStorage GetLPRoS(const NLBaseAssign& nla) { - std::vector c{1, nla.coef(0)}; // @todo: SmallVec - std::vector v{1, nla.var(0)}; + std::vector c{{nla.coef(0)}}; // @todo: SmallVec + std::vector v{{nla.var(0)}}; return {std::move(c), std::move(v)}; // std::move to pass storage } @@ -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