Scippy

SCIP

Solving Constraint Integer Programs

reader_dec.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-2020 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 visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file reader_dec.c
17  * @brief file reader for decompositions in the constraint based dec-file format.
18  * @author Gregor Hendel
19  *
20  * This reader allows to read a file containing decompositions for constraints of the current original problem. The
21  * standard line ending for this format is '.dec'. The content of the file should obey the following format
22  *
23  * \\ place for comments and statistics
24  * NBLOCKS
25  * 2
26  * BLOCK 0
27  * consA
28  * consB
29  * [...]
30  * BLOCK 1
31  * consC
32  * consD
33  * [...]
34  * MASTERCONSS
35  * linkingcons
36  *
37  * A block in a problem decomposition is a set of constraints that are independent from all other blocks after removing
38  * the special blocks of linking constraints denoted as MASTERCONSS.
39  *
40  * Imagine the following example, which involves seven variables
41  * and the five constraints from the file above. The asterisks (*) indicate that the variable affects the feasibility
42  * of the constraint. In the special case of a linear optimization problem, the asterisks correspond to the
43  * nonzero entries of the constraint matrix.
44  *
45  * x1 x2 x3 x4 x5 x6 x7
46  * consA * * \ BLOCK 0
47  * consB * * /
48  * consC * * \ BLOCK 1
49  * consD * * /
50  * linkingconss * * * * * * * > MASTERCONSS
51  *
52  * The nonzero pattern has been chosen in a way that after the removal of the last constraint 'linkingcons', the remaining problem
53  * consists of two independent parts, namely the blocks '0' and '1'.
54  *
55  * The corresponding variable labels are inferred from the constraint labels. A variable is assigned the label
56  *
57  * - of its unique block, if it only occurs in exactly 1 named block, and probably in MASTERCONSS.
58  * - the special label of a linking variable if it occurs only in the master constraints or in 2 or even more named blocks.
59  *
60  * @note A trivial decomposition is to assign all constraints of a problem to the MASTERCONSS.
61  *
62  * @note The number of blocks is the number of named blocks: a trivial decomposition should have 0 blocks
63  */
64 
65 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
66 
67 #include "scip/pub_dcmp.h"
68 #include "scip/pub_fileio.h"
69 #include "scip/pub_message.h"
70 #include "scip/pub_misc.h"
71 #include "scip/pub_reader.h"
72 #include "scip/pub_var.h"
73 #include "scip/reader_dec.h"
74 #include "scip/scip_dcmp.h"
75 #include "scip/scip_general.h"
76 #include "scip/scip_message.h"
77 #include "scip/scip_numerics.h"
78 #include "scip/scip_param.h"
79 #include "scip/scip_prob.h"
80 #include "scip/scip_reader.h"
81 #include "scip/scip_solve.h"
82 #include "scip/scip_var.h"
83 #include "scip/scip_mem.h"
84 #include "scip/type_dcmp.h"
85 #include <string.h>
86 
87 #define READER_NAME "decreader"
88 #define READER_DESC "file reader for constraint decompositions"
89 #define READER_EXTENSION "dec"
90 
91 /*
92  * local methods
93  */
94 
95 /* enumerator for the current section */
97  DEC_SECTION_INIT = 0, /**< initial section before the number of blocks is specified */
98  DEC_SECTION_NBLOCKS = 1, /**< section that contains the number of */
99  DEC_SECTION_BLOCK = 2, /**< */
100  DEC_SECTION_MASTER = 3 /**< */
101 };
103 
104 /** reads the given decomposition file */
105 static
107  SCIP* scip, /**< SCIP data structure */
108  const char* filename /**< name of the input file */
109  )
110 {
111  SCIP_FILE* file;
112  SCIP_CONS** conss;
113  SCIP_CONS** scip_conss;
114  SCIP_Bool error;
115  int lineno;
116  int nblocks;
117  int currblock = SCIP_DECOMP_LINKCONS;
118  int* labels;
119  int nconss;
120  int consptr;
121  int nblockscounted;
122 
124 
125  SCIP_DECOMP* decomp;
126 
127  assert(scip != NULL);
128  assert(filename != NULL);
129 
130  /* cannot read a decomposition after problem has been transformed */
131  if( SCIPgetStage(scip) != SCIP_STAGE_PROBLEM )
132  {
133  SCIPwarningMessage(scip, "Cannot read decomposition after problem has been transformed.\n");
134 
135  return SCIP_OKAY;
136  }
137 
138  /* open input file */
139  file = SCIPfopen(filename, "r");
140  if( file == NULL )
141  {
142  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
143  SCIPprintSysError(filename);
144  return SCIP_NOFILE;
145  }
146 
147  /* read the file */
148  error = FALSE;
149  lineno = 0;
150  nblocks = -1;
151 
152  /* use the number of constraints of the problem as buffer storage size */
153  nconss = SCIPgetNConss(scip);
154 
155  SCIP_CALL( SCIPallocBufferArray(scip, &conss, nconss) );
156  SCIP_CALL( SCIPallocBufferArray(scip, &labels, nconss) );
157 
158  /* start parsing the file */
159  section = DEC_SECTION_INIT;
160  consptr = 0;
161  nblockscounted = 0;
162  while( !SCIPfeof(file) && !error )
163  {
164  char buffer[SCIP_MAXSTRLEN];
165  char consname[SCIP_MAXSTRLEN];
166  SCIP_CONS* cons = NULL;
167  int nread;
168 
169  /* get next line */
170  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
171  break;
172  lineno++;
173 
174  /* check if a new section begins */
175  if( strncmp(buffer, "NBLOCKS", 7) == 0 )
176  {
177  section = DEC_SECTION_NBLOCKS;
178  continue;
179  }
180  else if( strncmp(buffer, "BLOCK", 5) == 0 )
181  {
182  section = DEC_SECTION_BLOCK;
183 
184  nread = sscanf(buffer, "BLOCK %1018d\n", &currblock);
185  if( nread < 1 )
186  {
187  error = TRUE;
188  break;
189  }
190  /* count number of block manually. If it is different from the number of specified blocks, throw an error */
191  else if( ++nblockscounted > nblocks )
192  {
193  error = TRUE;
194  break;
195  }
196 
197  SCIPdebugMsg(scip, "Switching block to %d\n", currblock);
198  continue;
199  }
200  else if( strncmp(buffer, "MASTERCONSS", 11) == 0 )
201  {
202  section = DEC_SECTION_MASTER;
203  currblock = SCIP_DECOMP_LINKCONS;
204 
205  SCIPdebugMsg(scip, "Continuing with master constraint block.\n");
206 
207  continue;
208  }
209 
210  /* base the parsing on the currently active section */
211  switch (section)
212  {
213  case DEC_SECTION_INIT:
214  break;
215  case DEC_SECTION_NBLOCKS:
216  /* read in number of blocks */
217  assert(nblocks == -1);
218  nread = sscanf(buffer, "%1024d\n", &nblocks);
219  if( nread < 1 )
220  error = TRUE;
221 
222  SCIPdebugMsg(scip, "Decomposition with %d number of blocks\n", nblocks);
223  break;
224  case DEC_SECTION_BLOCK:
225  case DEC_SECTION_MASTER:
226  /* read constraint name in both cases */
227  nread = sscanf(buffer, "%1024s\n", consname);
228  if( nread < 1 )
229  error = TRUE;
230 
231  cons = SCIPfindCons(scip, consname);
232  /* check if the constraint exists */
233  if( cons == NULL )
234  {
235  SCIPwarningMessage(scip, "Constraint <%s> in line %d does not exist.\n", consname, lineno);
236  continue;
237  }
238  break;
239 
240  default:
241  break;
242  }
243 
244  if( section == DEC_SECTION_NBLOCKS || section == DEC_SECTION_INIT )
245  continue;
246 
247  /* check if buffer storage capacity has been reached, which means that there is a duplicate constraint entry */
248  if( consptr == nconss )
249  {
250  SCIPerrorMessage("Error: Too many constraints in decomposition file: Is there a double entry?\n");
251  error = TRUE;
252  break;
253  }
254 
255  /* store constraint and corresponding label */
256  conss[consptr] = cons;
257  labels[consptr] = currblock;
258  ++consptr;
259  }
260 
261  /* close input file */
262  SCIPfclose(file);
263 
264  /* compare specified and actual number of blocks; stop on mismatch */
265  if( nblockscounted != nblocks )
266  {
267  SCIPerrorMessage("Error: Block number specification is wrong: Specified %d blocks, counted %d.\n",
268  nblocks, nblockscounted);
269  error = TRUE;
270  }
271 
272  /* create a decomposition and add it to the decomposition storage of SCIP */
273  if( ! error )
274  {
275  char strbuf[SCIP_MAXSTRLEN];
276  SCIP_Bool benderslabels;
277 
278  /* retrieving the Benders' variable labels setting */
279  SCIP_CALL( SCIPgetBoolParam(scip, "decomposition/benderslabels", &benderslabels) );
280 
281  SCIP_CALL( SCIPcreateDecomp(scip, &decomp, nblocks, TRUE, benderslabels) );
282 
283  SCIP_CALL( SCIPdecompSetConsLabels(decomp, conss, labels, consptr) );
284  SCIPdebugMsg(scip, "Setting labels for %d constraints.\n", nconss);
285 
286  scip_conss = SCIPgetConss(scip);
287 
288  SCIPdebugMsg(scip, "Using %d SCIP constraints for labeling variables.\n", nconss);
289  SCIP_CALL( SCIPcomputeDecompVarsLabels(scip, decomp, scip_conss, nconss) );
290 
291  SCIP_CALL( SCIPcomputeDecompStats(scip, decomp, TRUE) );
292 
293  SCIP_CALL( SCIPaddDecomp(scip, decomp) );
294 
295  /* display result */
296  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "Added decomposition <%s> with %d blocks to SCIP\n", filename, nblocks);
297 
298  /* print decomposition statistics */
299  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "Decomposition statistics:\n%s\n", SCIPdecompPrintStats(decomp, strbuf));
300  }
301  else
302  {
303  SCIPerrorMessage("Errors parsing decomposition <%s>. No decomposition added\n.", filename);
304  }
305 
306  SCIPfreeBufferArray(scip, &labels);
307  SCIPfreeBufferArray(scip, &conss);
308 
309  if( error )
310  return SCIP_READERROR;
311  else
312  return SCIP_OKAY;
313 }
314 
315 /*
316  * Callback methods of reader
317  */
318 
319 /** copy method for reader plugins (called when SCIP copies plugins) */
320 static
321 SCIP_DECL_READERCOPY(readerCopyDec)
322 { /*lint --e{715}*/
323  assert(scip != NULL);
324  assert(reader != NULL);
325  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
326 
327  /* call inclusion method of reader */
329 
330  return SCIP_OKAY;
331 }
332 
333 /** problem reading method of reader */
334 static
335 SCIP_DECL_READERREAD(readerReadDec)
336 { /*lint --e{715}*/
337  assert(reader != NULL);
338  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
339  assert(result != NULL);
340 
341  *result = SCIP_DIDNOTRUN;
342 
344  {
345  SCIPerrorMessage("reading of decomposition file is only possible after a problem was created\n");
346  return SCIP_READERROR;
347  }
348 
349  SCIP_CALL( readDecomposition(scip, filename) );
350 
351  *result = SCIP_SUCCESS;
352 
353  return SCIP_OKAY;
354 }
355 
356 /*
357  * dec file reader specific interface methods
358  */
359 
360 /** includes the dec file reader in SCIP */
362  SCIP* scip /**< SCIP data structure */
363  )
364 {
365  SCIP_READER* reader;
366 
367  /* include reader */
369 
370  /* set non fundamental callbacks via setter functions */
371  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyDec) );
372  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadDec) );
373 
374  return SCIP_OKAY;
375 }
SCIP_EXPORT const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
public methods for SCIP parameter handling
public methods for memory management
#define SCIP_MAXSTRLEN
Definition: def.h:273
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:123
#define READER_NAME
Definition: reader_dec.c:87
public solving methods
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip_param.c:241
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:186
int SCIPgetNConss(SCIP *scip)
Definition: scip_prob.c:3036
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:216
#define FALSE
Definition: def.h:73
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:138
#define TRUE
Definition: def.h:72
SCIP_CONS ** SCIPgetConss(SCIP *scip)
Definition: scip_prob.c:3082
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
#define READER_DESC
Definition: reader_dec.c:88
public methods for problem variables
#define SCIP_DECOMP_LINKCONS
Definition: type_dcmp.h:36
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:123
public methods for SCIP variables
#define SCIPdebugMsg
Definition: scip_message.h:69
public methods for numerical tolerances
public methods for decompositions
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:144
#define SCIPerrorMessage
Definition: pub_message.h:55
SCIP_RETCODE SCIPincludeReaderDec(SCIP *scip)
Definition: reader_dec.c:361
int SCIPfeof(SCIP_FILE *stream)
Definition: fileio.c:218
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:191
enum Dec_Section DEC_SECTION
Definition: reader_dec.c:102
#define NULL
Definition: lpi_spx1.cpp:155
SCIP_RETCODE SCIPcomputeDecompStats(SCIP *scip, SCIP_DECOMP *decomp, SCIP_Bool uselimits)
Definition: scip_dcmp.c:1125
static SCIP_DECL_READERREAD(readerReadDec)
Definition: reader_dec.c:335
#define SCIP_CALL(x)
Definition: def.h:364
#define READER_EXTENSION
Definition: reader_dec.c:89
wrapper functions to map file i/o to standard or zlib file i/o
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:111
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:70
SCIP_CONS * SCIPfindCons(SCIP *scip, const char *name)
Definition: scip_prob.c:2941
static SCIP_DECL_READERCOPY(readerCopyDec)
Definition: reader_dec.c:321
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip_reader.c:100
SCIP_RETCODE SCIPaddDecomp(SCIP *scip, SCIP_DECOMP *decomp)
Definition: scip_dcmp.c:235
SCIP_RETCODE SCIPcreateDecomp(SCIP *scip, SCIP_DECOMP **decomp, int nblocks, SCIP_Bool original, SCIP_Bool benderslabels)
Definition: scip_dcmp.c:208
general public methods
public methods for message output
public methods for input file readers
public methods for message handling
type definitions for decompositions and the decomposition store
SCIP_RETCODE SCIPdecompSetConsLabels(SCIP_DECOMP *decomp, SCIP_CONS **conss, int *labels, int nconss)
Definition: dcmp.c:163
void SCIPprintSysError(const char *message)
Definition: misc.c:10499
char * SCIPdecompPrintStats(SCIP_DECOMP *decomp, char *strbuf)
Definition: dcmp.c:349
SCIP_RETCODE SCIPcomputeDecompVarsLabels(SCIP *scip, SCIP_DECOMP *decomp, SCIP_CONS **conss, int nconss)
Definition: scip_dcmp.c:444
static SCIP_RETCODE readDecomposition(SCIP *scip, const char *filename)
Definition: reader_dec.c:106
file reader for decompositions in the constraint based dec-file format.
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:223
public methods for decompositions
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:356
public methods for reader plugins
public methods for global and local (sub)problems
Dec_Section
Definition: reader_dec.c:96