Scippy

SCIP

Solving Constraint Integer Programs

reader_rcp.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_rcp.c
17  * @brief file reader for "pack" scheduling instances
18  * @author Stefan Heinz
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 #include <ctype.h>
26 
27 #include "reader_rcp.h"
28 #include "reader_sm.h"
29 
30 /**@name Reader properties
31  *
32  * @{
33  */
34 
35 #define READER_NAME "rcpreader"
36 #define READER_DESC "reader for \"pack\" scheduling instances"
37 #define READER_EXTENSION "rcp"
38 
39 /**@} */
40 
41 
42 /**@name Local methods
43  *
44  * @{
45  */
46 
47 /** parse job and capacities details */
48 static
50  SCIP* scip, /**< SCIP data structure */
51  SCIP_FILE* file, /**< file to parse */
52  int* lineno, /**< pointer to store line number of the file */
53  int** demands, /**< demand matrix resource job demand */
54  SCIP_DIGRAPH* precedencegraph, /**< direct graph to store the precedence conditions */
55  int* durations, /**< array to store the processing for each job */
56  int* capacities, /**< array to store the different capacities */
57  int njobs, /**< number of jobs to be parsed */
58  int nresources /**< number of capacities to be parsed */
59  )
60 {
61  char buf[SCIP_MAXSTRLEN];
62  char* endptr;
63  int j;
64 
65  /* get resources capacities */
66  if( nresources > 0 && NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
67  {
68  int r;
69 
70  SCIPdebugMessage("line %d %s", *lineno, buf);
71 
72  if( !SCIPstrToIntValue(buf, &capacities[0], &endptr) )
73  return SCIP_READERROR;
74 
75  SCIPdebugMessage("paresed capacities: <%d>", capacities[0]);
76 
77  for( r = 1; r < nresources; ++r )
78  {
79  if( !SCIPstrToIntValue(endptr, &capacities[r], &endptr) )
80  return SCIP_READERROR;
81 
82  SCIPdebugPrintf(", <%d>", capacities[r]);
83  }
84 
85  SCIPdebugPrintf("\n");
86 
87  (*lineno)++;
88  }
89  else
90  return SCIP_READERROR;
91 
92  /* get job details */
93  for( j = 0; j < njobs; ++j )
94  {
95  if( NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
96  {
97  int nsuccessors;
98  int r;
99  int s;
100 
101  /* get job duration */
102  if( !SCIPstrToIntValue(buf, &durations[j], &endptr) )
103  return SCIP_READERROR;
104 
105  SCIPdebugMessage("job %d: duration %d, demands (", j, durations[j]);
106 
107  /* parse resources demands */
108  for( r = 0; r < nresources; ++r )
109  {
110  if( !SCIPstrToIntValue(endptr, &demands[j][r], &endptr) )
111  return SCIP_READERROR;
112 
113  SCIPdebugPrintf(" %d ", demands[j][r]);
114  }
115 
116  /* get number of successors */
117  if( !SCIPstrToIntValue(endptr, &nsuccessors, &endptr) )
118  return SCIP_READERROR;
119 
120  SCIPdebugPrintf("), successors %d:", nsuccessors);
121 
122  /* parse successor job ids */
123  for( s = 0; s < nsuccessors; ++s )
124  {
125  int successor;
126 
127  if( !SCIPstrToIntValue(endptr, &successor, &endptr) )
128  return SCIP_READERROR;
129 
130  /* add precedence to precedence graph */
131  SCIP_CALL( SCIPdigraphAddArc(precedencegraph, j, successor-1, (void*)(size_t)INT_MAX) );
132 
133  SCIPdebugPrintf(" %d ", successor);
134  }
135 
136  SCIPdebugPrintf("\n");
137  }
138  else
139  return SCIP_READERROR;
140 
141  (*lineno)++;
142  }
143 
144  return SCIP_OKAY;
145 }
146 
147 /** read file and create problem */
148 static
150  SCIP* scip, /**< SCIP data structure */
151  SCIP_FILE* file, /**< file to pares */
152  const char* filename /**< name of input file */
153  )
154 {
155  SCIP_RETCODE retcode;
156  char buf[SCIP_MAXSTRLEN];
157  SCIP_DIGRAPH* precedencegraph;
158  int** demands;
159  int* durations;
160  int* capacities;
161  int lineno;
162  int njobs;
163  int nresources;
164  int j;
165 
166  assert(scip != NULL);
167  assert(file != NULL);
168  assert(filename != NULL);
169 
170  lineno = 0;
171 
172  /* get number of jobs and resources */
173  if( NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
174  {
175  char* endptr;
176 
177  lineno++;
178 
179  /* get number of jobs */
180  if( !SCIPstrToIntValue(buf, &njobs, &endptr) )
181  return SCIP_READERROR;
182 
183  /* get number of resources */
184  if( !SCIPstrToIntValue(endptr, &nresources, &endptr) )
185  return SCIP_READERROR;
186  }
187  else
188  return SCIP_READERROR;
189 
190  SCIP_CALL( SCIPallocBufferArray(scip, &capacities, nresources) );
191  SCIP_CALL( SCIPallocBufferArray(scip, &durations, njobs) );
192  SCIP_CALL( SCIPallocBufferArray(scip, &demands, njobs) );
193 
194  for( j = 0; j < njobs; ++j )
195  {
196  SCIP_CALL( SCIPallocBufferArray(scip, &demands[j], nresources) ); /*lint !e866*/
197  BMSclearMemoryArray(demands[j], nresources); /*lint !e866*/
198  }
199 
200  SCIP_CALL( SCIPcreateDigraph(scip, &precedencegraph, njobs) );
201 
202  SCIPdebugMessage("problem has <%d> jobs and <%d> resources\n", njobs, nresources);
203 
204  retcode = parseDetails(scip, file, &lineno, demands, precedencegraph, durations, capacities, njobs, nresources);
205 
206  /* create problem */
207  if( retcode == SCIP_OKAY )
208  {
209  SCIP_CALL( SCIPcreateSchedulingProblem(scip, filename, NULL, NULL, demands,
210  precedencegraph, durations, capacities, njobs, nresources, TRUE) );
211  }
212 
213  /* free the precedence graph */
214  SCIPdigraphFree(&precedencegraph);
215 
216  /* free buffer before evaluating the retcode */
217  for( j = njobs - 1; j >= 0; --j )
218  {
219  SCIPfreeBufferArray(scip, &demands[j]);
220  }
221  SCIPfreeBufferArray(scip, &demands);
222  SCIPfreeBufferArray(scip, &durations);
223  SCIPfreeBufferArray(scip, &capacities);
224 
225  SCIP_CALL( retcode );
226 
227  return SCIP_OKAY;
228 }
229 
230 /**@} */
231 
232 /**@name Callback methods of reader
233  *
234  * @{
235  */
236 
237 /** copy method for reader plugins (called when SCIP copies plugins) */
238 static
239 SCIP_DECL_READERCOPY(readerCopyRcp)
240 { /*lint --e{715}*/
241  assert(scip != NULL);
242  assert(reader != NULL);
243  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
244 
245  /* call inclusion method of reader handler */
247 
248  return SCIP_OKAY;
249 }
250 
251 /** destructor of reader to free user data (called when SCIP is exiting) */
252 #define readerFreeSch NULL
253 
254 
255 /** problem reading method of reader */
256 static
257 SCIP_DECL_READERREAD(readerReadRcp)
258 { /*lint --e{715}*/
259  SCIP_FILE* file;
260  SCIP_RETCODE retcode;
261 
262  if( NULL == (file = SCIPfopen(filename, "r")) )
263  {
264  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
265  SCIPprintSysError(filename);
266  return SCIP_NOFILE;
267  }
268 
269  /* read file and create problem */
270  retcode = readFile(scip, file, filename);
271 
272  /* close file */
273  SCIPfclose(file);
274 
275  /* check retcode after the file was closed */
276  SCIP_CALL( retcode );
277 
278  (*result) = SCIP_SUCCESS;
279 
280  return SCIP_OKAY;
281 }
282 
283 
284 /** problem writing method of reader */
285 #define readerWriteSch NULL
286 
287 
288 /**@} */
289 
290 /**@name Interface methods
291  *
292  * @{
293  */
294 
295 /*
296  * reader specific interface methods
297  */
298 
299 /** includes the rcp file reader into SCIP */
301  SCIP* scip /**< SCIP data structure */
302  )
303 {
304  /* include sch reader */
306  readerCopyRcp, readerFreeSch, readerReadRcp, readerWriteSch, NULL) );
307 
308  return SCIP_OKAY;
309 }
310 
311 /**@} */
SCIP_EXPORT const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
SCIP_RETCODE SCIPcreateDigraph(SCIP *scip, SCIP_DIGRAPH **digraph, int nnodes)
static SCIP_RETCODE parseDetails(SCIP *scip, SCIP_FILE *file, int *lineno, int **demands, SCIP_DIGRAPH *precedencegraph, int *durations, int *capacities, int njobs, int nresources)
Definition: reader_rcp.c:49
#define SCIP_MAXSTRLEN
Definition: def.h:273
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
void SCIPdigraphFree(SCIP_DIGRAPH **digraph)
Definition: misc.c:7459
#define SCIPdebugMessage
Definition: pub_message.h:87
SCIP_FILE * file
Definition: reader_cmin.c:61
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:123
static SCIP_DECL_READERREAD(readerReadRcp)
Definition: reader_rcp.c:257
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:144
file reader for "pack" scheduling instances
#define SCIPerrorMessage
Definition: pub_message.h:55
#define SCIPdebugPrintf
Definition: pub_message.h:90
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:191
#define NULL
Definition: lpi_spx1.cpp:155
static SCIP_DECL_READERCOPY(readerCopyRcp)
Definition: reader_rcp.c:239
SCIP_RETCODE SCIPincludeReaderRcp(SCIP *scip)
Definition: reader_rcp.c:300
#define SCIP_CALL(x)
Definition: def.h:364
#define READER_EXTENSION
Definition: reader_rcp.c:37
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:111
static SCIP_RETCODE readFile(SCIP *scip, SCIP_FILE *file, const char *filename)
Definition: reader_rcp.c:149
SCIP_RETCODE SCIPcreateSchedulingProblem(SCIP *scip, const char *problemname, const char **jobnames, const char **resourcenames, int **demands, SCIP_DIGRAPH *precedencegraph, int *durations, int *capacities, int njobs, int nresources, SCIP_Bool initialize)
Definition: reader_sm.c:738
SCIP_RETCODE SCIPincludeReader(SCIP *scip, const char *name, const char *desc, const char *extension, SCIP_DECL_READERCOPY((*readercopy)), SCIP_DECL_READERFREE((*readerfree)), SCIP_DECL_READERREAD((*readerread)), SCIP_DECL_READERWRITE((*readerwrite)), SCIP_READERDATA *readerdata)
Definition: scip_reader.c:57
#define READER_DESC
Definition: reader_rcp.c:36
#define readerFreeSch
Definition: reader_rcp.c:252
scheduling problem file reader for RCPSP format
SCIP_Real * r
Definition: circlepacking.c:50
void SCIPprintSysError(const char *message)
Definition: misc.c:10499
#define readerWriteSch
Definition: reader_rcp.c:285
#define BMSclearMemoryArray(ptr, num)
Definition: memory.h:122
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:223
SCIP_Bool SCIPstrToIntValue(const char *str, int *value, char **endptr)
Definition: misc.c:10660
#define READER_NAME
Definition: reader_rcp.c:35
SCIP_RETCODE SCIPdigraphAddArc(SCIP_DIGRAPH *digraph, int startnode, int endnode, void *data)
Definition: misc.c:7553