SCIP Doxygen Documentation
Loading...
Searching...
No Matches
reader_smps.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-2026 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_smps.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief SMPS file reader - smps files list the cor, tim and sto files for a single instance
28 * @author Stephen J. Maher
29 */
30
31
32/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
33
35#include "scip/pub_fileio.h"
36#include "scip/pub_message.h"
37#include "scip/pub_misc.h"
38#include "scip/pub_reader.h"
39#include "scip/reader_cor.h"
40#include "scip/reader_smps.h"
41#include "scip/reader_sto.h"
42#include "scip/reader_tim.h"
43#include "scip/scip_mem.h"
44#include "scip/scip_message.h"
45#include "scip/scip_prob.h"
46#include "scip/scip_reader.h"
47
48
49/*
50 * The SMPS reader coordinates the reading of the cor, tim and sto files. The public reading methods from the cor, tim
51 * and sto readers are called from the SMPS reader. So, the header files for the cor, tim and sto readers are required.
52 */
53
54#define READER_NAME "smpsreader"
55#define READER_DESC "file reader for core problem of stochastic programs in the SMPS file format"
56#define READER_EXTENSION "smps"
57
58#define SMPS_MAX_LINELEN 1024
59#define BLANK ' '
60#define LINEWIDTH 80
61
62#define COR_FILEEXTENSION "cor"
63#define TIM_FILEEXTENSION "tim"
64#define STO_FILEEXTENSION "sto"
65
66/** enum for the file types that are read by the SMPS reader */
74
75
76/** smps input structure */
78{
80 int lineno;
83 const char* f0;
84 const char* f1;
85};
86typedef struct SmpsInput SMPSINPUT;
87
88
89/** creates the smps input structure */
90static
92 SCIP* scip, /**< SCIP data structure */
93 SMPSINPUT** smpsi, /**< smps input structure */
94 SCIP_FILE* fp /**< file object for the input file */
95 )
96{
97 assert(smpsi != NULL);
98 assert(fp != NULL);
99
101
102 (*smpsi)->fp = fp;
103 (*smpsi)->lineno = 0;
104 (*smpsi)->haserror = FALSE;
105 (*smpsi)->buf [0] = '\0';
106 (*smpsi)->f0 = NULL;
107 (*smpsi)->f1 = NULL;
108
109 return SCIP_OKAY;
110}
111
112/** free the smps input structure */
113static
115 SCIP* scip, /**< SCIP data structure */
116 SMPSINPUT** smpsi /**< smps input structure */
117 )
118{
120}
121
122/** return the current value of field 0 */
123static
124const char* smpsinputField0(
125 const SMPSINPUT* smpsi /**< smps input structure */
126 )
127{
128 assert(smpsi != NULL);
129
130 return smpsi->f0;
131}
132
133/** fill the line from \p pos up to column LINEWIDTH with blanks. */
134static
136 char* buf, /**< buffer to clear */
137 unsigned int pos /**< position to start the clearing process */
138 )
139{
140 unsigned int i;
141
142 for(i = pos; i < LINEWIDTH; i++)
143 buf[i] = BLANK;
144 buf[LINEWIDTH] = '\0';
145}
146
147/** read a smps format data line and parse the fields. */
148static
150 SMPSINPUT* smpsi /**< smps input structure */
151 )
152{
153 unsigned int len;
154 unsigned int i;
155 SCIP_Bool is_marker;
156 SCIP_Bool is_empty;
157 char* nexttok;
158
159 do
160 {
161 smpsi->f0 = smpsi->f1 = 0;
162 is_marker = FALSE;
163
164 /* Read until we have not a comment line. */
165 do
166 {
167 smpsi->buf[SMPS_MAX_LINELEN-1] = '\0';
168 if( NULL == SCIPfgets(smpsi->buf, (int) sizeof(smpsi->buf), smpsi->fp) )
169 return FALSE;
170 smpsi->lineno++;
171 }
172 while( *smpsi->buf == '*' );
173
174 /* Normalize line */
175 len = (unsigned int) strlen(smpsi->buf);
176
177 /* replace tabs and new lines by blanks */
178 for( i = 0; i < len; i++ )
179 {
180 if( (smpsi->buf[i] == '\t') || (smpsi->buf[i] == '\n') || (smpsi->buf[i] == '\r') )
181 smpsi->buf[i] = BLANK;
182 }
183
184 if( len < LINEWIDTH )
185 clearFrom(smpsi->buf, len);
186
187 SCIPdebugMessage("line %d: <%s>\n", smpsi->lineno, smpsi->buf);
188
189 assert(strlen(smpsi->buf) >= LINEWIDTH);
190
191 /* Look for new section */
192 if( *smpsi->buf != BLANK )
193 {
194 smpsi->f0 = SCIPstrtok(&smpsi->buf[0], " ", &nexttok);
195
196 assert(smpsi->f0 != 0);
197
198 smpsi->f1 = SCIPstrtok(NULL, " ", &nexttok);
199
200 return TRUE;
201 }
202
203 /* check for empty lines */
204 is_empty = (smpsi->f0 == NULL && smpsi->f1 == NULL);
205 }
206 while( is_marker || is_empty );
207
208 return TRUE;
209}
210
211/*
212 * Callback methods of reader
213 */
214
215/** copy method for reader plugins (called when SCIP copies plugins) */
216static
217SCIP_DECL_READERCOPY(readerCopySmps)
218{ /*lint --e{715}*/
219 assert(scip != NULL);
220 assert(reader != NULL);
221
223
224 /* call inclusion method of reader */
226
227 return SCIP_OKAY;
228}
229
230
231/** problem reading method of reader */
232static
233SCIP_DECL_READERREAD(readerReadSmps)
234{ /*lint --e{715}*/
235 SCIP_FILE* fp;
236 SMPSINPUT* smpsi;
237 SCIP_RETCODE retcode = SCIP_OKAY;
238
239 char corfilename[SCIP_MAXSTRLEN];
240 char timfilename[SCIP_MAXSTRLEN];
241 char stofilename[SCIP_MAXSTRLEN];
242 char* tmpfilename;
243 char* probname;
244 char* fileextension;
245 char* fromlastslash;
246 char parent[SCIP_MAXSTRLEN];
247 size_t parentlen;
248
249 SCIP_Bool hascorfile;
250 SCIP_Bool hastimfile;
251 SCIP_Bool hasstofile;
252
253 int i;
254
255 assert(scip != NULL);
256 assert(filename != NULL);
257 assert(result != NULL);
258
260
261 /* copy filename */
262 SCIP_CALL( SCIPduplicateBufferArray(scip, &tmpfilename, filename, (int)strlen(filename)+1) );
263
264 /* getting the problem name from the SMPS file name */
265 SCIPsplitFilename(tmpfilename, NULL, &probname, NULL, NULL);
266
267 fromlastslash = (char*) strrchr(filename, '/');
268
269 if( fromlastslash == NULL )
270 parentlen = 0;
271 else
272 parentlen = strlen(filename) - (strlen(fromlastslash) - 1);
273
274 (void)SCIPstrncpy(parent, filename, (int)parentlen + 1);
275
276 fp = SCIPfopen(filename, "r");
277 if( fp == NULL )
278 {
279 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
280 SCIPprintSysError(filename);
281
282 return SCIP_NOFILE;
283 }
284
285 SCIP_CALL( smpsinputCreate(scip, &smpsi, fp) );
286
287 hascorfile = FALSE;
288 hastimfile = FALSE;
289 hasstofile = FALSE;
290 while( smpsinputReadLine(smpsi) )
291 {
292 char* tmpinput;
293
294 /* copy the input */
296 (int)strlen(smpsinputField0(smpsi))+1) ); /*lint !e666*/
297
298 /* get extension from filename */
299 SCIPsplitFilename(tmpinput, NULL, NULL, &fileextension, NULL);
300
301 if( SCIPstrcasecmp(fileextension, COR_FILEEXTENSION) == 0 )
302 {
303 (void) SCIPsnprintf(corfilename, SCIP_MAXSTRLEN, "%s%s", parent, smpsinputField0(smpsi));
304 hascorfile = TRUE;
305 }
306 else if( SCIPstrcasecmp(fileextension, TIM_FILEEXTENSION) == 0 )
307 {
308 (void) SCIPsnprintf(timfilename, SCIP_MAXSTRLEN, "%s%s", parent, smpsinputField0(smpsi));
309 hastimfile = TRUE;
310 }
311 else if( SCIPstrcasecmp(fileextension, STO_FILEEXTENSION) == 0 )
312 {
313 (void) SCIPsnprintf(stofilename, SCIP_MAXSTRLEN, "%s%s", parent, smpsinputField0(smpsi));
314 hasstofile = TRUE;
315 }
316
317 SCIPfreeBufferArray(scip, &tmpinput);
318 }
319
320 /* printing errors if the correct files have not been provided */
321 if( !hascorfile )
322 {
323 SCIPerrorMessage("The core file has not been listed in <%s>\n", filename);
324 }
325
326 if( !hastimfile )
327 {
328 SCIPerrorMessage("The tim file has not been listed in <%s>\n", filename);
329 }
330
331 if( !hasstofile )
332 {
333 SCIPerrorMessage("The sto file has not been listed in <%s>\n", filename);
334 }
335
336 /* if one of the necessary file has not been provided, then an error will be returned */
337 if( !hascorfile || !hastimfile || !hasstofile )
338 {
339 retcode = SCIP_READERROR;
340 goto TERMINATE;
341 }
342
343 for( i = 0; i < 3; i++ )
344 {
345 int nvars;
346 int nbinvars;
347 int nintvars;
348 int nimplintvars;
349 int ncontvars;
351
352 type = (SCIP_SMPSFILETYPE) i;
353 switch( type )
354 {
356 SCIPinfoMessage(scip, NULL, "reading core file <%s> for problem %s\n", corfilename, probname);
357 SCIPinfoMessage(scip, NULL, "============\n");
358
359 /* reading the CORE file */
360 SCIP_CALL_TERMINATE( retcode, SCIPreadCor(scip, corfilename, result), TERMINATE );
361
362 /* getting the variable information */
363 SCIP_CALL( SCIPgetOrigVarsData(scip, NULL, &nvars, &nbinvars, &nintvars, &nimplintvars, &ncontvars) );
365 "core problem has %d variables (%d bin, %d int, %d impl, %d cont) and %d constraints\n",
366 nvars, nbinvars, nintvars, nimplintvars, ncontvars, SCIPgetNOrigConss(scip));
367 break;
369 SCIPinfoMessage(scip, NULL, "reading the time file <%s> for problem %s\n", timfilename, probname);
370 SCIPinfoMessage(scip, NULL, "============\n");
371
372 /* reading the TIME file */
373 SCIP_CALL_TERMINATE( retcode, SCIPreadTim(scip, timfilename, result), TERMINATE );
374
375 SCIPinfoMessage(scip, NULL, "problem %s has %d stages\n", probname, SCIPtimGetNStages(scip));
376 break;
378#ifdef BENDERSBRANCH
379 SCIP_Bool usebenders;
380#endif
381
382 SCIPinfoMessage(scip, NULL, "read problem <%s>\n", stofilename);
383 SCIPinfoMessage(scip, NULL, "============\n");
384
385 /* reading the STO file */
386 SCIP_CALL_TERMINATE( retcode, SCIPreadSto(scip, stofilename, result), TERMINATE );
387
388 SCIPinfoMessage(scip, NULL, "problem %s has extended with a total of %d scenarios\n", probname,
390
391 /* getting the variable information */
392 SCIP_CALL( SCIPgetOrigVarsData(scip, NULL, &nvars, &nbinvars, &nintvars, &nimplintvars, &ncontvars) );
393
394 /* if Benders' decomposition is used, the variable will be distributed to a number of subproblems */
395#ifdef BENDERSBRANCH
396 SCIP_CALL( SCIPgetBoolParam(scip, "reading/sto/usebenders", &usebenders) );
397 if( usebenders )
398 {
399 SCIPinfoMessage(scip, NULL, "Benders' decomposition master problem ");
400 }
401 else
402#endif
403 {
404 SCIPinfoMessage(scip, NULL, "deterministic equivalent problem ");
405 }
406
408 "has %d variables (%d bin, %d int, %d impl, %d cont) and %d constraints\n",
409 nvars, nbinvars, nintvars, nimplintvars, ncontvars, SCIPgetNOrigConss(scip));
410 break;
411 /* coverity[dead_error_begin] */
412 default:
413 SCIPerrorMessage("This should not happen. Aborting.\n");
414 SCIPABORT();
415 retcode = SCIP_READERROR;
416 goto TERMINATE;
417 }
418
419 SCIPinfoMessage(scip, NULL, "\n\n");
420 }
421
422 SCIPfclose(fp);
423
424TERMINATE:
425 smpsinputFree(scip, &smpsi);
426
427 /* freeing buffer array */
428 SCIPfreeBufferArray(scip, &tmpfilename);
429
430 if( retcode == SCIP_PLUGINNOTFOUND )
431 retcode = SCIP_READERROR;
432
433 if( retcode == SCIP_NOFILE || retcode == SCIP_READERROR )
434 return retcode;
435
436 SCIP_CALL( retcode );
437
439
440 return SCIP_OKAY;
441}
442
443
444/*
445 * reader specific interface methods
446 */
447
448/** includes the smps file reader in SCIP */
450 SCIP* scip /**< SCIP data structure */
451 )
452{
453 SCIP_READER* reader;
454
455 /* include reader */
457
458 assert(reader != NULL);
459
460 /* set non fundamental callbacks via setter functions */
461 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySmps) );
462 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSmps) );
463
464 return SCIP_OKAY;
465}
#define NULL
Definition def.h:257
#define SCIP_MAXSTRLEN
Definition def.h:278
#define SCIP_Bool
Definition def.h:100
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define TRUE
Definition def.h:102
#define FALSE
Definition def.h:103
#define SCIP_CALL_TERMINATE(retcode, x, TERM)
Definition def.h:385
#define SCIPABORT()
Definition def.h:336
#define SCIP_CALL(x)
Definition def.h:364
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition fileio.c:153
int SCIPfclose(SCIP_FILE *fp)
Definition fileio.c:232
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition fileio.c:200
int SCIPstoGetNScenarios(SCIP *scip)
SCIP_RETCODE SCIPreadCor(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition reader_cor.c:202
SCIP_RETCODE SCIPreadTim(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition reader_tim.c:895
SCIP_RETCODE SCIPreadSto(SCIP *scip, const char *filename, SCIP_RESULT *result)
void SCIPsplitFilename(char *filename, char **path, char **name, char **extension, char **compression)
Definition misc.c:11073
SCIP_RETCODE SCIPincludeReaderSmps(SCIP *scip)
SCIP_RETCODE SCIPgetOrigVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition scip_prob.c:2753
int SCIPgetNOrigConss(SCIP *scip)
Definition scip_prob.c:3712
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition scip_param.c:250
#define SCIPfreeBufferArray(scip, ptr)
Definition scip_mem.h:136
#define SCIPduplicateBufferArray(scip, ptr, source, num)
Definition scip_mem.h:132
#define SCIPfreeBlockMemory(scip, ptr)
Definition scip_mem.h:108
#define SCIPallocBlockMemory(scip, ptr)
Definition scip_mem.h:89
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader,)
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition reader.c:700
int SCIPstrcasecmp(const char *s1, const char *s2)
Definition misc.c:10863
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition misc.c:10827
void SCIPprintSysError(const char *message)
Definition misc.c:10719
int SCIPstrncpy(char *t, const char *s, int size)
Definition misc.c:10897
char * SCIPstrtok(char *s, const char *delim, char **ptrptr)
Definition misc.c:10768
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
int nvars
memory allocation routines
wrapper functions to map file i/o to standard or zlib file i/o
struct SCIP_File SCIP_FILE
Definition pub_fileio.h:43
public methods for message output
#define SCIPerrorMessage
Definition pub_message.h:64
#define SCIPdebugMessage
Definition pub_message.h:96
public data structures and miscellaneous methods
public methods for input file readers
#define READER_DESC
Definition reader_bnd.c:62
#define READER_EXTENSION
Definition reader_bnd.c:63
#define READER_NAME
Definition reader_bnd.c:61
COR file reader (MPS format of the core problem for stochastic programs).
#define BLANK
Definition reader_mps.c:98
#define STO_FILEEXTENSION
Definition reader_smps.c:64
static SCIP_RETCODE smpsinputCreate(SCIP *scip, SMPSINPUT **smpsi, SCIP_FILE *fp)
Definition reader_smps.c:91
static SCIP_Bool smpsinputReadLine(SMPSINPUT *smpsi)
static void clearFrom(char *buf, unsigned int pos)
#define TIM_FILEEXTENSION
Definition reader_smps.c:63
SCIP_SmpsFileType
Definition reader_smps.c:68
@ SCIP_SMPSFILETYPE_STO
Definition reader_smps.c:71
@ SCIP_SMPSFILETYPE_COR
Definition reader_smps.c:69
@ SCIP_SMPSFILETYPE_TIM
Definition reader_smps.c:70
enum SCIP_SmpsFileType SCIP_SMPSFILETYPE
Definition reader_smps.c:73
static void smpsinputFree(SCIP *scip, SMPSINPUT **smpsi)
#define LINEWIDTH
Definition reader_smps.c:60
struct SmpsInput SMPSINPUT
Definition reader_smps.c:86
#define SMPS_MAX_LINELEN
Definition reader_smps.c:58
static const char * smpsinputField0(const SMPSINPUT *smpsi)
#define COR_FILEEXTENSION
Definition reader_smps.c:62
SMPS file reader - SMPS files lists the cor, tim and sto files for a single instance to be read.
STO file reader - the stochastic information of an instance in SMPS format.
int SCIPtimGetNStages(SCIP *scip)
Definition reader_tim.c:978
TIM file reader - the stage information for a stochastic programming instance in SMPS format.
public methods for memory management
public methods for message handling
public methods for global and local (sub)problems
public methods for reader plugins
const char * f1
Definition reader_smps.c:84
SCIP_FILE * fp
Definition reader_smps.c:79
char buf[SMPS_MAX_LINELEN]
Definition reader_smps.c:82
const char * f0
Definition reader_smps.c:83
SCIP_Bool haserror
Definition reader_smps.c:81
struct SCIP_Reader SCIP_READER
Definition type_reader.h:53
#define SCIP_DECL_READERREAD(x)
Definition type_reader.h:88
#define SCIP_DECL_READERCOPY(x)
Definition type_reader.h:63
@ SCIP_DIDNOTRUN
Definition type_result.h:42
@ SCIP_SUCCESS
Definition type_result.h:58
@ SCIP_NOFILE
@ SCIP_READERROR
@ SCIP_PLUGINNOTFOUND
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39