43 bool Load(
const std::string& filename, LinearBooleanProblem* problem) {
45 problem->set_name(ExtractProblemName(filename));
49 for (
const std::string& line :
FileLines(filename)) {
51 ProcessNewLine(problem, line);
54 LOG(FATAL) <<
"File '" << filename <<
"' is empty or can't be read.";
56 problem->set_num_variables(num_variables_);
63 static std::string ExtractProblemName(
const std::string& filename) {
64 const int found = filename.find_last_of(
'/');
65 const std::string problem_name =
66 found != std::string::npos ? filename.substr(found + 1) : filename;
70 void ProcessNewLine(LinearBooleanProblem* problem,
const std::string& line) {
71 const std::vector<std::string> words =
72 absl::StrSplit(line, absl::ByAnyChar(
" ;"), absl::SkipEmpty());
73 if (words.empty() || words[0].empty() || words[0][0] ==
'*') {
77 if (words[0] ==
"min:") {
78 LinearObjective* objective = problem->mutable_objective();
79 for (
int i = 1;
i < words.size(); ++
i) {
80 const std::string& word = words[
i];
81 if (word.empty() || word[0] ==
';')
continue;
84 CHECK(absl::SimpleAtoi(word.substr(1), &literal));
85 num_variables_ = std::max(num_variables_, literal);
86 objective->add_literals(literal);
89 CHECK(absl::SimpleAtoi(word, &value));
90 objective->add_coefficients(value);
93 if (objective->literals_size() != objective->coefficients_size()) {
94 LOG(INFO) <<
"words.size() = " << words.size();
95 LOG(FATAL) <<
"Failed to parse objective:\n " << line;
99 LinearBooleanConstraint* constraint = problem->add_constraints();
100 for (
int i = 0;
i < words.size(); ++
i) {
101 const std::string& word = words[
i];
102 CHECK(!word.empty());
104 CHECK_LT(
i + 1, words.size());
106 CHECK(absl::SimpleAtoi(words[
i + 1], &value));
107 constraint->set_lower_bound(value);
109 }
else if (word ==
"=") {
110 CHECK_LT(
i + 1, words.size());
112 CHECK(absl::SimpleAtoi(words[
i + 1], &value));
113 constraint->set_upper_bound(value);
114 constraint->set_lower_bound(value);
117 if (word[0] ==
'x') {
119 CHECK(absl::SimpleAtoi(word.substr(1), &literal));
120 num_variables_ = std::max(num_variables_, literal);
121 constraint->add_literals(literal);
124 CHECK(absl::SimpleAtoi(words[
i], &value));
125 constraint->add_coefficients(value);
129 if (constraint->literals_size() != constraint->coefficients_size()) {
130 LOG(FATAL) <<
"Failed to parse constraint:\n " << line;