ERGO-Code / HiGHS

Linear optimization software
MIT License
957 stars 177 forks source link

Highs_getModel always returns kMinimize for ObjectiveSense #1712

Closed benjaminplong closed 6 months ago

benjaminplong commented 6 months ago

The method below is for getting the model from HiGHs. The sense output parameter is initialized to kMinimize and never updated. Calling Highs_getObjectiveSense returns the correct value.

HighsInt Highs_getModel(const void highs, const HighsInt a_format, const HighsInt q_format, HighsInt num_col, HighsInt num_row, HighsInt num_nz, HighsInt q_num_nz, HighsInt sense, double offset, double col_cost, double col_lower, double col_upper, double row_lower, double row_upper, HighsInt a_start, HighsInt a_index, double a_value, HighsInt q_start, HighsInt q_index, double qvalue, HighsInt integrality) { const HighsModel& model = ((Highs)highs)->getModel(); const HighsLp& lp = model.lp; const HighsHessian& hessian = model.hessian_; ObjSense obj_sense = ObjSense::kMinimize; // bug starts here sense = (HighsInt)obj_sense; offset = lp.offset_; num_col = lp.numcol; num_row = lp.numrow; if (num_col > 0) { memcpy(col_cost, lp.colcost.data(), num_col sizeof(double)); memcpy(col_lower, lp.collower.data(), num_col sizeof(double)); memcpy(col_upper, lp.colupper.data(), num_col sizeof(double)); } if (num_row > 0) { memcpy(row_lower, lp.rowlower.data(), num_row sizeof(double)); memcpy(row_upper, lp.rowupper.data(), num_row sizeof(double)); }

// Save the original orientation so that it is recovered MatrixFormat original_a_format = lp.amatrix.format_; // Determine the desired orientation and number of start entries to // be copied MatrixFormat desired_a_format = MatrixFormat::kColwise; HighsInt num_start_entries = num_col; if (a_format == (HighsInt)MatrixFormat::kRowwise) { desired_a_format = MatrixFormat::kRowwise; num_start_entries = num_row; } // Ensure the desired orientation HighsInt return_status; return_status = (HighsInt)((Highs*)highs)->setMatrixFormat(desired_a_format); if (return_status != kHighsStatusOk) return return_status;

if (num_col > 0 && num_row > 0) { num_nz = lp.amatrix.numNz(); memcpy(a_start, lp.amatrix.start_.data(), num_start_entries sizeof(HighsInt)); memcpy(a_index, lp.amatrix.index_.data(), num_nz sizeof(HighsInt)); memcpy(a_value, lp.amatrix.value_.data(), num_nz sizeof(double)); } if (hessian.dim_ > 0) { q_numnz = hessian.start[num_col]; memcpy(qstart, hessian.start.data(), num_col sizeof(HighsInt)); memcpy(qindex, hessian.index.data(), q_num_nz sizeof(HighsInt)); memcpy(qvalue, hessian.value.data(), q_num_nz sizeof(double)); } if ((HighsInt)lp.integrality_.size()) { for (int iCol = 0; iCol < numcol; iCol++) integrality[iCol] = (HighsInt)lp.integrality[iCol]; } // Restore the original orientation return_status = (HighsInt)((Highs)highs)->setMatrixFormat(original_a_format); if (return_status != kHighsStatusOk) return return_status; return kHighsStatusOk; }

jajhall commented 6 months ago

This refers to an old version of HiGHS: it's present in v1.5.3.

Although I don't remember fixing it, I can't see the equivalent error in v1.7.0

I'll add a unit test to TestCAPI.c just to make sure

benjaminplong commented 6 months ago

I just verified I am using version 1.6 so perhaps it was fixed for 1.7

jajhall commented 6 months ago

Closed by #1713