Skip to content

Commit ad27e11

Browse files
committed
Now setting HEkk::cost_scale_ from lp_.scale_.cost if lp_.is_scaled_
1 parent 5a670fa commit ad27e11

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

check/TestBasisSolves.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ TEST_CASE("scaling-kappa", "[highs_basis_solves]") {
639639
highs.run();
640640
*/
641641
}
642-
const bool lp_test = false;
642+
const bool lp_test = true;
643643
if (lp_test) {
644644
const double cost = 1e6;
645645
HighsLp lp;
@@ -659,5 +659,6 @@ TEST_CASE("scaling-kappa", "[highs_basis_solves]") {
659659
highs.setOptionValue("simplex_scale_strategy",
660660
kSimplexScaleStrategyMaxValueMatrixAndCost);
661661
highs.run();
662+
highs.writeSolution("", 1);
662663
}
663664
}

highs/lp_data/HighsLpUtils.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,20 @@ bool lpDimensionsOk(std::string message, const HighsLp& lp,
219219
bool legal_scale_row_size = false;
220220
bool legal_scale_col_size = false;
221221
if (lp.scale_.has_scaling) {
222-
legal_scale_num_col = lp.scale_.num_col == num_col;
223-
legal_scale_num_row = lp.scale_.num_row == num_row;
224-
legal_scale_row_size = scale_row_size >= num_row;
225-
legal_scale_col_size = scale_col_size >= num_col;
222+
if (scale_col_size) {
223+
legal_scale_num_col = lp.scale_.num_col == num_col;
224+
legal_scale_col_size = scale_col_size >= num_col;
225+
} else {
226+
legal_scale_num_col = lp.scale_.num_col == 0;
227+
legal_scale_col_size = true; // Since scale_col_size = 0
228+
}
229+
if (scale_row_size) {
230+
legal_scale_num_row = lp.scale_.num_row == num_row;
231+
legal_scale_row_size = scale_row_size >= num_row;
232+
} else {
233+
legal_scale_num_row = lp.scale_.num_row == 0;
234+
legal_scale_row_size = true; // Since scale_row_size = 0
235+
}
226236
} else {
227237
legal_scale_num_col = lp.scale_.num_col == 0;
228238
legal_scale_num_row = lp.scale_.num_row == 0;

highs/simplex/HEkk.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ void HEkk::moveLp(HighsLpSolverObject& solver_object) {
414414
// The simplex algorithm runs in the same space as the LP that has
415415
// just been moved in. This is a scaled space if the LP is scaled.
416416
this->simplex_in_scaled_space_ = this->lp_.is_scaled_;
417+
// Set the cost scale value so internal primal and dual objectives
418+
// are computed using the unscaled costs
419+
this->cost_scale_ = this->lp_.is_scaled_ ? this->lp_.scale_.cost : 1.0;
417420
//
418421
// Update other EKK pointers. Currently just pointers to the
419422
// HighsOptions and HighsTimer members of the Highs class that are
@@ -1297,16 +1300,21 @@ void HEkk::deleteRows(const HighsIndexCollection& index_collection) {
12971300

12981301
void HEkk::unscaleSimplex(const HighsLp& incumbent_lp) {
12991302
if (!this->simplex_in_scaled_space_) return;
1300-
assert(incumbent_lp.scale_.has_scaling);
1303+
const HighsScale& scale = incumbent_lp.scale_;
1304+
assert(scale.has_scaling);
1305+
const bool has_cost_scaling = scale.cost > 1.0;
1306+
const bool has_matrix_scaling = scale.col.size() && scale.row.size();
1307+
assert(has_cost_scaling || has_matrix_scaling);
1308+
assert(scale.cost);
13011309
const HighsInt num_col = incumbent_lp.num_col_;
13021310
const HighsInt num_row = incumbent_lp.num_row_;
1303-
const vector<double>& col_scale = incumbent_lp.scale_.col;
1304-
const vector<double>& row_scale = incumbent_lp.scale_.row;
1311+
const vector<double>& col_scale = scale.col;
1312+
const vector<double>& row_scale = scale.row;
13051313
for (HighsInt iCol = 0; iCol < num_col; iCol++) {
13061314
const HighsInt iVar = iCol;
1307-
const double factor = col_scale[iCol];
1308-
this->info_.workCost_[iVar] /= factor;
1309-
this->info_.workDual_[iVar] /= factor;
1315+
const double factor = has_matrix_scaling ? col_scale[iCol] : 1.0;
1316+
this->info_.workCost_[iVar] /= (factor / scale.cost);
1317+
this->info_.workDual_[iVar] /= (factor / scale.cost);
13101318
this->info_.workShift_[iVar] /= factor;
13111319
this->info_.workLower_[iVar] *= factor;
13121320
this->info_.workUpper_[iVar] *= factor;
@@ -1317,9 +1325,9 @@ void HEkk::unscaleSimplex(const HighsLp& incumbent_lp) {
13171325
}
13181326
for (HighsInt iRow = 0; iRow < num_row; iRow++) {
13191327
const HighsInt iVar = num_col + iRow;
1320-
const double factor = row_scale[iRow];
1321-
this->info_.workCost_[iVar] *= factor;
1322-
this->info_.workDual_[iVar] *= factor;
1328+
const double factor = has_matrix_scaling ? row_scale[iRow] : 1.0;
1329+
this->info_.workCost_[iVar] *= (factor * scale.cost);
1330+
this->info_.workDual_[iVar] *= (factor * scale.cost);
13231331
this->info_.workShift_[iVar] *= factor;
13241332
this->info_.workLower_[iVar] /= factor;
13251333
this->info_.workUpper_[iVar] /= factor;
@@ -1328,17 +1336,19 @@ void HEkk::unscaleSimplex(const HighsLp& incumbent_lp) {
13281336
this->info_.workLowerShift_[iVar] /= factor;
13291337
this->info_.workUpperShift_[iVar] /= factor;
13301338
}
1331-
for (HighsInt iRow = 0; iRow < num_row; iRow++) {
1332-
double factor;
1333-
const HighsInt iVar = this->basis_.basicIndex_[iRow];
1334-
if (iVar < num_col) {
1335-
factor = col_scale[iVar];
1336-
} else {
1337-
factor = 1.0 / row_scale[iVar - num_col];
1339+
if (has_matrix_scaling) {
1340+
for (HighsInt iRow = 0; iRow < num_row; iRow++) {
1341+
double factor;
1342+
const HighsInt iVar = this->basis_.basicIndex_[iRow];
1343+
if (iVar < num_col) {
1344+
factor = col_scale[iVar];
1345+
} else {
1346+
factor = 1.0 / row_scale[iVar - num_col];
1347+
}
1348+
this->info_.baseLower_[iRow] *= factor;
1349+
this->info_.baseUpper_[iRow] *= factor;
1350+
this->info_.baseValue_[iRow] *= factor;
13381351
}
1339-
this->info_.baseLower_[iRow] *= factor;
1340-
this->info_.baseUpper_[iRow] *= factor;
1341-
this->info_.baseValue_[iRow] *= factor;
13421352
}
13431353
this->simplex_in_scaled_space_ = false;
13441354
}

highs/simplex/HSimplex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ void simplexUnscaleSolution(HighsSolution& solution, const HighsScale& scale) {
10581058
assert(scale.has_scaling);
10591059
const bool has_cost_scaling = scale.cost > 1.0;
10601060
const bool has_matrix_scaling = scale.col.size() && scale.row.size();
1061+
assert(scale.cost);
10611062
assert(has_cost_scaling || has_matrix_scaling);
10621063
HighsInt num_col = solution.col_value.size();
10631064
HighsInt num_row = solution.row_value.size();

0 commit comments

Comments
 (0)