| 1 |
#include "reader.hpp" |
| 2 |
|
| 3 |
#include <algorithm> |
| 4 |
#include <array> |
| 5 |
#include <cassert> |
| 6 |
#include <cctype> |
| 7 |
#include <cstring> |
| 8 |
#include <fstream> |
| 9 |
#include <iostream> |
| 10 |
#include <limits> |
| 11 |
#include <map> |
| 12 |
#include <memory> |
| 13 |
#include <unordered_map> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "HConfig.h" // for ZLIB_FOUND |
| 17 |
#include "builder.hpp" |
| 18 |
#include "def.hpp" |
| 19 |
#ifdef ZLIB_FOUND |
| 20 |
#include "../extern/zstr/zstr.hpp" |
| 21 |
#endif |
| 22 |
|
| 23 |
// Cygwin doesn't come with an implementation for strdup if compiled with |
| 24 |
// std=cxx |
| 25 |
#ifdef __CYGWIN__ |
| 26 |
#include <cstdlib> |
| 27 |
char* strdup(const char* s) { |
| 28 |
size_t slen = strlen(s); |
| 29 |
char* result = (char*)malloc(slen + 1); |
| 30 |
if (result == NULL) { |
| 31 |
return NULL; |
| 32 |
} |
| 33 |
|
| 34 |
memcpy(result, s, slen + 1); |
| 35 |
return result; |
| 36 |
} |
| 37 |
#endif |
| 38 |
|
| 39 |
enum class RawTokenType { |
| 40 |
NONE, |
| 41 |
STR, |
| 42 |
CONS, |
| 43 |
LESS, |
| 44 |
GREATER, |
| 45 |
EQUAL, |
| 46 |
COLON, |
| 47 |
LNEND, |
| 48 |
FLEND, |
| 49 |
BRKOP, |
| 50 |
BRKCL, |
| 51 |
PLUS, |
| 52 |
MINUS, |
| 53 |
HAT, |
| 54 |
SLASH, |
| 55 |
ASTERISK |
| 56 |
}; |
| 57 |
|
| 58 |
struct RawToken { |
| 59 |
RawTokenType type = RawTokenType::NONE; |
| 60 |
std::string svalue; |
| 61 |
double dvalue = 0.0; |
| 62 |
|
| 63 |
inline bool istype(RawTokenType t) const { return this->type == t; } |
| 64 |
|
| 65 |
RawToken& operator=(RawTokenType t) { |
| 66 |
type = t; |
| 67 |
return *this; |
| 68 |
} |
| 69 |
RawToken& operator=(const std::string& v) { |
| 70 |
svalue = v; |
| 71 |
type = RawTokenType::STR; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
RawToken& operator=(const std::pair<double, std::string> vs) { |
| 75 |
dvalue = vs.first; |
| 76 |
svalue = vs.second; |
| 77 |
type = RawTokenType::CONS; |
| 78 |
return *this; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
enum class ProcessedTokenType { |
| 83 |
NONE, |
| 84 |
SECID, |
| 85 |
VARID, |
| 86 |
CONID, |
| 87 |
CONST, |
| 88 |
FREE, |
| 89 |
BRKOP, |
| 90 |
BRKCL, |
| 91 |
COMP, |
| 92 |
LNEND, |
| 93 |
SLASH, |
| 94 |
ASTERISK, |
| 95 |
HAT, |
| 96 |
SOSTYPE |
| 97 |
}; |
| 98 |
|
| 99 |
enum class LpSectionKeyword { |
| 100 |
NONE, |
| 101 |
OBJMIN, |
| 102 |
OBJMAX, |
| 103 |
CON, |
| 104 |
BOUNDS, |
| 105 |
GEN, |
| 106 |
BIN, |
| 107 |
SEMI, |
| 108 |
SOS, |
| 109 |
END |
| 110 |
}; |
| 111 |
|
| 112 |
static const std::unordered_map<std::string, LpSectionKeyword> |
| 113 |
sectionkeywordmap{{"minimize", LpSectionKeyword::OBJMIN}, |
| 114 |
{"min", LpSectionKeyword::OBJMIN}, |
| 115 |
{"minimum", LpSectionKeyword::OBJMIN}, |
| 116 |
{"maximize", LpSectionKeyword::OBJMAX}, |
| 117 |
{"max", LpSectionKeyword::OBJMAX}, |
| 118 |
{"maximum", LpSectionKeyword::OBJMAX}, |
| 119 |
{"subject to", LpSectionKeyword::CON}, |
| 120 |
{"such that", LpSectionKeyword::CON}, |
| 121 |
{"st", LpSectionKeyword::CON}, |
| 122 |
{"s.t.", LpSectionKeyword::CON}, |
| 123 |
{"bounds", LpSectionKeyword::BOUNDS}, |
| 124 |
{"bound", LpSectionKeyword::BOUNDS}, |
| 125 |
{"binary", LpSectionKeyword::BIN}, |
| 126 |
{"binaries", LpSectionKeyword::BIN}, |
| 127 |
{"bin", LpSectionKeyword::BIN}, |
| 128 |
{"general", LpSectionKeyword::GEN}, |
| 129 |
{"generals", LpSectionKeyword::GEN}, |
| 130 |
{"gen", LpSectionKeyword::GEN}, |
| 131 |
{"integer", LpSectionKeyword::GEN}, |
| 132 |
{"integers", LpSectionKeyword::GEN}, |
| 133 |
{"semi-continuous", LpSectionKeyword::SEMI}, |
| 134 |
{"semi", LpSectionKeyword::SEMI}, |
| 135 |
{"semis", LpSectionKeyword::SEMI}, |
| 136 |
{"sos", LpSectionKeyword::SOS}, |
| 137 |
{"end", LpSectionKeyword::END}}; |
| 138 |
|
| 139 |
enum class SosType { SOS1, SOS2 }; |
| 140 |
|
| 141 |
enum class LpComparisonType { LEQ, L, EQ, G, GEQ }; |
| 142 |
|
| 143 |
struct ProcessedToken { |
| 144 |
ProcessedTokenType type; |
| 145 |
union { |
| 146 |
LpSectionKeyword keyword; |
| 147 |
SosType sostype; |
| 148 |
char* name; |
| 149 |
double value; |
| 150 |
LpComparisonType dir; |
| 151 |
}; |
| 152 |
|
| 153 |
ProcessedToken(const ProcessedToken&) = delete; |
| 154 |
ProcessedToken(ProcessedToken&& t) : type(t.type) { |
| 155 |
switch (type) { |
| 156 |
case ProcessedTokenType::SECID: |
| 157 |
keyword = t.keyword; |
| 158 |
break; |
| 159 |
case ProcessedTokenType::SOSTYPE: |
| 160 |
sostype = t.sostype; |
| 161 |
break; |
| 162 |
case ProcessedTokenType::CONID: |
| 163 |
case ProcessedTokenType::VARID: |
| 164 |
name = t.name; |
| 165 |
break; |
| 166 |
case ProcessedTokenType::CONST: |
| 167 |
value = t.value; |
| 168 |
break; |
| 169 |
case ProcessedTokenType::COMP: |
| 170 |
dir = t.dir; |
| 171 |
break; |
| 172 |
default:; |
| 173 |
} |
| 174 |
t.type = ProcessedTokenType::NONE; |
| 175 |
} |
| 176 |
|
| 177 |
ProcessedToken(ProcessedTokenType t) : type(t){}; |
| 178 |
|
| 179 |
ProcessedToken(LpSectionKeyword kw) |
| 180 |
: type(ProcessedTokenType::SECID), keyword(kw){}; |
| 181 |
|
| 182 |
ProcessedToken(SosType sos) |
| 183 |
: type(ProcessedTokenType::SOSTYPE), sostype(sos){}; |
| 184 |
|
| 185 |
ProcessedToken(ProcessedTokenType t, const std::string& s) : type(t) { |
| 186 |
assert(t == ProcessedTokenType::CONID || t == ProcessedTokenType::VARID); |
| 187 |
#ifndef _WIN32 |
| 188 |
name = strdup(s.c_str()); |
| 189 |
#else |
| 190 |
name = _strdup(s.c_str()); |
| 191 |
#endif |
| 192 |
}; |
| 193 |
|
| 194 |
ProcessedToken(double v) : type(ProcessedTokenType::CONST), value(v){}; |
| 195 |
|
| 196 |
ProcessedToken(LpComparisonType comp) |
| 197 |
: type(ProcessedTokenType::COMP), dir(comp){}; |
| 198 |
|
| 199 |
~ProcessedToken() { |
| 200 |
if (type == ProcessedTokenType::CONID || type == ProcessedTokenType::VARID) |
| 201 |
free(name); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
// how many raw tokens to cache |
| 206 |
// set to how many tokens we may need to look ahead |
| 207 |
#define NRAWTOKEN 3 |
| 208 |
|
| 209 |
const double kHighsInf = std::numeric_limits<double>::infinity(); |
| 210 |
class Reader { |
| 211 |
private: |
| 212 |
#ifdef ZLIB_FOUND |
| 213 |
zstr::ifstream file; |
| 214 |
#else |
| 215 |
std::ifstream file; |
| 216 |
#endif |
| 217 |
std::string linebuffer; |
| 218 |
std::size_t linebufferpos; |
| 219 |
std::array<RawToken, NRAWTOKEN> rawtokens; |
| 220 |
std::vector<ProcessedToken> processedtokens; |
| 221 |
// store for each section a pointer to its begin and end (pointer to element |
| 222 |
// after last) |
| 223 |
std::map<LpSectionKeyword, std::pair<std::vector<ProcessedToken>::iterator, |
| 224 |
std::vector<ProcessedToken>::iterator> > |
| 225 |
sectiontokens; |
| 226 |
|
| 227 |
Builder builder; |
| 228 |
|
| 229 |
bool readnexttoken(RawToken&); |
| 230 |
void nextrawtoken(size_t howmany = 1); |
| 231 |
void processtokens(); |
| 232 |
void splittokens(); |
| 233 |
void processsections(); |
| 234 |
void processnonesec(); |
| 235 |
void processobjsec(); |
| 236 |
void processconsec(); |
| 237 |
void processboundssec(); |
| 238 |
void processbinsec(); |
| 239 |
void processgensec(); |
| 240 |
void processsemisec(); |
| 241 |
void processsossec(); |
| 242 |
void processendsec(); |
| 243 |
void parseexpression(std::vector<ProcessedToken>::iterator& it, |
| 244 |
std::vector<ProcessedToken>::iterator end, |
| 245 |
std::shared_ptr<Expression> expr, bool isobj); |
| 246 |
|
| 247 |
public: |
| 248 |
Reader(std::string filename) { |
| 249 |
#ifdef ZLIB_FOUND |
| 250 |
try { |
| 251 |
file.open(filename); |
| 252 |
} catch (const strict_fstream::Exception& e) { |
| 253 |
} |
| 254 |
#else |
| 255 |
file.open(filename); |
| 256 |
#endif |
| 257 |
lpassert(file.is_open()); |
| 258 |
}; |
| 259 |
|
| 260 |
~Reader() { file.close(); } |
| 261 |
|
| 262 |
Model read(); |
| 263 |
}; |
| 264 |
|
| 265 |
Model readinstance(std::string filename) { |
| 266 |
Reader reader(filename); |
| 267 |
return reader.read(); |
| 268 |
} |
| 269 |
|
| 270 |
// convert string to lower-case, modifies string |
| 271 |
static inline void tolower(std::string& s) { |
| 272 |
std::transform(s.begin(), s.end(), s.begin(), |
| 273 |
[](unsigned char c) { return std::tolower(c); }); |
| 274 |
} |
| 275 |
|
| 276 |
static inline bool iskeyword(const std::string& str, |
| 277 |
const std::string* keywords, const int nkeywords) { |
| 278 |
for (int i = 0; i < nkeywords; i++) { |
| 279 |
if (str == keywords[i]) { |
| 280 |
return true; |
| 281 |
} |
| 282 |
} |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
static inline LpSectionKeyword parsesectionkeyword(const std::string& str) { |
| 287 |
// look up lower case |
| 288 |
auto it(sectionkeywordmap.find(str)); |
| 289 |
if (it != sectionkeywordmap.end()) return it->second; |
| 290 |
|
| 291 |
return LpSectionKeyword::NONE; |
| 292 |
} |
| 293 |
|
| 294 |
Model Reader::read() { |
| 295 |
// std::clog << "Reading input, tokenizing..." << std::endl; |
| 296 |
this->linebufferpos = 0; |
| 297 |
// read first NRAWTOKEN token |
| 298 |
// if file ends early, then all remaining tokens are set to FLEND |
| 299 |
for (size_t i = 0; i < NRAWTOKEN; ++i) |
| 300 |
while (!readnexttoken(rawtokens[i])) |
| 301 |
; |
| 302 |
|
| 303 |
processtokens(); |
| 304 |
|
| 305 |
linebuffer.clear(); |
| 306 |
linebuffer.shrink_to_fit(); |
| 307 |
|
| 308 |
// std::clog << "Splitting tokens..." << std::endl; |
| 309 |
splittokens(); |
| 310 |
|
| 311 |
// std::clog << "Setting up model..." << std::endl; |
| 312 |
processsections(); |
| 313 |
processedtokens.clear(); |
| 314 |
processedtokens.shrink_to_fit(); |
| 315 |
|
| 316 |
return builder.model; |
| 317 |
} |
| 318 |
|
| 319 |
void Reader::processnonesec() { |
| 320 |
lpassert(sectiontokens.count(LpSectionKeyword::NONE) == 0); |
| 321 |
} |
| 322 |
|
| 323 |
void Reader::parseexpression(std::vector<ProcessedToken>::iterator& it, |
| 324 |
std::vector<ProcessedToken>::iterator end, |
| 325 |
std::shared_ptr<Expression> expr, bool isobj) { |
| 326 |
if (it != end && it->type == ProcessedTokenType::CONID) { |
| 327 |
expr->name = it->name; |
| 328 |
++it; |
| 329 |
} |
| 330 |
|
| 331 |
while (it != end) { |
| 332 |
std::vector<ProcessedToken>::iterator next = it; |
| 333 |
++next; |
| 334 |
// const var |
| 335 |
if (next != end && it->type == ProcessedTokenType::CONST && |
| 336 |
next->type == ProcessedTokenType::VARID) { |
| 337 |
std::string name = next->name; |
| 338 |
|
| 339 |
std::shared_ptr<LinTerm> linterm = |
| 340 |
std::shared_ptr<LinTerm>(new LinTerm()); |
| 341 |
linterm->coef = it->value; |
| 342 |
linterm->var = builder.getvarbyname(name); |
| 343 |
expr->linterms.push_back(linterm); |
| 344 |
|
| 345 |
++it; |
| 346 |
++it; |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
// const |
| 351 |
if (it->type == ProcessedTokenType::CONST) { |
| 352 |
expr->offset += it->value; |
| 353 |
++it; |
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
// var |
| 358 |
if (it->type == ProcessedTokenType::VARID) { |
| 359 |
std::string name = it->name; |
| 360 |
|
| 361 |
std::shared_ptr<LinTerm> linterm = |
| 362 |
std::shared_ptr<LinTerm>(new LinTerm()); |
| 363 |
linterm->coef = 1.0; |
| 364 |
linterm->var = builder.getvarbyname(name); |
| 365 |
expr->linterms.push_back(linterm); |
| 366 |
|
| 367 |
++it; |
| 368 |
continue; |
| 369 |
} |
| 370 |
|
| 371 |
// quadratic expression |
| 372 |
if (next != end && it->type == ProcessedTokenType::BRKOP) { |
| 373 |
++it; |
| 374 |
while (it != end && it->type != ProcessedTokenType::BRKCL) { |
| 375 |
// const var hat const |
| 376 |
std::vector<ProcessedToken>::iterator next1 = it; // token after it |
| 377 |
std::vector<ProcessedToken>::iterator next2 = it; // token 2nd-after it |
| 378 |
std::vector<ProcessedToken>::iterator next3 = it; // token 3rd-after it |
| 379 |
++next1; |
| 380 |
++next2; |
| 381 |
++next3; |
| 382 |
if (next1 != end) { |
| 383 |
++next2; |
| 384 |
++next3; |
| 385 |
} |
| 386 |
if (next2 != end) ++next3; |
| 387 |
|
| 388 |
if (next3 != end && it->type == ProcessedTokenType::CONST && |
| 389 |
next1->type == ProcessedTokenType::VARID && |
| 390 |
next2->type == ProcessedTokenType::HAT && |
| 391 |
next3->type == ProcessedTokenType::CONST) { |
| 392 |
std::string name = next1->name; |
| 393 |
|
| 394 |
lpassert(next3->value == 2.0); |
| 395 |
|
| 396 |
std::shared_ptr<QuadTerm> quadterm = |
| 397 |
std::shared_ptr<QuadTerm>(new QuadTerm()); |
| 398 |
quadterm->coef = it->value; |
| 399 |
quadterm->var1 = builder.getvarbyname(name); |
| 400 |
quadterm->var2 = builder.getvarbyname(name); |
| 401 |
expr->quadterms.push_back(quadterm); |
| 402 |
|
| 403 |
it = ++next3; |
| 404 |
continue; |
| 405 |
} |
| 406 |
|
| 407 |
// var hat const |
| 408 |
if (next2 != end && it->type == ProcessedTokenType::VARID && |
| 409 |
next1->type == ProcessedTokenType::HAT && |
| 410 |
next2->type == ProcessedTokenType::CONST) { |
| 411 |
std::string name = it->name; |
| 412 |
|
| 413 |
lpassert(next2->value == 2.0); |
| 414 |
|
| 415 |
std::shared_ptr<QuadTerm> quadterm = |
| 416 |
std::shared_ptr<QuadTerm>(new QuadTerm()); |
| 417 |
quadterm->coef = 1.0; |
| 418 |
quadterm->var1 = builder.getvarbyname(name); |
| 419 |
quadterm->var2 = builder.getvarbyname(name); |
| 420 |
expr->quadterms.push_back(quadterm); |
| 421 |
|
| 422 |
it = next3; |
| 423 |
continue; |
| 424 |
} |
| 425 |
|
| 426 |
// const var asterisk var |
| 427 |
if (next3 != end && it->type == ProcessedTokenType::CONST && |
| 428 |
next1->type == ProcessedTokenType::VARID && |
| 429 |
next2->type == ProcessedTokenType::ASTERISK && |
| 430 |
next3->type == ProcessedTokenType::VARID) { |
| 431 |
std::string name1 = next1->name; |
| 432 |
std::string name2 = next3->name; |
| 433 |
|
| 434 |
std::shared_ptr<QuadTerm> quadterm = |
| 435 |
std::shared_ptr<QuadTerm>(new QuadTerm()); |
| 436 |
quadterm->coef = it->value; |
| 437 |
quadterm->var1 = builder.getvarbyname(name1); |
| 438 |
quadterm->var2 = builder.getvarbyname(name2); |
| 439 |
expr->quadterms.push_back(quadterm); |
| 440 |
|
| 441 |
it = ++next3; |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
// var asterisk var |
| 446 |
if (next2 != end && it->type == ProcessedTokenType::VARID && |
| 447 |
next1->type == ProcessedTokenType::ASTERISK && |
| 448 |
next2->type == ProcessedTokenType::VARID) { |
| 449 |
std::string name1 = it->name; |
| 450 |
std::string name2 = next2->name; |
| 451 |
|
| 452 |
std::shared_ptr<QuadTerm> quadterm = |
| 453 |
std::shared_ptr<QuadTerm>(new QuadTerm()); |
| 454 |
quadterm->coef = 1.0; |
| 455 |
quadterm->var1 = builder.getvarbyname(name1); |
| 456 |
quadterm->var2 = builder.getvarbyname(name2); |
| 457 |
expr->quadterms.push_back(quadterm); |
| 458 |
|
| 459 |
it = next3; |
| 460 |
continue; |
| 461 |
} |
| 462 |
break; |
| 463 |
} |
| 464 |
if (isobj) { |
| 465 |
// only in the objective function, a quadratic term is followed by |
| 466 |
// "/2.0" |
| 467 |
std::vector<ProcessedToken>::iterator next1 = it; // token after it |
| 468 |
std::vector<ProcessedToken>::iterator next2 = it; // token 2nd-after it |
| 469 |
++next1; |
| 470 |
++next2; |
| 471 |
if (next1 != end) ++next2; |
| 472 |
|
| 473 |
lpassert(next2 != end); |
| 474 |
lpassert(it->type == ProcessedTokenType::BRKCL); |
| 475 |
lpassert(next1->type == ProcessedTokenType::SLASH); |
| 476 |
lpassert(next2->type == ProcessedTokenType::CONST); |
| 477 |
lpassert(next2->value == 2.0); |
| 478 |
it = ++next2; |
| 479 |
} else { |
| 480 |
lpassert(it != end); |
| 481 |
lpassert(it->type == ProcessedTokenType::BRKCL); |
| 482 |
++it; |
| 483 |
} |
| 484 |
continue; |
| 485 |
} |
| 486 |
|
| 487 |
break; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
void Reader::processobjsec() { |
| 492 |
builder.model.objective = std::shared_ptr<Expression>(new Expression); |
| 493 |
if (sectiontokens.count(LpSectionKeyword::OBJMIN)) { |
| 494 |
builder.model.sense = ObjectiveSense::MIN; |
| 495 |
parseexpression(sectiontokens[LpSectionKeyword::OBJMIN].first, |
| 496 |
sectiontokens[LpSectionKeyword::OBJMIN].second, |
| 497 |
builder.model.objective, true); |
| 498 |
lpassert(sectiontokens[LpSectionKeyword::OBJMIN].first == |
| 499 |
sectiontokens[LpSectionKeyword::OBJMIN] |
| 500 |
.second); // all section tokens should have been processed |
| 501 |
} else if (sectiontokens.count(LpSectionKeyword::OBJMAX)) { |
| 502 |
builder.model.sense = ObjectiveSense::MAX; |
| 503 |
parseexpression(sectiontokens[LpSectionKeyword::OBJMAX].first, |
| 504 |
sectiontokens[LpSectionKeyword::OBJMAX].second, |
| 505 |
builder.model.objective, true); |
| 506 |
lpassert(sectiontokens[LpSectionKeyword::OBJMAX].first == |
| 507 |
sectiontokens[LpSectionKeyword::OBJMAX] |
| 508 |
.second); // all section tokens should have been processed |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
void Reader::processconsec() { |
| 513 |
if (!sectiontokens.count(LpSectionKeyword::CON)) return; |
| 514 |
std::vector<ProcessedToken>::iterator& begin( |
| 515 |
sectiontokens[LpSectionKeyword::CON].first); |
| 516 |
std::vector<ProcessedToken>::iterator& end( |
| 517 |
sectiontokens[LpSectionKeyword::CON].second); |
| 518 |
while (begin != end) { |
| 519 |
std::shared_ptr<Constraint> con = |
| 520 |
std::shared_ptr<Constraint>(new Constraint); |
| 521 |
parseexpression(begin, end, con->expr, false); |
| 522 |
// should not be at end of section yet, but a comparison operator should be |
| 523 |
// next |
| 524 |
lpassert(begin != sectiontokens[LpSectionKeyword::CON].second); |
| 525 |
lpassert(begin->type == ProcessedTokenType::COMP); |
| 526 |
LpComparisonType dir = begin->dir; |
| 527 |
++begin; |
| 528 |
|
| 529 |
// should still not be at end of section yet, but a right-hand-side value |
| 530 |
// should be next |
| 531 |
lpassert(begin != sectiontokens[LpSectionKeyword::CON].second); |
| 532 |
lpassert(begin->type == ProcessedTokenType::CONST); |
| 533 |
switch (dir) { |
| 534 |
case LpComparisonType::EQ: |
| 535 |
con->lowerbound = con->upperbound = begin->value; |
| 536 |
break; |
| 537 |
case LpComparisonType::LEQ: |
| 538 |
con->upperbound = begin->value; |
| 539 |
break; |
| 540 |
case LpComparisonType::GEQ: |
| 541 |
con->lowerbound = begin->value; |
| 542 |
break; |
| 543 |
default: |
| 544 |
lpassert(false); |
| 545 |
} |
| 546 |
builder.model.constraints.push_back(con); |
| 547 |
++begin; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
void Reader::processboundssec() { |
| 552 |
if (!sectiontokens.count(LpSectionKeyword::BOUNDS)) return; |
| 553 |
std::vector<ProcessedToken>::iterator& begin( |
| 554 |
sectiontokens[LpSectionKeyword::BOUNDS].first); |
| 555 |
std::vector<ProcessedToken>::iterator& end( |
| 556 |
sectiontokens[LpSectionKeyword::BOUNDS].second); |
| 557 |
while (begin != end) { |
| 558 |
std::vector<ProcessedToken>::iterator next1 = begin; // token after begin |
| 559 |
++next1; |
| 560 |
|
| 561 |
// VAR free |
| 562 |
if (next1 != end && begin->type == ProcessedTokenType::VARID && |
| 563 |
next1->type == ProcessedTokenType::FREE) { |
| 564 |
std::string name = begin->name; |
| 565 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 566 |
var->lowerbound = -kHighsInf; |
| 567 |
var->upperbound = kHighsInf; |
| 568 |
begin = ++next1; |
| 569 |
continue; |
| 570 |
} |
| 571 |
|
| 572 |
std::vector<ProcessedToken>::iterator next2 = |
| 573 |
next1; // token 2nd-after begin |
| 574 |
std::vector<ProcessedToken>::iterator next3 = |
| 575 |
next1; // token 3rd-after begin |
| 576 |
std::vector<ProcessedToken>::iterator next4 = |
| 577 |
next1; // token 4th-after begin |
| 578 |
if (next1 != end) { |
| 579 |
++next2; |
| 580 |
++next3; |
| 581 |
++next4; |
| 582 |
} |
| 583 |
if (next2 != end) { |
| 584 |
++next3; |
| 585 |
++next4; |
| 586 |
} |
| 587 |
if (next3 != end) ++next4; |
| 588 |
|
| 589 |
// CONST COMP VAR COMP CONST |
| 590 |
if (next4 != end && begin->type == ProcessedTokenType::CONST && |
| 591 |
next1->type == ProcessedTokenType::COMP && |
| 592 |
next2->type == ProcessedTokenType::VARID && |
| 593 |
next3->type == ProcessedTokenType::COMP && |
| 594 |
next4->type == ProcessedTokenType::CONST) { |
| 595 |
lpassert(next1->dir == LpComparisonType::LEQ); |
| 596 |
lpassert(next3->dir == LpComparisonType::LEQ); |
| 597 |
|
| 598 |
double lb = begin->value; |
| 599 |
double ub = next4->value; |
| 600 |
|
| 601 |
std::string name = next2->name; |
| 602 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 603 |
|
| 604 |
var->lowerbound = lb; |
| 605 |
var->upperbound = ub; |
| 606 |
|
| 607 |
begin = ++next4; |
| 608 |
continue; |
| 609 |
} |
| 610 |
|
| 611 |
// CONST COMP VAR |
| 612 |
if (next2 != end && begin->type == ProcessedTokenType::CONST && |
| 613 |
next1->type == ProcessedTokenType::COMP && |
| 614 |
next2->type == ProcessedTokenType::VARID) { |
| 615 |
double value = begin->value; |
| 616 |
std::string name = next2->name; |
| 617 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 618 |
LpComparisonType dir = next1->dir; |
| 619 |
|
| 620 |
lpassert(dir != LpComparisonType::L && dir != LpComparisonType::G); |
| 621 |
|
| 622 |
switch (dir) { |
| 623 |
case LpComparisonType::LEQ: |
| 624 |
var->lowerbound = value; |
| 625 |
break; |
| 626 |
case LpComparisonType::GEQ: |
| 627 |
var->upperbound = value; |
| 628 |
break; |
| 629 |
case LpComparisonType::EQ: |
| 630 |
var->lowerbound = var->upperbound = value; |
| 631 |
break; |
| 632 |
default: |
| 633 |
lpassert(false); |
| 634 |
} |
| 635 |
begin = next3; |
| 636 |
continue; |
| 637 |
} |
| 638 |
|
| 639 |
// VAR COMP CONST |
| 640 |
if (next2 != end && begin->type == ProcessedTokenType::VARID && |
| 641 |
next1->type == ProcessedTokenType::COMP && |
| 642 |
next2->type == ProcessedTokenType::CONST) { |
| 643 |
double value = next2->value; |
| 644 |
std::string name = begin->name; |
| 645 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 646 |
LpComparisonType dir = next1->dir; |
| 647 |
|
| 648 |
lpassert(dir != LpComparisonType::L && dir != LpComparisonType::G); |
| 649 |
|
| 650 |
switch (dir) { |
| 651 |
case LpComparisonType::LEQ: |
| 652 |
var->upperbound = value; |
| 653 |
break; |
| 654 |
case LpComparisonType::GEQ: |
| 655 |
var->lowerbound = value; |
| 656 |
break; |
| 657 |
case LpComparisonType::EQ: |
| 658 |
var->lowerbound = var->upperbound = value; |
| 659 |
break; |
| 660 |
default: |
| 661 |
lpassert(false); |
| 662 |
} |
| 663 |
begin = next3; |
| 664 |
continue; |
| 665 |
} |
| 666 |
|
| 667 |
lpassert(false); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
void Reader::processbinsec() { |
| 672 |
const LpSectionKeyword this_section_keyword = LpSectionKeyword::BIN; |
| 673 |
if (!sectiontokens.count(this_section_keyword)) return; |
| 674 |
std::vector<ProcessedToken>::iterator& begin( |
| 675 |
sectiontokens[this_section_keyword].first); |
| 676 |
std::vector<ProcessedToken>::iterator& end( |
| 677 |
sectiontokens[this_section_keyword].second); |
| 678 |
for (; begin != end; ++begin) { |
| 679 |
if (begin->type == ProcessedTokenType::SECID) { |
| 680 |
// Possible to have repeat of keyword for this section type |
| 681 |
lpassert(begin->keyword == this_section_keyword); |
| 682 |
continue; |
| 683 |
} |
| 684 |
lpassert(begin->type == ProcessedTokenType::VARID); |
| 685 |
std::string name = begin->name; |
| 686 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 687 |
var->type = VariableType::BINARY; |
| 688 |
// Respect any bounds already declared |
| 689 |
if (var->upperbound == kHighsInf) var->upperbound = 1.0; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
void Reader::processgensec() { |
| 694 |
const LpSectionKeyword this_section_keyword = LpSectionKeyword::GEN; |
| 695 |
if (!sectiontokens.count(this_section_keyword)) return; |
| 696 |
std::vector<ProcessedToken>::iterator& begin( |
| 697 |
sectiontokens[this_section_keyword].first); |
| 698 |
std::vector<ProcessedToken>::iterator& end( |
| 699 |
sectiontokens[this_section_keyword].second); |
| 700 |
for (; begin != end; ++begin) { |
| 701 |
if (begin->type == ProcessedTokenType::SECID) { |
| 702 |
// Possible to have repeat of keyword for this section type |
| 703 |
lpassert(begin->keyword == this_section_keyword); |
| 704 |
continue; |
| 705 |
} |
| 706 |
lpassert(begin->type == ProcessedTokenType::VARID); |
| 707 |
std::string name = begin->name; |
| 708 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 709 |
if (var->type == VariableType::SEMICONTINUOUS) { |
| 710 |
var->type = VariableType::SEMIINTEGER; |
| 711 |
} else { |
| 712 |
var->type = VariableType::GENERAL; |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
void Reader::processsemisec() { |
| 718 |
const LpSectionKeyword this_section_keyword = LpSectionKeyword::SEMI; |
| 719 |
if (!sectiontokens.count(this_section_keyword)) return; |
| 720 |
std::vector<ProcessedToken>::iterator& begin( |
| 721 |
sectiontokens[this_section_keyword].first); |
| 722 |
std::vector<ProcessedToken>::iterator& end( |
| 723 |
sectiontokens[this_section_keyword].second); |
| 724 |
for (; begin != end; ++begin) { |
| 725 |
if (begin->type == ProcessedTokenType::SECID) { |
| 726 |
// Possible to have repeat of keyword for this section type |
| 727 |
lpassert(begin->keyword == this_section_keyword); |
| 728 |
continue; |
| 729 |
} |
| 730 |
lpassert(begin->type == ProcessedTokenType::VARID); |
| 731 |
std::string name = begin->name; |
| 732 |
std::shared_ptr<Variable> var = builder.getvarbyname(name); |
| 733 |
if (var->type == VariableType::GENERAL) { |
| 734 |
var->type = VariableType::SEMIINTEGER; |
| 735 |
} else { |
| 736 |
var->type = VariableType::SEMICONTINUOUS; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
void Reader::processsossec() { |
| 742 |
const LpSectionKeyword this_section_keyword = LpSectionKeyword::SOS; |
| 743 |
if (!sectiontokens.count(this_section_keyword)) return; |
| 744 |
std::vector<ProcessedToken>::iterator& begin( |
| 745 |
sectiontokens[this_section_keyword].first); |
| 746 |
std::vector<ProcessedToken>::iterator& end( |
| 747 |
sectiontokens[this_section_keyword].second); |
| 748 |
while (begin != end) { |
| 749 |
std::shared_ptr<SOS> sos = std::shared_ptr<SOS>(new SOS); |
| 750 |
|
| 751 |
// sos1: S1 :: x1 : 1 x2 : 2 x3 : 3 |
| 752 |
|
| 753 |
// name of SOS is mandatory |
| 754 |
lpassert(begin->type == ProcessedTokenType::CONID); |
| 755 |
sos->name = begin->name; |
| 756 |
++begin; |
| 757 |
|
| 758 |
// SOS type |
| 759 |
lpassert(begin != end); |
| 760 |
lpassert(begin->type == ProcessedTokenType::SOSTYPE); |
| 761 |
sos->type = begin->sostype == SosType::SOS1 ? 1 : 2; |
| 762 |
++begin; |
| 763 |
|
| 764 |
while (begin != end) { |
| 765 |
// process all "var : weight" entries |
| 766 |
// when processtokens() sees a string followed by a colon, it classifies |
| 767 |
// this as a CONID but in a SOS section, this is actually a variable |
| 768 |
// identifier |
| 769 |
if (begin->type != ProcessedTokenType::CONID) break; |
| 770 |
std::string name = begin->name; |
| 771 |
std::vector<ProcessedToken>::iterator next = begin; |
| 772 |
++next; |
| 773 |
if (next != end && next->type == ProcessedTokenType::CONST) { |
| 774 |
auto var = builder.getvarbyname(name); |
| 775 |
double weight = next->value; |
| 776 |
|
| 777 |
sos->entries.push_back({var, weight}); |
| 778 |
|
| 779 |
begin = ++next; |
| 780 |
continue; |
| 781 |
} |
| 782 |
|
| 783 |
break; |
| 784 |
} |
| 785 |
|
| 786 |
builder.model.soss.push_back(sos); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
void Reader::processendsec() { |
| 791 |
lpassert(sectiontokens.count(LpSectionKeyword::END) == 0); |
| 792 |
} |
| 793 |
|
| 794 |
void Reader::processsections() { |
| 795 |
processnonesec(); |
| 796 |
processobjsec(); |
| 797 |
processconsec(); |
| 798 |
processboundssec(); |
| 799 |
processgensec(); |
| 800 |
processbinsec(); |
| 801 |
processsemisec(); |
| 802 |
processsossec(); |
| 803 |
processendsec(); |
| 804 |
} |
| 805 |
|
| 806 |
void Reader::splittokens() { |
| 807 |
LpSectionKeyword currentsection = LpSectionKeyword::NONE; |
| 808 |
|
| 809 |
bool debug_open_section = false; |
| 810 |
for (std::vector<ProcessedToken>::iterator it(processedtokens.begin()); |
| 811 |
it != processedtokens.end(); ++it) { |
| 812 |
// Look for section keywords |
| 813 |
if (it->type != ProcessedTokenType::SECID) continue; |
| 814 |
// currentsection is initially LpSectionKeyword::NONE, so the |
| 815 |
// first section ID will be a new section type |
| 816 |
// |
| 817 |
// Only record change of section and check for repeated |
| 818 |
// section if the keyword is for a different section. Allows |
| 819 |
// repetition of Integers and General (cf #1299) for example |
| 820 |
const bool new_section_type = currentsection != it->keyword; |
| 821 |
if (new_section_type) { |
| 822 |
if (currentsection != LpSectionKeyword::NONE) { |
| 823 |
// Current section is non-trivial, so mark its end, using the |
| 824 |
// value of currentsection to indicate that there is no open |
| 825 |
// section |
| 826 |
lpassert(debug_open_section); |
| 827 |
sectiontokens[currentsection].second = it; |
| 828 |
debug_open_section = false; |
| 829 |
currentsection = LpSectionKeyword::NONE; |
| 830 |
} |
| 831 |
} |
| 832 |
std::vector<ProcessedToken>::iterator next = it; |
| 833 |
++next; |
| 834 |
if (next == processedtokens.end() || |
| 835 |
next->type == ProcessedTokenType::SECID) { |
| 836 |
// Reached the end of the tokens or the new section is empty |
| 837 |
// |
| 838 |
// currentsection will be LpSectionKeyword::NONE unless the |
| 839 |
// second of two sections of the same type is empty and the |
| 840 |
// next section is of a new type, in which case mark the end of |
| 841 |
// the current section |
| 842 |
if (currentsection != LpSectionKeyword::NONE && |
| 843 |
currentsection != next->keyword) { |
| 844 |
lpassert(debug_open_section); |
| 845 |
sectiontokens[currentsection].second = it; |
| 846 |
debug_open_section = false; |
| 847 |
} |
| 848 |
currentsection = LpSectionKeyword::NONE; |
| 849 |
lpassert(!debug_open_section); |
| 850 |
continue; |
| 851 |
} |
| 852 |
// Next section is non-empty |
| 853 |
if (new_section_type) { |
| 854 |
// Section type change |
| 855 |
currentsection = it->keyword; |
| 856 |
// Make sure the new section type has not occurred previously |
| 857 |
lpassert(sectiontokens.count(currentsection) == 0); |
| 858 |
// Remember the beginning of the new section: its the token |
| 859 |
// following the current one |
| 860 |
lpassert(!debug_open_section); |
| 861 |
sectiontokens[currentsection].first = next; |
| 862 |
debug_open_section = true; |
| 863 |
} |
| 864 |
// Always ends with either an open section or a section type of |
| 865 |
// LpSectionKeyword::NONE |
| 866 |
lpassert(debug_open_section != (currentsection == LpSectionKeyword::NONE)); |
| 867 |
} |
| 868 |
// Check that the last section has been closed |
| 869 |
lpassert(currentsection == LpSectionKeyword::NONE); |
| 870 |
} |
| 871 |
|
| 872 |
void Reader::processtokens() { |
| 873 |
std::string svalue_lc; |
| 874 |
while (!rawtokens[0].istype(RawTokenType::FLEND)) { |
| 875 |
fflush(stdout); |
| 876 |
|
| 877 |
// Slash + asterisk: comment, skip everything up to next asterisk + slash |
| 878 |
if (rawtokens[0].istype(RawTokenType::SLASH) && |
| 879 |
rawtokens[1].istype(RawTokenType::ASTERISK)) { |
| 880 |
do { |
| 881 |
nextrawtoken(2); |
| 882 |
} while (!(rawtokens[0].istype(RawTokenType::ASTERISK) && |
| 883 |
rawtokens[1].istype(RawTokenType::SLASH)) && |
| 884 |
!rawtokens[0].istype(RawTokenType::FLEND)); |
| 885 |
nextrawtoken(2); |
| 886 |
continue; |
| 887 |
} |
| 888 |
|
| 889 |
if (rawtokens[0].istype(RawTokenType::STR)) { |
| 890 |
svalue_lc = rawtokens[0].svalue; |
| 891 |
tolower(svalue_lc); |
| 892 |
} |
| 893 |
|
| 894 |
// long section keyword semi-continuous |
| 895 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 896 |
rawtokens[1].istype(RawTokenType::MINUS) && |
| 897 |
rawtokens[2].istype(RawTokenType::STR)) { |
| 898 |
std::string temp = rawtokens[2].svalue; |
| 899 |
tolower(temp); |
| 900 |
LpSectionKeyword keyword = parsesectionkeyword(svalue_lc + "-" + temp); |
| 901 |
if (keyword != LpSectionKeyword::NONE) { |
| 902 |
processedtokens.emplace_back(keyword); |
| 903 |
nextrawtoken(3); |
| 904 |
continue; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
// long section keyword subject to/such that |
| 909 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 910 |
rawtokens[1].istype(RawTokenType::STR)) { |
| 911 |
std::string temp = rawtokens[1].svalue; |
| 912 |
tolower(temp); |
| 913 |
LpSectionKeyword keyword = parsesectionkeyword(svalue_lc + " " + temp); |
| 914 |
if (keyword != LpSectionKeyword::NONE) { |
| 915 |
processedtokens.emplace_back(keyword); |
| 916 |
nextrawtoken(2); |
| 917 |
continue; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
// other section keyword |
| 922 |
if (rawtokens[0].istype(RawTokenType::STR)) { |
| 923 |
LpSectionKeyword keyword = parsesectionkeyword(svalue_lc); |
| 924 |
if (keyword != LpSectionKeyword::NONE) { |
| 925 |
processedtokens.emplace_back(keyword); |
| 926 |
nextrawtoken(); |
| 927 |
continue; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
// sos type identifier? "S1 ::" or "S2 ::" |
| 932 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 933 |
rawtokens[1].istype(RawTokenType::COLON) && |
| 934 |
rawtokens[2].istype(RawTokenType::COLON)) { |
| 935 |
lpassert(rawtokens[0].svalue.length() == 2); |
| 936 |
lpassert(rawtokens[0].svalue[0] == 'S' || rawtokens[0].svalue[0] == 's'); |
| 937 |
lpassert(rawtokens[0].svalue[1] == '1' || rawtokens[0].svalue[1] == '2'); |
| 938 |
processedtokens.emplace_back( |
| 939 |
rawtokens[0].svalue[1] == '1' ? SosType::SOS1 : SosType::SOS2); |
| 940 |
nextrawtoken(3); |
| 941 |
continue; |
| 942 |
} |
| 943 |
|
| 944 |
// constraint identifier? |
| 945 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 946 |
rawtokens[1].istype(RawTokenType::COLON)) { |
| 947 |
processedtokens.emplace_back(ProcessedTokenType::CONID, |
| 948 |
rawtokens[0].svalue); |
| 949 |
nextrawtoken(2); |
| 950 |
continue; |
| 951 |
} |
| 952 |
|
| 953 |
// constraint identifier - with numeric constant value as name? |
| 954 |
if (rawtokens[0].istype(RawTokenType::CONS) && |
| 955 |
rawtokens[1].istype(RawTokenType::COLON)) { |
| 956 |
processedtokens.emplace_back(ProcessedTokenType::CONID, |
| 957 |
rawtokens[0].svalue); |
| 958 |
nextrawtoken(2); |
| 959 |
continue; |
| 960 |
} |
| 961 |
|
| 962 |
// check if free |
| 963 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 964 |
iskeyword(svalue_lc, LP_KEYWORD_FREE, LP_KEYWORD_FREE_N)) { |
| 965 |
processedtokens.emplace_back(ProcessedTokenType::FREE); |
| 966 |
nextrawtoken(); |
| 967 |
continue; |
| 968 |
} |
| 969 |
|
| 970 |
// check if infinity |
| 971 |
if (rawtokens[0].istype(RawTokenType::STR) && |
| 972 |
iskeyword(svalue_lc, LP_KEYWORD_INF, LP_KEYWORD_INF_N)) { |
| 973 |
processedtokens.emplace_back(kHighsInf); |
| 974 |
nextrawtoken(); |
| 975 |
continue; |
| 976 |
} |
| 977 |
|
| 978 |
// assume var identifier |
| 979 |
if (rawtokens[0].istype(RawTokenType::STR)) { |
| 980 |
processedtokens.emplace_back(ProcessedTokenType::VARID, |
| 981 |
rawtokens[0].svalue); |
| 982 |
nextrawtoken(); |
| 983 |
continue; |
| 984 |
} |
| 985 |
|
| 986 |
// + or - |
| 987 |
if (rawtokens[0].istype(RawTokenType::PLUS) || |
| 988 |
rawtokens[0].istype(RawTokenType::MINUS)) { |
| 989 |
double sign = rawtokens[0].istype(RawTokenType::PLUS) ? 1.0 : -1.0; |
| 990 |
nextrawtoken(); |
| 991 |
|
| 992 |
// another + or - for #948, #950 |
| 993 |
if (rawtokens[0].istype(RawTokenType::PLUS) || |
| 994 |
rawtokens[0].istype(RawTokenType::MINUS)) { |
| 995 |
sign *= rawtokens[0].istype(RawTokenType::PLUS) ? 1.0 : -1.0; |
| 996 |
nextrawtoken(); |
| 997 |
} |
| 998 |
|
| 999 |
// +/- Constant |
| 1000 |
if (rawtokens[0].istype(RawTokenType::CONS)) { |
| 1001 |
processedtokens.emplace_back(sign * rawtokens[0].dvalue); |
| 1002 |
nextrawtoken(); |
| 1003 |
continue; |
| 1004 |
} |
| 1005 |
|
| 1006 |
// + [, + + [, - - [ |
| 1007 |
if (rawtokens[0].istype(RawTokenType::BRKOP) && sign == 1.0) { |
| 1008 |
processedtokens.emplace_back(ProcessedTokenType::BRKOP); |
| 1009 |
nextrawtoken(); |
| 1010 |
continue; |
| 1011 |
} |
| 1012 |
|
| 1013 |
// - [, + - [, - + [ |
| 1014 |
if (rawtokens[0].istype(RawTokenType::BRKOP)) lpassert(false); |
| 1015 |
|
| 1016 |
// +/- variable name |
| 1017 |
if (rawtokens[0].istype(RawTokenType::STR)) { |
| 1018 |
processedtokens.emplace_back(sign); |
| 1019 |
continue; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// +/- (possibly twice) followed by something that isn't a constant, |
| 1023 |
// opening bracket, or string (variable name) |
| 1024 |
if (rawtokens[0].istype(RawTokenType::GREATER)) { |
| 1025 |
// ">" suggests that the file contains indicator constraints |
| 1026 |
printf( |
| 1027 |
"File appears to contain indicator constraints: cannot currently " |
| 1028 |
"be handled by HiGHS\n"); |
| 1029 |
} |
| 1030 |
lpassert(false); |
| 1031 |
} |
| 1032 |
|
| 1033 |
// constant [ |
| 1034 |
if (rawtokens[0].istype(RawTokenType::CONS) && |
| 1035 |
rawtokens[1].istype(RawTokenType::BRKOP)) { |
| 1036 |
lpassert(false); |
| 1037 |
} |
| 1038 |
|
| 1039 |
// constant |
| 1040 |
if (rawtokens[0].istype(RawTokenType::CONS)) { |
| 1041 |
processedtokens.emplace_back(rawtokens[0].dvalue); |
| 1042 |
nextrawtoken(); |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
|
| 1046 |
// [ |
| 1047 |
if (rawtokens[0].istype(RawTokenType::BRKOP)) { |
| 1048 |
processedtokens.emplace_back(ProcessedTokenType::BRKOP); |
| 1049 |
nextrawtoken(); |
| 1050 |
continue; |
| 1051 |
} |
| 1052 |
|
| 1053 |
// ] |
| 1054 |
if (rawtokens[0].istype(RawTokenType::BRKCL)) { |
| 1055 |
processedtokens.emplace_back(ProcessedTokenType::BRKCL); |
| 1056 |
nextrawtoken(); |
| 1057 |
continue; |
| 1058 |
} |
| 1059 |
|
| 1060 |
// / |
| 1061 |
if (rawtokens[0].istype(RawTokenType::SLASH)) { |
| 1062 |
processedtokens.emplace_back(ProcessedTokenType::SLASH); |
| 1063 |
nextrawtoken(); |
| 1064 |
continue; |
| 1065 |
} |
| 1066 |
|
| 1067 |
// * |
| 1068 |
if (rawtokens[0].istype(RawTokenType::ASTERISK)) { |
| 1069 |
processedtokens.emplace_back(ProcessedTokenType::ASTERISK); |
| 1070 |
nextrawtoken(); |
| 1071 |
continue; |
| 1072 |
} |
| 1073 |
|
| 1074 |
// ^ |
| 1075 |
if (rawtokens[0].istype(RawTokenType::HAT)) { |
| 1076 |
processedtokens.emplace_back(ProcessedTokenType::HAT); |
| 1077 |
nextrawtoken(); |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
|
| 1081 |
// <= |
| 1082 |
if (rawtokens[0].istype(RawTokenType::LESS) && |
| 1083 |
rawtokens[1].istype(RawTokenType::EQUAL)) { |
| 1084 |
processedtokens.emplace_back(LpComparisonType::LEQ); |
| 1085 |
nextrawtoken(2); |
| 1086 |
continue; |
| 1087 |
} |
| 1088 |
|
| 1089 |
// < |
| 1090 |
if (rawtokens[0].istype(RawTokenType::LESS)) { |
| 1091 |
processedtokens.emplace_back(LpComparisonType::L); |
| 1092 |
nextrawtoken(); |
| 1093 |
continue; |
| 1094 |
} |
| 1095 |
|
| 1096 |
// >= |
| 1097 |
if (rawtokens[0].istype(RawTokenType::GREATER) && |
| 1098 |
rawtokens[1].istype(RawTokenType::EQUAL)) { |
| 1099 |
processedtokens.emplace_back(LpComparisonType::GEQ); |
| 1100 |
nextrawtoken(2); |
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
|
| 1104 |
// > |
| 1105 |
if (rawtokens[0].istype(RawTokenType::GREATER)) { |
| 1106 |
processedtokens.emplace_back(LpComparisonType::G); |
| 1107 |
nextrawtoken(); |
| 1108 |
continue; |
| 1109 |
} |
| 1110 |
|
| 1111 |
// = |
| 1112 |
if (rawtokens[0].istype(RawTokenType::EQUAL)) { |
| 1113 |
processedtokens.emplace_back(LpComparisonType::EQ); |
| 1114 |
nextrawtoken(); |
| 1115 |
continue; |
| 1116 |
} |
| 1117 |
|
| 1118 |
// FILEEND should have been handled in condition of while() |
| 1119 |
assert(!rawtokens[0].istype(RawTokenType::FLEND)); |
| 1120 |
|
| 1121 |
// catch all unknown symbols |
| 1122 |
lpassert(false); |
| 1123 |
break; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
void Reader::nextrawtoken(size_t howmany) { |
| 1128 |
assert(howmany > 0); |
| 1129 |
assert(howmany <= NRAWTOKEN); |
| 1130 |
static_assert(NRAWTOKEN == 3, |
| 1131 |
"code below need to be adjusted if NRAWTOKEN changes"); |
| 1132 |
switch (howmany) { |
| 1133 |
case 1: { |
| 1134 |
rawtokens[0] = std::move(rawtokens[1]); |
| 1135 |
rawtokens[1] = std::move(rawtokens[2]); |
| 1136 |
while (!readnexttoken(rawtokens[2])) |
| 1137 |
; |
| 1138 |
break; |
| 1139 |
} |
| 1140 |
case 2: { |
| 1141 |
rawtokens[0] = std::move(rawtokens[2]); |
| 1142 |
while (!readnexttoken(rawtokens[1])) |
| 1143 |
; |
| 1144 |
while (!readnexttoken(rawtokens[2])) |
| 1145 |
; |
| 1146 |
break; |
| 1147 |
} |
| 1148 |
case 3: { |
| 1149 |
while (!readnexttoken(rawtokens[0])) |
| 1150 |
; |
| 1151 |
while (!readnexttoken(rawtokens[1])) |
| 1152 |
; |
| 1153 |
while (!readnexttoken(rawtokens[2])) |
| 1154 |
; |
| 1155 |
break; |
| 1156 |
} |
| 1157 |
default: { |
| 1158 |
size_t i = 0; |
| 1159 |
// move tokens up |
| 1160 |
for (; i < NRAWTOKEN - howmany; ++i) |
| 1161 |
rawtokens[i] = std::move(rawtokens[i + howmany]); |
| 1162 |
// read new tokens at end positions |
| 1163 |
for (; i < NRAWTOKEN; ++i) |
| 1164 |
// call readnexttoken() to overwrite current token |
| 1165 |
// if it didn't actually read a token (returns false), then call again |
| 1166 |
while (!readnexttoken(rawtokens[i])) |
| 1167 |
; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
// return true, if token has been set; return false if skipped over whitespace |
| 1173 |
// only |
| 1174 |
bool Reader::readnexttoken(RawToken& t) { |
| 1175 |
if (this->linebufferpos == this->linebuffer.size()) { |
| 1176 |
// read next line if any are left. |
| 1177 |
if (this->file.eof()) { |
| 1178 |
t = RawTokenType::FLEND; |
| 1179 |
return true; |
| 1180 |
} |
| 1181 |
std::getline(this->file, linebuffer); |
| 1182 |
|
| 1183 |
// drop \r |
| 1184 |
if (!linebuffer.empty() && linebuffer.back() == '\r') linebuffer.pop_back(); |
| 1185 |
|
| 1186 |
// reset linebufferpos |
| 1187 |
this->linebufferpos = 0; |
| 1188 |
} |
| 1189 |
|
| 1190 |
// check single character tokens |
| 1191 |
char nextchar = this->linebuffer[this->linebufferpos]; |
| 1192 |
switch (nextchar) { |
| 1193 |
// check for comment |
| 1194 |
case '\\': |
| 1195 |
// skip rest of line |
| 1196 |
this->linebufferpos = this->linebuffer.size(); |
| 1197 |
return false; |
| 1198 |
|
| 1199 |
// check for bracket opening |
| 1200 |
case '[': |
| 1201 |
t = RawTokenType::BRKOP; |
| 1202 |
this->linebufferpos++; |
| 1203 |
return true; |
| 1204 |
|
| 1205 |
// check for bracket closing |
| 1206 |
case ']': |
| 1207 |
t = RawTokenType::BRKCL; |
| 1208 |
this->linebufferpos++; |
| 1209 |
return true; |
| 1210 |
|
| 1211 |
// check for less sign |
| 1212 |
case '<': |
| 1213 |
t = RawTokenType::LESS; |
| 1214 |
this->linebufferpos++; |
| 1215 |
return true; |
| 1216 |
|
| 1217 |
// check for greater sign |
| 1218 |
case '>': |
| 1219 |
t = RawTokenType::GREATER; |
| 1220 |
this->linebufferpos++; |
| 1221 |
return true; |
| 1222 |
|
| 1223 |
// check for equal sign |
| 1224 |
case '=': |
| 1225 |
t = RawTokenType::EQUAL; |
| 1226 |
this->linebufferpos++; |
| 1227 |
return true; |
| 1228 |
|
| 1229 |
// check for colon |
| 1230 |
case ':': |
| 1231 |
t = RawTokenType::COLON; |
| 1232 |
this->linebufferpos++; |
| 1233 |
return true; |
| 1234 |
|
| 1235 |
// check for plus |
| 1236 |
case '+': |
| 1237 |
t = RawTokenType::PLUS; |
| 1238 |
this->linebufferpos++; |
| 1239 |
return true; |
| 1240 |
|
| 1241 |
// check for hat |
| 1242 |
case '^': |
| 1243 |
t = RawTokenType::HAT; |
| 1244 |
this->linebufferpos++; |
| 1245 |
return true; |
| 1246 |
|
| 1247 |
// check for slash |
| 1248 |
case '/': |
| 1249 |
t = RawTokenType::SLASH; |
| 1250 |
this->linebufferpos++; |
| 1251 |
return true; |
| 1252 |
|
| 1253 |
// check for asterisk |
| 1254 |
case '*': |
| 1255 |
t = RawTokenType::ASTERISK; |
| 1256 |
this->linebufferpos++; |
| 1257 |
return true; |
| 1258 |
|
| 1259 |
// check for minus |
| 1260 |
case '-': |
| 1261 |
t = RawTokenType::MINUS; |
| 1262 |
this->linebufferpos++; |
| 1263 |
return true; |
| 1264 |
|
| 1265 |
// check for whitespace |
| 1266 |
case ' ': |
| 1267 |
case '\t': |
| 1268 |
this->linebufferpos++; |
| 1269 |
return false; |
| 1270 |
|
| 1271 |
// check for line end |
| 1272 |
case ';': |
| 1273 |
case '\n': // \n should not happen due to using getline() |
| 1274 |
this->linebufferpos = this->linebuffer.size(); |
| 1275 |
return false; |
| 1276 |
|
| 1277 |
case '\0': // empty line |
| 1278 |
lpassert(this->linebufferpos == this->linebuffer.size()); |
| 1279 |
return false; |
| 1280 |
} |
| 1281 |
|
| 1282 |
// check for double value |
| 1283 |
const char* startptr = this->linebuffer.data() + this->linebufferpos; |
| 1284 |
char* endptr; |
| 1285 |
double constant = strtod(startptr, &endptr); |
| 1286 |
if (endptr != startptr) { |
| 1287 |
// Extract the string corresponding to the double, in case the |
| 1288 |
// double is a constraint name |
| 1289 |
size_t double_len = endptr - startptr; |
| 1290 |
std::string double_name = this->linebuffer.substr(this->linebufferpos, double_len); |
| 1291 |
// t = constant; |
| 1292 |
t = std::make_pair(constant, double_name); |
| 1293 |
this->linebufferpos += endptr - startptr; |
| 1294 |
return true; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// assume it's an (section/variable/constraint) identifier |
| 1298 |
auto endpos = |
| 1299 |
this->linebuffer.find_first_of("\t\n\\:+<>^= /-*[]", this->linebufferpos); |
| 1300 |
if (endpos == std::string::npos) |
| 1301 |
endpos = this->linebuffer.size(); // take complete rest of string |
| 1302 |
if (endpos > this->linebufferpos) { |
| 1303 |
t = std::string(this->linebuffer, this->linebufferpos, |
| 1304 |
endpos - this->linebufferpos); |
| 1305 |
this->linebufferpos = endpos; |
| 1306 |
return true; |
| 1307 |
} |
| 1308 |
|
| 1309 |
lpassert(false); |
| 1310 |
return false; |
| 1311 |
} |