MCPcopy Create free account
hub / github.com/Gecode/gecode / parseDIMACS

Method parseDIMACS

examples/sat.cpp:141–233  ·  view source on GitHub ↗

Post constraints according to DIMACS file \a f

Source from the content-addressed store, hash-verified

139
140 /// Post constraints according to DIMACS file \a f
141 void parseDIMACS(const char* f) {
142 int variables = 0;
143 int clauses = 0;
144 std::ifstream dimacs(f);
145 if (!dimacs) {
146 std::cerr << "error: file '" << f << "' not found" << std::endl;
147 exit(1);
148 }
149 std::cout << "Solving problem from DIMACS file '" << f << "'"
150 << std::endl;
151 std::string line;
152 int c = 0;
153 while (dimacs.good()) {
154 std::getline(dimacs,line);
155 // Comments (ignore them)
156 if (line[0] == 'c' || line == "") {
157 }
158 // Line has format "p cnf <variables> <clauses>"
159 else if (variables == 0 && clauses == 0 &&
160 line[0] == 'p' && line[1] == ' ' &&
161 line[2] == 'c' && line[3] == 'n' &&
162 line[4] == 'f' && line[5] == ' ') {
163 int i = 6;
164 while (line[i] >= '0' && line[i] <= '9') {
165 variables = 10*variables + line[i] - '0';
166 i++;
167 }
168 i++;
169 while (line[i] >= '0' && line[i] <= '9') {
170 clauses = 10*clauses + line[i] - '0';
171 i++;
172 }
173 std::cout << "(" << variables << " variables, "
174 << clauses << " clauses)" << std::endl << std::endl;
175 // Add variables to array
176 x = BoolVarArray(*this, variables, 0, 1);
177 }
178 // Parse regular clause
179 else if (variables > 0 &&
180 ((line[0] >= '0' && line[0] <= '9') || line[0] == '-' || line[0] == ' ')) {
181 c++;
182 std::vector<int> pos;
183 std::vector<int> neg;
184 int i = 0;
185 while (line[i] != 0) {
186 if (line[i] == ' ') {
187 i++;
188 continue;
189 }
190 bool positive = true;
191 if (line[i] == '-') {
192 positive = false;
193 i++;
194 }
195 int value = 0;
196 while (line[i] >= '0' && line[i] <= '9') {
197 value = 10 * value + line[i] - '0';
198 i++;

Callers

nothing calls this directly

Calls 3

clauseFunction · 0.85
BoolVarArrayClass · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected