Skip to content

Commit

Permalink
Export variable names #232
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Mar 6, 2024
1 parent e040c80 commit 7a8bf0c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,8 @@ class ConstraintManager {
}

/// Retrieve file logger
BasicFileAppender& GetFileAppender() { return *graph_exporter_app_; }
BasicFileAppender& GetFileAppender() const
{ return *graph_exporter_app_; }

private:
std::multimap<double, BasicConstraintKeeper&> con_keepers_;
Expand Down
35 changes: 34 additions & 1 deletion include/mp/flat/converter_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class FlatModel
for (int i=0;
GetFileAppender().IsOpen() && i<(int)lbs.size(); ++i) {
fmt::MemoryWriter wrt;
if (0==i && i_start==0) {
{
MiniJSONWriter jw(wrt);
jw["COMMENT"]
= "Initial model information. "
"Can be updated later with new bounds and names, etc.";
}
wrt.write("\n"); // with EOL
}
{
MiniJSONWriter jw(wrt);
int i_actual = i+i_start;
Expand Down Expand Up @@ -310,8 +319,32 @@ class FlatModel
for (const std::string& s : var_names_storage_)
var_names_.push_back(s.c_str());
backend.AddVariables({ var_lb_, var_ub_, var_type_, var_names_ });
} else
} else {
backend.AddVariables({ var_lb_, var_ub_, var_type_ });
}
for (int i=0;
GetFileAppender().IsOpen() && i<(int)var_lb_.size(); ++i) {
fmt::MemoryWriter wrt;
if (0==i) {
{
MiniJSONWriter jw(wrt);
jw["COMMENT"]
= "Updated variable information.";
}
wrt.write("\n"); // with EOL
}
{
MiniJSONWriter jw(wrt);
jw["VAR_index"] = i;
if (var_names_storage_.size() > i)
jw["name"] = var_names_[i];
jw["bounds"] << var_lb_[i] << var_ub_[i];
jw["type"] = (int)var_type_[i];
jw["is_from_nl"] = (int)is_var_original(i);
}
wrt.write("\n"); // with EOL
GetFileAppender().Append(wrt);
}
}

template <class Backend>
Expand Down
11 changes: 10 additions & 1 deletion include/mp/flat/problem_flattener.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class ProblemFlattener :
{
MiniJSONWriter jw(wrt);
jw["NL_COMMON_EXPR_index"] = i;
// We don't receive defvar names from AMPL
jw["name"] = "ce" + std::to_string(i+1);
auto ce = GetModel().common_expr(i);
fmt::MemoryWriter w2;
WriteExpr<typename ProblemType::ExprTypes>(
Expand All @@ -205,6 +207,8 @@ class ProblemFlattener :
{
MiniJSONWriter jw(wrt);
jw["NL_OBJECTIVE_index"] = i;
if (GetModel().obj_names().size()>i)
jw["name"] = GetModel().obj_names()[i];
auto obj = GetModel().obj(i);
jw["sense"] = (int)obj.type();
fmt::MemoryWriter w2;
Expand All @@ -226,6 +230,8 @@ class ProblemFlattener :
auto con = GetModel().algebraic_con(i);
jw["NL_CON_TYPE"] = (con.nonlinear_expr() ? "nonlin" : "lin");
jw["index"] = i;
if (GetModel().con_names().size()>i)
jw["name"] = GetModel().con_names()[i];
fmt::MemoryWriter w2;
WriteAlgCon<typename ProblemType::ExprTypes>(w2, con);
jw["printed"] = w2.c_str();
Expand All @@ -243,7 +249,10 @@ class ProblemFlattener :
MiniJSONWriter jw(wrt);
auto con = GetModel().logical_con(i);
jw["NL_CON_TYPE"] = "logical";
jw["index"] = GetModel().num_algebraic_cons() + i;
int i_actual = GetModel().num_algebraic_cons() + i;
jw["index"] = i_actual;
if (GetModel().con_names().size()>i_actual)
jw["name"] = GetModel().con_names()[i_actual];
fmt::MemoryWriter w2;
WriteExpr<typename ProblemType::ExprTypes>(w2, con.expr());
jw["printed"] = w2.c_str();
Expand Down
6 changes: 3 additions & 3 deletions include/mp/problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,11 @@ class BasicProblem : public ExprFactory, public SuffixManager {


/** Returns the variable names (if present). */
std::vector<std::string>& var_names() { return var_names_; }
const std::vector<std::string>& var_names() { return var_names_; }
/** Returns the constraint names (if present). */
std::vector<std::string>& con_names() { return con_names_; }
const std::vector<std::string>& con_names() { return con_names_; }
/** Returns the objective names (if present). */
std::vector<std::string>& obj_names() { return obj_names_; }
const std::vector<std::string>& obj_names() { return obj_names_; }

/** Returns the number of objectives. */
int num_objs() const { return static_cast<int>(linear_objs_.size()); }
Expand Down

0 comments on commit 7a8bf0c

Please sign in to comment.