44 bool Load(
const std::string& filename, LinearBooleanProblem* problem) {
46 problem->set_name(ExtractProblemName(filename));
52 ProcessNewLine(problem,
line);
55 LOG(FATAL) <<
"File '" << filename <<
"' is empty or can't be read.";
57 problem->set_num_variables(num_variables_);
64 static std::string ExtractProblemName(
const std::string& filename) {
65 const int found = filename.find_last_of(
'/');
66 const std::string problem_name =
67 found != std::string::npos ? filename.substr(found + 1) : filename;
71 void ProcessNewLine(LinearBooleanProblem* problem,
const std::string&
line) {
72 const std::vector<std::string> words =
73 absl::StrSplit(
line, absl::ByAnyChar(
" ;"), absl::SkipEmpty());
74 if (words.empty() || words[0].empty() || words[0][0] ==
'*') {
78 if (words[0] ==
"min:") {
79 LinearObjective* objective = problem->mutable_objective();
80 for (
int i = 1;
i < words.size(); ++
i) {
81 const std::string& word = words[
i];
82 if (word.empty() || word[0] ==
';')
continue;
85 CHECK(absl::SimpleAtoi(word.substr(1), &
literal));
86 num_variables_ = std::max(num_variables_,
literal);
87 objective->add_literals(
literal);
90 CHECK(absl::SimpleAtoi(word, &
value));
91 objective->add_coefficients(
value);
94 if (objective->literals_size() != objective->coefficients_size()) {
95 LOG(INFO) <<
"words.size() = " << words.size();
96 LOG(FATAL) <<
"Failed to parse objective:\n " <<
line;
100 LinearBooleanConstraint* constraint = problem->add_constraints();
101 for (
int i = 0;
i < words.size(); ++
i) {
102 const std::string& word = words[
i];
103 CHECK(!word.empty());
105 CHECK_LT(
i + 1, words.size());
107 CHECK(absl::SimpleAtoi(words[
i + 1], &
value));
108 constraint->set_lower_bound(
value);
110 }
else if (word ==
"=") {
111 CHECK_LT(
i + 1, words.size());
113 CHECK(absl::SimpleAtoi(words[
i + 1], &
value));
114 constraint->set_upper_bound(
value);
115 constraint->set_lower_bound(
value);
118 if (word[0] ==
'x') {
120 CHECK(absl::SimpleAtoi(word.substr(1), &
literal));
121 num_variables_ = std::max(num_variables_,
literal);
122 constraint->add_literals(
literal);
125 CHECK(absl::SimpleAtoi(words[
i], &
value));
126 constraint->add_coefficients(
value);
130 if (constraint->literals_size() != constraint->coefficients_size()) {
131 LOG(FATAL) <<
"Failed to parse constraint:\n " <<
line;