Skip to content

Commit 959a783

Browse files
committed
Fix: make objective_sense optional in create_lp_problem and repair test build
1 parent acc8205 commit 959a783

5 files changed

Lines changed: 21 additions & 20 deletions

File tree

docs/C_API.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ lp_problem_t *create_lp_problem(
1515
const double *con_ub, // constraint upper bounds (length m)
1616
const double *var_lb, // variable lower bounds (length n)
1717
const double *var_ub, // variable upper bounds (length n)
18-
const double *objective_constant // scalar objective offset
18+
const double *objective_constant, // scalar objective offset
19+
const objective_sense_t *objective_sense // objective sense (NULL → minimize)
1920
);
2021

2122
cupdlpx_result_t* solve_lp_problem(
@@ -32,6 +33,7 @@ cupdlpx_result_t* solve_lp_problem(
3233
- `var_lb`: Variable lower bounds. If `NULL`, defaults to all `-INFINITY`.
3334
- `var_ub`: Variable upper bounds. If `NULL`, defaults to all `+INFINITY`.
3435
- `objective_constant`: Scalar constant term added to the objective value. If `NULL`, defaults to `0.0`.
36+
- `objective_sense`: Objective sense, `OBJECTIVE_SENSE_MINIMIZE` or `OBJECTIVE_SENSE_MAXIMIZE`. If `NULL`, defaults to minimize.
3537

3638

3739
`solve_lp_problem` parameters:
@@ -70,7 +72,7 @@ int main() {
7072

7173
// Build the problem
7274
lp_problem_t* prob = create_lp_problem(
73-
c, &A_desc, l, u, NULL, NULL, NULL);
75+
c, &A_desc, l, u, NULL, NULL, NULL, NULL);
7476

7577
// Solve (NULL → use default parameters)
7678
cupdlpx_result_t* res = solve_lp_problem(prob, NULL);

include/cupdlpx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C"
3131
const double *var_lb,
3232
const double *var_ub,
3333
const double *objective_constant,
34-
objective_sense_t objective_sense);
34+
const objective_sense_t *objective_sense);
3535

3636
// Set up initial primal and dual solution for an lp_problem_t
3737
void set_start_values(lp_problem_t *prob, const double *primal, const double *dual);

python_bindings/_core_bindings.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,15 @@ static py::dict solve_once(py::object A,
493493
}
494494

495495
// build problem
496-
lp_problem_t *prob = create_lp_problem(c_ptr, // objective vector
497-
&view.desc, // constraint matrix
498-
l_ptr, // constraint lower bound
499-
u_ptr, // constraint upper bound
500-
lb_ptr, // variable lower bound
501-
ub_ptr, // variable upper bound
502-
c0_ptr, // objective constant
503-
minimize ? OBJECTIVE_SENSE_MINIMIZE : OBJECTIVE_SENSE_MAXIMIZE // objective sense
496+
objective_sense_t sense = minimize ? OBJECTIVE_SENSE_MINIMIZE : OBJECTIVE_SENSE_MAXIMIZE;
497+
lp_problem_t *prob = create_lp_problem(c_ptr, // objective vector
498+
&view.desc, // constraint matrix
499+
l_ptr, // constraint lower bound
500+
u_ptr, // constraint upper bound
501+
lb_ptr, // variable lower bound
502+
ub_ptr, // variable upper bound
503+
c0_ptr, // objective constant
504+
&sense // objective sense
504505
);
505506
if (!prob)
506507
{

src/cupdlpx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ lp_problem_t *create_lp_problem(const double *objective_c,
3030
const double *var_lb,
3131
const double *var_ub,
3232
const double *objective_constant,
33-
objective_sense_t objective_sense)
33+
const objective_sense_t *objective_sense)
3434
{
3535
lp_problem_t *prob = (lp_problem_t *)safe_malloc(sizeof(lp_problem_t));
3636
prob->primal_start = NULL;
@@ -106,7 +106,7 @@ lp_problem_t *create_lp_problem(const double *objective_c,
106106

107107
// default fill values
108108
prob->objective_constant = objective_constant ? *objective_constant : 0.0;
109-
prob->objective_sense = objective_sense;
109+
prob->objective_sense = objective_sense ? *objective_sense : OBJECTIVE_SENSE_MINIMIZE;
110110
fill_or_copy(&prob->objective_vector, prob->num_variables, objective_c, 0.0);
111111
fill_or_copy(&prob->variable_lower_bound, prob->num_variables, var_lb, -INFINITY);
112112
fill_or_copy(&prob->variable_upper_bound, prob->num_variables, var_ub, INFINITY);

test/test_interface.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ static void run_once(const char *tag, const matrix_desc_t *A_desc, const double
3737
u, // constraint upper bound con_ub
3838
NULL, // variable lower bound var_lb (defaults to 0)
3939
NULL, // variable upper bound var_ub (defaults to +inf)
40-
NULL // objective constant c0 (defaults to 0.0)
40+
NULL, // objective constant c0 (defaults to 0.0)
41+
NULL // objective sense (defaults to minimize)
4142
);
4243
if (!prob)
4344
{
@@ -70,7 +71,7 @@ test_warm_start(const char *tag, const matrix_desc_t *A_desc, const double *c, c
7071
int n = A_desc->n;
7172
int m = A_desc->m;
7273

73-
lp_problem_t *prob = create_lp_problem(c, A_desc, l, u, NULL, NULL, NULL);
74+
lp_problem_t *prob = create_lp_problem(c, A_desc, l, u, NULL, NULL, NULL, NULL);
7475
if (!prob)
7576
{
7677
fprintf(stderr, "[test] create_lp_problem failed for %s.\n", tag);
@@ -120,7 +121,6 @@ int main()
120121
A_dense.m = m;
121122
A_dense.n = n;
122123
A_dense.fmt = matrix_dense;
123-
A_dense.zero_tolerance = 0.0;
124124
A_dense.data.dense.A = &A[0][0];
125125

126126
// A as a CSR matrix
@@ -133,7 +133,6 @@ int main()
133133
A_csr.m = m;
134134
A_csr.n = n;
135135
A_csr.fmt = matrix_csr;
136-
A_csr.zero_tolerance = 0.0;
137136
A_csr.data.csr.nnz = 5;
138137
A_csr.data.csr.row_ptr = csr_row_ptr;
139138
A_csr.data.csr.col_ind = csr_col_ind;
@@ -149,7 +148,6 @@ int main()
149148
A_csc.m = m;
150149
A_csc.n = n;
151150
A_csc.fmt = matrix_csc;
152-
A_csc.zero_tolerance = 0.0;
153151
A_csc.data.csc.nnz = 5;
154152
A_csc.data.csc.col_ptr = csc_col_ptr;
155153
A_csc.data.csc.row_ind = csc_row_ind;
@@ -165,7 +163,6 @@ int main()
165163
A_coo.m = m;
166164
A_coo.n = n;
167165
A_coo.fmt = matrix_coo;
168-
A_coo.zero_tolerance = 0.0;
169166
A_coo.data.coo.nnz = 5;
170167
A_coo.data.coo.row_ind = coo_row_ind;
171168
A_coo.data.coo.col_ind = coo_col_ind;
@@ -208,7 +205,8 @@ int main()
208205
u, // con_ub
209206
NULL, // var_lb
210207
NULL, // var_ub
211-
NULL // objective_constant
208+
NULL, // objective_constant
209+
NULL // objective sense (defaults to minimize)
212210
);
213211
if (!prob)
214212
{

0 commit comments

Comments
 (0)