Skip to content

Commit

Permalink
Standard form conversion now works for maximizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jajhall committed Oct 26, 2024
1 parent afe2f9a commit ed462ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 8 additions & 4 deletions check/TestLpSolvers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Highs.h"
#include "catch.hpp"

const bool dev_run = false;
const bool dev_run = true; // false;

struct IterationCount {
HighsInt simplex;
Expand Down Expand Up @@ -474,6 +474,7 @@ TEST_CASE("blending-lp-ipm", "[highs_lp_solver]") {
void testStandardForm(const HighsLp& lp) {
Highs highs;
highs.setOptionValue("output_flag", dev_run);
HighsInt sense = HighsInt(lp.sense_);
highs.passModel(lp);
highs.run();
// highs.writeSolution("", kSolutionStylePretty);
Expand Down Expand Up @@ -513,7 +514,8 @@ void testStandardForm(const HighsLp& lp) {
REQUIRE(highs.run() == HighsStatus::kOk);
REQUIRE(highs.getModelStatus() == HighsModelStatus::kOptimal);
highs.writeSolution("", kSolutionStylePretty);
double objective_function_value = highs.getInfo().objective_function_value;
double objective_function_value =
sense * highs.getInfo().objective_function_value;
double objective_difference =
std::fabs(objective_function_value - required_objective_function_value) /
std::max(1.0, std::fabs(required_objective_function_value));
Expand Down Expand Up @@ -567,7 +569,7 @@ TEST_CASE("standard-form-lp", "[highs_lp_solver]") {

std::vector<HighsInt> index;
std::vector<double> value;
// Add a fixed column and a fixed row
// Add a fixed column and a fixed row, and maximize
highs.passModel(lp);
index = {0, 1, 2};
value = {-1, 1, -1};
Expand All @@ -577,6 +579,8 @@ TEST_CASE("standard-form-lp", "[highs_lp_solver]") {
value = {-2, -1, 1, 3};
REQUIRE(highs.addRow(1.0, 1.0, 4, index.data(), value.data()) ==
HighsStatus::kOk);

REQUIRE(highs.changeObjectiveSense(ObjSense::kMaximize) == HighsStatus::kOk);
printf(
"\nNow test by adding a fixed column and a fixed row, and maximizing\n");
testStandardForm(highs.getLp());
}
14 changes: 8 additions & 6 deletions src/lp_data/HighsInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ HighsStatus Highs::formStandardFormLp() {
// boxed rows can be transformed to pairs of one-sided rows,
// requiring the standard form matrix to be row-wise. The original
// columns are assumed to come before any new columns, so their
// costs must be defined befor costs of new columns.
this->standard_form_cost_ = lp.col_cost_;
// costs (as a minimization) must be defined befor costs of new
// columns.
// Determine the objective scaling, and apply it to any offset
HighsInt sense = HighsInt(lp.sense_);
this->standard_form_offset_ = sense * lp.offset_;
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++)
this->standard_form_cost_.push_back(sense * lp.col_cost_[iCol]);
this->standard_form_matrix_.format_ = MatrixFormat::kRowwise;
this->standard_form_matrix_.num_col_ = lp.num_col_;
// Create a HighsSparseMatrix instance to store rows extracted from
Expand Down Expand Up @@ -143,12 +148,9 @@ HighsStatus Highs::formStandardFormLp() {
// can be added
matrix.ensureColwise();
this->standard_form_matrix_.ensureColwise();
// Determine the objective scaling, and apply it to any offset
double objective_mu = double(lp.sense_);
this->standard_form_offset_ = objective_mu * lp.offset_;
// Work through the columns, ensuring that all have non-negativity bounds
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
double cost = objective_mu * lp.col_cost_[iCol];
double cost = sense * lp.col_cost_[iCol];
double lower = lp.col_lower_[iCol];
double upper = lp.col_upper_[iCol];
if (lower > -kHighsInf) {
Expand Down

0 comments on commit ed462ed

Please sign in to comment.