Skip to content

Commit 98670c5

Browse files
committed
Now saving the string corresponding to the double in Reader::readnexttoken and using the string if the double precedes a colon so is actually a constraint name
1 parent 9424c34 commit 98670c5

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

check/TestFilereader.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ TEST_CASE("handle-blank-space-names", "[highs_filereader]") {
491491
lp.a_matrix_.index_ = {0, 1, 0, 1};
492492
lp.a_matrix_.value_ = {1, 2, 2, 4};
493493
Highs h;
494+
h.setOptionValue("output_flag", dev_run);
494495
REQUIRE(h.passModel(lp) == HighsStatus::kOk);
495496
h.run();
496497
REQUIRE(h.writeSolution("", 1) == HighsStatus::kOk);
@@ -523,3 +524,33 @@ TEST_CASE("handle-blank-space-names", "[highs_filereader]") {
523524

524525
h.resetGlobalScheduler(true);
525526
}
527+
528+
TEST_CASE("read-highs-lp-file", "[highs_filereader]") {
529+
const std::string test_name = Catch::getResultCapture().getCurrentTestName();
530+
const std::string model_name0 = test_name + "-0.lp";
531+
const std::string model_name1 = test_name + "-1.lp";
532+
HighsLp lp;
533+
lp.num_col_ = 1;
534+
lp.num_row_ = 3;
535+
lp.col_cost_ = {8};
536+
lp.col_lower_ = {0};
537+
lp.col_upper_ = {inf};
538+
lp.row_lower_ = {-21, 31, 43};
539+
lp.row_upper_ = {inf, inf, inf};
540+
lp.a_matrix_.start_ = {0, 3};
541+
lp.a_matrix_.index_ = {0, 1, 2};
542+
lp.a_matrix_.value_ = {2, -3, 7};
543+
lp.col_names_ = {"col"};
544+
lp.row_names_ = {"row", "55", "9.9"};
545+
Highs h;
546+
// h.setOptionValue("output_flag", dev_run);
547+
REQUIRE(h.passModel(lp) == HighsStatus::kOk);
548+
REQUIRE(h.writeModel(model_name0) == HighsStatus::kOk);
549+
REQUIRE(h.readModel(model_name0) == HighsStatus::kOk);
550+
REQUIRE(h.writeModel(model_name1) == HighsStatus::kOk);
551+
552+
// std::remove(model_name0.c_str());
553+
// std::remove(model_name1.c_str());
554+
555+
h.resetGlobalScheduler(true);
556+
}

extern/filereaderlp/reader.cpp

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,19 @@ struct RawToken {
7171
type = RawTokenType::STR;
7272
return *this;
7373
}
74+
/*
7475
RawToken& operator=(const double v) {
7576
dvalue = v;
7677
type = RawTokenType::CONS;
7778
return *this;
7879
}
80+
*/
81+
RawToken& operator=(const std::pair<double, std::string> vs) {
82+
dvalue = vs.first;
83+
svalue = vs.second;
84+
type = RawTokenType::CONS;
85+
return *this;
86+
}
7987
};
8088

8189
enum class ProcessedTokenType {
@@ -875,7 +883,32 @@ void Reader::splittokens() {
875883

876884
void Reader::processtokens() {
877885
std::string svalue_lc;
886+
int pass_n = 0;
887+
const bool report_tokens = true;
888+
printf("RawTokenType::NONE = %d\n", int(RawTokenType::NONE));
889+
printf("RawTokenType::STR = %d\n", int(RawTokenType::STR));
890+
printf("RawTokenType::CONS = %d\n", int(RawTokenType::CONS));
878891
while (!rawtokens[0].istype(RawTokenType::FLEND)) {
892+
if (report_tokens) {
893+
pass_n++;
894+
if (pass_n == 9) {
895+
printf("pass_n = 9\n");
896+
}
897+
printf("\nPass %d\n", pass_n);
898+
/*
899+
if (rawtokens[2].dvalue == 55) {
900+
rawtokens[2].type = RawTokenType(1);
901+
rawtokens[2].dvalue = 0;
902+
rawtokens[2].svalue = "55";
903+
}
904+
*/
905+
for (int i = 0; i < NRAWTOKEN; i++) {
906+
printf("Token %d: type = %2d; dvalue = %d; svalue = %s\n", i,
907+
int(rawtokens[i].type),
908+
int(rawtokens[i].dvalue),
909+
rawtokens[i].svalue.c_str());
910+
}
911+
}
879912
fflush(stdout);
880913

881914
// Slash + asterisk: comment, skip everything up to next asterisk + slash
@@ -954,6 +987,17 @@ void Reader::processtokens() {
954987
continue;
955988
}
956989

990+
// constraint identifier - with numeric constant value as name?
991+
if (rawtokens[0].istype(RawTokenType::CONS) &&
992+
rawtokens[1].istype(RawTokenType::COLON)) {
993+
printf("rawtokens[0] - CONS; rawtokens[1] - COLON; rawtokens[0].dvalue = %g; rawtokens[0].svalue = %s\n", rawtokens[0].dvalue, rawtokens[0].svalue.c_str());
994+
// rawtokens[0].svalue = std::to_string(rawtokens[0].dvalue);
995+
processedtokens.emplace_back(ProcessedTokenType::CONID,
996+
rawtokens[0].svalue);
997+
nextrawtoken(2);
998+
continue;
999+
}
1000+
9571001
// check if free
9581002
if (rawtokens[0].istype(RawTokenType::STR) &&
9591003
iskeyword(svalue_lc, LP_KEYWORD_FREE, LP_KEYWORD_FREE_N)) {
@@ -1113,6 +1157,14 @@ void Reader::processtokens() {
11131157
// FILEEND should have been handled in condition of while()
11141158
assert(!rawtokens[0].istype(RawTokenType::FLEND));
11151159

1160+
if (report_tokens) {
1161+
for (int i = 0; i < NRAWTOKEN; i++) {
1162+
printf("Token %d: type = %2d; dvalue = %d; svalue = %s\n", i,
1163+
int(rawtokens[i].type),
1164+
int(rawtokens[i].dvalue),
1165+
rawtokens[i].svalue.c_str());
1166+
}
1167+
}
11161168
// catch all unknown symbols
11171169
lpassert(false);
11181170
break;
@@ -1184,7 +1236,12 @@ bool Reader::readnexttoken(RawToken& t) {
11841236

11851237
// check single character tokens
11861238
char nextchar = this->linebuffer[this->linebufferpos];
1187-
1239+
printf("Reader::readnexttoken: nextchar = %c\n", nextchar);
1240+
char char_1 = '1';
1241+
char char_r = 'r';
1242+
if (nextchar == char_1 || nextchar == char_r) {
1243+
printf("Reader::readnexttoken: nextchar = %c!\n", nextchar);
1244+
}
11881245
switch (nextchar) {
11891246
// check for comment
11901247
case '\\':
@@ -1280,7 +1337,13 @@ bool Reader::readnexttoken(RawToken& t) {
12801337
char* endptr;
12811338
double constant = strtod(startptr, &endptr);
12821339
if (endptr != startptr) {
1283-
t = constant;
1340+
// Extract the string corresponding to the double, in case the
1341+
// double is a constraint name
1342+
size_t double_len = endptr - startptr;
1343+
std::string double_name = this->linebuffer.substr(this->linebufferpos, double_len);
1344+
printf("double_len = %d; double_name = %s\n", int(double_len), double_name.c_str());
1345+
// t = constant;
1346+
t = std::make_pair(constant, double_name);
12841347
this->linebufferpos += endptr - startptr;
12851348
return true;
12861349
}

0 commit comments

Comments
 (0)