SCIP Doxygen Documentation
Loading...
Searching...
No Matches
reader_tim.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_tim.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief TIM file reader - the stage information for a stochastic programming instance in SMPS format
28 * @author Stephen J. Maher
29 */
30
31/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32
33#include "scip/pub_cons.h"
34#include "scip/pub_fileio.h"
35#include "scip/pub_message.h"
36#include "scip/pub_misc.h"
37#include "scip/pub_reader.h"
38#include "scip/reader_cor.h"
39#include "scip/reader_tim.h"
40#include "scip/reader_sto.h"
41#include "scip/scip_mem.h"
42#include "scip/scip_message.h"
43#include "scip/scip_numerics.h"
44#include "scip/scip_prob.h"
45#include "scip/scip_reader.h"
46
47
48#define READER_NAME "timreader"
49#define READER_DESC "file reader for the TIME file of a stochastic program in SMPS format"
50#define READER_EXTENSION "tim"
51
52/*
53 * tim reader internal methods
54 */
55
56#define TIM_MAX_LINELEN 1025
57#define TIM_MAX_NAMELEN 256
58#define TIM_DEFAULT_STAGESIZE 10
59#define TIM_DEFAULT_ARRAYSIZE 100
60
61#define BLANK ' '
62
74typedef struct TimStage TIMSTAGE;
75
76/** TIM reading data */
77struct SCIP_ReaderData
78{
79 SCIP_Bool read; /**< flag to indicate whether the time file has been read */
80 int nstages; /**< the number of stages in the stochastic program */
81 const char** stagestartvars; /**< the variables that start each stage */
82 const char** stagestartcons; /**< the constraints that start each stage */
83 const char** stagenames; /**< the name of the stage */
84 TIMSTAGE** stages; /**< the stages for the stochastic program */
85};
86
87/** enum containing all tim sections */
95
96/** tim input structure */
98{
104 const char* f0;
105 const char* f1;
106 const char* f2;
107 const char* f3;
109 const char** stagestartvars;
110 const char** stagestartcons;
111 const char** stagenames;
114};
115typedef struct TimInput TIMINPUT;
116
117/** adds the variable to the given stage */
118static
120 SCIP* scip, /**< SCIP data structure */
121 TIMSTAGE* stage, /**< the stage structure */
122 const char* varname /**< the name of the variable to add to the stage */
123 )
124{
125 SCIP_VAR* var;
126
127 assert(scip != NULL);
128 assert(stage != NULL);
129
130 var = SCIPfindVar(scip, varname);
131
132 if( var == NULL )
133 {
134 SCIPwarningMessage(scip, "This is an error. All variables should in the problem.\n");
135 return SCIP_OKAY;
136 }
137
138 /* adding the variable to the hashmap */
139 SCIP_CALL( SCIPhashmapInsert(stage->varnametovar, (void*) varname, var) );
140
141 /* adding the variable to the variable storage */
142 SCIP_CALL( SCIPensureBlockMemoryArray(scip, &stage->vars, &stage->varssize, stage->nvars + 1) );
143 stage->vars[stage->nvars] = var;
144 stage->nvars++;
145
146 return SCIP_OKAY;
147}
148
149/** adds the constraint to the given stage */
150static
152 SCIP* scip, /**< SCIP data structure */
153 TIMSTAGE* stage, /**< the stage structure */
154 const char* consname /**< the name of the constraint to add to the stage */
155 )
156{
157 SCIP_CONS* cons;
158
159 assert(scip != NULL);
160 assert(stage != NULL);
161
162 cons = SCIPfindCons(scip, consname);
163
164 if( cons == NULL )
165 {
166 SCIPwarningMessage(scip, "This is an error. All constraints should in the problem.\n");
167 return SCIP_OKAY;
168 }
169
170 /* adding the constraint to the hashmap */
171 SCIP_CALL( SCIPhashmapInsert(stage->consnametocons, (void*) consname, cons) );
172
173 /* adding the constraint to the constraint storage */
174 SCIP_CALL( SCIPensureBlockMemoryArray(scip, &stage->conss, &stage->conssize, stage->nconss + 1) );
175 stage->conss[stage->nconss] = cons;
176 stage->nconss++;
177
178 return SCIP_OKAY;
179}
180
181/** creates the stage data */
182static
184 SCIP* scip, /**< SCIP data structure */
185 SCIP_READER* reader, /**< the reader structure */
186 SCIP_READER* correader /**< the reader structure for the core file */
187 )
188{
189 SCIP_READERDATA* readerdata;
190 int stage;
191 int i;
192
193 assert(scip != NULL);
194 assert(reader != NULL);
195 assert(correader != NULL);
196
197 readerdata = SCIPreaderGetData(reader);
198 assert(readerdata != NULL);
199
200 stage = 0;
201
202 /* assigning the variables to the stages */
203 for( i = 0; i < SCIPcorGetNVarNames(correader); i++ )
204 {
205 /* the first variable in the var names list should be the start of the first stage */
206 assert((stage == 0 && i == 0 && strcmp(SCIPcorGetVarName(correader, i), readerdata->stagestartvars[stage]) == 0)
207 || i > 0);
208 /* checking whether the next stage has been found */
209 if( i > 0 && stage < readerdata->nstages - 1
210 && strcmp(SCIPcorGetVarName(correader, i), readerdata->stagestartvars[stage + 1]) == 0 )
211 stage++;
212
213 /* adding the variable to the stage */
214 SCIP_CALL( addVariableToStage(scip, readerdata->stages[stage], SCIPcorGetVarName(correader, i)) );
215 }
216
217 stage = 0;
218
219 /* assigning the constraint to the stages */
220 for( i = 0; i < SCIPcorGetNConsNames(correader); i++ )
221 {
222 /* checking whether the next stage has been found */
223 if( i > 0 && stage < readerdata->nstages - 1
224 && strcmp(SCIPcorGetConsName(correader, i), readerdata->stagestartcons[stage + 1]) == 0 )
225 stage++;
226
227 /* adding the consiable to the stage */
228 SCIP_CALL( addConstraintToStage(scip, readerdata->stages[stage], SCIPcorGetConsName(correader, i)) );
229 }
230
231 return SCIP_OKAY;
232}
233
234/** creates the reader data for the time input data */
235static
237 SCIP* scip, /**< SCIP data structure */
238 SCIP_READER* reader, /**< the reader structure */
239 TIMINPUT* timi /**< tim input structure */
240 )
241{
242 SCIP_READERDATA* readerdata;
243 int hashmapsize;
244 int nvars;
245 int i;
246
247 assert(scip != NULL);
248 assert(reader != NULL);
249 assert(timi != NULL);
250
251 readerdata = SCIPreaderGetData(reader);
252
253 assert(readerdata != NULL);
254
255 /* getting the total number of variables in the problem. The hash maps will be of size nvars/nstages. */
257
258 readerdata->read = TRUE;
259 readerdata->nstages = timi->nstages;
260
261 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stagestartvars, readerdata->nstages) );
262 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stagestartcons, readerdata->nstages) );
263 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stagenames, readerdata->nstages) );
264 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stages, readerdata->nstages) );
265
266 for( i = 0; i < readerdata->nstages; i++ )
267 {
268 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &readerdata->stagestartvars[i],
269 timi->stagestartvars[i], strlen(timi->stagestartvars[i]) + 1) ); /*lint !e866*/
270 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &readerdata->stagestartcons[i],
271 timi->stagestartcons[i], strlen(timi->stagestartcons[i]) + 1) ); /*lint !e866*/
272 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &readerdata->stagenames[i],
273 timi->stagenames[i], strlen(timi->stagenames[i]) + 1) ); /*lint !e866*/
274
275 /* creating the data for the stages */
276 SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata->stages[i]) ); /*lint !e866*/
277 readerdata->stages[i]->nvars = 0;
278 readerdata->stages[i]->nconss = 0;
279 readerdata->stages[i]->varssize = TIM_DEFAULT_ARRAYSIZE;
280 readerdata->stages[i]->conssize = TIM_DEFAULT_ARRAYSIZE;
281 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stages[i]->vars, readerdata->stages[i]->varssize) ); /*lint !e866*/
282 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->stages[i]->conss, readerdata->stages[i]->conssize) ); /*lint !e866*/
283
284 /* creating the hashmaps */
285 hashmapsize = (int) SCIPceil(scip, (SCIP_Real) nvars/(SCIP_Real) readerdata->nstages);
286 SCIP_CALL( SCIPhashmapCreate(&readerdata->stages[i]->varnametovar, SCIPblkmem(scip), hashmapsize) );
287 SCIP_CALL( SCIPhashmapCreate(&readerdata->stages[i]->consnametocons, SCIPblkmem(scip), hashmapsize) );
288 }
289
290 return SCIP_OKAY;
291}
292
293/** free the reader data */
294static
296 SCIP* scip, /**< SCIP data structure */
297 SCIP_READERDATA* readerdata /**< the reader data structure */
298 )
299{
300 int i;
301
302 assert(scip != NULL);
303 assert(readerdata != NULL);
304
305 /* only free the reader data is a file has been read */
306 if( readerdata->read )
307 {
308 for( i = 0; i < readerdata->nstages; i++ )
309 {
310 /* freeing the hashmaps */
311 SCIPhashmapFree(&readerdata->stages[i]->consnametocons);
312 SCIPhashmapFree(&readerdata->stages[i]->varnametovar);
313
314 SCIPfreeBlockMemoryArray(scip, &readerdata->stagestartvars[i], strlen(readerdata->stagestartvars[i]) + 1);
315 SCIPfreeBlockMemoryArray(scip, &readerdata->stagestartcons[i], strlen(readerdata->stagestartcons[i]) + 1);
316 SCIPfreeBlockMemoryArray(scip, &readerdata->stagenames[i], strlen(readerdata->stagenames[i]) + 1);
317
318 /* freeing the memory for the stage data */
319 SCIPfreeBlockMemoryArray(scip, &readerdata->stages[i]->vars, readerdata->stages[i]->varssize);
320 SCIPfreeBlockMemoryArray(scip, &readerdata->stages[i]->conss, readerdata->stages[i]->conssize);
321 SCIPfreeBlockMemory(scip, &readerdata->stages[i]); /*lint !e866*/
322 }
323
324 SCIPfreeBlockMemoryArray(scip, &readerdata->stages, readerdata->nstages);
325 SCIPfreeBlockMemoryArray(scip, &readerdata->stagenames, readerdata->nstages);
326 SCIPfreeBlockMemoryArray(scip, &readerdata->stagestartcons, readerdata->nstages);
327 SCIPfreeBlockMemoryArray(scip, &readerdata->stagestartvars, readerdata->nstages);
328
329 readerdata->read = FALSE;
330 }
331}
332
333
334/** creates the tim input structure */
335static
337 SCIP* scip, /**< SCIP data structure */
338 TIMINPUT** timi, /**< tim input structure */
339 SCIP_FILE* fp /**< file object for the input file */
340 )
341{
342 assert(timi != NULL);
343 assert(fp != NULL);
344
346
347 (*timi)->section = TIM_TIME;
348 (*timi)->fp = fp;
349 (*timi)->lineno = 0;
350 (*timi)->haserror = FALSE;
351 (*timi)->buf [0] = '\0';
352 (*timi)->probname[0] = '\0';
353 (*timi)->f0 = NULL;
354 (*timi)->f1 = NULL;
355 (*timi)->f2 = NULL;
356 (*timi)->f3 = NULL;
357 (*timi)->nstages = 0;
358 (*timi)->stagesize = TIM_DEFAULT_STAGESIZE;
359
360 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*timi)->stagestartvars, (*timi)->stagesize) );
361 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*timi)->stagestartcons, (*timi)->stagesize) );
362 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*timi)->stagenames, (*timi)->stagesize) );
363
364 return SCIP_OKAY;
365}
366
367/** free the tim input structure */
368static
370 SCIP* scip, /**< SCIP data structure */
371 TIMINPUT** timi /**< tim input structure */
372 )
373{
374 int i;
375
376 for( i = 0; i < (*timi)->nstages; i++ )
377 {
378 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagestartvars[i], strlen((*timi)->stagestartvars[i]) + 1);
379 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagestartcons[i], strlen((*timi)->stagestartcons[i]) + 1);
380 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagenames[i], strlen((*timi)->stagenames[i]) + 1);
381 }
382
383 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagestartvars, (*timi)->stagesize);
384 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagestartcons, (*timi)->stagesize);
385 SCIPfreeBlockMemoryArray(scip, &(*timi)->stagenames, (*timi)->stagesize);
386
388}
389
390/** returns the current section */
391static
393 const TIMINPUT* timi /**< tim input structure */
394 )
395{
396 assert(timi != NULL);
397
398 return timi->section;
399}
400
401/** return the current value of field 0 */
402static
403const char* timinputField0(
404 const TIMINPUT* timi /**< tim input structure */
405 )
406{
407 assert(timi != NULL);
408
409 return timi->f0;
410}
411
412/** return the current value of field 1 */
413static
414const char* timinputField1(
415 const TIMINPUT* timi /**< tim input structure */
416 )
417{
418 assert(timi != NULL);
419
420 return timi->f1;
421}
422
423/** return the current value of field 2 */
424static
425const char* timinputField2(
426 const TIMINPUT* timi /**< tim input structure */
427 )
428{
429 assert(timi != NULL);
430
431 return timi->f2;
432}
433
434/** return the current value of field 3 */
435static
436const char* timinputField3(
437 const TIMINPUT* timi /**< tim input structure */
438 )
439{
440 assert(timi != NULL);
441
442 return timi->f3;
443}
444
445/** returns if an error was detected */
446static
448 const TIMINPUT* timi /**< tim input structure */
449 )
450{
451 assert(timi != NULL);
452
453 return timi->haserror;
454}
455
456/** set the section in the tim input structure to given section */
457static
459 TIMINPUT* timi, /**< tim input structure */
460 TIMSECTION section /**< section that is set */
461 )
462{
463 assert(timi != NULL);
464
465 timi->section = section;
466}
467
468/** set the problem name in the tim input structure to given problem name */
469static
471 TIMINPUT* timi, /**< tim input structure */
472 const char* probname /**< name of the problem to set */
473 )
474{
475 assert(timi != NULL);
476 assert(probname != NULL);
477 assert(strlen(probname) < sizeof(timi->probname));
478
479 (void)SCIPmemccpy(timi->probname, probname, '\0', TIM_MAX_NAMELEN - 1);
480}
481
482/** set the problem var name that starts a stage in the tim input structure to given objective name */
483static
485 TIMINPUT* timi, /**< tim input structure */
486 SCIP* scip, /**< SCIP data structure */
487 const char* varname, /**< name of the variable that starts the stage */
488 int stagenum /**< the stage number the variable starts */
489 )
490{
491 assert(timi != NULL);
492 assert(varname != NULL);
493
494 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &timi->stagestartvars[stagenum], varname, strlen(varname) + 1) ); /*lint !e866*/
495
496 return SCIP_OKAY;
497}
498
499/** set the problem constraint name that starts a stage in the tim input structure to given objective name */
500static
502 TIMINPUT* timi, /**< tim input structure */
503 SCIP* scip, /**< SCIP data structure */
504 const char* consname, /**< name of the constraint that starts the stage */
505 int stagenum /**< the stage number the constraint starts */
506 )
507{
508 assert(timi != NULL);
509 assert(consname != NULL);
510
511 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &timi->stagestartcons[stagenum], consname, strlen(consname) + 1) ); /*lint !e866*/
512
513 return SCIP_OKAY;
514}
515
516/** set the stage name in the tim input structure to given objective name */
517static
519 TIMINPUT* timi, /**< tim input structure */
520 SCIP* scip, /**< SCIP data structure */
521 const char* stagename, /**< name of the stage */
522 int stagenum /**< the stage number the constraint starts */
523 )
524{
525 assert(timi != NULL);
526 assert(stagename != NULL);
527
528 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &timi->stagenames[stagenum], stagename, strlen(stagename) + 1) ); /*lint !e866*/
529
530 return SCIP_OKAY;
531}
532
533static
535 TIMINPUT* timi /**< tim input structure */
536 )
537{
538 assert(timi != NULL);
539
540 SCIPerrorMessage("Syntax error in line %d\n", timi->lineno);
541 timi->section = TIM_ENDATA;
542 timi->haserror = TRUE;
543}
544
545/** fill the line from \p pos up to column 80 with blanks. */
546static
548 char* buf, /**< buffer to clear */
549 unsigned int pos /**< position to start the clearing process */
550 )
551{
552 unsigned int i;
553
554 for(i = pos; i < 80; i++)
555 buf[i] = BLANK;
556 buf[80] = '\0';
557}
558
559/** read a tim format data line and parse the fields. */
560static
562 TIMINPUT* timi /**< tim input structure */
563 )
564{
565 unsigned int len;
566 unsigned int i;
567 char* s;
568 SCIP_Bool is_empty;
569 char* nexttok;
570
571 do
572 {
573 timi->f0 = timi->f1 = timi->f2 = timi->f3 = 0;
574
575 /* Read until we have not a comment line. */
576 do
577 {
578 timi->buf[TIM_MAX_LINELEN-1] = '\0';
579 if( NULL == SCIPfgets(timi->buf, (int) sizeof(timi->buf), timi->fp) )
580 return FALSE;
581 timi->lineno++;
582 }
583 while( *timi->buf == '*' ); /* coverity[a_loop_bound] */
584
585 /* Normalize line */
586 len = (unsigned int) strlen(timi->buf);
587
588 for( i = 0; i < len; i++ )
589 if( (timi->buf[i] == '\t') || (timi->buf[i] == '\n') || (timi->buf[i] == '\r') )
590 timi->buf[i] = BLANK;
591
592 if( len < 80 )
593 clearFrom(timi->buf, len);
594
595 SCIPdebugMessage("line %d: <%s>\n", timi->lineno, timi->buf);
596
597 assert(strlen(timi->buf) >= 80);
598
599 /* Look for new section */
600 if( *timi->buf != BLANK )
601 {
602 timi->f0 = SCIPstrtok(&timi->buf[0], " ", &nexttok);
603
604 assert(timi->f0 != 0);
605
606 timi->f1 = SCIPstrtok(NULL, " ", &nexttok);
607
608 return TRUE;
609 }
610
611 s = &timi->buf[1];
612
613 /* At this point it is not clear if we have a indicator field.
614 * If there is none (e.g. empty) f1 will be the first name field.
615 * If there is one, f2 will be the first name field.
616 *
617 * Initially comment marks '$' are only allowed in the beginning
618 * of the 2nd and 3rd name field. We test all fields but the first.
619 * This makes no difference, since if the $ is at the start of a value
620 * field, the line will be erroneous anyway.
621 */
622 do
623 {
624 if( NULL == (timi->f1 = SCIPstrtok(s, " ", &nexttok)) )
625 break;
626
627 if( (NULL == (timi->f2 = SCIPstrtok(NULL, " ", &nexttok))) || (*timi->f2 == '$') )
628 {
629 timi->f2 = 0;
630 break;
631 }
632
633 if( (NULL == (timi->f3 = SCIPstrtok(NULL, " ", &nexttok))) || (*timi->f3 == '$') )
634 {
635 timi->f3 = 0;
636 break;
637 }
638 }
639 while( FALSE );
640
641 /* check for empty lines */
642 is_empty = (timi->f0 == NULL && timi->f1 == NULL);
643 }
644 while( is_empty );
645
646 return TRUE;
647}
648
649/** Process TIME section. */
650static
652 SCIP* scip, /**< SCIP data structure */
653 TIMINPUT* timi /**< tim input structure */
654 )
655{
656 SCIPdebugMsg(scip, "read problem name from TIME section\n");
657
658 /* This has to be the Line with the TIME section. */
659 if( !timinputReadLine(timi) || timinputField0(timi) == NULL || strcmp(timinputField0(timi), "TIME") )
660 {
662 return SCIP_OKAY;
663 }
664
665 /* Sometimes the name is omitted. */
666 timinputSetProbname(timi, (timinputField1(timi) == 0) ? "_TIM_" : timinputField1(timi));
667
668 /* This has to be a new section */
669 /* coverity[tainted_data] */
670 if( !timinputReadLine(timi) || (timinputField0(timi) == NULL) )
671 {
673 return SCIP_OKAY;
674 }
675
676 if( strncmp(timinputField0(timi), "PERIODS", 7) == 0 )
678 else
679 {
681 return SCIP_OKAY;
682 }
683
684 return SCIP_OKAY;
685}
686
687/** Process PERIODS section. */
688static
690 TIMINPUT* timi, /**< tim input structure */
691 SCIP* scip /**< SCIP data structure */
692 )
693{
694 SCIPdebugMsg(scip, "read Periods\n");
695
696 /* coverity[tainted_data_sink_lv_call] */
697 /* coverity[tainted_data] */
698 while( timinputReadLine(timi) )
699 {
700 if( timinputField0(timi) != NULL )
701 {
702 if( strcmp(timinputField0(timi), "PERIODS") == 0 )
704 else if( strcmp(timinputField0(timi), "ENDATA") == 0 )
706 else
708
709 return SCIP_OKAY;
710 }
711
712 if( timi->nstages + 1 >= timi->stagesize )
713 {
717 }
718
722
723 timi->nstages++;
724 }
726
727 return SCIP_OKAY;
728}
729
730
731/** Read time data for the SMPS file format. */
732static
734 SCIP* scip, /**< SCIP data structure */
735 SCIP_READER* reader, /**< the file reader itself */
736 const char* filename /**< name of the input file */
737 )
738{
739 SCIP_FILE* fp;
740 TIMINPUT* timi;
741 SCIP_RETCODE retcode;
742 SCIP_Bool error = TRUE;
743
744 assert(scip != NULL);
745 assert(filename != NULL);
746
747 fp = SCIPfopen(filename, "r");
748 if( fp == NULL )
749 {
750 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
751 SCIPprintSysError(filename);
752
753 return SCIP_NOFILE;
754 }
755
757
758 SCIP_CALL_TERMINATE( retcode, readTime(scip, timi), TERMINATE );
759
760 while( timinputSection(timi) == TIM_PERIODS )
761 {
762 /* coverity[tainted_data] */
763 SCIP_CALL_TERMINATE( retcode, readPeriods(timi, scip), TERMINATE );
764 }
765 if( timinputSection(timi) != TIM_ENDATA )
767
768 error = timinputHasError(timi);
769
770 if( !error )
771 {
772 SCIP_CALL_TERMINATE( retcode, createReaderdata(scip, reader, timi), TERMINATE );
773 }
774
775 TERMINATE:
776 timinputFree(scip, &timi);
777 SCIPfclose(fp);
778
779 if( error )
780 return SCIP_READERROR;
781 else
782 return SCIP_OKAY;
783}
784
785/*
786 * Callback methods of reader
787 */
788
789/** copy method for reader plugins (called when SCIP copies plugins) */
790static
792{ /*lint --e{715}*/
793 assert(scip != NULL);
794 assert(reader != NULL);
795
797
798 /* call inclusion method of reader */
800
801 return SCIP_OKAY;
802}
803
804/** destructor of reader to free user data (called when SCIP is exiting) */
805static
807{
808 SCIP_READERDATA* readerdata;
809
810 assert(scip != NULL);
811 assert(reader != NULL);
812
813 readerdata = SCIPreaderGetData(reader);
814 assert(readerdata != NULL);
815
816 freeReaderdata(scip, readerdata);
817
818 SCIPfreeBlockMemory(scip, &readerdata);
819
820 return SCIP_OKAY;
821}
822
823/** reads the stage information for a stochastic programming instance in SMPS format */
824static
826{ /*lint --e{715}*/
827 SCIP_READER* correader;
828
829 assert(reader != NULL);
830 assert(result != NULL);
831
833
835
836 correader = SCIPfindReader(scip, "correader");
837
838 if( correader == NULL )
839 {
840 SCIPwarningMessage(scip, "It is necessary to include the \"cor\" reader\n");
841 (*result) = SCIP_DIDNOTRUN;
842 return SCIP_OKAY;
843 }
844
845 /* checking whether the cor file has been read */
846 if( !SCIPcorHasRead(correader) )
847 {
848 SCIPwarningMessage(scip, "The core file must be read before the time and stochastic files.\n");
849 (*result) = SCIP_DIDNOTRUN;
850 return SCIP_OKAY;
851 }
852
853 /* when reading the TIM file, it is necessary to free the TIM and STO reader data. This is the STO file generates
854 * data based on the data from the TIM file. For most readers, there is no problem data stored in the reader data,
855 * and hence the reader data doesn't need to be freed.
856 */
859
860 SCIP_CALL( SCIPreadTim(scip, filename, result) );
861
862 return SCIP_OKAY;
863}
864
865
866/*
867 * tim file reader specific interface methods
868 */
869
870/** includes the tim file reader in SCIP */
872 SCIP* scip /**< SCIP data structure */
873 )
874{
875 SCIP_READERDATA* readerdata;
876 SCIP_READER* reader;
877
878 /* create reader data */
879 SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
880 readerdata->read = FALSE;
881
882 /* include reader */
884
885 /* set non fundamental callbacks via setter functions */
886 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyTim) );
887 SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeTim) );
888 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadTim) );
889
890 return SCIP_OKAY;
891}
892
893
894/** reads problem from file */
896 SCIP* scip, /**< SCIP data structure */
897 const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
898 SCIP_RESULT* result /**< pointer to store the result of the file reading call */
899 )
900{
901 SCIP_READER* reader;
902 SCIP_RETCODE retcode;
903 SCIP_READERDATA* readerdata;
904
905 assert(scip != NULL);
906 assert(result != NULL);
907
909 assert(reader != NULL);
910
911 retcode = readTim(scip, reader, filename);
912
913 if( retcode == SCIP_PLUGINNOTFOUND )
914 retcode = SCIP_READERROR;
915
916 if( retcode == SCIP_NOFILE || retcode == SCIP_READERROR )
917 return retcode;
918
919 SCIP_CALL( retcode );
920
921 /* creating the stages */
922 SCIP_CALL( createStages(scip, reader, SCIPfindReader(scip, "correader")) );
923
924 /* setting the read flag to TRUE */
925 readerdata = SCIPreaderGetData(reader);
926 readerdata->read = TRUE;
927
929
930 return SCIP_OKAY;
931}
932
933/** frees the reader data for the tim file */
935 SCIP* scip /**< the SCIP data structure */
936 )
937{
938 SCIP_READER* reader;
939 SCIP_READERDATA* readerdata;
940
941 assert(scip != NULL);
942
944 assert(reader != NULL);
945
946 readerdata = SCIPreaderGetData(reader);
947 assert(readerdata != NULL);
948
949 freeReaderdata(scip, readerdata);
950
951 return SCIP_OKAY;
952}
953
954
955/*
956 * Interface methods for the cor and sto files
957 */
958
959/* return whether the tim file has been read */
961 SCIP_READER* reader /**< the file reader itself */
962 )
963{
964 SCIP_READERDATA* readerdata;
965
966 assert(reader != NULL);
967
969
970 readerdata = SCIPreaderGetData(reader);
971 assert(readerdata != NULL);
972
973 return readerdata->read;
974}
975
976
977/* returns the number of stages */
979 SCIP* scip /**< SCIP data structure */
980 )
981{
982 SCIP_READER* reader;
983 SCIP_READERDATA* readerdata;
984
986
987 assert(reader != NULL);
988
990
991 readerdata = SCIPreaderGetData(reader);
992 assert(readerdata != NULL);
993
994 return readerdata->nstages;
995}
996
997/* returns the name for a given stage */
999 SCIP* scip, /**< SCIP data structure */
1000 int stagenum /**< the number of the requested stage */
1001 )
1002{
1003 SCIP_READER* reader;
1004 SCIP_READERDATA* readerdata;
1005
1006 reader = SCIPfindReader(scip, READER_NAME);
1007
1008 assert(reader != NULL);
1009
1011
1012 readerdata = SCIPreaderGetData(reader);
1013 assert(readerdata != NULL);
1014 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1015
1016 return readerdata->stagenames[stagenum];
1017}
1018
1019/* returns the stage name for a given constraint name */
1021 SCIP* scip, /**< SCIP data structure */
1022 const char* consname /**< the constraint to search for */
1023 )
1024{
1025 SCIP_READER* reader;
1026 SCIP_READERDATA* readerdata;
1027 int stagenum;
1028 int i;
1029 int j;
1030
1031 reader = SCIPfindReader(scip, READER_NAME);
1032
1033 assert(reader != NULL);
1034
1036
1037 readerdata = SCIPreaderGetData(reader);
1038 assert(readerdata != NULL);
1039
1040 /* looping over all stages to find the provided constraint */
1041 stagenum = -1;
1042 for( i = 0; i < readerdata->nstages; i++ )
1043 {
1044 for( j = 0; j < readerdata->stages[i]->nconss; j++ )
1045 {
1046 if( strcmp(SCIPconsGetName(readerdata->stages[i]->conss[j]), consname) == 0 )
1047 {
1048 stagenum = i;
1049 break;
1050 }
1051 }
1052
1053 if( stagenum >= 0 )
1054 break;
1055 }
1056 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1057
1058 return readerdata->stagenames[stagenum];
1059}
1060
1061/* returns the number for a given stage */
1063 SCIP* scip, /**< SCIP data structure */
1064 const char* stage /**< the name of the requested stage */
1065 )
1066{
1067 SCIP_READER* reader;
1068 SCIP_READERDATA* readerdata;
1069 int i;
1070 int stagenum;
1071
1072 reader = SCIPfindReader(scip, READER_NAME);
1073
1074 assert(reader != NULL);
1075
1077
1078 readerdata = SCIPreaderGetData(reader);
1079 assert(readerdata != NULL);
1080
1081 stagenum = -1;
1082 for( i = 0; i < readerdata->nstages; i++ )
1083 {
1084 if( strcmp(readerdata->stagenames[i], stage) == 0 )
1085 {
1086 stagenum = i;
1087 break;
1088 }
1089 }
1090
1091 if( stagenum < 0 )
1092 {
1093 SCIPerrorMessage("Stage <%s> was not found in the TIM file. Check the SMPS files (COR, TIM and STO)\n", stage);
1094 SCIPABORT();
1095 }
1096
1097 return stagenum;
1098}
1099
1100/* returns the array of variables for a given stage */
1102 SCIP* scip, /**< SCIP data structure */
1103 int stagenum /**< the number of the requested stage */
1104 )
1105{
1106 SCIP_READER* reader;
1107 SCIP_READERDATA* readerdata;
1108
1109 reader = SCIPfindReader(scip, READER_NAME);
1110
1111 assert(reader != NULL);
1112
1114
1115 readerdata = SCIPreaderGetData(reader);
1116 assert(readerdata != NULL);
1117 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1118
1119 return readerdata->stages[stagenum]->vars;
1120}
1121
1122/* returns an array of constraints for a given stage */
1124 SCIP* scip, /**< SCIP data structure */
1125 int stagenum /**< the number of the requested stage */
1126 )
1127{
1128 SCIP_READER* reader;
1129 SCIP_READERDATA* readerdata;
1130
1131 reader = SCIPfindReader(scip, READER_NAME);
1132
1133 assert(reader != NULL);
1134
1136
1137 readerdata = SCIPreaderGetData(reader);
1138 assert(readerdata != NULL);
1139 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1140
1141 return readerdata->stages[stagenum]->conss;
1142}
1143
1144/* returns the number of variables for a given stage */
1146 SCIP* scip, /**< SCIP data structure */
1147 int stagenum /**< the number of the requested stage */
1148 )
1149{
1150 SCIP_READER* reader;
1151 SCIP_READERDATA* readerdata;
1152
1153 reader = SCIPfindReader(scip, READER_NAME);
1154
1155 assert(reader != NULL);
1156
1158
1159 readerdata = SCIPreaderGetData(reader);
1160 assert(readerdata != NULL);
1161 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1162
1163 return readerdata->stages[stagenum]->nvars;
1164}
1165
1166/* returns the number of constraints for a given stage */
1168 SCIP* scip, /**< SCIP data structure */
1169 int stagenum /**< the number of the requested stage */
1170 )
1171{
1172 SCIP_READER* reader;
1173 SCIP_READERDATA* readerdata;
1174
1175 reader = SCIPfindReader(scip, READER_NAME);
1176
1177 assert(reader != NULL);
1178
1180
1181 readerdata = SCIPreaderGetData(reader);
1182 assert(readerdata != NULL);
1183 assert(stagenum >= 0 && stagenum < readerdata->nstages);
1184
1185 return readerdata->stages[stagenum]->nconss;
1186}
#define NULL
Definition def.h:257
#define SCIP_Bool
Definition def.h:100
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define SCIP_Real
Definition def.h:165
#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
#define SCIP_CALL_FINALLY(x, y)
Definition def.h:406
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
SCIP_RETCODE SCIPreadTim(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition reader_tim.c:895
const char * SCIPcorGetVarName(SCIP_READER *reader, int i)
Definition reader_cor.c:320
SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
Definition reader_cor.c:269
const char * SCIPcorGetConsName(SCIP_READER *reader, int i)
Definition reader_cor.c:339
SCIP_RETCODE SCIPfreeReaderdataSto(SCIP *scip)
int SCIPcorGetNConsNames(SCIP_READER *reader)
Definition reader_cor.c:303
int SCIPcorGetNVarNames(SCIP_READER *reader)
Definition reader_cor.c:286
SCIP_RETCODE SCIPincludeReaderTim(SCIP *scip)
Definition reader_tim.c:871
int SCIPgetNVars(SCIP *scip)
Definition scip_prob.c:2246
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition scip_prob.c:3189
SCIP_CONS * SCIPfindCons(SCIP *scip, const char *name)
Definition scip_prob.c:3525
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition misc.c:3095
SCIP_RETCODE SCIPhashmapInsert(SCIP_HASHMAP *hashmap, void *origin, void *image)
Definition misc.c:3143
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition misc.c:3061
#define SCIPdebugMsg
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
const char * SCIPconsGetName(SCIP_CONS *cons)
Definition cons.c:8393
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition scip_mem.h:110
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition scip_mem.c:57
#define SCIPensureBlockMemoryArray(scip, ptr, arraysizeptr, minsize)
Definition scip_mem.h:107
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition scip_mem.h:93
#define SCIPfreeBlockMemory(scip, ptr)
Definition scip_mem.h:108
#define SCIPallocBlockMemory(scip, ptr)
Definition scip_mem.h:89
#define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
Definition scip_mem.h:105
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_READERDATA * SCIPreaderGetData(SCIP_READER *reader)
Definition reader.c:625
SCIP_READER * SCIPfindReader(SCIP *scip, const char *name)
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition reader.c:700
SCIP_RETCODE SCIPsetReaderFree(SCIP *scip, SCIP_READER *reader,)
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
void SCIPprintSysError(const char *message)
Definition misc.c:10719
char * SCIPstrtok(char *s, const char *delim, char **ptrptr)
Definition misc.c:10768
int SCIPmemccpy(char *dest, const char *src, char stop, unsigned int cnt)
Definition misc.c:10694
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
int nvars
SCIP_VAR * var
public methods for managing constraints
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
STO file reader - the stochastic information of an instance in SMPS format.
static SCIP_RETCODE timinputCreate(SCIP *scip, TIMINPUT **timi, SCIP_FILE *fp)
Definition reader_tim.c:336
static SCIP_RETCODE addConstraintToStage(SCIP *scip, TIMSTAGE *stage, const char *consname)
Definition reader_tim.c:151
int SCIPtimFindStage(SCIP *scip, const char *stage)
#define TIM_DEFAULT_STAGESIZE
Definition reader_tim.c:58
static const char * timinputField0(const TIMINPUT *timi)
Definition reader_tim.c:403
#define TIM_MAX_LINELEN
Definition reader_tim.c:56
static void timinputFree(SCIP *scip, TIMINPUT **timi)
Definition reader_tim.c:369
static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READER *reader, TIMINPUT *timi)
Definition reader_tim.c:236
static SCIP_RETCODE timinputSetStageStartVar(TIMINPUT *timi, SCIP *scip, const char *varname, int stagenum)
Definition reader_tim.c:484
static SCIP_RETCODE createStages(SCIP *scip, SCIP_READER *reader, SCIP_READER *correader)
Definition reader_tim.c:183
static void clearFrom(char *buf, unsigned int pos)
Definition reader_tim.c:547
static const char * timinputField3(const TIMINPUT *timi)
Definition reader_tim.c:436
enum TimSection TIMSECTION
Definition reader_tim.c:94
static void timinputSetProbname(TIMINPUT *timi, const char *probname)
Definition reader_tim.c:470
#define TIM_MAX_NAMELEN
Definition reader_tim.c:57
int SCIPtimGetStageNVars(SCIP *scip, int stagenum)
static void timinputSetSection(TIMINPUT *timi, TIMSECTION section)
Definition reader_tim.c:458
static SCIP_Bool timinputReadLine(TIMINPUT *timi)
Definition reader_tim.c:561
SCIP_RETCODE SCIPfreeReaderdataTim(SCIP *scip)
Definition reader_tim.c:934
static SCIP_RETCODE readTime(SCIP *scip, TIMINPUT *timi)
Definition reader_tim.c:651
static const char * timinputField1(const TIMINPUT *timi)
Definition reader_tim.c:414
SCIP_VAR ** SCIPtimGetStageVars(SCIP *scip, int stagenum)
static SCIP_RETCODE timinputSetStageStartCons(TIMINPUT *timi, SCIP *scip, const char *consname, int stagenum)
Definition reader_tim.c:501
static SCIP_RETCODE timinputSetStageName(TIMINPUT *timi, SCIP *scip, const char *stagename, int stagenum)
Definition reader_tim.c:518
static void freeReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition reader_tim.c:295
SCIP_Bool SCIPtimHasRead(SCIP_READER *reader)
Definition reader_tim.c:960
TimSection
Definition reader_tim.c:89
@ TIM_TIME
Definition reader_tim.c:90
@ TIM_ENDATA
Definition reader_tim.c:92
@ TIM_PERIODS
Definition reader_tim.c:91
int SCIPtimGetNStages(SCIP *scip)
Definition reader_tim.c:978
static const char * timinputField2(const TIMINPUT *timi)
Definition reader_tim.c:425
const char * SCIPtimGetStageName(SCIP *scip, int stagenum)
Definition reader_tim.c:998
struct TimStage TIMSTAGE
Definition reader_tim.c:74
#define TIM_DEFAULT_ARRAYSIZE
Definition reader_tim.c:59
static SCIP_RETCODE readTim(SCIP *scip, SCIP_READER *reader, const char *filename)
Definition reader_tim.c:733
static SCIP_RETCODE addVariableToStage(SCIP *scip, TIMSTAGE *stage, const char *varname)
Definition reader_tim.c:119
static TIMSECTION timinputSection(const TIMINPUT *timi)
Definition reader_tim.c:392
SCIP_CONS ** SCIPtimGetStageConss(SCIP *scip, int stagenum)
static void timinputSyntaxerror(TIMINPUT *timi)
Definition reader_tim.c:534
static SCIP_RETCODE readPeriods(TIMINPUT *timi, SCIP *scip)
Definition reader_tim.c:689
static SCIP_Bool timinputHasError(const TIMINPUT *timi)
Definition reader_tim.c:447
int SCIPtimGetStageNConss(SCIP *scip, int stagenum)
const char * SCIPtimConsGetStageName(SCIP *scip, const char *consname)
struct TimInput TIMINPUT
Definition reader_tim.c:115
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 numerical tolerances
public methods for global and local (sub)problems
public methods for reader plugins
int stagesize
Definition reader_tim.c:113
SCIP_FILE * fp
Definition reader_tim.c:100
const char * f1
Definition reader_tim.c:105
SCIP_Bool haserror
Definition reader_tim.c:102
const char ** stagenames
Definition reader_tim.c:111
char buf[TIM_MAX_LINELEN]
Definition reader_tim.c:103
const char ** stagestartcons
Definition reader_tim.c:110
const char * f3
Definition reader_tim.c:107
int nstages
Definition reader_tim.c:112
char probname[TIM_MAX_NAMELEN]
Definition reader_tim.c:108
TIMSECTION section
Definition reader_tim.c:99
const char * f2
Definition reader_tim.c:106
const char * f0
Definition reader_tim.c:104
const char ** stagestartvars
Definition reader_tim.c:109
SCIP_CONS ** conss
Definition reader_tim.c:66
SCIP_HASHMAP * consnametocons
Definition reader_tim.c:68
SCIP_HASHMAP * varnametovar
Definition reader_tim.c:67
SCIP_VAR ** vars
Definition reader_tim.c:65
int conssize
Definition reader_tim.c:72
int varssize
Definition reader_tim.c:71
int nvars
Definition reader_tim.c:69
int nconss
Definition reader_tim.c:70
struct SCIP_Cons SCIP_CONS
Definition type_cons.h:63
struct SCIP_HashMap SCIP_HASHMAP
Definition type_misc.h:106
struct SCIP_ReaderData SCIP_READERDATA
Definition type_reader.h:54
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
#define SCIP_DECL_READERFREE(x)
Definition type_reader.h:72
@ SCIP_DIDNOTRUN
Definition type_result.h:42
@ SCIP_SUCCESS
Definition type_result.h:58
enum SCIP_Result SCIP_RESULT
Definition type_result.h:61
@ SCIP_NOFILE
@ SCIP_READERROR
@ SCIP_PLUGINNOTFOUND
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39
struct SCIP_Var SCIP_VAR
Definition type_var.h:166