Skip to content

Commit 0347451

Browse files
committed
Now bypass row/col scaling in Simplex NLA when only costs need to be unscaled
1 parent 28245d6 commit 0347451

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

highs/lp_data/HighsLp.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,24 @@ void HighsLp::unapplyScale() {
282282
// Unapply the scaling to the bounds, costs and matrix, and record
283283
// that it has been unapplied
284284
assert(scale.cost);
285-
const double unscale_cost_mu = 1.0 / scale.cost;
286-
for (HighsInt iCol = 0; iCol < this->num_col_; iCol++) {
287-
this->col_lower_[iCol] *= scale.col[iCol];
288-
this->col_upper_[iCol] *= scale.col[iCol];
289-
this->col_cost_[iCol] /= (unscale_cost_mu * scale.col[iCol]);
290-
}
291-
for (HighsInt iRow = 0; iRow < this->num_row_; iRow++) {
292-
this->row_lower_[iRow] /= scale.row[iRow];
293-
this->row_upper_[iRow] /= scale.row[iRow];
285+
const bool has_cost_scaling = scale.cost > 1.0;
286+
const bool has_matrix_scaling = scale.col.size() && scale.row.size();
287+
assert(has_cost_scaling || has_matrix_scaling);
288+
if (has_matrix_scaling) {
289+
for (HighsInt iCol = 0; iCol < this->num_col_; iCol++) {
290+
this->col_lower_[iCol] *= scale.col[iCol];
291+
this->col_upper_[iCol] *= scale.col[iCol];
292+
this->col_cost_[iCol] /= (scale.col[iCol] / scale.cost);
293+
}
294+
for (HighsInt iRow = 0; iRow < this->num_row_; iRow++) {
295+
this->row_lower_[iRow] /= scale.row[iRow];
296+
this->row_upper_[iRow] /= scale.row[iRow];
297+
}
298+
this->a_matrix_.unapplyScale(scale);
299+
} else {
300+
for (HighsInt iCol = 0; iCol < this->num_col_; iCol++)
301+
this->col_cost_[iCol] *= scale.cost;
294302
}
295-
this->a_matrix_.unapplyScale(scale);
296303
this->is_scaled_ = false;
297304
}
298305

highs/simplex/HSimplex.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,18 +1056,31 @@ HighsStatus applyScalingToLpRow(HighsLp& lp, const HighsInt row,
10561056

10571057
void simplexUnscaleSolution(HighsSolution& solution, const HighsScale& scale) {
10581058
assert(scale.has_scaling);
1059-
assert(solution.col_value.size() == static_cast<size_t>(scale.num_col));
1060-
assert(solution.col_dual.size() == static_cast<size_t>(scale.num_col));
1061-
assert(solution.row_value.size() == static_cast<size_t>(scale.num_row));
1062-
assert(solution.row_dual.size() == static_cast<size_t>(scale.num_row));
1063-
1064-
for (HighsInt iCol = 0; iCol < scale.num_col; iCol++) {
1065-
solution.col_value[iCol] *= scale.col[iCol];
1066-
solution.col_dual[iCol] /= (scale.col[iCol] / scale.cost);
1067-
}
1068-
for (HighsInt iRow = 0; iRow < scale.num_row; iRow++) {
1069-
solution.row_value[iRow] /= scale.row[iRow];
1070-
solution.row_dual[iRow] *= (scale.row[iRow] * scale.cost);
1059+
const bool has_cost_scaling = scale.cost > 1.0;
1060+
const bool has_matrix_scaling = scale.col.size() && scale.row.size();
1061+
assert(has_cost_scaling || has_matrix_scaling);
1062+
HighsInt num_col = solution.col_value.size();
1063+
HighsInt num_row = solution.row_value.size();
1064+
if (has_matrix_scaling) {
1065+
assert(scale.num_col == num_col);
1066+
assert(scale.num_row == num_row);
1067+
assert(solution.col_value.size() == scale.col.size());
1068+
assert(solution.row_value.size() == scale.row.size());
1069+
assert(solution.col_dual.size() == scale.col.size());
1070+
assert(solution.row_dual.size() == scale.row.size());
1071+
for (HighsInt iCol = 0; iCol < num_col; iCol++) {
1072+
solution.col_value[iCol] *= scale.col[iCol];
1073+
solution.col_dual[iCol] /= (scale.col[iCol] / scale.cost);
1074+
}
1075+
for (HighsInt iRow = 0; iRow < num_row; iRow++) {
1076+
solution.row_value[iRow] /= scale.row[iRow];
1077+
solution.row_dual[iRow] *= (scale.row[iRow] * scale.cost);
1078+
}
1079+
} else {
1080+
for (HighsInt iCol = 0; iCol < num_col; iCol++)
1081+
solution.col_dual[iCol] *= scale.cost;
1082+
for (HighsInt iRow = 0; iRow < num_row; iRow++)
1083+
solution.row_dual[iRow] *= scale.cost;
10711084
}
10721085
}
10731086

@@ -1095,6 +1108,7 @@ void simplexScaleCost(const HighsOptions& options, HighsLp& lp) {
10951108
cost_scale = pow(2.0, floor(log(cost_scale) / log(2.0) + 0.5));
10961109
cost_scale = min(cost_scale, max_allowed_cost_scale);
10971110
}
1111+
assert(cost_scale >= 1.0);
10981112
if (cost_scale == 1.0) {
10991113
highsLogDev(options.log_options, HighsLogType::kInfo,
11001114
"LP cost vector not scaled down: max cost is %g\n",

0 commit comments

Comments
 (0)