Scippy

SCIP

Solving Constraint Integer Programs

reader_cnf.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file reader_cnf.c
17  * @brief CNF file reader
18  * @author Thorsten Koch
19  * @author Tobias Achterberg
20  *
21  * The DIMACS CNF (conjunctive normal form) is a file format used for example for SAT problems. For a detailed description of
22  * this format see http://people.sc.fsu.edu/~jburkardt/data/cnf/cnf.html .
23  */
24 
25 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
26 
27 #include <assert.h>
28 #include <string.h>
29 
30 #include "scip/reader_cnf.h"
31 #include "scip/cons_linear.h"
32 #include "scip/cons_logicor.h"
33 #include "scip/cons_setppc.h"
34 #include "scip/pub_misc.h"
35 
36 
37 #define READER_NAME "cnfreader"
38 #define READER_DESC "file reader for SAT problems in conjunctive normal form"
39 #define READER_EXTENSION "cnf"
40 
41 #define MAXLINELEN 65536
42 
43 
44 /*
45  * cnf reader internal methods
46  */
47 
48 static
49 void readError(
50  SCIP* scip, /**< SCIP data structure */
51  int linecount, /**< line number of error */
52  const char* errormsg /**< error message */
53  )
54 {
55  SCIPerrorMessage("read error in line <%d>: %s\n", linecount, errormsg);
56 }
57 
58 static
60  SCIP* scip, /**< SCIP data structure */
61  int linecount, /**< line number of error */
62  const char* warningmsg /**< warning message */
63  )
64 {
65  SCIPwarningMessage(scip, "Line <%d>: %s\n", linecount, warningmsg);
66 }
67 
68 /** reads the next non-empty non-comment line of a cnf file */
69 static
71  SCIP* scip, /**< SCIP data structure */
72  SCIP_FILE* file, /**< input file */
73  char* buffer, /**< buffer for storing the input line */
74  int size, /**< size of the buffer */
75  int* linecount /**< pointer to the line number counter */
76  )
77 {
78  char* line;
79  int linelen;
80 
81  assert(file != NULL);
82  assert(buffer != NULL);
83  assert(size >= 2);
84  assert(linecount != NULL);
85 
86  do
87  {
88  (*linecount)++;
89  line = SCIPfgets(buffer, size, file);
90  if( line != NULL )
91  {
92  linelen = (int)strlen(line);
93  if( linelen == size-1 )
94  {
95  char s[SCIP_MAXSTRLEN];
96  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "line too long (exceeds %d characters)", size-2);
97  readError(scip, *linecount, s);
98  return SCIP_READERROR;
99  }
100  }
101  else
102  linelen = 0;
103  }
104  while( line != NULL && (*line == 'c' || *line == '\n') );
105 
106  if( line != NULL && linelen >= 2 && line[linelen-2] == '\n' )
107  line[linelen-2] = '\0';
108  else if( linelen == 0 )
109  *buffer = '\0';
110 
111  assert((line == NULL) == (*buffer == '\0'));
112 
113  return SCIP_OKAY;
114 }
115 
116 /* Read SAT formula in "CNF File Format".
117  *
118  * The specification is taken from the
119  *
120  * Satisfiability Suggested Format
121  *
122  * Online available at http://www.intellektik.informatik.tu-darmstadt.de/SATLIB/Benchmarks/SAT/satformat.ps
123  *
124  * The method reads all files of CNF format. Other formats (SAT, SATX, SATE) are not supported.
125  */
126 static
128  SCIP* scip, /**< SCIP data structure */
129  SCIP_FILE* file /**< input file */
130  )
131 {
132  SCIP_RETCODE retcode;
133  SCIP_VAR** vars;
134  SCIP_VAR** clausevars;
135  SCIP_CONS* cons;
136  int* varsign;
137  char* tok;
138  char* nexttok;
139  char line[MAXLINELEN];
140  char format[SCIP_MAXSTRLEN];
141  char varname[SCIP_MAXSTRLEN];
142  char s[SCIP_MAXSTRLEN];
143  SCIP_Bool initialconss;
144  SCIP_Bool dynamicconss;
145  SCIP_Bool dynamiccols;
146  SCIP_Bool dynamicrows;
147  SCIP_Bool useobj;
148  int linecount;
149  int clauselen;
150  int clausenum;
151  int nvars;
152  int nclauses;
153  int varnum;
154  int v;
155 
156  assert(scip != NULL);
157  assert(file != NULL);
158 
159  linecount = 0;
160 
161  /* read header */
162  SCIP_CALL( readCnfLine(scip, file, line, (int) sizeof(line), &linecount) );
163  if( *line != 'p' )
164  {
165  readError(scip, linecount, "problem declaration line expected");
166  return SCIP_READERROR;
167  }
168  if( sscanf(line, "p %8s %d %d", format, &nvars, &nclauses) != 3 )
169  {
170  readError(scip, linecount, "invalid problem declaration (must be 'p cnf <nvars> <nclauses>')");
171  return SCIP_READERROR;
172  }
173  if( strcmp(format, "cnf") != 0 )
174  {
175  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid format tag <%s> (must be 'cnf')", format);
176  readError(scip, linecount, s);
177  return SCIP_READERROR;
178  }
179  if( nvars <= 0 )
180  {
181  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid number of variables <%d> (must be positive)", nvars);
182  readError(scip, linecount, s);
183  return SCIP_READERROR;
184  }
185  if( nclauses <= 0 )
186  {
187  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid number of clauses <%d> (must be positive)", nclauses);
188  readError(scip, linecount, s);
189  return SCIP_READERROR;
190  }
191 
192  /* get parameter values */
193  SCIP_CALL( SCIPgetBoolParam(scip, "reading/initialconss", &initialconss) );
194  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicconss", &dynamicconss) );
195  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamiccols", &dynamiccols) );
196  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicrows", &dynamicrows) );
197  SCIP_CALL( SCIPgetBoolParam(scip, "reading/cnfreader/useobj", &useobj) );
198 
199  /* get temporary memory */
200  SCIP_CALL( SCIPallocBufferArray(scip, &vars, nvars) );
201  SCIP_CALL( SCIPallocBufferArray(scip, &clausevars, nvars) );
202  SCIP_CALL( SCIPallocBufferArray(scip, &varsign, nvars) );
203 
204  /* create the variables */
205  for( v = 0; v < nvars; ++v )
206  {
207  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "x%d", v+1);
208  SCIP_CALL( SCIPcreateVar(scip, &vars[v], varname, 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY, !dynamiccols, dynamiccols,
209  NULL, NULL, NULL, NULL, NULL) );
210  SCIP_CALL( SCIPaddVar(scip, vars[v]) );
211  varsign[v] = 0;
212  }
213 
214  /* read clauses */
215  clausenum = 0;
216  clauselen = 0;
217  do
218  {
219  retcode = readCnfLine(scip, file, line, (int) sizeof(line), &linecount);
220  if( retcode != SCIP_OKAY )
221  goto TERMINATE;
222 
223  if( *line != '\0' && *line != '%' )
224  {
225  tok = SCIPstrtok(line, " \f\n\r\t", &nexttok);
226  while( tok != NULL )
227  {
228  /* parse literal and check for errors */
229  if( sscanf(tok, "%d", &v) != 1 )
230  {
231  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid literal <%s>", tok);
232  readError(scip, linecount, s);
233  retcode = SCIP_READERROR;
234  goto TERMINATE;
235  }
236 
237  /* interpret literal number: v == 0: end of clause, v < 0: negated literal, v > 0: positive literal */
238  if( v == 0 )
239  {
240  /* end of clause: construct clause and add it to SCIP */
241  if( clauselen == 0 )
242  readWarning(scip, linecount, "empty clause detected in line -- problem infeasible");
243 
244  clausenum++;
245  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "c%d", clausenum);
246 
247  if( SCIPfindConshdlr(scip, "logicor") != NULL )
248  {
249  /* if the constraint handler logicor exit create a logicor constraint */
250  SCIP_CALL( SCIPcreateConsLogicor(scip, &cons, s, clauselen, clausevars,
251  initialconss, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) );
252  }
253  else if( SCIPfindConshdlr(scip, "setppc") != NULL )
254  {
255  /* if the constraint handler logicor does not exit but constraint
256  * handler setppc create a setppc constraint */
257  SCIP_CALL( SCIPcreateConsSetcover(scip, &cons, s, clauselen, clausevars,
258  initialconss, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) );
259  }
260  else
261  {
262  /* if none of the previous constraint handler exits create a linear
263  * constraint */
264  SCIP_Real* vals;
265  int i;
266 
267  SCIP_CALL( SCIPallocBufferArray(scip, &vals, clauselen) );
268 
269  for( i = 0; i < clauselen; ++i )
270  vals[i] = 1.0;
271 
272  SCIP_CALL( SCIPcreateConsLinear(scip, &cons, s, clauselen, clausevars, vals, 1.0, SCIPinfinity(scip),
273  initialconss, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) );
274 
275  SCIPfreeBufferArray(scip, &vals);
276  }
277 
278  SCIP_CALL( SCIPaddCons(scip, cons) );
279  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
280  clauselen = 0;
281  }
282  else if( v >= -nvars && v <= nvars )
283  {
284  if( clauselen >= nvars )
285  {
286  readError(scip, linecount, "too many literals in clause");
287  retcode = SCIP_READERROR;
288  goto TERMINATE;
289  }
290 
291  /* add literal to clause */
292  varnum = ABS(v)-1;
293  if( v < 0 )
294  {
295  SCIP_CALL( SCIPgetNegatedVar(scip, vars[varnum], &clausevars[clauselen]) );
296  varsign[varnum]--;
297  }
298  else
299  {
300  clausevars[clauselen] = vars[varnum];
301  varsign[varnum]++;
302  }
303  clauselen++;
304  }
305  else
306  {
307  (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid variable number <%d>", ABS(v));
308  readError(scip, linecount, s);
309  retcode = SCIP_READERROR;
310  goto TERMINATE;
311  }
312 
313  /* get next token */
314  tok = SCIPstrtok(NULL, " \f\n\r\t", &nexttok);
315  }
316  }
317  }
318  while( *line != '\0' && *line != '%' );
319 
320  /* check for additional literals */
321  if( clauselen > 0 )
322  {
323  SCIPwarningMessage(scip, "found %d additional literals after last clause\n", clauselen);
324  }
325 
326  /* check number of clauses */
327  if( clausenum != nclauses )
328  {
329  SCIPwarningMessage(scip, "expected %d clauses, but found %d\n", nclauses, clausenum);
330  }
331 
332  TERMINATE:
333  /* change objective values and release variables */
335  for( v = 0; v < nvars; ++v )
336  {
337  if( useobj )
338  {
339  SCIP_CALL( SCIPchgVarObj(scip, vars[v], (SCIP_Real)varsign[v]) );
340  }
341  SCIP_CALL( SCIPreleaseVar(scip, &vars[v]) );
342  }
343 
344  /* free temporary memory */
345  SCIPfreeBufferArray(scip, &varsign);
346  SCIPfreeBufferArray(scip, &clausevars);
347  SCIPfreeBufferArray(scip, &vars);
348 
349  return retcode;
350 }
351 
352 
353 /*
354  * Callback methods
355  */
356 
357 /** copy method for reader plugins (called when SCIP copies plugins) */
358 static
359 SCIP_DECL_READERCOPY(readerCopyCnf)
360 { /*lint --e{715}*/
361  assert(scip != NULL);
362  assert(reader != NULL);
363  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
364 
365  /* call inclusion method of reader */
367 
368  return SCIP_OKAY;
369 }
370 
371 
372 /** problem reading method of reader */
373 static
374 SCIP_DECL_READERREAD(readerReadCnf)
375 { /*lint --e{715}*/
376  SCIP_FILE* f;
377  SCIP_RETCODE retcode;
378 
379  assert(reader != NULL);
380  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
381  assert(filename != NULL);
382  assert(result != NULL);
383 
384  /* open file */
385  f = SCIPfopen(filename, "r");
386  if( f == NULL )
387  {
388  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
389  SCIPprintSysError(filename);
390  return SCIP_NOFILE;
391  }
392 
393  /* create problem */
394  SCIP_CALL( SCIPcreateProb(scip, filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
395 
396  /* read cnf file */
397  retcode = readCnf(scip, f);
398 
399  /* close file */
400  SCIPfclose(f);
401 
402  *result = SCIP_SUCCESS;
403 
404  return retcode;
405 }
406 
407 
408 /*
409  * cnf file reader specific interface methods
410  */
411 
412 /** includes the cnf file reader in SCIP */
414  SCIP* scip /**< SCIP data structure */
415  )
416 {
417  SCIP_READER* reader;
418 
419  /* include reader */
421 
422  /* set non fundamental callbacks via setter functions */
423  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCnf) );
424  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCnf) );
425 
426  /* add cnf reader parameters */
428  "reading/cnfreader/useobj", "should an artificial objective, depending on the number of clauses a variable appears in, be used?",
429  NULL, FALSE, FALSE, NULL, NULL) );
430 
431  return SCIP_OKAY;
432 }
433 
static void readWarning(SCIP *scip, int linecount, const char *warningmsg)
Definition: reader_cnf.c:59
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip.c:6541
CNF file reader.
static SCIP_DECL_READERCOPY(readerCopyCnf)
Definition: reader_cnf.c:359
#define SCIP_MAXSTRLEN
Definition: def.h:215
SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
Definition: scip.c:9778
static SCIP_RETCODE readCnfLine(SCIP *scip, SCIP_FILE *file, char *buffer, int size, int *linecount)
Definition: reader_cnf.c:70
SCIP_RETCODE SCIPcreateConsSetcover(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
Definition: cons_setppc.c:9093
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:515
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip.c:18384
#define FALSE
Definition: def.h:64
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:45816
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:9340
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_RETCODE readCnf(SCIP *scip, SCIP_FILE *file)
Definition: reader_cnf.c:127
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:21937
Constraint handler for the set partitioning / packing / covering constraints .
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1260
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
SCIP_RETCODE SCIPsetObjsense(SCIP *scip, SCIP_OBJSENSE objsense)
Definition: scip.c:10898
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:12410
Constraint handler for logicor constraints (equivalent to set covering, but algorithms are suited fo...
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:187
#define READER_DESC
Definition: reader_cnf.c:38
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip.c:4369
#define NULL
Definition: lpi_spx1.cpp:137
#define SCIP_CALL(x)
Definition: def.h:306
#define READER_EXTENSION
Definition: reader_cnf.c:39
SCIP_RETCODE SCIPchgVarObj(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
Definition: scip.c:21448
SCIP_RETCODE SCIPincludeReaderCnf(SCIP *scip)
Definition: reader_cnf.c:413
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:21925
public data structures and miscellaneous methods
#define READER_NAME
Definition: reader_cnf.c:37
#define SCIP_Bool
Definition: def.h:61
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip.c:5201
void SCIPprintSysError(const char *message)
Definition: misc.c:9276
SCIP_RETCODE SCIPcreateVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
Definition: scip.c:17237
Constraint handler for linear constraints in their most general form, .
SCIP_RETCODE SCIPcreateConsLogicor(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip.c:5239
SCIP_RETCODE SCIPcreateConsLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
static SCIP_DECL_READERREAD(readerReadCnf)
Definition: reader_cnf.c:374
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:11311
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:27323
#define MAXLINELEN
Definition: reader_cnf.c:41
#define SCIP_Real
Definition: def.h:135
static void readError(SCIP *scip, int linecount, const char *errormsg)
Definition: reader_cnf.c:49
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip.c:5287
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
char * SCIPstrtok(char *s, const char *delim, char **ptrptr)
Definition: misc.c:9298
SCIP_RETCODE SCIPgetNegatedVar(SCIP *scip, SCIP_VAR *var, SCIP_VAR **negvar)
Definition: scip.c:18663
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:4176