Skip to content

Commit a260eaa

Browse files
committed
NLModel: allow empty obj #30
1 parent a2e7d6c commit a260eaa

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

nl-writer2/include/mp/nl-model.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class NLModel {
6969
/// Add linear objective (only single objective supported.)
7070
/// Sense: NLW2_ObjSenseM....
7171
/// Coefficients: dense vector.
72-
void SetLinearObjective(int sense, double c0, const double* c)
72+
void SetLinearObjective(int sense, double c0,
73+
const double* c = nullptr)
7374
{ obj_sense_=sense; obj_c0_=c0; obj_c_=c; }
7475

7576
/// Add Q for the objective quadratic part 0.5 @ x.T @ Q @ x.

nl-writer2/src/nl-solver.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class NLFeeder2_Easy
8383
auto c = NLME().ObjCoefficients();
8484
for (int j=0; j<header_.num_vars; ++j)
8585
if (obj_grad_supp_[j])
86-
svw.Write(VPerm(j), c[j]);
86+
svw.Write(VPerm(j), c ? c[j] : 0.0);
8787
}
8888
}
8989

@@ -378,9 +378,11 @@ class NLFeeder2_Easy
378378

379379
void FillObjNonzeros() {
380380
obj_grad_supp_.resize(NLME().NumCols());
381-
// Linear part
382-
for (auto i=NLME().NumCols(); i--; )
383-
obj_grad_supp_[i] = (NLME().ObjCoefficients()[i]);
381+
// Linear part -- if provided
382+
if (NLME().ObjCoefficients()) {
383+
for (auto i=NLME().NumCols(); i--; )
384+
obj_grad_supp_[i] = (NLME().ObjCoefficients()[i]);
385+
}
384386
// QP part
385387
auto Q = NLME().Hessian();
386388
if (Q.num_nz_) {

0 commit comments

Comments
 (0)