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