Skip to content

Commit

Permalink
Export NL item names and flat objectives #232
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Mar 14, 2024
1 parent a0e92ee commit 9317221
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
6 changes: 5 additions & 1 deletion include/mp/flat/converter_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ class FlatModel
{
MiniJSONWriter jw(wrt);
jw["OBJECTIVE_index"] = i_obj;
if (*obj.name())
if (obj.name() && *obj.name()) {
jw["name"] = obj.name();
fmt::MemoryWriter pr;
WriteModelItem(pr, obj, var_names_storage_);
jw["printed"] = pr.c_str();
}
jw["sense"] = (int)obj.obj_sense();
WriteJSON(jw["qp_terms"], obj.GetQPTerms());
WriteJSON(jw["lin_terms"], obj.GetLinTerms());
Expand Down
5 changes: 5 additions & 0 deletions include/mp/flat/expr_algebraic.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class AlgebraicExpression : public Body {
double constant_term_ = 0.0;
};

/// Very general template to write any flat con/obj
template <class Writer, class Item>
void WriteModelItem(Writer& wrt, const Item& i,
const std::vector<std::string>& vnam);

/// Very general template to write
/// a JSON-like representation of anything
template <class Writer, class Obj>
Expand Down
5 changes: 5 additions & 0 deletions include/mp/flat/problem_flattener.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class ProblemFlattener :
jw["name"] = vn.dvname(i);
auto ce = GetModel().common_expr(i);
fmt::MemoryWriter w2;
w2 << "var " << vn.dvname(i) << " = ";
WriteExpr<typename ProblemType::ExprTypes>(
w2, ce.linear_expr(), ce.nonlinear_expr(), vn);
jw["printed"] = w2.c_str();
Expand All @@ -216,6 +217,8 @@ class ProblemFlattener :
auto obj = GetModel().obj(i);
jw["sense"] = (int)obj.type();
fmt::MemoryWriter w2;
w2 << (obj::MAX==obj.type() ? "maximize " : "minimize ");
w2 << GetModel().obj_name(i) << ": ";
WriteExpr<typename ProblemType::ExprTypes>(
w2, obj.linear_expr(), obj.nonlinear_expr(),
GetModel().GetVarNamer());
Expand All @@ -237,6 +240,7 @@ class ProblemFlattener :
jw["index"] = i;
jw["name"] = GetModel().con_name(i);
fmt::MemoryWriter w2;
w2 << GetModel().con_name(i) << ": ";
WriteAlgCon<typename ProblemType::ExprTypes>(
w2, con, GetModel().GetVarNamer());
jw["printed"] = w2.c_str();
Expand All @@ -258,6 +262,7 @@ class ProblemFlattener :
jw["index"] = i_actual;
jw["name"] = GetModel().con_name(i_actual);
fmt::MemoryWriter w2;
w2 << GetModel().con_name(i_actual) << ": ";
WriteExpr<typename ProblemType::ExprTypes>(
w2, con.expr(), GetModel().GetVarNamer());
jw["printed"] = w2.c_str();
Expand Down
48 changes: 47 additions & 1 deletion src/std_constr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "mp/common.h"

#include "mp/flat/expr_quadratic.h"
#include "mp/flat/obj_std.h"
#include "mp/flat/model_info.hpp"

namespace mp {
Expand Down Expand Up @@ -89,7 +90,52 @@ void WriteVar(Writer& pr, const char* name,
if (var::INTEGER == ty)
pr << " integer";
}
pr << ';';
}

template <>
void WriteModelItem(fmt::MemoryWriter& wrt, const LinTerms& lt,
const std::vector<std::string>& vnam) {
for (int i=0; i<(int)lt.size(); ++i) {
if (i) {
wrt << (lt.coef(i)>=0.0 ? " + " : " - ");
}
wrt << std::fabs(lt.coef(i)) << '*' << vnam.at(lt.var(i));
}
}

template <>
void WriteModelItem(fmt::MemoryWriter& wrt, const QuadTerms& qt,
const std::vector<std::string>& vnam) {
for (int i=0; i<(int)qt.size(); ++i) {
if (i) {
wrt << (qt.coef(i)>=0.0 ? " + " : " - ");
}
wrt << std::fabs(qt.coef(i))
<< '*' << vnam.at(qt.var1(i))
<< '*' << vnam.at(qt.var2(i));
}
}

template <>
void WriteModelItem(fmt::MemoryWriter& wrt, const QuadAndLinTerms& qlt,
const std::vector<std::string>& vnam) {

}

template <>
void WriteModelItem(fmt::MemoryWriter& wrt, const QuadraticObjective& obj,
const std::vector<std::string>& vnam) {
wrt << (obj.obj_sense() ? "maximize " : "minimize ");
assert(obj.name() && *obj.name());
wrt << obj.name() << ": ";
WriteModelItem(wrt, obj.GetLinTerms(), vnam);
if (obj.GetQPTerms().size()) {
if (obj.GetLinTerms().size())
wrt << " + ";
wrt << '(';
WriteModelItem(wrt, obj.GetQPTerms(), vnam);
wrt << ')';
}
}

// Generate
Expand Down
3 changes: 3 additions & 0 deletions support/modelexplore/scripts/python/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def _matchRecords(self, cnt, keyw, keyNeed1=None):
pr = str(i) ## TODO printed form
if "printed" in i:
pr = i["printed"]
assert len(pr)
if ';'!=pr[-1]:
pr = pr + ';'
if (""==keyw or keyw in pr) \
and (keyNeed1==None \
or (keyNeed1 in i and 1==i[keyNeed1])):
Expand Down

0 comments on commit 9317221

Please sign in to comment.