@@ -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 ;
@@ -435,7 +435,7 @@ lp_problem_t *read_mps_file(const char *filename)
435435 if (n_tokens == 0 )
436436 continue ;
437437
438- if (n_tokens == 1 && isalpha (tokens [0 ][0 ]))
438+ if (isalpha (( unsigned char ) tokens [0 ][0 ]))
439439 {
440440 MpsSection next_section = SEC_NONE ;
441441 if (strcmp (tokens [0 ], "ROWS" ) == 0 )
@@ -448,32 +448,51 @@ lp_problem_t *read_mps_file(const char *filename)
448448 next_section = SEC_RANGES ;
449449 else if (strcmp (tokens [0 ], "BOUNDS" ) == 0 )
450450 next_section = SEC_BOUNDS ;
451- else if (strcmp (tokens [0 ], "OBJSENSE" ) == 0 )
451+ else if (strcmp (tokens [0 ], "OBJSENSE" ) == 0 || strcmp ( tokens [ 0 ], "OBJSENS" ) == 0 )
452452 next_section = SEC_OBJSENSE ;
453453 else if (strcmp (tokens [0 ], "ENDATA" ) == 0 )
454454 {
455455 next_section = SEC_ENDATA ;
456456 }
457457
458- if (current_section == SEC_ROWS && next_section != SEC_ROWS && !rows_finalized )
458+ bool inline_max = next_section == SEC_OBJSENSE && n_tokens >= 2 &&
459+ (strcmp (tokens [1 ], "MAX" ) == 0 || strcmp (tokens [1 ], "MAXIMIZE" ) == 0 );
460+ bool inline_min = next_section == SEC_OBJSENSE && n_tokens >= 2 &&
461+ (strcmp (tokens [1 ], "MIN" ) == 0 || strcmp (tokens [1 ], "MINIMIZE" ) == 0 );
462+ bool is_header = next_section != SEC_NONE && (n_tokens == 1 || inline_max || inline_min );
463+
464+ if (is_header )
459465 {
460- if (finalize_rows (& state ) != 0 )
461- state .error_flag = 1 ;
462- rows_finalized = true;
463- }
466+ if (current_section == SEC_ROWS && next_section != SEC_ROWS && !rows_finalized )
467+ {
468+ if (finalize_rows (& state ) != 0 )
469+ state .error_flag = 1 ;
470+ rows_finalized = true;
471+ }
464472
465- current_section = next_section ;
466- if (current_section == SEC_ENDATA )
467- break ;
468- continue ;
473+ current_section = next_section ;
474+ if (current_section == SEC_ENDATA )
475+ break ;
476+
477+ if (inline_max )
478+ state .objective_sense = OBJECTIVE_SENSE_MAXIMIZE ;
479+ else if (inline_min )
480+ state .objective_sense = OBJECTIVE_SENSE_MINIMIZE ;
481+
482+ continue ;
483+ }
469484 }
470485
471486 switch (current_section )
472487 {
473488 case SEC_OBJSENSE :
474- if (n_tokens > 0 && (strcmp (tokens [0 ], "MAX" ) == 0 || strcmp (tokens [0 ], "MAXIMIZE" ) == 0 ))
489+ if (strcmp (tokens [0 ], "MAX" ) == 0 || strcmp (tokens [0 ], "MAXIMIZE" ) == 0 )
490+ {
491+ state .objective_sense = OBJECTIVE_SENSE_MAXIMIZE ;
492+ }
493+ else if (strcmp (tokens [0 ], "MIN" ) == 0 || strcmp (tokens [0 ], "MINIMIZE" ) == 0 )
475494 {
476- state .is_maximize = true ;
495+ state .objective_sense = OBJECTIVE_SENSE_MINIMIZE ;
477496 }
478497 break ;
479498 case SEC_ROWS :
@@ -516,7 +535,8 @@ lp_problem_t *read_mps_file(const char *filename)
516535 prob -> num_variables = state .col_map .size ;
517536 prob -> num_constraints = state .row_map .size ;
518537 prob -> constraint_matrix_num_nonzeros = state .coo_matrix .nnz ;
519- prob -> objective_constant = state .is_maximize ? - state .objective_constant : state .objective_constant ;
538+ prob -> objective_constant = state .objective_constant ;
539+ prob -> objective_sense = state .objective_sense ;
520540
521541 prob -> objective_vector = state .objective_coeffs ;
522542 prob -> variable_lower_bound = state .var_lower_bounds ;
@@ -533,14 +553,6 @@ lp_problem_t *read_mps_file(const char *filename)
533553 state .constraint_lower_bounds = NULL ;
534554 state .constraint_upper_bounds = NULL ;
535555
536- if (state .is_maximize )
537- {
538- for (int i = 0 ; i < prob -> num_variables ; ++ i )
539- {
540- prob -> objective_vector [i ] *= -1.0 ;
541- }
542- }
543-
544556 if (mps_coo_to_csr (prob , & state .coo_matrix , prob -> num_constraints ) != 0 )
545557 {
546558 fprintf (stderr , "ERROR: Failed to convert matrix to CSR format.\n" );
0 commit comments