Skip to content

Commit 9424c34

Browse files
committed
Solution and basis files read by variable name matching, using generic method getIndexFromName
1 parent efc864d commit 9424c34

File tree

11 files changed

+216
-240
lines changed

11 files changed

+216
-240
lines changed

check/TestCheckSolution.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,21 @@ TEST_CASE("read-lp-file-solution", "[highs_check_solution]") {
466466
const bool with_names = false;
467467
HighsLp lp;
468468
lp.num_col_ = 3;
469-
lp.num_row_ = 1;
469+
lp.num_row_ = 2;
470470
lp.col_cost_ = {0, 1, 1};
471471
lp.col_lower_ = {0, 10, 0};
472472
lp.col_upper_ = {kHighsInf, kHighsInf, kHighsInf};
473473
if (with_names) lp.col_names_ = {"x", "y", "z"};
474-
lp.row_lower_ = {1};
475-
lp.row_upper_ = {2};
476-
if (with_names) lp.row_names_ = {"r"};
477-
lp.a_matrix_.start_ = {0, 1, 1, 2};
478-
lp.a_matrix_.index_ = {0, 0};
479-
lp.a_matrix_.value_ = {1, 1};
480-
lp.integrality_ = {HighsVarType::kContinuous, HighsVarType::kContinuous, HighsVarType::kInteger};
474+
lp.row_lower_ = {1, -kHighsInf};
475+
lp.row_upper_ = {kHighsInf, 2};
476+
if (with_names) lp.row_names_ = {"r-lo", "r-up"};
477+
lp.a_matrix_.start_ = {0, 2, 2, 4};
478+
lp.a_matrix_.index_ = {0, 1, 0, 1};
479+
lp.a_matrix_.value_ = {1, 1, 1, 1};
480+
lp.integrality_ = {HighsVarType::kContinuous, HighsVarType::kContinuous,
481+
HighsVarType::kInteger};
481482
Highs h;
482-
// h.setOptionValue("output_flag", dev_run);
483+
// h.setOptionValue("output_flag", dev_run);
483484
REQUIRE(h.passModel(lp) == HighsStatus::kOk);
484485
h.run();
485486
h.writeModel(model_file_name);
@@ -489,10 +490,10 @@ TEST_CASE("read-lp-file-solution", "[highs_check_solution]") {
489490
h.writeModel("");
490491
h.readSolution(solution_file_name);
491492
h.run();
492-
493+
493494
std::remove(model_file_name.c_str());
494495
std::remove(solution_file_name.c_str());
495-
496+
496497
h.resetGlobalScheduler(true);
497498
}
498499

@@ -515,7 +516,7 @@ TEST_CASE("read-lp-file-basis", "[highs_check_solution]") {
515516
lp.a_matrix_.index_ = {0, 1, 0, 1};
516517
lp.a_matrix_.value_ = {1, 1, 1, 1};
517518
Highs h;
518-
// h.setOptionValue("output_flag", dev_run);
519+
h.setOptionValue("output_flag", dev_run);
519520
REQUIRE(h.passModel(lp) == HighsStatus::kOk);
520521
h.run();
521522
// Optimally x - basic; y - lower; z - lower
@@ -532,11 +533,11 @@ TEST_CASE("read-lp-file-basis", "[highs_check_solution]") {
532533
// original ordering with new ordering. Not optimal - in fact basis
533534
// matrix B = [0] is singular!
534535
h.run();
535-
// REQUIRE(h.getInfo().simplex_iteration_count == 0);
536-
536+
REQUIRE(h.getInfo().simplex_iteration_count == 0);
537+
537538
std::remove(model_file_name.c_str());
538-
// std::remove(basis_file_name.c_str());
539-
539+
std::remove(basis_file_name.c_str());
540+
540541
h.resetGlobalScheduler(true);
541542
}
542543

check/TestRays.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "catch.hpp"
55
#include "lp_data/HConst.h"
66

7-
const bool dev_run = false;//true; //
7+
const bool dev_run = false; // true; //
88
const double zero_ray_value_tolerance = 1e-14;
99

1010
void reportRay(std::string message, HighsInt dim, double* computed,

highs/io/FilereaderLp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
299299
if (iCol != iRow) coef *= 2;
300300
if (coef != 0.0) {
301301
this->writeToFileValue(file, coef);
302-
this->writeToFileVar(file, lp.col_names_[iCol]);
303-
this->writeToFile(file, " *");
304-
this->writeToFileVar(file, lp.col_names_[iRow]);
302+
this->writeToFileVar(file, lp.col_names_[iCol]);
303+
this->writeToFile(file, " *");
304+
this->writeToFileVar(file, lp.col_names_[iRow]);
305305
}
306306
}
307307
}
@@ -333,7 +333,7 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
333333
lp.row_lower_[iRow] > -kHighsInf && lp.row_upper_[iRow] < kHighsInf;
334334
if (lp.row_lower_[iRow] > -kHighsInf) {
335335
// Has a lower bound
336-
this->writeToFileVar(file, lp.row_names_[iRow]);
336+
this->writeToFileVar(file, lp.row_names_[iRow]);
337337
if (boxed) {
338338
this->writeToFile(file, "lo:");
339339
} else {
@@ -346,7 +346,7 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
346346
}
347347
if (lp.row_upper_[iRow] < kHighsInf) {
348348
// Has an upper bound
349-
this->writeToFileVar(file, lp.row_names_[iRow]);
349+
this->writeToFileVar(file, lp.row_names_[iRow]);
350350
if (boxed) {
351351
this->writeToFile(file, "up:");
352352
} else {
@@ -400,7 +400,7 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
400400
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
401401
if (lp.integrality_[iCol] == HighsVarType::kInteger) {
402402
if (lp.col_lower_[iCol] == 0.0 && lp.col_upper_[iCol] == 1.0) {
403-
this->writeToFileVar(file, lp.col_names_[iCol]);
403+
this->writeToFileVar(file, lp.col_names_[iCol]);
404404
this->writeToFileLineEnd(file);
405405
}
406406
}
@@ -412,7 +412,7 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
412412
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
413413
if (lp.integrality_[iCol] == HighsVarType::kInteger) {
414414
if (lp.col_lower_[iCol] != 0.0 || lp.col_upper_[iCol] != 1.0) {
415-
this->writeToFileVar(file, lp.col_names_[iCol]);
415+
this->writeToFileVar(file, lp.col_names_[iCol]);
416416
this->writeToFileLineEnd(file);
417417
}
418418
}
@@ -424,7 +424,7 @@ HighsStatus FilereaderLp::writeModelToFile(const HighsOptions& options,
424424
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
425425
if (lp.integrality_[iCol] == HighsVarType::kSemiContinuous ||
426426
lp.integrality_[iCol] == HighsVarType::kSemiInteger) {
427-
this->writeToFileVar(file, lp.col_names_[iCol]);
427+
this->writeToFileVar(file, lp.col_names_[iCol]);
428428
this->writeToFileLineEnd(file);
429429
}
430430
}

highs/io/HMPSIO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ HighsStatus writeMps(
617617
}
618618
highsLogDev(log_options, HighsLogType::kInfo, "writeMPS: Opened file OK\n");
619619
// Check that the names are no longer than 8 characters for fixed format write
620-
HighsInt max_name_length = std::max(maxNameLength(col_names),
621-
maxNameLength(row_names));
620+
HighsInt max_name_length =
621+
std::max(maxNameLength(col_names), maxNameLength(row_names));
622622
if (!use_free_format && max_name_length > 8) {
623623
highsLogUser(
624624
log_options, HighsLogType::kError,

highs/lp_data/HConst.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ enum BasisValidity {
144144
kBasisValidityMax = kBasisValidityValid
145145
};
146146

147-
const std::string kHighsBasisFileV1 = "v1"; // Deprecated
147+
const std::string kHighsBasisFileV1 = "v1"; // Deprecated
148148
const std::string kHighsBasisFileV2 = "v2";
149149

150150
enum SolutionStyle {

highs/lp_data/Highs.cpp

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ HighsStatus Highs::readBasis(const std::string& filename) {
715715
HighsBasis read_basis = basis_;
716716
return_status = interpretCallStatus(
717717
options_.log_options,
718-
readBasisFile(options_.log_options, model_.lp_, read_basis, filename), return_status,
719-
"readBasis");
718+
readBasisFile(options_.log_options, model_.lp_, read_basis, filename),
719+
return_status, "readBasis");
720720
if (return_status != HighsStatus::kOk) return return_status;
721721
// Basis read OK: check whether it's consistent with the LP
722722
if (!isBasisConsistent(model_.lp_, read_basis)) {
@@ -3081,20 +3081,10 @@ HighsStatus Highs::getColByName(const std::string& name, HighsInt& col) {
30813081
HighsLp& lp = model_.lp_;
30823082
if (!lp.col_names_.size()) return HighsStatus::kError;
30833083
if (!lp.col_hash_.name2index.size()) lp.col_hash_.form(lp.col_names_);
3084-
auto search = lp.col_hash_.name2index.find(name);
3085-
if (search == lp.col_hash_.name2index.end()) {
3086-
highsLogUser(options_.log_options, HighsLogType::kError,
3087-
"Highs::getColByName: name %s is not found\n", name.c_str());
3088-
return HighsStatus::kError;
3089-
}
3090-
if (search->second == kHashIsDuplicate) {
3091-
highsLogUser(options_.log_options, HighsLogType::kError,
3092-
"Highs::getColByName: name %s is duplicated\n", name.c_str());
3093-
return HighsStatus::kError;
3094-
}
3095-
col = search->second;
3096-
assert(lp.col_names_[col] == name);
3097-
return HighsStatus::kOk;
3084+
std::string from_method = "Highs::getColByName";
3085+
const bool is_column = true;
3086+
return getIndexFromName(options_.log_options, from_method, is_column, name,
3087+
lp.col_hash_.name2index, col, lp.col_names_);
30983088
}
30993089

31003090
HighsStatus Highs::getColIntegrality(const HighsInt col,
@@ -3196,20 +3186,10 @@ HighsStatus Highs::getRowByName(const std::string& name, HighsInt& row) {
31963186
HighsLp& lp = model_.lp_;
31973187
if (!lp.row_names_.size()) return HighsStatus::kError;
31983188
if (!lp.row_hash_.name2index.size()) lp.row_hash_.form(lp.row_names_);
3199-
auto search = lp.row_hash_.name2index.find(name);
3200-
if (search == lp.row_hash_.name2index.end()) {
3201-
highsLogUser(options_.log_options, HighsLogType::kError,
3202-
"Highs::getRowByName: name %s is not found\n", name.c_str());
3203-
return HighsStatus::kError;
3204-
}
3205-
if (search->second == kHashIsDuplicate) {
3206-
highsLogUser(options_.log_options, HighsLogType::kError,
3207-
"Highs::getRowByName: name %s is duplicated\n", name.c_str());
3208-
return HighsStatus::kError;
3209-
}
3210-
row = search->second;
3211-
assert(lp.row_names_[row] == name);
3212-
return HighsStatus::kOk;
3189+
std::string from_method = "Highs::getRowByName";
3190+
const bool is_column = false;
3191+
return getIndexFromName(options_.log_options, from_method, is_column, name,
3192+
lp.row_hash_.name2index, row, lp.row_names_);
32133193
}
32143194

32153195
HighsStatus Highs::getCoeff(const HighsInt row, const HighsInt col,

highs/lp_data/HighsLp.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ void HighsLp::exactResize() {
164164
}
165165

166166
bool HighsLp::okNames() const {
167-
if (this->col_names_.size() != static_cast<size_t>(this->num_col_)) return false;
168-
if (this->row_names_.size() != static_cast<size_t>(this->num_row_)) return false;
167+
if (this->col_names_.size() != static_cast<size_t>(this->num_col_))
168+
return false;
169+
if (this->row_names_.size() != static_cast<size_t>(this->num_row_))
170+
return false;
169171
// Look for names that are empty or have spaces
170172
for (HighsInt iCol = 0; iCol < this->num_col_; iCol++) {
171173
const std::string& name = this->col_names_[iCol];
@@ -352,7 +354,8 @@ void HighsLp::addColNames(const std::string name, const HighsInt num_new_col) {
352354
this->col_hash_.form(this->col_names_);
353355
// Handle the addition of user-defined names later
354356
assert(name == "");
355-
if (this->col_name_prefix_ == "") col_name_prefix_ = kHighsUniqueColNamePrefix;
357+
if (this->col_name_prefix_ == "")
358+
col_name_prefix_ = kHighsUniqueColNamePrefix;
356359
for (HighsInt iCol = this->num_col_; iCol < this->num_col_ + num_new_col;
357360
iCol++) {
358361
const std::string col_name =
@@ -397,7 +400,8 @@ void HighsLp::addRowNames(const std::string name, const HighsInt num_new_row) {
397400
this->row_hash_.form(this->row_names_);
398401
// Handle the addition of user-defined names later
399402
assert(name == "");
400-
if (this->row_name_prefix_ == "") row_name_prefix_ = kHighsUniquerowNamePrefix;
403+
if (this->row_name_prefix_ == "")
404+
row_name_prefix_ = kHighsUniquerowNamePrefix;
401405
for (HighsInt iRow = this->num_row_; iRow < this->num_row_ + num_new_row;
402406
iRow++) {
403407
const std::string row_name =

0 commit comments

Comments
 (0)