Skip to content

Commit

Permalink
fix: better system for printing output
Browse files Browse the repository at this point in the history
  • Loading branch information
mimizh2418 committed Oct 15, 2024
1 parent 901ee0c commit 74622ba
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/LinearProblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,39 @@ const MatrixXd& LinearProblem::getConstraintMatrix() const { return A; }
const VectorXd& LinearProblem::getConstraintRHS() const { return b; }

std::string LinearProblem::objectiveFunctionString() const {
string ret = format("{:.3f}x_1", c(0));
for (Index i = 1; i < c.size(); i++) {
ret = format("{}{}{:.3f}x_{}", ret, c(i) > 0 ? " + " : " - ", abs(c(i)), i + 1);
string ret;
bool first = true;
for (Index i = 0; i < c.size(); i++) {
const double coeff = c(i);
if (!first)
ret += coeff > 0 ? " + " : " - ";
else {
if (coeff < 0) ret += "-";
first = false;
}
if (abs(coeff) != 1) ret += format("{}", abs(coeff));
ret += format("x_{}", i + 1);
}
return ret;
}

vector<string> LinearProblem::constraintStrings() const {
vector<string> ret(num_constraints);
for (Index i = 0; i < num_constraints; i++) {
ret[i] = format("{:.3f}x_1", A(i, 0));
for (Index j = 1; j < num_decision_vars; j++) {
if (A(i, j) == 0) continue;
ret[i] = format("{}{}{:.3f}x_{}", ret[i], A(i, j) > 0 ? " + " : " - ", abs(A(i, j)), j + 1);
bool first = true;
for (Index j = 0; j < num_decision_vars; j++) {
const double coeff = A(i, j);
if (coeff == 0) continue;
if (!first)
ret[i] += coeff > 0 ? " + " : " - ";
else {
if (coeff < 0) ret[i] += "-";
first = false;
}
if (abs(coeff) != 1) ret[i] += format("{}", abs(coeff));
ret[i] += "x_" + to_string(j + 1);
}
ret[i] += format(" ≤ {:.3f}", b(i));
ret[i] += format(" ≤ {}", b(i));
}
return ret;
}

0 comments on commit 74622ba

Please sign in to comment.