Scippy

SCIP

Solving Constraint Integer Programs

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