Skip to content

Commit

Permalink
NLModel: allow empty obj #30
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Feb 13, 2024
1 parent a2e7d6c commit a260eaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion nl-writer2/include/mp/nl-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class NLModel {
/// Add linear objective (only single objective supported.)
/// Sense: NLW2_ObjSenseM....
/// Coefficients: dense vector.
void SetLinearObjective(int sense, double c0, const double* c)
void SetLinearObjective(int sense, double c0,
const double* c = nullptr)
{ obj_sense_=sense; obj_c0_=c0; obj_c_=c; }

/// Add Q for the objective quadratic part 0.5 @ x.T @ Q @ x.
Expand Down
10 changes: 6 additions & 4 deletions nl-writer2/src/nl-solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class NLFeeder2_Easy
auto c = NLME().ObjCoefficients();
for (int j=0; j<header_.num_vars; ++j)
if (obj_grad_supp_[j])
svw.Write(VPerm(j), c[j]);
svw.Write(VPerm(j), c ? c[j] : 0.0);
}
}

Expand Down Expand Up @@ -378,9 +378,11 @@ class NLFeeder2_Easy

void FillObjNonzeros() {
obj_grad_supp_.resize(NLME().NumCols());
// Linear part
for (auto i=NLME().NumCols(); i--; )
obj_grad_supp_[i] = (NLME().ObjCoefficients()[i]);
// Linear part -- if provided
if (NLME().ObjCoefficients()) {
for (auto i=NLME().NumCols(); i--; )
obj_grad_supp_[i] = (NLME().ObjCoefficients()[i]);
}
// QP part
auto Q = NLME().Hessian();
if (Q.num_nz_) {
Expand Down

0 comments on commit a260eaa

Please sign in to comment.