Skip to content

Commit 5a81991

Browse files
committed
Fix: handle MPS OBJSENSE MAXIMIZE in reader
1 parent 931c94c commit 5a81991

5 files changed

Lines changed: 33 additions & 17 deletions

File tree

include/cupdlpx_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ extern "C"
4343
NORM_TYPE_L_INF = 1
4444
} norm_type_t;
4545

46+
typedef enum
47+
{
48+
OBJECTIVE_SENSE_MINIMIZE = 0,
49+
OBJECTIVE_SENSE_MAXIMIZE = 1
50+
} objective_sense_t;
51+
4652
typedef struct
4753
{
4854
int num_variables;
@@ -51,6 +57,7 @@ extern "C"
5157
double *variable_upper_bound;
5258
double *objective_vector;
5359
double objective_constant;
60+
objective_sense_t objective_sense;
5461

5562
int *constraint_matrix_row_pointers;
5663
int *constraint_matrix_col_indices;

internal/internal_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct
4343
double *variable_upper_bound;
4444
double *objective_vector;
4545
double objective_constant;
46+
double objective_sign;
4647
cu_sparse_matrix_csr_t *constraint_matrix;
4748
cu_sparse_matrix_csr_t *constraint_matrix_t;
4849
double *constraint_lower_bound;

src/mps_parser.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ typedef struct
326326
char *objective_row_name;
327327
char *current_col_name;
328328
double objective_constant;
329-
bool is_maximize;
329+
objective_sense_t objective_sense;
330330
int error_flag;
331331

332332
} MpsParserState;
@@ -454,25 +454,28 @@ lp_problem_t *read_mps_file(const char *filename)
454454
next_section = SEC_ENDATA;
455455
}
456456

457-
if (current_section == SEC_ROWS && next_section != SEC_ROWS && !rows_finalized)
457+
if (next_section != SEC_NONE)
458458
{
459-
if (finalize_rows(&state) != 0)
460-
state.error_flag = 1;
461-
rows_finalized = true;
462-
}
459+
if (current_section == SEC_ROWS && next_section != SEC_ROWS && !rows_finalized)
460+
{
461+
if (finalize_rows(&state) != 0)
462+
state.error_flag = 1;
463+
rows_finalized = true;
464+
}
463465

464-
current_section = next_section;
465-
if (current_section == SEC_ENDATA)
466-
break;
467-
continue;
466+
current_section = next_section;
467+
if (current_section == SEC_ENDATA)
468+
break;
469+
continue;
470+
}
468471
}
469472

470473
switch (current_section)
471474
{
472475
case SEC_OBJSENSE:
473476
if (n_tokens > 0 && (strcmp(tokens[0], "MAX") == 0 || strcmp(tokens[0], "MAXIMIZE") == 0))
474477
{
475-
state.is_maximize = true;
478+
state.objective_sense = OBJECTIVE_SENSE_MAXIMIZE;
476479
}
477480
break;
478481
case SEC_ROWS:
@@ -515,7 +518,9 @@ lp_problem_t *read_mps_file(const char *filename)
515518
prob->num_variables = state.col_map.size;
516519
prob->num_constraints = state.row_map.size;
517520
prob->constraint_matrix_num_nonzeros = state.coo_matrix.nnz;
518-
prob->objective_constant = state.is_maximize ? -state.objective_constant : state.objective_constant;
521+
prob->objective_constant =
522+
(state.objective_sense == OBJECTIVE_SENSE_MAXIMIZE) ? -state.objective_constant : state.objective_constant;
523+
prob->objective_sense = state.objective_sense;
519524

520525
prob->objective_vector = state.objective_coeffs;
521526
prob->variable_lower_bound = state.var_lower_bounds;
@@ -532,7 +537,7 @@ lp_problem_t *read_mps_file(const char *filename)
532537
state.constraint_lower_bounds = NULL;
533538
state.constraint_upper_bounds = NULL;
534539

535-
if (state.is_maximize)
540+
if (state.objective_sense == OBJECTIVE_SENSE_MAXIMIZE)
536541
{
537542
for (int i = 0; i < prob->num_variables; ++i)
538543
{

src/presolve.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ lp_problem_t *convert_pslp_to_cupdlpx(PresolvedProblem *reduced_prob, const lp_p
3939
cupdlpx_prob->dual_start = NULL;
4040

4141
cupdlpx_prob->objective_constant = original_prob->objective_constant + reduced_prob->obj_offset;
42+
cupdlpx_prob->objective_sense = original_prob->objective_sense;
4243
cupdlpx_prob->objective_vector = reduced_prob->c;
4344

4445
cupdlpx_prob->constraint_lower_bound = reduced_prob->lhs;
@@ -216,8 +217,9 @@ void pslp_postsolve(const cupdlpx_presolve_info_t *info, cupdlpx_result_t *resul
216217
obj += original_prob->objective_vector[i] * result->primal_solution[i];
217218
}
218219
obj += original_prob->objective_constant;
219-
result->primal_objective_value = obj;
220-
result->dual_objective_value = obj;
220+
double objective_sign = (original_prob->objective_sense == OBJECTIVE_SENSE_MAXIMIZE) ? -1.0 : 1.0;
221+
result->primal_objective_value = objective_sign * obj;
222+
result->dual_objective_value = objective_sign * obj;
221223
}
222224
// if (info->presolver->stats != NULL) {
223225
// result->presolve_stats = *(info->presolver->stats);

src/solver.cu

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ static pdhg_solver_state_t *initialize_solver_state(const lp_problem_t *working_
327327
state->num_variables = n_vars;
328328
state->num_constraints = n_cons;
329329
state->objective_constant = working_problem->objective_constant;
330+
state->objective_sign = (working_problem->objective_sense == OBJECTIVE_SENSE_MAXIMIZE) ? -1.0 : 1.0;
330331

331332
state->constraint_matrix = (cu_sparse_matrix_csr_t *)safe_malloc(sizeof(cu_sparse_matrix_csr_t));
332333
state->constraint_matrix_t = (cu_sparse_matrix_csr_t *)safe_malloc(sizeof(cu_sparse_matrix_csr_t));
@@ -1260,8 +1261,8 @@ static cupdlpx_result_t *create_result_from_state(pdhg_solver_state_t *state, co
12601261
results->cumulative_time_sec = state->cumulative_time_sec;
12611262
results->relative_primal_residual = state->relative_primal_residual;
12621263
results->relative_dual_residual = state->relative_dual_residual;
1263-
results->primal_objective_value = state->primal_objective_value;
1264-
results->dual_objective_value = state->dual_objective_value;
1264+
results->primal_objective_value = state->objective_sign * state->primal_objective_value;
1265+
results->dual_objective_value = state->objective_sign * state->dual_objective_value;
12651266
results->objective_gap = state->objective_gap;
12661267
results->relative_objective_gap = state->relative_objective_gap;
12671268
results->max_primal_ray_infeasibility = state->max_primal_ray_infeasibility;

0 commit comments

Comments
 (0)