Scippy

SCIP

Solving Constraint Integer Programs

reader_cip.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-2014 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_cip.c
17  * @brief CIP file reader
18  * @author Stefan Heinz
19  * @author Marc Pfetsch
20  * @author Michael Winkler
21  *
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include <string.h>
27 #include <ctype.h>
28 
29 #include "scip/reader_cip.h"
30 #include "scip/cons_linear.h"
31 
32 #define READER_NAME "cipreader"
33 #define READER_DESC "file reader for CIP (Constraint Integer Program) format"
34 #define READER_EXTENSION "cip"
35 
36 #define DEFAULT_CIP_WRITEFIXEDVARS TRUE /**< Should fixed and aggregated variables be written when writing? */
37 
38 
39 /** CIP reading data */
40 struct SCIP_ReaderData
41 {
42  SCIP_Bool writefixedvars; /**< Should fixed and aggregated variables be written when writing? */
43 };
44 
45 
46 /** Section of the in CIP files */
48 {
49  CIP_START, /**< start tag */
50  CIP_STATISTIC, /**< statistics section */
51  CIP_OBJECTIVE, /**< objective */
52  CIP_VARS, /**< list of (free) variables */
53  CIP_FIXEDVARS, /**< list of fixed variables */
54  CIP_CONSTRAINTS, /**< constraints */
55  CIP_END /**< end of file tag */
56 };
57 typedef enum CipSection CIPSECTION; /**< Section of the in CIP files */
58 
59 
60 /*
61  * Data structures
62  */
63 
64 /** CIP reading data */
65 struct CipInput
66 {
67  SCIP_FILE* file; /**< input file */
68  char* strbuf; /**< string buffer for input lines */
69  int len; /**< length of strbuf */
70  int readingsize; /**< size of block in which len is increased if necessary */
71  int linenumber; /**< number of line in input file */
72  CIPSECTION section; /**< current section */
73  SCIP_Bool haserror; /**< some error occurred */
74  SCIP_Bool endfile; /**< we have reached the end of the file */
75 };
76 typedef struct CipInput CIPINPUT; /**< CIP reading data */
77 
78 
79 /*
80  * Local methods for reading/parsing
81  */
82 
83 /** get next input line; this are all characters until the next semicolon */
84 static
86  SCIP* scip, /**< SCIP data structure */
87  CIPINPUT* cipinput /**< CIP parsing data */
88  )
89 {
90  char* endline;
91  char* endcharacter;
92  char* windowsendlinechar;
93 
94  assert(cipinput != NULL);
95 
96  /* read next line */
97  cipinput->endfile = (SCIPfgets(cipinput->strbuf, cipinput->len, cipinput->file) == NULL);
98 
99  if( cipinput->endfile )
100  {
101  /* clear the line for safety reason */
102  BMSclearMemoryArray(cipinput->strbuf, cipinput->len);
103  return SCIP_OKAY;
104  }
105 
106  cipinput->linenumber++;
107  endline = strchr(cipinput->strbuf, '\n');
108 
109  endcharacter = strchr(cipinput->strbuf, ';');
110  while( endline == NULL || (endcharacter == NULL && cipinput->section == CIP_CONSTRAINTS && strncmp(cipinput->strbuf, "END", 3) != 0 ) )
111  {
112  int pos;
113 
114  /* we refill the buffer from the '\n' character */
115  if( endline == NULL )
116  pos = cipinput->len - 1;
117  else
118  pos = endline - cipinput->strbuf;
119 
120  /* don't erase the '\n' from all buffers for constraints */
121  if( endline != NULL && cipinput->section == CIP_CONSTRAINTS )
122  pos++;
123 
124  /* if necessary reallocate memory */
125  if( pos + cipinput->readingsize >= cipinput->len )
126  {
127  cipinput->len = SCIPcalcMemGrowSize(scip, pos + cipinput->readingsize);
128  SCIP_CALL( SCIPreallocBufferArray(scip, &(cipinput->strbuf), cipinput->len) );
129  }
130 
131  /* read next line */
132  cipinput->endfile = (SCIPfgets(&(cipinput->strbuf[pos]), cipinput->len - pos, cipinput->file) == NULL);
133 
134  if( cipinput->endfile )
135  {
136  /* clear the line for safety reason */
137  BMSclearMemoryArray(cipinput->strbuf, cipinput->len);
138  return SCIP_OKAY;
139  }
140 
141  cipinput->linenumber++;
142  endline = strrchr(cipinput->strbuf, '\n');
143  endcharacter = strchr(cipinput->strbuf, ';');
144  }
145  assert(endline != NULL);
146 
147  /*SCIPdebugMessage("read line: %s\n", cipinput->strbuf);*/
148 
149  /* check for windows "carriage return" endline character */
150  windowsendlinechar = strrchr(cipinput->strbuf, '\r');
151  if( windowsendlinechar != NULL && windowsendlinechar + 1 == endline )
152  --endline;
153  else
154  /* if the assert should not hold we found a windows "carriage return" which was not at the end of the line */
155  assert(windowsendlinechar == NULL);
156 
157  if( cipinput->section == CIP_CONSTRAINTS && endcharacter != NULL && endline - endcharacter != 1 )
158  {
159  SCIPerrorMessage("Constraint line has to end with ';\\n' (line: %d).\n", cipinput->linenumber);
160  cipinput->haserror = TRUE;
161  return SCIP_OKAY; /* return error at hightest level */
162  }
163 
164  *endline = '\0';
165 
166  return SCIP_OKAY;
167 }
168 
169 /** read the problem name out of the statistics */
170 static
171 void getStart(
172  SCIP* scip, /**< SCIP data structure */
173  CIPINPUT* cipinput /**< CIP parsing data */
174  )
175 {
176  char* buf;
177 
178  buf = cipinput->strbuf;
179 
180  if( strncmp(buf, "STATISTICS", 9) == 0 )
181  {
182  cipinput->section = CIP_STATISTIC;
183  return;
184  }
185 
186  if( strncmp(buf, "VARIABLES", 8) == 0 || strncmp(buf, "FIXED", 5) == 0 || strncmp(buf, "CONSTRAINTS", 11) == 0 || strncmp(buf, "OBJECTIVE", 9) == 0 )
187  {
188  SCIPerrorMessage("Syntax Error: File has to start with 'STATISTICS' section.\n");
189  cipinput->haserror = TRUE;
190  }
191 }
192 
193 
194 /** read the problem name out of the statistics */
195 static
197  SCIP* scip, /**< SCIP data structure */
198  CIPINPUT* cipinput /**< CIP parsing data */
199  )
200 {
201  char* buf;
202 
203  buf = cipinput->strbuf;
204 
205  if( strncmp(buf, "OBJECTIVE", 9) == 0 )
206  {
207  cipinput->section = CIP_OBJECTIVE;
208  return SCIP_OKAY;
209  }
210 
211  SCIPdebugMessage("parse statistics\n");
212 
213  if( strncmp(buf, " Problem name", 14) == 0 )
214  {
215  char* name;
216  char* s;
217 
218  name = strchr(buf, ':');
219 
220  if( name == NULL )
221  {
222  SCIPwarningMessage(scip, "did not find problem name (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
223  return SCIP_OKAY; /* no error, might work with empty problem name */
224  }
225 
226  /* skip ':' */
227  ++name;
228 
229  /* make sure that we terminate the string at comments ('#') or newline ('\r', '\n')*/
230  if( NULL != (s = strpbrk(name, "#\r\n")) )
231  *s = '\0';
232 
233  /* remove white space (tabs, ' ') in front of the name */
234  while( isspace((unsigned char)* name) )
235  ++name;
236 
237  /* set problem name */
238  SCIP_CALL( SCIPsetProbName(scip, name) );
239 
240  SCIPdebugMessage("problem name <%s>\n", name);
241  }
242 
243  return SCIP_OKAY;
244 }
245 
246 /** read objective sense, offset, and scale */
247 static
249  SCIP* scip, /**< SCIP data structure */
250  CIPINPUT* cipinput, /**< CIP parsing data */
251  SCIP_Real* objscale, /**< buffer where to multiply with objective scale */
252  SCIP_Real* objoffset /**< buffer where to add with objective offset */
253  )
254 {
255  char* buf;
256  char* name;
257 
258  assert(objscale != NULL);
259  assert(objoffset != NULL);
260 
261  buf = cipinput->strbuf;
262 
263  if( strncmp(buf, "VARIABLES", 8) == 0 )
264  cipinput->section = CIP_VARS;
265  else if( strncmp(buf, "FIXED", 5) == 0 )
266  cipinput->section = CIP_FIXEDVARS;
267  else if( strncmp(buf, "CONSTRAINTS", 11) == 0 )
268  cipinput->section = CIP_CONSTRAINTS;
269  else if( strncmp(buf, "END", 3) == 0 )
270  cipinput->section = CIP_END;
271 
272  if( cipinput->section != CIP_OBJECTIVE )
273  return SCIP_OKAY;
274 
275  SCIPdebugMessage("parse objective information\n");
276 
277  /* remove white space */
278  while ( isspace((unsigned char)* buf) )
279  ++buf;
280 
281  if( strncasecmp(buf, "Sense", 5) == 0 )
282  {
283  SCIP_OBJSENSE objsense;
284 
285  name = strchr(buf, ':');
286 
287  if( name == NULL )
288  {
289  SCIPwarningMessage(scip, "did not find objective sense (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
290  return SCIP_OKAY; /* no error - might work with default */
291  }
292 
293  /* skip ':' */
294  ++name;
295 
296  /* remove white space in front of the name */
297  while( isspace((unsigned char)* name) )
298  ++name;
299 
300  if( strncasecmp(name, "minimize", 3) == 0 )
301  objsense = SCIP_OBJSENSE_MINIMIZE;
302  else if( strncasecmp(name, "maximize", 3) == 0 )
303  objsense = SCIP_OBJSENSE_MAXIMIZE;
304  else
305  {
306  SCIPwarningMessage(scip, "unknown objective sense '%s' (line: %d):\n%s\n", name, cipinput->linenumber, cipinput->strbuf);
307  return SCIP_OKAY; /* no error - might work with default */
308  }
309 
310  /* set problem name */
311  SCIP_CALL( SCIPsetObjsense(scip, objsense) );
312  SCIPdebugMessage("objective sense <%s>\n", objsense == SCIP_OBJSENSE_MINIMIZE ? "minimize" : "maximize");
313  }
314  else if( strncasecmp(buf, "Offset", 6) == 0 )
315  {
316  SCIP_Real off = 0;
317  char* endptr;
318 
319  name = strchr(buf, ':');
320 
321  if( name == NULL )
322  {
323  SCIPwarningMessage(scip, "did not find offset (line: %d)\n", cipinput->linenumber);
324  return SCIP_OKAY;
325  }
326 
327  /* skip ':' */
328  ++name;
329 
330  /* remove white space in front of the name */
331  while(isspace((unsigned char)*name))
332  ++name;
333 
334  if ( SCIPstrToRealValue(name, &off, &endptr) )
335  {
336  *objoffset += off;
337  SCIPdebugMessage("offset <%g> (total: %g)\n", off, *objoffset);
338  }
339  else
340  {
341  SCIPwarningMessage(scip, "could not parse offset (line: %d)\n%s\n", cipinput->linenumber, cipinput->strbuf);
342  return SCIP_OKAY;
343  }
344  }
345  else if( strncasecmp(buf, "Scale", 5) == 0 )
346  {
347  SCIP_Real scale = 1.0;
348  char* endptr;
349 
350  name = strchr(buf, ':');
351 
352  if( name == NULL )
353  {
354  SCIPwarningMessage(scip, "did not find scale (line: %d)\n", cipinput->linenumber);
355  return SCIP_OKAY;
356  }
357 
358  /* skip ':' */
359  ++name;
360 
361  /* remove white space in front of the name */
362  while(isspace((unsigned char)*name))
363  ++name;
364 
365  if ( SCIPstrToRealValue(name, &scale, &endptr) )
366  {
367  *objscale *= scale;
368  SCIPdebugMessage("objscale <%g> (total: %g)\n", scale, *objscale);
369  }
370  else
371  {
372  SCIPwarningMessage(scip, "could not parse objective scale (line: %d)\n%s\n", cipinput->linenumber, cipinput->strbuf);
373  return SCIP_OKAY;
374  }
375  }
376 
377  return SCIP_OKAY;
378 }
379 
380 /** read variable */
381 static
383  SCIP* scip, /**< SCIP data structure */
384  CIPINPUT* cipinput, /**< CIP parsing data */
385  SCIP_Bool initial, /**< should var's column be present in the initial root LP? */
386  SCIP_Bool removable, /**< is var's column removable from the LP (due to aging or cleanup)? */
387  SCIP_Real objscale /**< objective scale */
388  )
389 {
390  SCIP_Bool success;
391  SCIP_VAR* var;
392  char* buf;
393  char* endptr;
394 
395  buf = cipinput->strbuf;
396 
397  if( strncmp(buf, "FIXED", 5) == 0 )
398  cipinput->section = CIP_FIXEDVARS;
399  else if( strncmp(buf, "CONSTRAINTS", 4) == 0 )
400  cipinput->section = CIP_CONSTRAINTS;
401  else if( strncmp(buf, "END", 3) == 0 )
402  cipinput->section = CIP_END;
403 
404  if( cipinput->section != CIP_VARS )
405  return SCIP_OKAY;
406 
407  SCIPdebugMessage("parse variable\n");
408 
409  /* parse the variable */
410  SCIP_CALL( SCIPparseVar(scip, &var, buf, initial, removable, NULL, NULL, NULL, NULL, NULL, &endptr, &success) );
411 
412  if( !success )
413  {
414  SCIPerrorMessage("syntax error in variable information (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
415  cipinput->haserror = TRUE;
416  return SCIP_OKAY;
417  }
418 
419  if( objscale != 1.0 )
420  {
421  SCIP_CALL( SCIPchgVarObj(scip, var, SCIPvarGetObj(var) * objscale) );
422  }
423 
424  SCIP_CALL( SCIPaddVar(scip, var) );
425 
426  SCIPdebug( SCIP_CALL( SCIPprintVar(scip, var, NULL) ) );
427 
428  SCIP_CALL( SCIPreleaseVar(scip, &var) );
429 
430  return SCIP_OKAY;
431 }
432 
433 /** read fixed variable */
434 static
436  SCIP* scip, /**< SCIP data structure */
437  CIPINPUT* cipinput /**< CIP parsing data */
438  )
439 {
440  SCIP_Bool success;
441  SCIP_VAR* var;
442  char* buf;
443  char* endptr;
444  char name[SCIP_MAXSTRLEN];
445 
446  buf = cipinput->strbuf;
447 
448  if( strncmp(buf, "CONSTRAINTS", 11) == 0 )
449  cipinput->section = CIP_CONSTRAINTS;
450  else if( strncmp(buf, "END", 3) == 0 )
451  cipinput->section = CIP_END;
452 
453  if( cipinput->section != CIP_FIXEDVARS )
454  return SCIP_OKAY;
455 
456  SCIPdebugMessage("parse fixed variable\n");
457 
458  /* parse the variable */
459  SCIP_CALL( SCIPparseVar(scip, &var, buf, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL, &endptr, &success) );
460 
461  if( !success )
462  {
463  SCIPerrorMessage("syntax error in variable information (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
464  cipinput->haserror = TRUE;
465  return SCIP_OKAY;
466  }
467 
468  /* skip intermediate stuff */
469  buf = endptr;
470 
471  while ( *buf != '\0' && (*buf == ' ' || *buf == ',') )
472  ++buf;
473 
474  /* check whether variable is fixed */
475  if ( strncmp(buf, "fixed:", 6) == 0 )
476  {
477  SCIP_CALL( SCIPaddVar(scip, var) );
478  SCIPdebug( SCIP_CALL( SCIPprintVar(scip, var, NULL) ) );
479  }
480  else if ( strncmp(buf, "negated:", 8) == 0 )
481  {
482  SCIP_CONS* lincons;
483  SCIP_VAR* negvar;
484  SCIP_Real vals[2];
485  SCIP_VAR* vars[2];
486 
487  buf += 8;
488 
489  /* we can just parse the next variable (ignoring all other information in between) */
490  SCIP_CALL( SCIPparseVarName(scip, buf, &negvar, &endptr) );
491 
492  if ( negvar == NULL )
493  {
494  SCIPerrorMessage("could not parse negated variable (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
495  cipinput->haserror = TRUE;
496  return SCIP_OKAY;
497  }
498  assert(SCIPvarIsBinary(var));
499  assert(SCIPvarIsBinary(negvar));
500 
501  SCIP_CALL( SCIPaddVar(scip, var) );
502 
503  SCIPdebugMessage("creating negated variable <%s> (of <%s>) ...\n", SCIPvarGetName(var), SCIPvarGetName(negvar) );
504  SCIPdebug( SCIP_CALL( SCIPprintVar(scip, var, NULL) ) );
505 
506  /* add linear constraint for negation */
507  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "neg_%s", SCIPvarGetName(var) );
508  vars[0] = var;
509  vars[1] = negvar;
510  vals[0] = 1.0;
511  vals[1] = 1.0;
512  SCIPdebugMessage("coupling constraint:\n");
513  SCIP_CALL( SCIPcreateConsLinear(scip, &lincons, name, 2, vars, vals, 1.0, 1.0, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) );
514  SCIPdebugPrintCons(scip, lincons, NULL);
515  SCIP_CALL( SCIPaddCons(scip, lincons) );
516  SCIP_CALL( SCIPreleaseCons(scip, &lincons) );
517  }
518  else if ( strncmp(buf, "aggregated:", 11) == 0 )
519  {
520  /* handle (multi-)aggregated variables */
521  SCIP_CONS* lincons;
522  SCIP_Real* vals;
523  SCIP_VAR** vars;
524  SCIP_Real rhs = 0.0;
525  const char* str;
526  int nvarssize = 20;
527  int requsize;
528  int nvars;
529 
530  buf += 11;
531 
532  SCIPdebugMessage("parsing aggregated variable <%s> ...\n", SCIPvarGetName(var));
533 
534  /* first parse constant */
535  if ( ! SCIPstrToRealValue(buf, &rhs, &endptr) )
536  {
537  SCIPerrorMessage("expected constant when aggregated variable information (line: %d):\n%s\n", cipinput->linenumber, buf);
538  cipinput->haserror = TRUE;
539  return SCIP_OKAY;
540  }
541 
542  /* check whether constant is 0.0 */
543  str = endptr;
544  while ( *str != '\0' && isspace(*str) )
545  ++str;
546  /* if next char is '<' we found a variable -> constant is 0 */
547  if ( *str != '<' )
548  {
549  SCIPdebugMessage("constant: %f\n", rhs);
550  buf = endptr;
551  }
552  else
553  {
554  /* otherwise keep buf */
555  rhs = 0.0;
556  }
557 
558  /* initialize buffers for storing the variables and values */
559  SCIP_CALL( SCIPallocBufferArray(scip, &vars, nvarssize) );
560  SCIP_CALL( SCIPallocBufferArray(scip, &vals, nvarssize) );
561 
562  vars[0] = var;
563  vals[0] = -1.0;
564  --nvarssize;
565 
566  /* parse linear sum to get variables and coefficients */
567  SCIP_CALL( SCIPparseVarsLinearsum(scip, buf, &(vars[1]), &(vals[1]), &nvars, nvarssize, &requsize, &endptr, &success) );
568  if ( success && requsize > nvarssize )
569  {
570  /* realloc buffers and try again */
571  nvarssize = requsize;
572  SCIP_CALL( SCIPreallocBufferArray(scip, &vars, nvarssize + 1) );
573  SCIP_CALL( SCIPreallocBufferArray(scip, &vals, nvarssize + 1) );
574 
575  SCIP_CALL( SCIPparseVarsLinearsum(scip, buf, &(vars[1]), &(vals[1]), &nvars, nvarssize, &requsize, &endptr, &success) );
576  assert( ! success || requsize <= nvarssize); /* if successful, then should have had enough space now */
577  }
578 
579  if( success )
580  {
581  /* add aggregated variable */
582  SCIP_CALL( SCIPaddVar(scip, var) );
583 
584  /* special handling of variables that seem to be slack variables of indicator constraints */
585  str = SCIPvarGetName(var);
586  if ( strncmp(str, "indslack", 8) == 0 )
587  {
588  (void) strcpy(name, "indlin");
589  (void) strcat(name, str+8);
590  }
591  else if ( strncmp(str, "t_indslack", 10) == 0 )
592  {
593  (void) strcpy(name, "indlin");
594  (void) strcat(name, str+10);
595  }
596  else
597  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(var) );
598 
599  /* add linear constraint for (multi-)aggregation */
600  SCIPdebugMessage("coupling constraint:\n");
601  SCIP_CALL( SCIPcreateConsLinear(scip, &lincons, name, nvars + 1, vars, vals, -rhs, -rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) );
602  SCIPdebugPrintCons(scip, lincons, NULL);
603  SCIP_CALL( SCIPaddCons(scip, lincons) );
604  SCIP_CALL( SCIPreleaseCons(scip, &lincons) );
605  }
606  else
607  {
608  SCIPwarningMessage(scip, "Could not read (multi-)aggregated variable <%s>: dependent variables unkown - consider changing the order (line: %d):\n%s\n",
609  SCIPvarGetName(var), cipinput->linenumber, buf);
610  }
611 
612  SCIPfreeBufferArray(scip, &vals);
613  SCIPfreeBufferArray(scip, &vars);
614  }
615  else
616  {
617  SCIPerrorMessage("unknown section when parsing variables (line: %d):\n%s\n", cipinput->linenumber, buf);
618  cipinput->haserror = TRUE;
619  return SCIP_OKAY;
620  }
621  SCIP_CALL( SCIPreleaseVar(scip, &var) );
622 
623  return SCIP_OKAY;
624 }
625 
626 /** read constraint */
627 static
629  SCIP* scip, /**< SCIP data structure */
630  CIPINPUT* cipinput, /**< CIP parsing data */
631  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
632  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
633  SCIP_Bool dynamic, /**< Is constraint subject to aging?
634  * Usually set to FALSE. Set to TRUE for own cuts which
635  * are separated as constraints. */
636  SCIP_Bool removable /**< should the relaxation be removed from the LP due to aging or cleanup?
637  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
638  )
639 {
640  SCIP_CONS* cons;
641  char* buf;
642  char* copybuf;
643  int len;
644 
645  SCIP_Bool separate;
646  SCIP_Bool enforce;
647  SCIP_Bool check;
648  SCIP_Bool propagate;
649  SCIP_Bool local;
650  SCIP_Bool modifiable;
651 
652  SCIP_Bool success;
653 
654  buf = cipinput->strbuf;
655 
656  if( strncmp(buf, "END", 3) == 0 )
657  {
658  cipinput->section = CIP_END;
659  return SCIP_OKAY;
660  }
661 
662  SCIPdebugMessage("parse constraints in line %d\n", cipinput->linenumber);
663 
664  separate = TRUE;
665  enforce = TRUE;
666  check = TRUE;
667  propagate = TRUE;
668  local = FALSE;
669  modifiable = FALSE;
670 
671  /* get length of line and check for correct ending of constraint line */
672  len = (int)strlen(buf);
673  if( len < 1 )
674  {
675  SCIPerrorMessage("syntax error: expected constraint in line %d.\n", cipinput->linenumber);
676  cipinput->haserror = TRUE;
677  return SCIP_OKAY;
678  }
679  if ( buf[len - 1] != ';' )
680  {
681  SCIPerrorMessage("syntax error: line has to end with ';' (line: %d)\n", cipinput->linenumber);
682  cipinput->haserror = TRUE;
683  return SCIP_OKAY;
684  }
685 
686  /* copy buffer for working purpose */
687  SCIP_CALL( SCIPduplicateMemoryArray(scip, &copybuf, buf, len) );
688  copybuf[len - 1] = '\0';
689 
690  /* parse the constraint */
691  SCIP_CALL( SCIPparseCons(scip, &cons, copybuf,
692  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, FALSE, &success) );
693 
694  /* free temporary buffer */
695  SCIPfreeMemoryArray(scip, &copybuf);
696 
697  if( !success )
698  {
699  SCIPerrorMessage("syntax error when reading constraint (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf);
700  cipinput->haserror = TRUE;
701  return SCIP_OKAY;
702  }
703 
704  SCIP_CALL( SCIPaddCons(scip, cons) );
705  SCIPdebugPrintCons(scip, cons, NULL);
706  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
707 
708  return SCIP_OKAY;
709 }
710 
711 /*
712  * Callback methods of reader
713  */
714 
715 /** copy method for reader plugins (called when SCIP copies plugins) */
716 static
717 SCIP_DECL_READERCOPY(readerCopyCip)
718 { /*lint --e{715}*/
719  assert(scip != NULL);
720  assert(reader != NULL);
721  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
722 
723  /* call inclusion method of reader */
725 
726  return SCIP_OKAY;
727 }
728 
729 /** destructor of reader to free user data (called when SCIP is exiting) */
730 static
731 SCIP_DECL_READERFREE(readerFreeCip)
732 {
733  SCIP_READERDATA* readerdata;
734 
735  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
736  readerdata = SCIPreaderGetData(reader);
737  assert(readerdata != NULL);
738  SCIPfreeMemory(scip, &readerdata);
739 
740  return SCIP_OKAY;
741 }
742 
743 
744 /** problem reading method of reader */
745 static
746 SCIP_DECL_READERREAD(readerReadCip)
747 { /*lint --e{715}*/
748 
749  CIPINPUT cipinput;
750  SCIP_Real objscale;
751  SCIP_Real objoffset;
752  SCIP_Bool initialconss;
753  SCIP_Bool dynamicconss;
754  SCIP_Bool dynamiccols;
755  SCIP_Bool dynamicrows;
756  SCIP_Bool initialvar;
757  SCIP_Bool removablevar;
758  SCIP_RETCODE retcode;
759 
760  if( NULL == (cipinput.file = SCIPfopen(filename, "r")) )
761  {
762  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
763  SCIPprintSysError(filename);
764  return SCIP_NOFILE;
765  }
766 
767  cipinput.len = 131071;
768  SCIP_CALL( SCIPallocBufferArray(scip, &(cipinput.strbuf), cipinput.len) );
769 
770  cipinput.linenumber = 0;
771  cipinput.section = CIP_START;
772  cipinput.haserror = FALSE;
773  cipinput.endfile = FALSE;
774  cipinput.readingsize = 65535;
775 
776  SCIP_CALL( SCIPcreateProb(scip, filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
777 
778  SCIP_CALL( SCIPgetBoolParam(scip, "reading/initialconss", &initialconss) );
779  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamiccols", &dynamiccols) );
780  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicconss", &dynamicconss) );
781  SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicrows", &dynamicrows) );
782 
783  initialvar = !dynamiccols;
784  removablevar = dynamiccols;
785 
786  objscale = 1.0;
787  objoffset = 0.0;
788 
789  while( cipinput.section != CIP_END && !cipinput.haserror )
790  {
791  /* get next input string */
792  SCIP_CALL( getInputString(scip, &cipinput) );
793 
794  if( cipinput.endfile )
795  break;
796 
797  switch( cipinput.section )
798  {
799  case CIP_START:
800  getStart(scip, &cipinput);
801  break;
802  case CIP_STATISTIC:
803  SCIP_CALL( getStatistics(scip, &cipinput) );
804  break;
805  case CIP_OBJECTIVE:
806  SCIP_CALL( getObjective(scip, &cipinput, &objscale, &objoffset) );
807  break;
808  case CIP_VARS:
809  retcode = getVariable(scip, &cipinput, initialvar, removablevar, objscale);
810 
811  if( retcode == SCIP_READERROR )
812  {
813  cipinput.haserror = TRUE;
814  goto TERMINATE;
815  }
816  SCIP_CALL(retcode);
817 
818  break;
819  case CIP_FIXEDVARS:
820  retcode = getFixedVariable(scip, &cipinput);
821 
822  if( retcode == SCIP_READERROR )
823  {
824  cipinput.haserror = TRUE;
825  goto TERMINATE;
826  }
827  SCIP_CALL(retcode);
828 
829  break;
830  case CIP_CONSTRAINTS:
831  retcode = getConstraint(scip, &cipinput, initialconss, dynamicconss, dynamicrows);
832 
833  if( retcode == SCIP_READERROR )
834  {
835  cipinput.haserror = TRUE;
836  goto TERMINATE;
837  }
838  SCIP_CALL(retcode);
839 
840  break;
841  default:
842  SCIPerrorMessage("invalid CIP state\n");
843  SCIPABORT();
844  return SCIP_INVALIDDATA; /*lint !e527*/
845  } /*lint !e788*/
846  }
847 
848  if( !SCIPisZero(scip, objoffset) && !cipinput.haserror )
849  {
850  SCIP_VAR* objoffsetvar;
851 
852  objoffset *= objscale;
853  SCIP_CALL( SCIPcreateVar(scip, &objoffsetvar, "objoffset", objoffset, objoffset, 1.0, SCIP_VARTYPE_CONTINUOUS,
854  TRUE, TRUE, NULL, NULL, NULL, NULL, NULL) );
855  SCIP_CALL( SCIPaddVar(scip, objoffsetvar) );
856  SCIP_CALL( SCIPreleaseVar(scip, &objoffsetvar) );
857  SCIPdebugMessage("added variables <objoffset> for objective offset of <%g>\n", objoffset);
858  }
859 
860  if( cipinput.section != CIP_END && !cipinput.haserror )
861  {
862  SCIPerrorMessage("unexpected EOF\n");
863  }
864 
865  TERMINATE:
866  /* close file stream */
867  SCIPfclose(cipinput.file);
868 
869  SCIPfreeBufferArray(scip, &cipinput.strbuf);
870 
871  if( cipinput.haserror )
872  return SCIP_READERROR;
873 
874  /* successfully parsed cip format */
875  *result = SCIP_SUCCESS;
876  return SCIP_OKAY;
877 }
878 
879 /** hash key retrieval function for variables */
880 static
881 SCIP_DECL_HASHGETKEY(hashGetKeyVar)
882 { /*lint --e{715}*/
883  return elem;
884 }
885 
886 /** returns TRUE iff the indices of both variables are equal */
887 static
888 SCIP_DECL_HASHKEYEQ(hashKeyEqVar)
889 { /*lint --e{715}*/
890  if( key1 == key2 )
891  return TRUE;
892  return FALSE;
893 }
894 
895 /** returns the hash value of the key */
896 static
897 SCIP_DECL_HASHKEYVAL(hashKeyValVar)
898 { /*lint --e{715}*/
899  assert( SCIPvarGetIndex((SCIP_VAR*) key) >= 0 );
900  return (unsigned int) SCIPvarGetIndex((SCIP_VAR*) key);
901 }
902 
903 /** problem writing method of reader */
904 static
905 SCIP_DECL_READERWRITE(readerWriteCip)
906 { /*lint --e{715}*/
907  SCIP_HASHTABLE* varhash = NULL;
908  SCIP_READERDATA* readerdata;
909  int i;
910 
911  assert(reader != NULL);
912  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
913 
914  SCIPinfoMessage(scip, file, "STATISTICS\n");
915  SCIPinfoMessage(scip, file, " Problem name : %s\n", name);
916  SCIPinfoMessage(scip, file, " Variables : %d (%d binary, %d integer, %d implicit integer, %d continuous)\n",
917  nvars, nbinvars, nintvars, nimplvars, ncontvars);
918  SCIPinfoMessage(scip, file, " Constraints : %d initial, %d maximal\n", startnconss, maxnconss);
919 
920  SCIPinfoMessage(scip, file, "OBJECTIVE\n");
921  SCIPinfoMessage(scip, file, " Sense : %s\n", objsense == SCIP_OBJSENSE_MINIMIZE ? "minimize" : "maximize");
922  if( !SCIPisZero(scip, objoffset) )
923  SCIPinfoMessage(scip, file, " Offset : %+.15g\n", objoffset);
924  if( !SCIPisEQ(scip, objscale, 1.0) )
925  SCIPinfoMessage(scip, file, " Scale : %.15g\n", objscale);
926 
927  if ( nfixedvars > 0 )
928  {
929  /* set up hash table for variables that have been written property (used for writing out fixed vars in the right order) */
930  SCIP_CALL( SCIPhashtableCreate(&varhash, SCIPblkmem(scip), SCIPcalcHashtableSize(10 * (nvars + nfixedvars)), hashGetKeyVar, hashKeyEqVar, hashKeyValVar, NULL) );
931  }
932 
933  if ( nvars + nfixedvars > 0 )
934  {
935  SCIPinfoMessage(scip, file, "VARIABLES\n");
936  }
937 
938  if( nvars > 0 )
939  {
940  for( i = 0; i < nvars; ++i )
941  {
942  SCIP_VAR* var;
943 
944  var = vars[i];
945  assert( var != NULL );
946  SCIP_CALL( SCIPprintVar(scip, var, file) );
947  if ( varhash != NULL )
948  {
949  /* add free variable to hashtable */
950  if ( ! SCIPhashtableExists(varhash, (void*) var) )
951  {
952  SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) );
953  }
954  }
955  }
956  }
957 
958  readerdata = SCIPreaderGetData(reader);
959  assert(readerdata != NULL);
960 
961  if( readerdata->writefixedvars && nfixedvars > 0 )
962  {
963  int nwritten = 0;
964 
965  SCIPinfoMessage(scip, file, "FIXED\n");
966 
967  /* loop through variables until each has been written after the variables that it depends on have been written; this
968  * requires several runs over the variables, but the depth (= number of loops) is usually small. */
969  while ( nwritten < nfixedvars )
970  {
971  SCIPdebugMessage("written %d of %d fixed variables.\n", nwritten, nfixedvars);
972  for (i = 0; i < nfixedvars; ++i)
973  {
974  SCIP_VAR* var;
975  SCIP_VAR* tmpvar;
976 
977  var = fixedvars[i];
978  assert( var != NULL );
979 
980  /* skip variables already written */
981  if ( SCIPhashtableExists(varhash, (void*) var) )
982  continue;
983 
984  switch ( SCIPvarGetStatus(var) )
985  {
987 
988  /* fixed variables can simply be output and added to the hashtable */
989  SCIP_CALL( SCIPprintVar(scip, var, file) );
990  assert( ! SCIPhashtableExists(varhash, (void*) var) );
991  SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) );
992  ++nwritten;
993 
994  break;
995 
997 
998  tmpvar = SCIPvarGetNegationVar(var);
999  assert( tmpvar != NULL );
1000  assert( var == SCIPvarGetNegatedVar(tmpvar) );
1001 
1002  /* if the negated variable has been written, we can write the current variable */
1003  if ( SCIPhashtableExists(varhash, (void*) tmpvar) )
1004  {
1005  SCIP_CALL( SCIPprintVar(scip, var, file) );
1006  assert( ! SCIPhashtableExists(varhash, (void*) var) );
1007  SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) );
1008  ++nwritten;
1009  }
1010  break;
1011 
1013 
1014  tmpvar = SCIPvarGetAggrVar(var);
1015  assert( tmpvar != NULL );
1016 
1017  /* if the aggregating variable has been written, we can write the current variable */
1018  if ( SCIPhashtableExists(varhash, (void*) tmpvar) )
1019  {
1020  SCIP_CALL( SCIPprintVar(scip, var, file) );
1021  assert( ! SCIPhashtableExists(varhash, (void*) var) );
1022  SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) );
1023  ++nwritten;
1024  }
1025  break;
1026 
1028  {
1029  SCIP_VAR** aggrvars;
1030  int naggrvars;
1031  int j;
1032 
1033  /* get the active representation */
1035 
1036  naggrvars = SCIPvarGetMultaggrNVars(var);
1037  aggrvars = SCIPvarGetMultaggrVars(var);
1038  assert(aggrvars != NULL || naggrvars == 0);
1039 
1040  for (j = 0; j < naggrvars; ++j)
1041  {
1042  if( !SCIPhashtableExists(varhash, (void*) aggrvars[j]) ) /*lint !e613*/
1043  break;
1044  }
1045 
1046  /* if all multi-aggregating variables have been written, we can write the current variable */
1047  if ( j >= naggrvars )
1048  {
1049  SCIP_CALL( SCIPprintVar(scip, var, file) );
1050  assert( ! SCIPhashtableExists(varhash, (void*) var) );
1051  SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) );
1052  ++nwritten;
1053  }
1054  break;
1055  }
1056 
1058  case SCIP_VARSTATUS_LOOSE:
1059  case SCIP_VARSTATUS_COLUMN:
1060  SCIPerrorMessage("Only fixed variables are allowed to be present in fixedvars list.\n");
1061  SCIPABORT();
1062  return SCIP_ERROR; /*lint !e527*/
1063  }
1064  }
1065  }
1066  }
1067 
1068  if( nconss > 0 )
1069  {
1070  SCIPinfoMessage(scip, file, "CONSTRAINTS\n");
1071 
1072  for( i = 0; i < nconss; ++i )
1073  {
1074  SCIP_CALL( SCIPprintCons(scip, conss[i], file) );
1075  SCIPinfoMessage(scip, file, ";\n");
1076  }
1077  }
1078  SCIPinfoMessage(scip, file, "END\n");
1079 
1080  *result = SCIP_SUCCESS;
1081 
1082  if( nfixedvars > 0 )
1083  SCIPhashtableFree(&varhash);
1084  else
1085  assert(varhash == NULL);
1086 
1087  return SCIP_OKAY;
1088 }
1089 
1090 
1091 /*
1092  * reader specific interface methods
1093  */
1094 
1095 /** includes the cip file reader in SCIP */
1097  SCIP* scip /**< SCIP data structure */
1098  )
1099 {
1100  SCIP_READERDATA* readerdata;
1101  SCIP_READER* reader;
1102 
1103  /* create cip reader data */
1104  SCIP_CALL( SCIPallocMemory(scip, &readerdata) );
1105 
1106  /* include reader */
1107  SCIP_CALL( SCIPincludeReaderBasic(scip, &reader, READER_NAME, READER_DESC, READER_EXTENSION, readerdata) );
1108 
1109  /* set non fundamental callbacks via setter functions */
1110  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCip) );
1111  SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeCip) );
1112  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCip) );
1113  SCIP_CALL( SCIPsetReaderWrite(scip, reader, readerWriteCip) );
1114 
1115  /* add cip reader parameters */
1117  "reading/cipreader/writefixedvars", "should fixed and aggregated variables be printed (if not, re-parsing might fail)",
1118  &readerdata->writefixedvars, FALSE, DEFAULT_CIP_WRITEFIXEDVARS, NULL, NULL) );
1119 
1120  return SCIP_OKAY;
1121 }
1122