Scippy

SCIP

Solving Constraint Integer Programs

reader_sto.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_sto.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief STO file reader - the stochastic information of an instance in SMPS format
28 * @author Stephen J. Maher
29 */
30
31
32/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
33
36#include "scip/cons_linear.h"
37#include "scip/pub_cons.h"
38#include "scip/pub_fileio.h"
39#include "scip/pub_message.h"
40#include "scip/pub_misc.h"
41#include "scip/pub_reader.h"
42#include "scip/pub_var.h"
43#include "scip/reader_cor.h"
44#include "scip/reader_sto.h"
45#include "scip/reader_tim.h"
46#include "scip/scip_cons.h"
47#include "scip/scip_debug.h"
48#include "scip/scipdefplugins.h"
49#include "scip/scip_general.h"
50#include "scip/scip_mem.h"
51#include "scip/scip_message.h"
52#include "scip/scip_numerics.h"
53#include "scip/scip_param.h"
54#include "scip/scip_prob.h"
55#include "scip/scip_reader.h"
56#include "scip/scip_var.h"
57#include <stdlib.h>
58#include <string.h>
59
60#define READER_NAME "storeader"
61#define READER_DESC "file reader for stochastic information of stochastic programs in the SMPS file format"
62#define READER_EXTENSION "sto"
63
64#define DEFAULT_USEBENDERS FALSE /**< should Benders' decomposition be used for the stochastic program? */
65
66/*
67 * sto reader internal methods
68 */
69
70#define STO_MAX_LINELEN 1024
71#define STO_MAX_NAMELEN 256
72
73#define STO_DEFAULT_ARRAYSIZE 100
74#define STO_DEFAULT_ENTRIESSIZE 20
75#define STO_DEFAULT_BLOCKARRAYSIZE 5
76#define STO_DEFAULT_CHILDRENSIZE 5
77
78#define BLANK ' '
79
80typedef struct StoScenario STOSCENARIO;
81
82/** STO reading data */
83struct SCIP_ReaderData
84{
85 SCIP_Bool created; /**< flag to indicate that the reader data has been created */
86 SCIP_Bool usebenders;
87 STOSCENARIO* scenariotree; /**< the multi stage scenario tree */
88 int numscenarios; /**< the total number of scenarios in the scenario tree */
89};
90
91
93{
94 SCIP* scip; /**< the SCIP instance for the scenario. Used for benders. */
95 SCIP** subproblems; /**< the SCIP instances for the subproblems */
96 STOSCENARIO* parent; /**< parent scenario. */
97 STOSCENARIO** children; /**< children scenarios. */
98 int nchildren; /**< the number of children scenarios. */
99 int childrensize; /**< the size of the children array. */
100 int nsubproblems; /**< the number of subproblems */
101 int stagenum; /**< the number of the stage */
102 int scenarionum; /**< the scenario number of this stage */
103 const char* stagename; /**< the stage name */
104 const char* name; /**< the scenario name. */
105 SCIP_Real probability; /**< the probability for this scenario. */
106 SCIP_Real lowerbound; /**< the lower bound for this scenario */
107 /* the following describes the modifications to the constraint matrix and rhs for each scenario. */
108 const char** rownames; /**< the names of the rows with a changed value. */
109 const char** colnames; /**< the names of the columns with a changed value. */
110 SCIP_Real* values; /**< the values for the given row/column pair. */
111 int nentries; /**< the number of row/column pairs */
112 int entriessize; /**< the size of the row/colum arrays */
113};
114
115
116
117/** enum containing all sto sections */
119{
127
128/** enum containing the types of stochastic information */
130{
139
140/** sto input structure */
142{
149 const char* f0;
150 const char* f1;
151 const char* f2;
152 const char* f3;
153 const char* f4;
154 const char* f5;
155 const char* f6;
158};
159typedef struct StoInput STOINPUT;
160
161/** creates a scenario structure */
162static
164 SCIP* scip, /**< SCIP data structure */
165 STOSCENARIO** scenariodata /**< the scenario to be created */
166 )
167{
168 assert(scip != NULL);
169
170 SCIPdebugMessage("Creating scenario data.\n");
171
172 SCIP_CALL( SCIPallocBlockMemory(scip, scenariodata) );
173
174 (*scenariodata)->scip = NULL;
175 (*scenariodata)->subproblems = NULL;
176 (*scenariodata)->parent = NULL;
177 (*scenariodata)->nchildren = 0;
178 (*scenariodata)->childrensize = STO_DEFAULT_CHILDRENSIZE;
179 (*scenariodata)->nsubproblems = 0;
180 (*scenariodata)->stagenum = -1;
181 (*scenariodata)->scenarionum = -1;
182 (*scenariodata)->stagename = NULL;
183 (*scenariodata)->name = NULL;
184 (*scenariodata)->probability = 1.0;
185 (*scenariodata)->lowerbound = -SCIPinfinity(scip);
186 (*scenariodata)->nentries = 0;
187 (*scenariodata)->entriessize = STO_DEFAULT_ENTRIESSIZE;
188
189 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*scenariodata)->children, (*scenariodata)->childrensize) );
190 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*scenariodata)->rownames, (*scenariodata)->entriessize) );
191 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*scenariodata)->colnames, (*scenariodata)->entriessize) );
192 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*scenariodata)->values, (*scenariodata)->entriessize) );
193
194 return SCIP_OKAY;
195}
196
197/** frees the memory used for the scenario tree */
198static
200 SCIP* scip, /**< the SCIP data structure */
201 STOSCENARIO** scenariotree /**< the scenario tree */
202 )
203{
204 int nchildren;
205 int i;
206
207 assert(scip != NULL);
208 assert(scenariotree != NULL);
209 assert(*scenariotree != NULL);
210
211 SCIPdebugMessage("Freeing scenario <%s> in stage <%s>\n", (*scenariotree)->name,
212 (*scenariotree)->stagename);
213
214 /* storing the number of children before starting the recursive freeing */
215 nchildren = (*scenariotree)->nchildren;
216
217 while( (*scenariotree)->nchildren > 0 )
218 {
219 SCIP_CALL( freeScenarioTree(scip, &(*scenariotree)->children[(*scenariotree)->nchildren - 1]) );
220 (*scenariotree)->nchildren--;
221 }
222
223 for( i = (*scenariotree)->nentries - 1; i >= 0; i-- )
224 {
225 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->colnames[i], strlen((*scenariotree)->colnames[i]) + 1);
226 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->rownames[i], strlen((*scenariotree)->rownames[i]) + 1);
227 }
228
229 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->values, (*scenariotree)->entriessize);
230 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->colnames, (*scenariotree)->entriessize);
231 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->rownames, (*scenariotree)->entriessize);
232 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->children, (*scenariotree)->childrensize);
233
234 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->name, strlen((*scenariotree)->name) + 1);
235 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->stagename, strlen((*scenariotree)->stagename) + 1);
236
237 /* freeing the subproblem SCIP instances */
238 for( i = (*scenariotree)->nsubproblems - 1; i >= 0; i-- )
239 SCIP_CALL( SCIPfree(&(*scenariotree)->subproblems[i]) );
240
241 /* freeing the array that stores the subproblems */
242 if( nchildren > 0 && (*scenariotree)->subproblems != NULL )
243 SCIPfreeBlockMemoryArray(scip, &(*scenariotree)->subproblems, nchildren);
244
245 SCIPfreeBlockMemory(scip, scenariotree);
246
247 return SCIP_OKAY;
248}
249
250/** sets the SCIP pointer to the scenario */
251static
253 STOSCENARIO* scenario, /**< the scenario */
254 SCIP* scip /**< the SCIP data structure */
255 )
256{
257 assert(scenario != NULL);
258 assert(scip != NULL);
259
260 scenario->scip = scip;
261}
262
263/** returns the SCIP pointer to the scenario */
264static
266 STOSCENARIO* scenario /**< the scenario */
267 )
268{
269 assert(scenario != NULL);
270
271 return scenario->scip;
272}
273
274/** creates the subproblem array. This array will be the same size as the number of children */
275static
277 SCIP* scip, /**< the SCIP data structure */
278 STOSCENARIO* scenario /**< the scenario */
279 )
280{
281 assert(scip != NULL);
282 assert(scenario != NULL);
283
285
286 return SCIP_OKAY;
287}
288
289/** adds a scenario to the subproblem array */
290static
292 STOSCENARIO* scenario, /**< the scenario */
293 SCIP* subproblem /**< the subproblems data structure */
294 )
295{
296 assert(scenario != NULL);
297 assert(subproblem != NULL);
298
299 assert(scenario->nsubproblems + 1 <= scenario->nchildren);
300
301 scenario->subproblems[scenario->nsubproblems] = subproblem;
302 scenario->nsubproblems++;
303}
304
305/** returns the subproblem array for the scenario */
306static
308 STOSCENARIO* scenario /**< the scenario */
309 )
310{
311 assert(scenario != NULL);
312
313 return scenario->subproblems;
314}
315
316/** returns the number of children for a given scenario */
317static
319 STOSCENARIO* scenario /**< the scenario */
320 )
321{
322 assert(scenario != NULL);
323
324 return scenario->nchildren;
325}
326
327/** returns a given child for a given scenario */
328static
330 STOSCENARIO* scenario, /**< the scenario */
331 int childnum /**< the number of the desired child */
332 )
333{
334 assert(scenario != NULL);
335 assert(childnum >= 0 && childnum < scenario->nchildren);
336
337 return scenario->children[childnum];
338}
339
340/** returns the parent of a scenario */
341static
343 STOSCENARIO* scenario /**< the scenario */
344 )
345{
346 assert(scenario != NULL);
347
348 return scenario->parent;
349}
350
351/** sets the stage name */
352static
354 SCIP* scip, /**< the SCIP data structure */
355 STOSCENARIO* scenario, /**< the scenario */
356 const char* stagename /**< the stage name */
357 )
358{
359 assert(scip != NULL);
360 assert(scenario != NULL);
361
362 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &scenario->stagename, stagename, strlen(stagename) + 1) );
363
364 return SCIP_OKAY;
365}
366
367/** returns the stage name */
368static
370 SCIP* scip, /**< the SCIP data structure */
371 STOSCENARIO* scenario /**< the scenario */
372 )
373{
374 assert(scip != NULL);
375 assert(scenario != NULL);
376
377 return scenario->stagename;
378}
379
380/** sets the stage num */
381static
383 SCIP* scip, /**< the SCIP data structure */
384 STOSCENARIO* scenario, /**< the scenario */
385 int stagenum /**< the stage num */
386 )
387{
388 assert(scip != NULL);
389 assert(scenario != NULL);
390
391 scenario->stagenum = stagenum;
392
393 return SCIP_OKAY;
394}
395
396/** returns the stage num */
397static
399 SCIP* scip, /**< the SCIP data structure */
400 STOSCENARIO* scenario /**< the scenario */
401 )
402{
403 assert(scip != NULL);
404 assert(scenario != NULL);
405
406 return scenario->stagenum;
407}
408
409/** sets the scenario name */
410static
412 SCIP* scip, /**< the SCIP data structure */
413 STOSCENARIO* scenario, /**< the scenario */
414 const char* name /**< the scenario name */
415 )
416{
417 assert(scip != NULL);
418 assert(scenario != NULL);
419
420 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &scenario->name, name, strlen(name) + 1) );
421
422 return SCIP_OKAY;
423}
424
425/** returns the scenario name */
426static
427const char* getScenarioName(
428 STOSCENARIO* scenario /**< the scenario */
429 )
430{
431 assert(scenario != NULL);
432
433 return scenario->name;
434}
435
436/** sets the scenario num */
437static
439 SCIP* scip, /**< the SCIP data structure */
440 STOSCENARIO* scenario, /**< the scenario */
441 int scenarionum /**< the scenario num */
442 )
443{
444 assert(scip != NULL);
445 assert(scenario != NULL);
446
447 scenario->scenarionum = scenarionum;
448
449 return SCIP_OKAY;
450}
451
452/** returns the scenario num */
453static
455 SCIP* scip, /**< the SCIP data structure */
456 STOSCENARIO* scenario /**< the scenario */
457 )
458{
459 assert(scip != NULL);
460 assert(scenario != NULL);
461
462 return scenario->scenarionum;
463}
464
465/** sets the scenario probability */
466static
468 SCIP* scip, /**< the SCIP data structure */
469 STOSCENARIO* scenario, /**< the scenario */
470 SCIP_Real probability /**< the scenario probability */
471 )
472{
473 assert(scip != NULL);
474 assert(scenario != NULL);
475
476 scenario->probability = probability;
477
478 return SCIP_OKAY;
479}
480
481/** returns the scenario probability */
482static
484 SCIP* scip, /**< the SCIP data structure */
485 STOSCENARIO* scenario /**< the scenario */
486 )
487{
488 assert(scip != NULL);
489 assert(scenario != NULL);
490
491 return scenario->probability;
492}
493
494/** sets the scenario lowerbound */
495static
497 SCIP* scip, /**< the SCIP data structure */
498 STOSCENARIO* scenario, /**< the scenario */
499 SCIP_Real lowerbound /**< the scenario lowerbound */
500 )
501{
502 assert(scip != NULL);
503 assert(scenario != NULL);
504
505 scenario->lowerbound = lowerbound;
506
507 return SCIP_OKAY;
508}
509
510/** returns the scenario lowerbound */
511static
513 SCIP* scip, /**< the SCIP data structure */
514 STOSCENARIO* scenario /**< the scenario */
515 )
516{
517 assert(scip != NULL);
518 assert(scenario != NULL);
519
520 return scenario->lowerbound;
521}
522
523/** add scenario entry */
524static
526 SCIP* scip, /**< the SCIP data structure */
527 STOSCENARIO* scenario, /**< the scenario */
528 const char* rowname, /**< the row name for the entry */
529 const char* colname, /**< the col name for the entry */
530 SCIP_Real value /**< the value for the entry */
531 )
532{
533 assert(scip != NULL);
534 assert(scenario != NULL);
535
536 if( scenario->nentries + 1 > scenario->entriessize )
537 {
538 int newsize;
539 newsize = SCIPcalcMemGrowSize(scip, scenario->nentries + 1);
540 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &scenario->rownames, scenario->entriessize, newsize) );
541 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &scenario->colnames, scenario->entriessize, newsize) );
542 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &scenario->values, scenario->entriessize, newsize) );
543 scenario->entriessize = newsize;
544 }
545
546 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &scenario->rownames[scenario->nentries], rowname, strlen(rowname) + 1) ); /*lint !e866*/
547 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &scenario->colnames[scenario->nentries], colname, strlen(colname) + 1) ); /*lint !e866*/
548
549 scenario->values[scenario->nentries] = value;
550 scenario->nentries++;
551
552 return SCIP_OKAY;
553}
554
555/** returns the number of entries for a scenario */
556static
558 STOSCENARIO* scenario /**< the scenario */
559 )
560{
561 assert(scenario != NULL);
562
563 return scenario->nentries;
564}
565
566/** returns an entry row for a scenario */
567static
569 STOSCENARIO* scenario, /**< the scenario */
570 int entry /**< the entry number */
571 )
572{
573 assert(scenario != NULL);
574 assert(entry >= 0 && entry < scenario->nentries);
575
576 return scenario->rownames[entry];
577}
578
579/** returns an entry column for a scenario */
580static
582 STOSCENARIO* scenario, /**< the scenario */
583 int entry /**< the entry number */
584 )
585{
586 assert(scenario != NULL);
587 assert(entry >= 0 && entry < scenario->nentries);
588
589 return scenario->colnames[entry];
590}
591
592/** returns an entry value for a scenario */
593static
595 STOSCENARIO* scenario, /**< the scenario */
596 int entry /**< the entry number */
597 )
598{
599 assert(scenario != NULL);
600 assert(entry >= 0 && entry < scenario->nentries);
601
602 return scenario->values[entry];
603}
604
605/** copies a scenario.
606 * In the case of blocks, the scenarios must be combined
607 */
608static
610 SCIP* scip, /**< the SCIP data structure */
611 STOSCENARIO* sourcescenario, /**< the source scenario */
612 STOSCENARIO** targetscenario, /**< the target scenario */
613 SCIP_Bool copyname /**< should the name be copied? */
614 )
615{
616 SCIP_Real probability;
617 SCIP_Real lowerbound;
618 int i;
619
620 assert(scip != NULL);
621 assert(sourcescenario != NULL);
622 assert(targetscenario != NULL);
623
624 /* setting the stage name */
625 if( copyname )
626 {
627 SCIP_CALL( setScenarioName(scip, (*targetscenario), sourcescenario->name) );
628 SCIP_CALL( setScenarioStageName(scip, (*targetscenario), sourcescenario->stagename) );
629 SCIP_CALL( setScenarioNum(scip, (*targetscenario), sourcescenario->scenarionum) );
630 SCIP_CALL( setScenarioStageNum(scip, (*targetscenario), sourcescenario->stagenum) );
631 }
632
633 /* adding the entries from scenario 1 and 2 to the merged scenario */
634 for( i = 0; i < sourcescenario->nentries; i++ )
635 SCIP_CALL( addScenarioEntry(scip, (*targetscenario), sourcescenario->rownames[i], sourcescenario->colnames[i],
636 sourcescenario->values[i]) );
637
638 /* setting the scenario probability */
639 probability = getScenarioProbability(scip, sourcescenario);
640 SCIP_CALL( setScenarioProbability(scip, (*targetscenario), probability) );
641
642 lowerbound = getScenarioLowerbound(scip, sourcescenario);
643 SCIP_CALL( setScenarioLowerbound(scip, (*targetscenario), lowerbound) );
644
645 return SCIP_OKAY;
646}
647
648/** merge scenarios.
649 * In the case of blocks, the scenarios must be combined
650 */
651static
653 SCIP* scip, /**< the SCIP data structure */
654 STOSCENARIO* scenario1, /**< the first scenario */
655 STOSCENARIO** mergedscenario /**< the merged scenario */
656 )
657{
658 SCIP_Real probability;
659 int i;
660
661 assert(scip != NULL);
662 assert(scenario1 != NULL);
663 assert(mergedscenario != NULL);
664
665 /* adding the entries from scenario 1 and 2 to the merged scenario */
666 for( i = 0; i < scenario1->nentries; i++ )
667 SCIP_CALL( addScenarioEntry(scip, (*mergedscenario), scenario1->rownames[i], scenario1->colnames[i],
668 scenario1->values[i]) );
669
670 /* setting the scenario probability */
671 probability = getScenarioProbability(scip, scenario1)*getScenarioProbability(scip, (*mergedscenario));
672 SCIP_CALL( setScenarioProbability(scip, (*mergedscenario), probability) );
673
674 return SCIP_OKAY;
675}
676
677/** adds a child to a given scenario */
678static
680 SCIP* scip, /**< the SCIP data structure */
681 STOSCENARIO** parent, /**< the parent scenario */
682 STOSCENARIO* child /**< the child scenario */
683 )
684{
685 STOSCENARIO* scenario;
686
687 assert(parent != NULL);
688 assert((*parent) != NULL);
689 assert(child != NULL);
690
691 if( (*parent)->nchildren + 1 > (*parent)->childrensize )
692 SCIP_CALL( SCIPensureBlockMemoryArray(scip, &(*parent)->children, &(*parent)->childrensize,
693 (*parent)->nchildren + 1) );
694
695 SCIP_CALL( createScenarioData(scip, &scenario) );
696 SCIP_CALL( copyScenario(scip, child, &scenario, TRUE) );
697 scenario->parent = (*parent);
698
699 (*parent)->children[(*parent)->nchildren] = scenario;
700 (*parent)->nchildren++;
701
702 return SCIP_OKAY;
703}
704
705/** recursively adds the scenarios to the reader data */
706static
708 SCIP* scip, /**< the SCIP data structure */
709 STOSCENARIO** scenariotree, /**< the scenario tree */
710 STOSCENARIO*** scenarios, /**< the array of scenarios */
711 int* numscenarios, /**< the number of scenarios per stage */
712 int numstages, /**< the number of stages */
713 int stage /**< the number of the stage. Also the depth of the tree */
714 )
715{
716 int stageindex;
717 int i;
718
719 assert(scip != NULL);
720 assert(scenariotree != NULL);
721 assert(stage >= 0 && stage < numstages);
722
723 /* finding the scenarios for this stage */
724 for( i = 0; i < numstages; i++ )
725 {
726 if( strcmp(getScenarioStageName(scip, scenarios[i][0]), SCIPtimGetStageName(scip, stage + 1)) == 0 )
727 break;
728 }
729 assert(i < numstages);
730
731 stageindex = i;
732
733 /* adds each scenario to the scenario tree */
734 for( i = 0; i < numscenarios[stageindex]; i++ )
735 {
736 /* adding child to the scenario tree */
737 SCIP_CALL( scenarioAddChild(scip, scenariotree, scenarios[stageindex][i]) );
738
739 /* building the tree below the recently added child */
740 if( stage < numstages - 1 )
741 {
742 STOSCENARIO* child = getScenarioChild((*scenariotree), getScenarioNChildren((*scenariotree)) - 1);
743 SCIP_CALL( buildScenarioTree(scip, &child, scenarios, numscenarios, numstages, stage + 1) );
744 }
745 }
746
747 return SCIP_OKAY;
748}
749
750
751/* adds the scenarios to the reader data */
752static
754 SCIP* scip, /**< the SCIP data structure */
755 SCIP_READERDATA* readerdata, /**< the reader data */
756 STOSCENARIO*** scenarios, /**< the array of scenarios */
757 int* numscenarios, /**< the number of scenarios per stage */
758 int numscenariostages /**< the number of stages for which scenarios were collected */
759 )
760{
761 int i;
762
763 assert(scip != NULL);
764 assert(readerdata != NULL);
765 assert(scenarios != NULL);
766 assert(numscenariostages == SCIPtimGetNStages(scip) - 1);
767
768 SCIP_CALL( buildScenarioTree(scip, &readerdata->scenariotree, scenarios, numscenarios, numscenariostages, 0) );
769
770 /* setting the number of scenarios per stage in the TIME reader data */
771 for( i = 0; i < numscenariostages; i++ )
772 readerdata->numscenarios += numscenarios[i];
773
774 return SCIP_OKAY;
775}
776
777
778/** finds a scenario with a given name */
779static
781 STOSCENARIO* scenariotree, /**< the scenario tree to search */
782 const char* scenname /**< the name of the scenario to search */
783 )
784{
785 STOSCENARIO* retscen;
786 int i;
787
788 if( strcmp(getScenarioName(scenariotree), scenname) == 0 )
789 return scenariotree;
790 else
791 {
792 retscen = NULL;
793 for( i = 0; i < getScenarioNChildren(scenariotree); i++ )
794 {
795 retscen = findScenarioInTree(scenariotree->children[i], scenname);
796 if( retscen != NULL )
797 return retscen;
798 }
799 }
800
801 return NULL;
802}
803
804
805/** inserts a scenario into the reader data scenario tree */
806static
808 SCIP* scip, /**< the SCIP data structure */
809 SCIP_READERDATA* readerdata, /**< the reader data */
810 STOSCENARIO* scenario, /**< the scenario to insert in the scenario tree */
811 char* parentname /**< the parent scenario for the inserting scenario */
812 )
813{
814 STOSCENARIO* parentscen;
815
816 assert(scip != NULL);
817 assert(readerdata != NULL);
818 assert(scenario != NULL);
819
820 /* searching for the parent scenario in the tree */
821 parentscen = findScenarioInTree(readerdata->scenariotree, parentname);
822
823 /* adding the scenario as a child of the parent scenario */
824 SCIP_CALL( scenarioAddChild(scip, &parentscen, scenario) );
825
826 readerdata->numscenarios++;
827
828 return SCIP_OKAY;
829}
830
831
832/* builds the scenarios from the blocks for a given stage */
833static
835 SCIP* scip, /**< the SCIP data structure */
836 STOSCENARIO*** blocks, /**< the block that form the scenarios */
837 STOSCENARIO*** scenarios, /**< the array to store the scenarios */
838 STOSCENARIO*** blocksforscen, /**< the blocks that will form the scenario */
839 int* numblocksforscen, /**< the number of blocks that form the scenario */
840 int numblocks, /**< the number of blocks */
841 int* numblocksperblock, /**< the number of blocks for a given block */
842 int* numscenarios, /**< the number of scenarios */
843 int* scenariossize, /**< the size of scenarios array */
844 const char* stage, /**< the stage for this scenario */
845 int stagenum, /**< the number of the stage */
846 int blocknum /**< the block number */
847 )
848{
849 SCIP_Bool processed;
850 int i;
851 int j;
852
853 assert(scip != NULL);
854 assert(blocks != NULL);
855 assert(scenarios != NULL);
856 assert(blocksforscen != NULL);
857
858 processed = FALSE;
859 i = blocknum + 1;
860 while( !processed && i < numblocks )
861 {
862 /* it is only necessary to process the next block in the list the belongs to the given stage. */
863 if( strcmp(getScenarioStageName(scip, blocks[i][0]), stage) == 0 )
864 {
865 processed = TRUE;
866
867 for( j = 0; j < numblocksperblock[i]; j++ )
868 {
869 /* adding the blocks that will build the scenario */
870 (*blocksforscen)[(*numblocksforscen)] = blocks[i][j];
871 (*numblocksforscen)++;
872 SCIP_CALL( buildScenariosFromBlocks(scip, blocks, scenarios, blocksforscen, numblocksforscen, numblocks,
873 numblocksperblock, numscenarios, scenariossize, stage, stagenum + 1, i) );
874
875 /* the last block needs to be removed so that a new block can be used in its place */
876 (*numblocksforscen)--;
877 }
878 }
879 else
880 {
881 /* the index is only incremented if no block is processed. This is necessary because the value of i is used in
882 * the next if statement for identifying whether all blocks have been processed.
883 */
884 i++;
885 }
886 }
887
888 /* when all blocks have been inspected, then it is possible to build the scenario */
889 if( i == numblocks )
890 {
891 char scenarioname[SCIP_MAXSTRLEN];
892
893 /* ensuring the correct amount of memory is available */
894 if( (*numscenarios) + 1 > (*scenariossize) )
895 {
896 int newsize;
897 newsize = SCIPcalcMemGrowSize(scip, (*numscenarios) + 1);
898 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, scenarios, (*scenariossize), newsize) );
899 (*scenariossize) = newsize;
900 }
901
902 SCIP_CALL( createScenarioData(scip, &(*scenarios)[(*numscenarios)]) );
903
904 /* setting the scenario name */
905 (void) SCIPsnprintf(scenarioname, SCIP_MAXSTRLEN, "Scenario_%s_%d", stage, (*numscenarios));
906 SCIP_CALL( setScenarioName(scip, (*scenarios)[(*numscenarios)], scenarioname) );
907 SCIP_CALL( setScenarioStageName(scip, (*scenarios)[(*numscenarios)], stage) );
908 SCIP_CALL( setScenarioNum(scip, (*scenarios)[(*numscenarios)], (*numscenarios)) );
909 SCIP_CALL( setScenarioStageNum(scip, (*scenarios)[(*numscenarios)], stagenum) );
910
911 /* if there is only a single block for the scenario, then we simply copy the block.
912 * Otherwise, the blocks are merged into a single scenario */
913 if( (*numblocksforscen) == 1 )
914 SCIP_CALL( copyScenario(scip, (*blocksforscen)[0], &(*scenarios)[(*numscenarios)], FALSE) );
915 else
916 {
917 SCIP_CALL( copyScenario(scip, (*blocksforscen)[0], &(*scenarios)[(*numscenarios)], FALSE) );
918 for( i = 1; i < (*numblocksforscen); i++ )
919 SCIP_CALL( mergeScenarios(scip, (*blocksforscen)[i], &(*scenarios)[(*numscenarios)]) );
920 }
921
922 (*numscenarios)++;
923 }
924
925 return SCIP_OKAY;
926}
927
928
929/* creates the scenarios from the blocks */
930static
932 SCIP* scip, /**< the SCIP data structure */
933 SCIP_READERDATA* readerdata, /**< the reader data */
934 STOSCENARIO*** blocks, /**< the block that form the scenarios */
935 int numblocks, /**< the number of blocks */
936 int* numblocksperblock, /**< the number of blocks for each block type */
937 int numstages /**< the number of stages */
938 )
939{
940 STOSCENARIO*** scenarios;
941 STOSCENARIO** blocksforscen;
942 int* numscenarios;
943 int* scenariossize;
944 int numblocksforscen;
945 int stagenum;
946 char periods[SCIP_MAXSTRLEN];
947 int i;
948 int j;
949
950 assert(scip != NULL);
951 assert(blocks != NULL);
952
953 /* allocating the memory for the scenarios array */
954 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &scenarios, numstages) );
955 SCIP_CALL( SCIPallocBufferArray(scip, &numscenarios, numstages) );
956 SCIP_CALL( SCIPallocBufferArray(scip, &scenariossize, numstages) );
957 for( i = 0; i < numstages; i++ )
958 {
959 scenariossize[i] = STO_DEFAULT_BLOCKARRAYSIZE;
960 numscenarios[i] = 0;
961 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &scenarios[i], scenariossize[i]) );
962 }
963
964 /* allocating the memory for the block for scenario array */
965 SCIP_CALL( SCIPallocBufferArray(scip, &blocksforscen, numblocks) );
966
967 (void) SCIPsnprintf(periods, SCIP_MAXSTRLEN, "");
968
969 stagenum = 0;
970 for( i = 0; i < numblocks; i++ )
971 {
972 numblocksforscen = 0;
973 if( strstr(periods, getScenarioStageName(scip, blocks[i][0])) == NULL )
974 {
975 /* recording the stage name as processed */
976 (void) SCIPsnprintf(periods, SCIP_MAXSTRLEN, "%s_%s", periods, getScenarioStageName(scip, blocks[i][0]));
977
978 SCIP_CALL( buildScenariosFromBlocks(scip, blocks, &scenarios[stagenum], &blocksforscen, &numblocksforscen,
979 numblocks, numblocksperblock, &numscenarios[stagenum], &scenariossize[stagenum],
980 getScenarioStageName(scip, blocks[i][0]), stagenum, i - 1) );
981
982 stagenum++;
983 }
984 }
985
986 /* adding the scenarios to the reader data */
987 SCIP_CALL( setScenarioNum(scip, readerdata->scenariotree, 0) );
988 SCIP_CALL( setScenarioStageNum(scip, readerdata->scenariotree, 0) );
989 SCIP_CALL( addScenariosToReaderdata(scip, readerdata, scenarios, numscenarios, numstages) );
990
991 SCIPfreeBufferArray(scip, &blocksforscen);
992 for( i = numstages - 1; i >= 0; i-- )
993 {
994 for( j = numscenarios[i] - 1; j >= 0; j-- )
995 SCIP_CALL( freeScenarioTree(scip, &scenarios[i][j]) );
996 SCIPfreeBlockMemoryArray(scip, &scenarios[i], scenariossize[i]);
997 }
998 SCIPfreeBufferArray(scip, &scenariossize);
999 SCIPfreeBufferArray(scip, &numscenarios);
1000 SCIPfreeBlockMemoryArray(scip, &scenarios, numstages);
1001
1002 return SCIP_OKAY;
1003}
1004
1005/** creates the reader data */
1006static
1008 SCIP* scip, /**< SCIP data structure */
1009 SCIP_READERDATA* readerdata /**< the reader data */
1010 )
1011{
1012 assert(scip != NULL);
1013 assert(readerdata != NULL);
1014
1015 if( !readerdata->created )
1016 {
1017 readerdata->created = TRUE;
1018
1019 /* creating the initial scenario */
1020 SCIP_CALL( createScenarioData(scip, &readerdata->scenariotree) );
1021
1022 /* setting the scenario name and stage name */
1023 SCIP_CALL( setScenarioName(scip, readerdata->scenariotree, "ROOT") );
1024 SCIP_CALL( setScenarioStageName(scip, readerdata->scenariotree, SCIPtimGetStageName(scip, 0)) );
1025 }
1026
1027 return SCIP_OKAY;
1028}
1029
1030/** frees the reader data */
1031static
1033 SCIP* scip, /**< the SCIP data structure */
1034 SCIP_READERDATA* readerdata /**< the reader data */
1035 )
1036{
1037 assert(scip != NULL);
1038 assert(readerdata != NULL);
1039
1040 if( readerdata->created )
1041 {
1042 /* freeing the scenario tree */
1043 if( readerdata->scenariotree != NULL )
1044 SCIP_CALL( freeScenarioTree(scip, &readerdata->scenariotree) );
1045
1046 readerdata->created = FALSE;
1047 }
1048
1049 return SCIP_OKAY;
1050}
1051
1052/** creates the sto input structure */
1053static
1055 SCIP* scip, /**< SCIP data structure */
1056 STOINPUT** stoi, /**< sto input structure */
1057 SCIP_FILE* fp /**< file object for the input file */
1058 )
1059{
1060 assert(stoi != NULL);
1061 assert(fp != NULL);
1062
1064
1065 (*stoi)->section = STO_STOCH;
1066 (*stoi)->stochinfotype = STO_STOCHINFO_NONE;
1067 (*stoi)->fp = fp;
1068 (*stoi)->lineno = 0;
1069 (*stoi)->haserror = FALSE;
1070 (*stoi)->buf [0] = '\0';
1071 (*stoi)->probname[0] = '\0';
1072 (*stoi)->stochtype[0] = '\0';
1073 (*stoi)->f0 = NULL;
1074 (*stoi)->f1 = NULL;
1075 (*stoi)->f2 = NULL;
1076 (*stoi)->f3 = NULL;
1077 (*stoi)->f4 = NULL;
1078 (*stoi)->f5 = NULL;
1079 (*stoi)->f6 = NULL;
1080
1081 return SCIP_OKAY;
1082}
1083
1084/** free the sto input structure */
1085static
1087 SCIP* scip, /**< SCIP data structure */
1088 STOINPUT** stoi /**< sto input structure */
1089 )
1090{
1092}
1093
1094/** returns the current section */
1095static
1097 const STOINPUT* stoi /**< sto input structure */
1098 )
1099{
1100 assert(stoi != NULL);
1101
1102 return stoi->section;
1103}
1104
1105/** returns the stochastic information type */
1106static
1108 const STOINPUT* stoi /**< sto input structure */
1109 )
1110{
1111 assert(stoi != NULL);
1112
1113 return stoi->stochinfotype;
1114}
1115
1116/** return the current value of field 0 */
1117static
1118const char* stoinputField0(
1119 const STOINPUT* stoi /**< sto input structure */
1120 )
1121{
1122 assert(stoi != NULL);
1123
1124 return stoi->f0;
1125}
1126
1127/** return the current value of field 1 */
1128static
1129const char* stoinputField1(
1130 const STOINPUT* stoi /**< sto input structure */
1131 )
1132{
1133 assert(stoi != NULL);
1134
1135 return stoi->f1;
1136}
1137
1138/** return the current value of field 2 */
1139static
1140const char* stoinputField2(
1141 const STOINPUT* stoi /**< sto input structure */
1142 )
1143{
1144 assert(stoi != NULL);
1145
1146 return stoi->f2;
1147}
1148
1149/** return the current value of field 3 */
1150static
1151const char* stoinputField3(
1152 const STOINPUT* stoi /**< sto input structure */
1153 )
1154{
1155 assert(stoi != NULL);
1156
1157 return stoi->f3;
1158}
1159
1160/** return the current value of field 4 */
1161static
1162const char* stoinputField4(
1163 const STOINPUT* stoi /**< sto input structure */
1164 )
1165{
1166 assert(stoi != NULL);
1167
1168 return stoi->f4;
1169}
1170
1171/** return the current value of field 5 */
1172static
1173const char* stoinputField5(
1174 const STOINPUT* stoi /**< sto input structure */
1175 )
1176{
1177 assert(stoi != NULL);
1178
1179 return stoi->f5;
1180}
1181
1182/** return the current value of field 6 */
1183static
1184const char* stoinputField6(
1185 const STOINPUT* stoi /**< sto input structure */
1186 )
1187{
1188 assert(stoi != NULL);
1189
1190 return stoi->f6;
1191}
1192
1193/** returns if an error was detected */
1194static
1196 const STOINPUT* stoi /**< sto input structure */
1197 )
1198{
1199 assert(stoi != NULL);
1200
1201 return stoi->haserror;
1202}
1203
1204/** set the section in the sto input structure to given section */
1205static
1207 STOINPUT* stoi, /**< sto input structure */
1208 STOSECTION section /**< section that is set */
1209 )
1210{
1211 assert(stoi != NULL);
1212
1213 stoi->section = section;
1214}
1215
1216/** set the stochastic info type in the sto input structure */
1217static
1219 STOINPUT* stoi, /**< sto input structure */
1220 STOSTOCHINFO stochinfotype /**< the stochastic infomation type */
1221 )
1222{
1223 assert(stoi != NULL);
1224
1226}
1227
1228/** set the problem name in the sto input structure to given problem name */
1229static
1231 STOINPUT* stoi, /**< sto input structure */
1232 const char* probname /**< name of the problem to set */
1233 )
1234{
1235 assert(stoi != NULL);
1236 assert(probname != NULL);
1237 assert(strlen(probname) < sizeof(stoi->probname));
1238
1239 (void)SCIPmemccpy(stoi->probname, probname, '\0', STO_MAX_NAMELEN - 1);
1240}
1241
1242/** set the type name in the sto input structure to given objective name */
1243static
1245 STOINPUT* stoi, /**< sto input structure */
1246 const char* stochtype /**< name of the scenario type */
1247 )
1248{
1249 assert(stoi != NULL);
1250 assert(stochtype != NULL);
1251 assert(strlen(stochtype) < sizeof(stoi->stochtype));
1252
1253 (void)SCIPmemccpy(stoi->stochtype, stochtype, '\0', STO_MAX_NAMELEN - 1);
1254}
1255
1256static
1258 STOINPUT* stoi /**< sto input structure */
1259 )
1260{
1261 assert(stoi != NULL);
1262
1263 SCIPerrorMessage("Syntax error in line %d\n", stoi->lineno);
1264 stoi->section = STO_ENDATA;
1265 stoi->haserror = TRUE;
1266}
1267
1268/** fill the line from \p pos up to column 80 with blanks. */
1269static
1271 char* buf, /**< buffer to clear */
1272 unsigned int pos /**< position to start the clearing process */
1273 )
1274{
1275 unsigned int i;
1276
1277 for(i = pos; i < 80; i++)
1278 buf[i] = BLANK;
1279 buf[80] = '\0';
1280}
1281
1282/** read a sto format data line and parse the fields. */
1283static
1285 STOINPUT* stoi /**< sto input structure */
1286 )
1287{
1288 unsigned int len;
1289 unsigned int i;
1290 char* s;
1291 SCIP_Bool is_marker;
1292 SCIP_Bool is_empty;
1293 char* nexttok;
1294
1295 do
1296 {
1297 stoi->f0 = stoi->f1 = stoi->f2 = stoi->f3 = stoi->f4 = stoi->f5 = stoi->f6 = 0;
1298 is_marker = FALSE;
1299
1300 /* Read until we have not a comment line. */
1301 do
1302 {
1303 stoi->buf[STO_MAX_LINELEN-1] = '\0';
1304 if( NULL == SCIPfgets(stoi->buf, (int) sizeof(stoi->buf), stoi->fp) )
1305 return FALSE;
1306 stoi->lineno++;
1307 }
1308 while( *stoi->buf == '*' ); /* coverity[a_loop_bound] */
1309
1310 /* Normalize line */
1311 len = (unsigned int) strlen(stoi->buf);
1312
1313 for( i = 0; i < len; i++ )
1314 {
1315 if( (stoi->buf[i] == '\t') || (stoi->buf[i] == '\n') || (stoi->buf[i] == '\r') )
1316 stoi->buf[i] = BLANK;
1317 }
1318
1319 if( len < 80 )
1320 clearFrom(stoi->buf, len);
1321
1322 SCIPdebugMessage("line %d: <%s>\n", stoi->lineno, stoi->buf);
1323
1324 assert(strlen(stoi->buf) >= 80);
1325
1326 /* Look for new section */
1327 if( *stoi->buf != BLANK )
1328 {
1329 stoi->f0 = SCIPstrtok(&stoi->buf[0], " ", &nexttok);
1330
1331 assert(stoi->f0 != 0);
1332
1333 stoi->f1 = SCIPstrtok(NULL, " ", &nexttok);
1334
1335 return TRUE;
1336 }
1337
1338 s = &stoi->buf[1];
1339
1340 /* At this point it is not clear if we have a indicator field.
1341 * If there is none (e.g. empty) f1 will be the first name field.
1342 * If there is one, f2 will be the first name field.
1343 *
1344 * Initially comment marks '$' are only allowed in the beginning
1345 * of the 2nd and 3rd name field. We test all fields but the first.
1346 * This makes no difference, since if the $ is at the start of a value
1347 * field, the line will be erroneous anyway.
1348 */
1349 do
1350 {
1351 if( NULL == (stoi->f1 = SCIPstrtok(s, " ", &nexttok)) )
1352 break;
1353
1354 if( (NULL == (stoi->f2 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f2 == '$') )
1355 {
1356 stoi->f2 = 0;
1357 break;
1358 }
1359
1360 if( (NULL == (stoi->f3 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f3 == '$') )
1361 {
1362 stoi->f3 = 0;
1363 break;
1364 }
1365
1366 if( (NULL == (stoi->f4 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f4 == '$') )
1367 {
1368 stoi->f4 = 0;
1369 break;
1370 }
1371
1372 if( (NULL == (stoi->f5 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f5 == '$') )
1373 {
1374 stoi->f5 = 0;
1375 break;
1376 }
1377
1378 if( (NULL == (stoi->f6 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f6 == '$') )
1379 stoi->f6 = 0;
1380 }
1381 while( FALSE );
1382
1383 /* check for empty lines */
1384 is_empty = (stoi->f0 == NULL && stoi->f1 == NULL);
1385 }
1386 while( is_marker || is_empty );
1387
1388 return TRUE;
1389}
1390
1391/** Process STOCH section. */
1392static
1394 SCIP* scip, /**< SCIP data structure */
1395 STOINPUT* stoi /**< sto input structure */
1396 )
1397{
1398 assert(stoi != NULL);
1399
1400 SCIPdebugMsg(scip, "read problem name\n");
1401
1402 /* This has to be the Line with the NAME section. */
1403 if( !stoinputReadLine(stoi) || stoinputField0(stoi) == NULL || strcmp(stoinputField0(stoi), "STOCH") )
1404 {
1405 stoinputSyntaxerror(stoi);
1406 return SCIP_OKAY;
1407 }
1408
1409 /* Sometimes the name is omitted. */
1410 stoinputSetProbname(stoi, (stoinputField1(stoi) == 0) ? "_STO_" : stoinputField1(stoi));
1411
1412 /* This hat to be a new section */
1413 /* coverity[tainted_data] */
1414 if( !stoinputReadLine(stoi) || (stoinputField0(stoi) == NULL) )
1415 {
1416 stoinputSyntaxerror(stoi);
1417 return SCIP_OKAY;
1418 }
1419
1420 /* setting the stochatic information section */
1421 if( !strncmp(stoinputField0(stoi), "BLOCKS", 6) )
1423 else if( !strncmp(stoinputField0(stoi), "SCENARIOS", 9) )
1425 else if( !strncmp(stoinputField0(stoi), "INDEP", 5) )
1427 else
1428 {
1429 stoinputSyntaxerror(stoi);
1430 return SCIP_OKAY;
1431 }
1432
1433 /* setting the stochastic information type */
1434 if( !strncmp(stoinputField1(stoi), "DISCRETE", 8) )
1436 else if( !strncmp(stoinputField1(stoi), "UNIFORM", 7) )
1438 else if( !strncmp(stoinputField1(stoi), "NORMAL", 6) )
1440 else if( !strncmp(stoinputField1(stoi), "SUB", 3) )
1442 else if( !strncmp(stoinputField1(stoi), "LINTR", 5) )
1444 else
1445 {
1446 stoinputSyntaxerror(stoi);
1447 return SCIP_OKAY;
1448 }
1449
1450 return SCIP_OKAY;
1451}
1452
1453/** Process BLOCKS section. */
1454static
1456 STOINPUT* stoi, /**< sto input structure */
1457 SCIP* scip, /**< SCIP data structure */
1458 SCIP_READERDATA* readerdata /**< the reader data */
1459 )
1460{
1461 STOSCENARIO*** blocks;
1462 int numblocks;
1463 int* numblocksperblock;
1464 int blockssize;
1465 int* blocksperblocksize;
1466 char BL[] = "BL";
1467 int blocknum;
1468 int blockindex;
1469 int i;
1470 int j;
1471 char stagenames[SCIP_MAXSTRLEN];
1472 int numstages;
1473
1474 SCIPdebugMsg(scip, "read Blocks\n");
1475
1476 /* This has to be the Line with the name. */
1477 if( stoinputField1(stoi) == NULL )
1478 {
1479 stoinputSyntaxerror(stoi);
1480 return SCIP_OKAY;
1481 }
1482
1484
1485 /* initializing the block data */
1486 numblocks = 0;
1487 blockssize = STO_DEFAULT_ARRAYSIZE;
1491
1492 blockindex = 0;
1493 blocknum = 0;
1494
1495 /* initializing the stage names record */
1496 numstages = 0;
1497 (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "");
1498
1499 /* coverity[tainted_data] */
1500 while( stoinputReadLine(stoi) )
1501 {
1502 if( stoinputField0(stoi) != NULL )
1503 {
1504 if( !strcmp(stoinputField0(stoi), "BLOCKS") )
1505 {
1507 if( strcmp(stoinputField1(stoi), "DISCRETE") )
1508 {
1509 SCIPerrorMessage("Sorry, %s blocks stucture is not currently supported.\n", stoinputField1(stoi));
1510 SCIPerrorMessage("Only DISCRETE blocks are supported.\n");
1511 goto TERMINATE;
1512 }
1513 }
1514 else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1515 {
1516 SCIP_CALL( createScenariosFromBlocks(scip, readerdata, blocks, numblocks, numblocksperblock, numstages) );
1518 }
1519 else
1520 stoinputSyntaxerror(stoi);
1521
1522 goto TERMINATE;
1523 }
1524
1525 if( strcmp(stoinputField1(stoi), BL) == 0 )
1526 {
1527 SCIP_Bool foundblock = FALSE;
1528
1529 /* checking whether the stage has been added previously */
1530 if( strstr(stagenames, stoinputField3(stoi)) == NULL )
1531 {
1532 /* recording the stage name as processed */
1533 (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "%s_%s", stagenames, stoinputField3(stoi));
1534 numstages++;
1535 }
1536
1537 /* determining whether a block name has previously been added */
1538 for( i = 0; i < numblocks; i++ )
1539 {
1540 if( strcmp(getScenarioName(blocks[i][0]), stoinputField2(stoi)) == 0 )
1541 {
1542 foundblock = TRUE;
1543 break;
1544 }
1545 }
1546 blocknum = i;
1547
1548 /* if the block is found, then the memory for the blocks array must be ensured */
1549 if( foundblock )
1550 {
1551 /* ensuring enough memory is available for the blocks */
1552 if( numblocksperblock[blocknum] + 1 > blocksperblocksize[blocknum] )
1553 {
1554 int newsize;
1555 newsize = SCIPcalcMemGrowSize(scip, numblocksperblock[blocknum] + 1);
1556 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum], newsize) ); /*lint !e866*/
1557 blocksperblocksize[blocknum] = newsize;
1558 }
1559 }
1560 else
1561 {
1562 /* ensuring enough memory is available for the blocks */
1563 if( numblocks + 1 > blockssize )
1564 {
1565 int newsize;
1566 newsize = SCIPcalcMemGrowSize(scip, numblocks + 1);
1567 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks, blockssize, newsize) );
1568 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &numblocksperblock, blockssize, newsize) );
1569 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocksperblocksize, blockssize, newsize) );
1570 blockssize = newsize;
1571 }
1572
1573 blocksperblocksize[blocknum] = STO_DEFAULT_BLOCKARRAYSIZE;
1574 numblocksperblock[blocknum] = 0;
1575 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum]) );
1576 }
1577
1578 blockindex = numblocksperblock[blocknum];
1579
1580 /* creating the scenario data structure */
1581 SCIP_CALL( createScenarioData(scip, &blocks[blocknum][blockindex]) );
1582
1583 SCIP_CALL( setScenarioName(scip, blocks[blocknum][blockindex], stoinputField2(stoi)) );
1584 SCIP_CALL( setScenarioStageName(scip, blocks[blocknum][blockindex], stoinputField3(stoi)) );
1585 SCIP_CALL( setScenarioProbability(scip, blocks[blocknum][blockindex], atof(stoinputField4(stoi))) );
1586 numblocksperblock[blocknum]++;
1587
1588 if( !foundblock )
1589 numblocks++;
1590 }
1591 else
1592 {
1593 SCIP_CALL( addScenarioEntry(scip, blocks[blocknum][blockindex], stoinputField2(stoi), stoinputField1(stoi),
1594 atof(stoinputField3(stoi))) );
1595 }
1596 }
1597 stoinputSyntaxerror(stoi);
1598
1599TERMINATE:
1600
1601 /* releasing the scenario data */
1602 for( i = numblocks - 1; i >= 0; i-- )
1603 {
1604 for( j = numblocksperblock[i] - 1; j >= 0; j-- )
1605 SCIP_CALL( freeScenarioTree(scip, &blocks[i][j]) );
1606 }
1607
1608 for( i = numblocks - 1; i >= 0; i-- )
1609 SCIPfreeBlockMemoryArray(scip, &blocks[i], blocksperblocksize[i]);
1610 SCIPfreeBlockMemoryArray(scip, &blocksperblocksize, blockssize);
1611 SCIPfreeBlockMemoryArray(scip, &numblocksperblock, blockssize);
1612 SCIPfreeBlockMemoryArray(scip, &blocks, blockssize);
1613
1614 return SCIP_OKAY;
1615}
1616
1617
1618/** Process SCENARIOS section. */
1619static
1621 STOINPUT* stoi, /**< sto input structure */
1622 SCIP* scip, /**< SCIP data structure */
1623 SCIP_READERDATA* readerdata /**< the reader data */
1624 )
1625{
1626 STOSCENARIO* scenario;
1627 char SC[] = "SC";
1628 char wrongroot[] = "\'ROOT\'";
1629 char parentname[SCIP_MAXSTRLEN];
1630 char scennames[SCIP_MAXSTRLEN];
1631 char tmpname[SCIP_MAXSTRLEN];
1632 int numscenarios;
1633 SCIP_Bool addscenario;
1634
1635 SCIPdebugMsg(scip, "read SCENARIOS\n");
1636
1637 /* This has to be the Line with the name. */
1638 if( stoinputField1(stoi) == NULL )
1639 {
1640 stoinputSyntaxerror(stoi);
1641 return SCIP_OKAY;
1642 }
1643
1645
1646 /* initializing the scen names record */
1647 numscenarios = 0;
1648 (void) SCIPsnprintf(scennames, SCIP_MAXSTRLEN, "ROOT");
1649
1650 scenario = NULL;
1651 addscenario = FALSE;
1652
1653 /* initializing the root scenario in the reader data */
1654 SCIP_CALL( setScenarioNum(scip, readerdata->scenariotree, 0) );
1655 SCIP_CALL( setScenarioStageNum(scip, readerdata->scenariotree, 0) );
1656
1657 /* coverity[tainted_data] */
1658 while( stoinputReadLine(stoi) )
1659 {
1660 if( stoinputField0(stoi) != NULL )
1661 {
1662 /* if a scenario has been created that needs to be added to the scenario tree */
1663 if( addscenario )
1664 {
1665 SCIP_CALL( insertScenarioInReaderdata(scip, readerdata, scenario, parentname) );
1666
1667 /* freeing the scenario */
1668 SCIP_CALL( freeScenarioTree(scip, &scenario) );
1669 }
1670
1671 if( !strcmp(stoinputField0(stoi), "SCENARIOS") )
1672 {
1674 if( strcmp(stoinputField1(stoi), "DISCRETE") )
1675 {
1676 SCIPerrorMessage("Sorry, %s scenarios is not currently supported.\n", stoinputField1(stoi));
1677 SCIPerrorMessage("Only DISCRETE scenarios are supported.\n");
1678 goto TERMINATE;
1679 }
1680 }
1681 else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1683 else
1684 stoinputSyntaxerror(stoi);
1685
1686 goto TERMINATE;
1687 }
1688
1689 if( strcmp(stoinputField1(stoi), SC) == 0 )
1690 {
1691 int stagenum;
1692
1693 /* if a scenario has been created that needs to be added to the scenario tree */
1694 if( addscenario )
1695 {
1696 SCIP_CALL( insertScenarioInReaderdata(scip, readerdata, scenario, parentname) );
1697
1698 /* freeing the scenario */
1699 SCIP_CALL( freeScenarioTree(scip, &scenario) );
1700 assert(scenario == NULL);
1701 }
1702
1703 if( strcmp(wrongroot, stoinputField3(stoi)) == 0 )
1704 (void) SCIPsnprintf(parentname, SCIP_MAXSTRLEN, "%s", "ROOT");
1705 else
1706 (void) SCIPsnprintf(parentname, SCIP_MAXSTRLEN, "%s", stoinputField3(stoi));
1707
1708 /* checking whether the stage has been added previously */
1709 if( strstr(scennames, stoinputField2(stoi)) == NULL )
1710 {
1711 /* recording the stage name as processed */
1712 (void) SCIPsnprintf(tmpname, SCIP_MAXSTRLEN, "%s_%s", scennames, stoinputField2(stoi));
1713 (void) SCIPsnprintf(scennames, SCIP_MAXSTRLEN, "%s", tmpname);
1714 }
1715
1716 /* checking whether the "common" scenario has been added yet */
1717 if( strstr(scennames, parentname) == NULL )
1718 {
1719 SCIPerrorMessage("Scenario <%s> needs to be read before scenario <%s>\n", parentname, stoinputField2(stoi));
1720 stoinputSyntaxerror(stoi);
1721 goto TERMINATE;
1722 }
1723
1724 /* the "common" scenario has been added before, so a child can be added to the scenario tree */
1725 SCIP_CALL( createScenarioData(scip, &scenario) );
1726
1727 SCIP_CALL( setScenarioName(scip, scenario, stoinputField2(stoi)) );
1729 SCIP_CALL( setScenarioNum(scip, scenario, numscenarios) );
1730
1731 stagenum = SCIPtimFindStage(scip, stoinputField5(stoi));
1732 if( stagenum < 0 )
1733 {
1734 stoinputSyntaxerror(stoi);
1735 goto TERMINATE;
1736 }
1737 SCIP_CALL( setScenarioStageNum(scip, scenario, stagenum) );
1738 SCIP_CALL( setScenarioProbability(scip, scenario, atof(stoinputField4(stoi))) );
1739 if( stoinputField6(stoi) != NULL )
1740 {
1741 SCIP_CALL( setScenarioLowerbound(scip, scenario, atof(stoinputField6(stoi))) );
1742 }
1743
1744 numscenarios++;
1745 addscenario = TRUE;
1746 }
1747 else if( addscenario )
1748 {
1750 atof(stoinputField3(stoi))) );
1751 }
1752 }
1753 stoinputSyntaxerror(stoi);
1754
1755TERMINATE:
1756
1757 return SCIP_OKAY;
1758}
1759
1760
1761/** Process INDEP section. */
1762static
1764 STOINPUT* stoi, /**< sto input structure */
1765 SCIP* scip, /**< SCIP data structure */
1766 SCIP_READERDATA* readerdata /**< the reader data */
1767 )
1768{
1769 STOSCENARIO*** blocks;
1770 int numblocks;
1771 int* numblocksperblock;
1772 int blockssize;
1773 int* blocksperblocksize;
1774 int blocknum;
1775 int blockindex;
1776 int i;
1777 int j;
1778 char stagenames[SCIP_MAXSTRLEN];
1779 int numstages;
1780 SCIP_Bool foundblock;
1781
1782 SCIP_Real probability;
1783 char currstagename[SCIP_MAXSTRLEN];
1784
1785 SCIPdebugMsg(scip, "read Indep\n");
1786
1787 /* This has to be the Line with the name. */
1788 if( stoinputField1(stoi) == NULL )
1789 {
1790 stoinputSyntaxerror(stoi);
1791 return SCIP_OKAY;
1792 }
1793
1795
1796 /* initializing the block data */
1797 numblocks = 0;
1798 blockssize = STO_DEFAULT_ARRAYSIZE;
1802
1803 /* initializing the stage names record */
1804 numstages = 0;
1805 (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "");
1806
1807 while( stoinputReadLine(stoi) )
1808 {
1809 if( stoinputField0(stoi) != NULL )
1810 {
1811 if( !strcmp(stoinputField0(stoi), "INDEP") )
1812 {
1814 }
1815 else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1816 {
1817 SCIP_CALL( createScenariosFromBlocks(scip, readerdata, blocks, numblocks, numblocksperblock, numstages) );
1819 }
1820 else
1821 stoinputSyntaxerror(stoi);
1822
1823 goto TERMINATE;
1824 }
1825
1826 /* if the 5th input is NULL, then the 4th input is the probability. Otherwise, the 4th input is the stage name and
1827 * the 5th input is the probability. The stage name is redundant information, but sometimes included for more
1828 * information.
1829 */
1830 if( stoinputField5(stoi) == NULL )
1831 {
1832 probability = atof(stoinputField4(stoi));
1833 (void) SCIPsnprintf(currstagename, SCIP_MAXSTRLEN, "%s", SCIPtimConsGetStageName(scip, stoinputField2(stoi)));
1834 }
1835 else
1836 {
1837 probability = atof(stoinputField5(stoi));
1838 (void) SCIPsnprintf(currstagename, SCIP_MAXSTRLEN, "%s", stoinputField4(stoi));
1839 }
1840
1841 /* checking whether the stage has been added previously */
1842 if( strstr(stagenames, currstagename) == NULL )
1843 {
1844 /* recording the stage name as processed */
1845 (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "%s_%s", stagenames, currstagename);
1846
1847 numstages++;
1848 }
1849
1850 foundblock = FALSE;
1851
1852 /* determining whether a block name has previously been added */
1853 for( i = 0; i < numblocks; i++ )
1854 {
1855 if( strcmp(getScenarioName(blocks[i][0]), stoinputField2(stoi)) == 0 )
1856 {
1857 foundblock = TRUE;
1858 break;
1859 }
1860 }
1861 blocknum = i;
1862
1863 /* if the block is found, then the memory for the blocks array must be ensured */
1864 if( foundblock )
1865 {
1866 /* ensuring enough memory is available for the blocks */
1867 if( numblocksperblock[blocknum] + 1 > blocksperblocksize[blocknum] )
1868 {
1869 int newsize;
1870 newsize = SCIPcalcMemGrowSize(scip, numblocksperblock[blocknum] + 1);
1871 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum], newsize) ); /*lint !e866*/
1872 blocksperblocksize[blocknum] = newsize;
1873 }
1874 }
1875 else
1876 {
1877 /* ensuring enough memory is available for the blocks */
1878 if( numblocks + 1 > blockssize )
1879 {
1880 int newsize;
1881 newsize = SCIPcalcMemGrowSize(scip, numblocks + 1);
1882 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks, blockssize, newsize) );
1883 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &numblocksperblock, blockssize, newsize) );
1884 SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocksperblocksize, blockssize, newsize) );
1885 blockssize = newsize;
1886 }
1887
1888 blocksperblocksize[blocknum] = STO_DEFAULT_BLOCKARRAYSIZE;
1889 numblocksperblock[blocknum] = 0;
1890 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum]) );
1891 }
1892
1893 blockindex = numblocksperblock[blocknum];
1894
1895 /* creating the scenario data structure */
1896 SCIP_CALL( createScenarioData(scip, &blocks[blocknum][blockindex]) );
1897
1898 SCIP_CALL( setScenarioName(scip, blocks[blocknum][blockindex], stoinputField2(stoi)) );
1899 SCIP_CALL( setScenarioStageName(scip, blocks[blocknum][blockindex], currstagename) );
1900 SCIP_CALL( setScenarioProbability(scip, blocks[blocknum][blockindex], probability) );
1901 numblocksperblock[blocknum]++;
1902
1903 if( !foundblock )
1904 numblocks++;
1905
1906 SCIP_CALL( addScenarioEntry(scip, blocks[blocknum][blockindex], stoinputField2(stoi), stoinputField1(stoi),
1907 atof(stoinputField3(stoi))) );
1908 }
1909 stoinputSyntaxerror(stoi);
1910
1911TERMINATE:
1912
1913 /* releasing the scenario data */
1914 for( i = numblocks - 1; i >= 0; i-- )
1915 {
1916 for( j = numblocksperblock[i] - 1; j >= 0; j-- )
1917 SCIP_CALL( freeScenarioTree(scip, &blocks[i][j]) );
1918 }
1919
1920 for( i = numblocks - 1; i >= 0; i-- )
1921 SCIPfreeBlockMemoryArray(scip, &blocks[i], blocksperblocksize[i]);
1922 SCIPfreeBlockMemoryArray(scip, &blocksperblocksize, blockssize);
1923 SCIPfreeBlockMemoryArray(scip, &numblocksperblock, blockssize);
1924 SCIPfreeBlockMemoryArray(scip, &blocks, blockssize);
1925
1926 return SCIP_OKAY;
1927}
1928
1929
1930/** computes the probability of a scenario */
1931static
1933 SCIP* scip, /**< the SCIP data structure */
1934 STOSCENARIO* scenario /**< the current scenario */
1935 )
1936{
1937 STOSCENARIO* checkscen;
1938 SCIP_Real probability;
1939
1940 assert(scip != NULL);
1941 assert(scenario != NULL);
1942
1943 /* computing the probability for the scenario */
1944 checkscen = scenario;
1945 probability = 1;
1946 while( checkscen != NULL )
1947 {
1948 probability *= getScenarioProbability(scip, checkscen);
1949 checkscen = getScenarioParent(checkscen);
1950 }
1951
1952 return probability;
1953}
1954
1955/** gets the variable name */
1956static
1958 char* name, /**< the name to be returned */
1959 const char* varname, /**< the root of the variable name */
1960 int stagenum, /**< the stage number */
1961 int scenarionum /**< the scenario number */
1962 )
1963{
1964 if( stagenum < 0 )
1965 (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_00_%d", varname, scenarionum);
1966 else
1967 (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_%d", varname, stagenum, scenarionum);
1968}
1969
1970
1971/** add variables to the scenario */
1972static
1974 SCIP* scip, /**< the SCIP data structure */
1975 STOSCENARIO* scenario, /**< the current scenario */
1976 SCIP_HASHMAP* varmap, /**< the variable map from the original to the subproblem variables */
1977 SCIP_VAR** vars, /**< the variables of the core problem associated with this scenario */
1978 int nvars /**< the number of variables for this scenario */
1979 )
1980{
1981 SCIP_Real probability;
1982 int i;
1983 char name[SCIP_MAXSTRLEN];
1984
1985 assert(scip != NULL);
1986 assert(scenario != NULL);
1987 assert(vars != NULL);
1988
1989 /* computing the probability for the scenario */
1990 probability = computeScenarioProbability(scip, scenario);
1991
1992 for( i = 0; i < nvars; i++ )
1993 {
1994 SCIP_VAR* var;
1995 SCIP_Real obj;
1996
1997 SCIPdebugMessage("Original problem variable <%s> is being duplicated for scenario %d\n", SCIPvarGetName(vars[i]),
1998 getScenarioNum(scip, scenario));
1999
2000 if( SCIPvarIsDeleted(vars[i]) )
2001 continue;
2002
2003 obj = SCIPvarGetObj(vars[i])*probability;
2004
2005 /* creating a variable as a copy of the original variable. */
2006 getScenarioEntityName(name, SCIPvarGetName(vars[i]), getScenarioStageNum(scip, scenario), getScenarioNum(scip, scenario));
2008 obj, SCIPvarGetType(vars[i]), SCIPvarGetImplType(vars[i]), SCIPvarIsInitial(vars[i]), SCIPvarIsRemovable(vars[i]),
2009 NULL, NULL, NULL, NULL, NULL) );
2010
2011 SCIPdebugMessage("Adding variable <%s>\n", name);
2012
2013 SCIP_CALL( SCIPaddVar(scip, var) );
2014
2015 /* inserting the scenario variable into the hashmap */
2016 SCIP_CALL( SCIPhashmapInsert(varmap, vars[i], var) );
2017
2018 SCIP_CALL( SCIPreleaseVar(scip, &var) );
2019 }
2020
2021 return SCIP_OKAY;
2022}
2023
2024
2025/** finds the scenario variable to add to a constraint */
2026static
2028 SCIP* scip, /**< the SCIP data structure */
2029 STOSCENARIO* scenario, /**< the current scenario */
2030 SCIP_VAR* consvar, /**< the variable in the constraint that is being searched for */
2031 SCIP_VAR** scenariovar /**< pointer to return the variable to be added to the constraint */
2032 )
2033{
2034 STOSCENARIO* checkscen;
2035 char varname[SCIP_MAXSTRLEN];
2036
2037 assert(scip != NULL);
2038 assert(scenario != NULL);
2039 assert(consvar != NULL);
2040 assert(scenariovar != NULL);
2041
2042 (*scenariovar) = NULL;
2043
2044 checkscen = scenario;
2045
2046 /* NOTE: if the variable does not exist, then we need to search the preceding scenarios. In the case of
2047 * decomposition, then we only check the preceding scenario. As such, a check count is used to limit the number
2048 * of scenario checks. */
2049 while( (*scenariovar) == NULL )
2050 {
2051 assert(checkscen != NULL);
2052 if( getScenarioStageNum(scip, checkscen) == 0 )
2053 (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(consvar));
2054 else
2055 getScenarioEntityName(varname, SCIPvarGetName(consvar), getScenarioStageNum(scip, checkscen),
2056 getScenarioNum(scip, checkscen));
2057
2058 (*scenariovar) = SCIPfindVar(scip, varname);
2059
2060 checkscen = getScenarioParent(checkscen);
2061 }
2062
2063 if( (*scenariovar) == NULL )
2064 {
2065 SCIPerrorMessage("There is no scenario variable could be found.\n");
2066 return SCIP_READERROR;
2067 }
2068
2069 return SCIP_OKAY;
2070}
2071
2072
2073/** create variable for the decomposed scenario */
2074static
2076 SCIP* scip, /**< the SCIP data structure */
2077 STOSCENARIO* scenario, /**< the current scenario */
2078 SCIP_VAR* consvar, /**< the variable in the constraint that is being searched for */
2079 SCIP_VAR** scenariovar, /**< pointer to return the variable to be added to the constraint */
2080 SCIP_Bool* varadded /**< pointer to indicate whether a variable has been added */
2081 )
2082{
2083 STOSCENARIO* checkscen;
2084 SCIP_VAR* searchvar;
2085 int checkcount;
2086 char varname[SCIP_MAXSTRLEN];
2087
2088 assert(scip != NULL);
2089 assert(scenario != NULL);
2090 assert(consvar != NULL);
2091
2092 (*varadded) = FALSE;
2093
2094 /* finding the scenario that the consvar belongs to */
2095 checkscen = scenario;
2096 searchvar = NULL;
2097 checkcount = 0;
2098 while( searchvar == NULL && checkcount < 2 )
2099 {
2100 assert(checkscen != NULL);
2101 if( getScenarioStageNum(scip, checkscen) == 0 )
2102 (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(consvar));
2103 else
2104 getScenarioEntityName(varname, SCIPvarGetName(consvar), getScenarioStageNum(scip, checkscen),
2105 getScenarioNum(scip, checkscen));
2106
2107 /* first checking whether the variable is included in the scenario */
2108 searchvar = SCIPfindVar(scip, varname);
2109 if( searchvar != NULL )
2110 {
2111 (*scenariovar) = searchvar;
2112 return SCIP_OKAY;
2113 }
2114
2115 searchvar = SCIPfindVar(getScenarioScip(checkscen), varname);
2116
2117 checkscen = getScenarioParent(checkscen);
2118 checkcount++;
2119 }
2120
2121 if( searchvar != NULL )
2122 {
2123 SCIP_VAR* var;
2124 /* creating a variable as a copy of the original variable. */
2125 SCIP_CALL( SCIPcreateVarImpl(scip, &var, varname, SCIPvarGetLbOriginal(searchvar), SCIPvarGetUbOriginal(searchvar),
2126 0.0, SCIPvarGetType(searchvar), SCIPvarGetImplType(searchvar), SCIPvarIsInitial(searchvar), SCIPvarIsRemovable(searchvar),
2127 NULL, NULL, NULL, NULL, NULL) );
2128
2129 SCIP_CALL( SCIPaddVar(scip, var) );
2130
2131 SCIPdebugMessage("Original problem variable <%s> is being duplicated for scenario %d\n",
2132 SCIPvarGetName(var), getScenarioNum(scip, scenario));
2133
2134 (*scenariovar) = var;
2135 (*varadded) = TRUE;
2136 }
2137
2138 return SCIP_OKAY;
2139}
2140
2141
2142/** adds the constraint to the scenario problem */
2143static
2145 SCIP* scip, /**< the SCIP data structure */
2146 SCIP* scenarioscip, /**< the scenario SCIP data structure */
2147 STOSCENARIO* scenario, /**< the current scenario */
2148 SCIP_HASHMAP* varmap, /**< the variable map from the original to the subproblem variables */
2149 SCIP_CONS** conss, /**< the constraints of the core problem associated with this scenario */
2150 int nconss, /**< the number of constraints for this scenario */
2151 SCIP_Bool decomp /**< is the problem being decomposed */
2152 )
2153{
2154 int i;
2155 int j;
2156 char name[SCIP_MAXSTRLEN];
2157 SCIP_Bool varadded;
2158
2159 assert(scip != NULL);
2160 assert(scenarioscip != NULL);
2161 assert(scenario != NULL);
2162 assert(conss != NULL);
2163
2164 /* Add constraints */
2165 /* NOTE: It is assumed that the problems only have linear constraints */
2166 for( i = 0; i < nconss; i++ )
2167 {
2168 SCIP_CONS* cons;
2169 SCIP_VAR** consvars = NULL;
2170 int nconsvars;
2171 SCIP_Bool success1 = TRUE;
2172 SCIP_Bool success2 = TRUE;
2173
2174 if( SCIPconsIsDeleted(conss[i]) )
2175 continue;
2176
2177 /* getting the number of variables in the constraints */
2178 SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &nconsvars, &success1) );
2179
2180 if( success1 )
2181 {
2182 SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nconsvars) );
2183 SCIP_CALL( SCIPgetConsVars(scip, conss[i], consvars, nconsvars, &success2) );
2184
2185 /* If the get variable callback is not implemented for the constraint, then the success flag will be returned
2186 * as FALSE. In this case, it is not possible to build the stochastic program, so an error will be returned.
2187 */
2188 if( !success2 )
2189 {
2190 SCIPfreeBufferArrayNull(scip, consvars);
2191 }
2192 }
2193
2194 if( !success1 || !success2 )
2195 {
2196 SCIPerrorMessage("It is not possible to copy constraint <%s>. The stochastic program can not be built.\n",
2197 SCIPconsGetName(conss[i]));
2198
2199 return SCIP_READERROR;
2200 }
2201
2202 assert(consvars != NULL);
2203 for( j = 0; j < nconsvars; j++ )
2204 {
2205 SCIP_VAR* scenariovar;
2206
2207 scenariovar = NULL;
2208
2209 varadded = FALSE;
2210
2211 if( decomp )
2212 SCIP_CALL( getScenarioDecompVar(scenarioscip, scenario, consvars[j], &scenariovar, &varadded) );
2213 else
2214 SCIP_CALL( findScenarioVar(scenarioscip, scenario, consvars[j], &scenariovar) );
2215
2216 if( scenariovar != NULL )
2217 {
2218 /* checking whether the variable is in the variable hashmap. If it doesn't exist, then it is added to the
2219 * variable hashmap
2220 */
2221 if( !SCIPhashmapExists(varmap, consvars[j]) )
2222 {
2223 SCIP_CALL( SCIPhashmapInsert(varmap, consvars[j], scenariovar) );
2224 }
2225 }
2226
2227 if( varadded )
2228 {
2229 SCIP_CALL( SCIPreleaseVar(scenarioscip, &scenariovar) );
2230 }
2231 }
2232
2233 /* creating a linear constraint as a copy of the original constraint. */
2234 getScenarioEntityName(name, SCIPconsGetName(conss[i]), getScenarioStageNum(scip, scenario), getScenarioNum(scip, scenario));
2235
2236 /* copying the constraint from the original SCIP to the stochastic program */
2237 SCIP_CALL( SCIPgetConsCopy(scip, scenarioscip, conss[i], &cons, SCIPconsGetHdlr(conss[i]), varmap, NULL, name,
2238 SCIPconsIsInitial(conss[i]), SCIPconsIsSeparated(conss[i]), SCIPconsIsEnforced(conss[i]),
2239 SCIPconsIsChecked(conss[i]), SCIPconsIsMarkedPropagate(conss[i]), SCIPconsIsLocal(conss[i]),
2240 SCIPconsIsModifiable(conss[i]), SCIPconsIsDynamic(conss[i]), SCIPconsIsRemovable(conss[i]),
2241 SCIPconsIsStickingAtNode(conss[i]), TRUE, &success1) );
2242
2243 /* freeing the cons vars buffer array */
2244 SCIPfreeBufferArray(scip, &consvars);
2245
2246 /* if the copy failed, then the scenarios can not be created. */
2247 if( !success1 )
2248 {
2249 SCIPerrorMessage("It is not possible to copy constraint <%s>. The stochastic program can not be built.\n",
2250 SCIPconsGetName(conss[i]));
2251 return SCIP_READERROR;
2252 }
2253
2254 SCIP_CALL( SCIPaddCons(scenarioscip, cons) );
2255 SCIP_CALL( SCIPreleaseCons(scenarioscip, &cons) );
2256 }
2257
2258 return SCIP_OKAY;
2259}
2260
2261/** add variables and constraint to problem */
2262static
2264 SCIP* scip, /**< the SCIP data structure of master problem */
2265 STOSCENARIO* scenario, /**< the current scenario */
2266 SCIP_Bool decomp /**< is the problem being decomposed */
2267 )
2268{
2269 SCIP* scenarioscip;
2270 SCIP_BENDERS* benders;
2271 SCIP_HASHMAP* varmap;
2272 SCIP_CONS** conss;
2273 SCIP_VAR** vars;
2274 SCIP_Real probability;
2275 int nconss;
2276 int nvars;
2277 int nmastervars;
2278 int nentries;
2279 int stagenum;
2280 int i;
2281 char name[SCIP_MAXSTRLEN];
2282
2283 assert(scip != NULL);
2284 assert(scenario != NULL);
2285
2286 stagenum = SCIPtimFindStage(scip, getScenarioStageName(scip, scenario));
2287 if( stagenum < 0 || stagenum >= SCIPtimGetNStages(scip) )
2288 {
2289 SCIPerrorMessage("Unable to build stochastic program - stage <%s> was not found\n",
2290 getScenarioStageName(scip, scenario));
2291 return SCIP_READERROR;
2292 }
2293
2294 SCIPdebugMessage("Creating scenario at stage <%d>. Scenario: %d Stage: %d\n", stagenum, getScenarioNum(scip, scenario),
2295 getScenarioStageNum(scip, scenario));
2296
2297 conss = SCIPtimGetStageConss(scip, stagenum);
2298 nconss = SCIPtimGetStageNConss(scip, stagenum);
2299 vars = SCIPtimGetStageVars(scip, stagenum);
2300 nvars = SCIPtimGetStageNVars(scip, stagenum);
2301
2302 nmastervars = SCIPgetNVars(scip);
2303
2304 /* this if 0 will be removed when the stochastic reader is merged with the Benders' branch */
2305 if( decomp )
2306 {
2307 SCIP_CALL( SCIPcreate(&scenarioscip) );
2308
2310
2311 /* creating the problem */
2312 SCIP_CALL( SCIPcreateProbBasic(scenarioscip, name) );
2313
2314 /* we explicitly enable the use of a debug solution for this main SCIP instance */
2315 SCIPenableDebugSol(scenarioscip);
2316
2317 /* include default SCIP plugins */
2318 SCIP_CALL( SCIPincludeDefaultPlugins(scenarioscip) );
2319
2320 /* activating the Benders' constraint handler for the scenario stages.
2321 * TODO: consider whether the two-phase method should be activated by default in the scenario stages.
2322 */
2323 SCIP_CALL( SCIPsetBoolParam(scenarioscip, "constraints/benders/active", TRUE) );
2324
2325 /* allocating memory for the subproblems */
2326 if( getScenarioNChildren(scenario) > 0 )
2328 }
2329 else
2330 scenarioscip = scip;
2331
2332 /* adding the scenarioscip to the scenario */
2333 setScenarioScip(scenario, scenarioscip);
2334
2335 /* creating the variable hashmap to copy the constraints */
2336 SCIP_CALL( SCIPhashmapCreate(&varmap, SCIPblkmem(scenarioscip), nmastervars) );
2337
2338 /* adding the variables to the scenario */
2339 SCIP_CALL( addScenarioVarsToProb(scenarioscip, scenario, varmap, vars, nvars) );
2340
2341 /* adding the constraints to the scenario */
2342 SCIP_CALL( addScenarioConsToProb(scip, scenarioscip, scenario, varmap, conss, nconss, decomp) );
2343
2344 /* destroying the hashmap */
2345 SCIPhashmapFree(&varmap);
2346
2347 /* add the variables and constraints of the child scenarios */
2348 for( i = 0; i < getScenarioNChildren(scenario); i++ )
2349 {
2350 /* the master SCIP is always passed to the recursive function. The scenario SCIP instances are generated in the
2351 * function call. */
2353 if( decomp )
2355 }
2356
2357 /* adding the Benders' decomposition */
2358 if( decomp && getScenarioNChildren(scenario) > 0 )
2359 {
2361
2362 /* getting the default Benders' decomposition */
2363 benders = SCIPfindBenders(scenarioscip, "default");
2364
2365 /* updating the lower bounds for the subproblems */
2366 for( i = 0; i < getScenarioNChildren(scenario); i++ )
2368 getScenarioLowerbound(scenarioscip, getScenarioChild(scenario, i)));
2369 }
2370
2371 /* computing the probability for the scenario */
2372 probability = computeScenarioProbability(scenarioscip, scenario);
2373
2374 /* change the constraints for the given scenario */
2375 nentries = getScenarioNEntries(scenario);
2376 for( i = 0; i < nentries; i++ )
2377 {
2378 SCIP_CONS* cons;
2379 SCIP_VAR* var;
2380 char RHS[] = "RHS";
2381 char rhs[] = "rhs";
2382 char RIGHT[] = "RIGHT";
2383 char MINI[] = "MINI";
2384 char obj[] = "obj";
2385 char OBJ[] = "OBJ";
2386
2387 /* finding the constraint associated with the row */
2388 getScenarioEntityName(name, getScenarioEntryRow(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2389 getScenarioNum(scenarioscip, scenario));
2390 cons = SCIPfindCons(scenarioscip, name);
2391
2392 if( strncmp(getScenarioEntryCol(scenario, i), RHS, 3) == 0 ||
2393 strncmp(getScenarioEntryCol(scenario, i), rhs, 3) == 0 ||
2394 strcmp(getScenarioEntryCol(scenario, i), RIGHT) == 0 )
2395 {
2396 /* if the constraint is NULL, then it is not possible to make any changes to the scenario */
2397 if( cons == NULL )
2398 {
2399 SCIPerrorMessage("There is no constraint <%s> in the current scenario.\n", name);
2400 return SCIP_READERROR;
2401 }
2402
2403 /* if the constraint is an equality constraint, then the LHS must also be changed */
2404 if( SCIPgetLhsLinear(scenarioscip, cons) >= SCIPgetRhsLinear(scenarioscip, cons) )
2405 {
2406 SCIP_CALL( SCIPchgLhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2407 SCIP_CALL( SCIPchgRhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2408 }
2409 else if( SCIPisLT(scenarioscip, SCIPgetRhsLinear(scenarioscip, cons), SCIPinfinity(scenarioscip)) )
2410 SCIP_CALL( SCIPchgRhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2411 else if( SCIPisLT(scenarioscip, SCIPgetLhsLinear(scenarioscip, cons), SCIPinfinity(scenarioscip)) )
2412 SCIP_CALL( SCIPchgLhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2413 }
2414 else if( strstr(getScenarioEntryRow(scenario, i), MINI) != NULL ||
2415 strstr(getScenarioEntryRow(scenario, i), obj) != NULL ||
2416 strstr(getScenarioEntryRow(scenario, i), OBJ) != NULL )
2417 {
2418 /* finding the variable associated with the column */
2419 getScenarioEntityName(name, getScenarioEntryCol(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2420 getScenarioNum(scenarioscip, scenario));
2421 var = SCIPfindVar(scenarioscip, name);
2422
2423 /* changing the coefficient for the variable */
2424 if( var == NULL )
2425 {
2426 SCIPerrorMessage("There is no variable <%s> in the current scenario.\n", name);
2427 return SCIP_READERROR;
2428 }
2429 else
2430 {
2431 SCIP_CALL( SCIPchgVarObj(scenarioscip, var, getScenarioEntryValue(scenario, i)*probability) );
2432 }
2433 }
2434 else
2435 {
2436 /* if the constraint is NULL, then it is not possible to make any changes to the scenario */
2437 if( cons == NULL )
2438 {
2439 SCIPerrorMessage("There is no constraint <%s> in the current scenario.\n", name);
2440 return SCIP_READERROR;
2441 }
2442
2443 /* finding the variable associated with the column */
2444 getScenarioEntityName(name, getScenarioEntryCol(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2445 getScenarioNum(scenarioscip, scenario));
2446 var = SCIPfindVar(scenarioscip, name);
2447
2448 if( var == NULL )
2449 {
2450 (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s", getScenarioEntryCol(scenario, i));
2451 var = SCIPfindVar(scenarioscip, name);
2452 }
2453
2454 /* changing the coefficient for the variable */
2455 if( var == NULL )
2456 {
2457 SCIPerrorMessage("There is no variable <%s> in the current scenario.\n", name);
2458 return SCIP_READERROR;
2459 }
2460 else
2461 {
2462 SCIP_CALL( SCIPchgCoefLinear(scenarioscip, cons, var, getScenarioEntryValue(scenario, i)) );
2463 }
2464 }
2465 }
2466
2467 return SCIP_OKAY;
2468}
2469
2470/** removes the core variables and constriants for stage 2 and lower */
2471static
2473 SCIP* scip /**< the SCIP data structure */
2474 )
2475{
2476 SCIP_CONS** conss;
2477 SCIP_VAR** vars;
2478 int nconss;
2479 int nvars;
2480 int numstages;
2481 int i;
2482 int j;
2483 SCIP_Bool deleted;
2484
2485 assert(scip != NULL);
2486
2487 numstages = SCIPtimGetNStages(scip);
2488
2489 /* looping through all stages to remove the variables and constraints. The first stage is not removed as these are
2490 * part of the complete problem */
2491 for( i = 1; i < numstages; i++ )
2492 {
2493 conss = SCIPtimGetStageConss(scip, i);
2494 vars = SCIPtimGetStageVars(scip, i);
2495 nconss = SCIPtimGetStageNConss(scip, i);
2496 nvars = SCIPtimGetStageNVars(scip, i);
2497
2498 /* removing constriants */
2499 for( j = 0; j < nconss; j++ )
2500 {
2501 if( !SCIPconsIsDeleted(conss[j]) )
2502 SCIP_CALL( SCIPdelCons(scip, conss[j]) );
2503 }
2504
2505 /* removing variables */
2506 for( j = 0; j < nvars; j++ )
2507 {
2508 if( !SCIPvarIsDeleted(vars[j]) )
2509 {
2510 SCIP_CALL( SCIPdelVar(scip, vars[j], &deleted) );
2511 assert(deleted);
2512 }
2513 }
2514 }
2515
2516 return SCIP_OKAY;
2517}
2518
2519
2520/* build the stochastic program completely as a MIP, i.e. no decomposition */
2521static
2523 SCIP* scip, /**< the SCIP data structure */
2524 SCIP_READERDATA* readerdata /**< the reader data */
2525 )
2526{
2527 int i;
2528
2529 assert(scip != NULL);
2530 assert(readerdata != NULL);
2531
2532 /* adding all variables and constraints for stages below the first stage.
2533 * The first stage is covered by the original problem. */
2534 for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2535 SCIP_CALL( addScenarioVarsAndConsToProb(scip, getScenarioChild(readerdata->scenariotree, i), FALSE) );
2536
2537 /* removing the variable and constraints that were included as part of the core file */
2539
2540 return SCIP_OKAY;
2541}
2542
2543
2544/** builds the stochastic program using Benders' decomposition */
2545static
2547 SCIP* scip, /**< the SCIP data structure */
2548 SCIP_READERDATA* readerdata /**< the reader data */
2549 )
2550{
2551 SCIP_BENDERS* benders;
2552 int i;
2553
2554 assert(scip != NULL);
2555 assert(readerdata != NULL);
2556
2557 SCIP_CALL( createScenarioSubproblemArray(scip, readerdata->scenariotree) );
2558
2559 /* activating the Benders' constraint handler. The two-phase method is activated by default. If the user desires not
2560 * to use the two-phase method, then the setting in cons_benderslp must be explicitly changed.
2561 */
2562 SCIP_CALL( SCIPsetBoolParam(scip, "constraints/benders/active", TRUE) );
2563
2564 setScenarioScip(readerdata->scenariotree, scip);
2565
2566 /* adding all variables and constraints for stages below the first stage.
2567 * The first stage is covered by the original problem. */
2568 for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2569 {
2570 SCIP_CALL( addScenarioVarsAndConsToProb(scip, getScenarioChild(readerdata->scenariotree, i), TRUE) );
2571 addScenarioSubproblem(readerdata->scenariotree, getScenarioScip(getScenarioChild(readerdata->scenariotree, i)));
2572 }
2573
2574 /* creating the Benders' decomposition */
2576 getScenarioNChildren(readerdata->scenariotree)) );
2577
2578 /* getting the default Benders' decomposition */
2579 benders = SCIPfindBenders(scip, "default");
2580
2581 /* updating the lower bounds for the subproblems */
2582 for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2583 {
2585 getScenarioLowerbound(scip, getScenarioChild(readerdata->scenariotree, i)));
2586 }
2587
2588 /* removing the variable and constraints that were included as part of the core file */
2590
2591 /* changing settings that are required for Benders' decomposition */
2593 SCIP_CALL( SCIPsetIntParam(scip, "propagating/maxrounds", 0) );
2594 SCIP_CALL( SCIPsetIntParam(scip, "propagating/maxroundsroot", 0) );
2595 SCIP_CALL( SCIPsetIntParam(scip, "heuristics/trysol/freq", 1) );
2596
2597 /* disabling aggregation since it can affect the mapping between the master and subproblem variables */
2598 SCIP_CALL( SCIPsetBoolParam(scip, "presolving/donotaggr", TRUE) );
2599 SCIP_CALL( SCIPsetBoolParam(scip, "presolving/donotmultaggr", TRUE) );
2600
2601 return SCIP_OKAY;
2602}
2603
2604/** Read the stochastic information of an SMPS file instance in "STO File Format". */
2605static
2607 SCIP* scip, /**< SCIP data structure */
2608 const char* filename, /**< name of the input file */
2609 SCIP_READERDATA* readerdata /**< the reader data */
2610 )
2611{
2612 SCIP_FILE* fp;
2613 STOINPUT* stoi;
2614 SCIP_RETCODE retcode;
2615 SCIP_Bool error = TRUE;
2616 SCIP_Bool unsupported = FALSE;
2617
2618 assert(scip != NULL);
2619 assert(filename != NULL);
2620
2621 fp = SCIPfopen(filename, "r");
2622 if( fp == NULL )
2623 {
2624 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
2625 SCIPprintSysError(filename);
2626 return SCIP_NOFILE;
2627 }
2628
2630 SCIP_CALL_TERMINATE( retcode, createReaderdata(scip, readerdata), TERMINATE );
2631
2632 SCIP_CALL_TERMINATE( retcode, readStoch(scip, stoi), TERMINATE );
2633
2634 /* checking for supported stochastic information types */
2636 {
2637 SCIPinfoMessage(scip, NULL, "\nSorry, currently only STO files with the stochastic information as DISCRETE are supported.\n\n");
2638 SCIPinfoMessage(scip, NULL, "NOTE: The problem provided by the COR file is loaded without stochastic information.\n\n");
2639 unsupported = TRUE;
2640 }
2641 else
2642 {
2643 if( stoinputSection(stoi) == STO_BLOCKS )
2644 {
2645 /* coverity[tainted_data] */
2646 SCIP_CALL_TERMINATE( retcode, readBlocks(stoi, scip, readerdata), TERMINATE );
2647 }
2648
2649 if( stoinputSection(stoi) == STO_SCENARIOS )
2650 {
2651 /* if there are more than two stages, then the sto file is not read. */
2652 if( SCIPtimGetNStages(scip) > 2 )
2653 {
2654 SCIPinfoMessage(scip, NULL, "\nThe scenarios for the stochastic programs are defined in <%s> as SCENARIOS\n", filename);
2655 SCIPinfoMessage(scip, NULL, "Sorry, currently only two-stage stochastic programs are supported when scenarios are defined as SCENARIOS.\n\n");
2656 SCIPinfoMessage(scip, NULL, "NOTE: The problem provided by the COR file is loaded without stochastic information.\n\n");
2657 unsupported = TRUE;
2658 }
2659 else
2660 {
2661 SCIP_CALL_TERMINATE( retcode, readScenarios(stoi, scip, readerdata), TERMINATE );
2662 }
2663 }
2664
2665 if( stoinputSection(stoi) == STO_INDEP )
2666 {
2667 SCIP_CALL_TERMINATE( retcode, readIndep(stoi, scip, readerdata), TERMINATE );
2668 }
2669 }
2670
2671 if( !unsupported && stoinputSection(stoi) != STO_ENDATA )
2672 stoinputSyntaxerror(stoi);
2673
2674 error = stoinputHasError(stoi);
2675
2676 if( !error && !unsupported )
2677 {
2678 if( readerdata->usebenders )
2679 {
2680 SCIP_CALL_TERMINATE( retcode, buildDecompProblem(scip, readerdata), TERMINATE );
2681 }
2682 else
2683 {
2684 SCIP_CALL_TERMINATE( retcode, buildFullProblem(scip, readerdata), TERMINATE );
2685 }
2686 }
2687
2688TERMINATE:
2689 stoinputFree(scip, &stoi);
2690 SCIPfclose(fp);
2691
2692 if( error || retcode != SCIP_OKAY )
2693 return SCIP_READERROR;
2694 else
2695 return SCIP_OKAY;
2696}
2697
2698
2699/*
2700 * Callback methods of reader
2701 */
2702
2703/** copy method for reader plugins (called when SCIP copies plugins) */
2704static
2706{ /*lint --e{715}*/
2707 assert(scip != NULL);
2708 assert(reader != NULL);
2709 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2710
2711 /* call inclusion method of reader */
2713
2714 return SCIP_OKAY;
2715}
2716
2717/** destructor of reader to free user data (called when SCIP is exiting) */
2718static
2720{
2721 SCIP_READERDATA* readerdata;
2722
2723 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2724 readerdata = SCIPreaderGetData(reader);
2725 assert(readerdata != NULL);
2726
2727 SCIP_CALL( freeReaderdata(scip, readerdata) );
2728
2729 SCIPfreeBlockMemory(scip, &readerdata);
2730
2731 return SCIP_OKAY;
2732}
2733
2734/** problem reading method of reader */
2735static
2737{ /*lint --e{715}*/
2738 SCIP_READER* correader;
2739 SCIP_READER* timreader;
2740
2741 assert(reader != NULL);
2742 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2743 assert(result != NULL);
2744
2745 *result = SCIP_DIDNOTRUN;
2746
2747 correader = SCIPfindReader(scip, "correader");
2748 timreader = SCIPfindReader(scip, "timreader");
2749
2750 if( correader == NULL )
2751 {
2752 SCIPwarningMessage(scip, "It is necessary to include the \"cor\" reader\n");
2753 (*result) = SCIP_DIDNOTRUN;
2754 return SCIP_OKAY;
2755 }
2756
2757 if( timreader == NULL )
2758 {
2759 SCIPwarningMessage(scip, "It is necessary to include the \"tim\" reader\n");
2760 (*result) = SCIP_DIDNOTRUN;
2761 return SCIP_OKAY;
2762 }
2763
2764 /* checking whether the cor file has been read */
2765 if( !SCIPcorHasRead(correader) )
2766 {
2767 SCIPwarningMessage(scip, "The core file must be read before the time and stochastic files.\n");
2768 (*result) = SCIP_DIDNOTRUN;
2769 return SCIP_OKAY;
2770 }
2771
2772 /* checking whether the tim file has been read */
2773 if( !SCIPtimHasRead(timreader) )
2774 {
2775 SCIPwarningMessage(scip, "The time file must be read before the stochastic files.\n");
2776 (*result) = SCIP_DIDNOTRUN;
2777 return SCIP_OKAY;
2778 }
2779
2780 SCIP_CALL( SCIPreadSto(scip, filename, result) );
2781
2782 return SCIP_OKAY;
2783}
2784
2785/*
2786 * sto file reader specific interface methods
2787 */
2788
2789/** includes the sto file reader in SCIP */
2791 SCIP* scip /**< SCIP data structure */
2792 )
2793{
2794 SCIP_READERDATA* readerdata;
2795 SCIP_READER* reader;
2796
2797 /* create reader data */
2798 SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
2799 readerdata->created = FALSE;
2800 readerdata->scenariotree = NULL;
2801 readerdata->numscenarios = 0;
2802
2803 /* include reader */
2805
2806 /* set non fundamental callbacks via setter functions */
2807 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySto) );
2808 SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeSto) );
2809 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSto) );
2810
2811 /* add decomposition parameters */
2813 "reading/" READER_NAME "/usebenders",
2814 "should Benders' decomposition be used?",
2815 &readerdata->usebenders, FALSE, DEFAULT_USEBENDERS, NULL, NULL) );
2816
2817 return SCIP_OKAY;
2818}
2819
2820
2821/** reads the stochastic information for a stochastic program that is in SMPS format */
2823 SCIP* scip, /**< SCIP data structure */
2824 const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
2825 SCIP_RESULT* result /**< pointer to store the result of the file reading call */
2826 )
2827{
2828 SCIP_READER* reader;
2829 SCIP_READERDATA* readerdata;
2830 SCIP_RETCODE retcode;
2831
2832 assert(scip != NULL);
2833 assert(result != NULL);
2834
2835 reader = SCIPfindReader(scip, READER_NAME);
2836 assert(reader != NULL);
2837 readerdata = SCIPreaderGetData(reader);
2838
2839 /* before the STO file can be read, the reader data needs to be freed. This is because the reader data stores problem
2840 * data, which needs to be removed before the next instance is read. For most readers, there is no problem data
2841 * stored in the reader data, and hence the reader data doesn't need to be freed.
2842 */
2843 SCIP_CALL( freeReaderdata(scip, readerdata) );
2844
2845 retcode = readSto(scip, filename, readerdata);
2846
2847 if( retcode == SCIP_PLUGINNOTFOUND )
2848 retcode = SCIP_READERROR;
2849
2850 if( retcode == SCIP_NOFILE || retcode == SCIP_READERROR )
2851 return retcode;
2852
2853 SCIP_CALL( retcode );
2854
2855 *result = SCIP_SUCCESS;
2856
2857 return SCIP_OKAY;
2858}
2859
2860/** returns the total number of scenarios added to the problem */
2862 SCIP* scip /**< SCIP data structure */
2863 )
2864{
2865 SCIP_READER* reader;
2866 SCIP_READERDATA* readerdata;
2867
2868 reader = SCIPfindReader(scip, READER_NAME);
2869
2870 assert(reader != NULL);
2871 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2872
2873 readerdata = SCIPreaderGetData(reader);
2874 assert(readerdata != NULL);
2875
2876 return readerdata->numscenarios;
2877}
2878
2879/** frees the STO reader data */
2881 SCIP* scip /**< the SCIP data structure */
2882 )
2883{
2884 SCIP_READER* reader;
2885 SCIP_READERDATA* readerdata;
2886
2887 assert(scip != NULL);
2888
2889 reader = SCIPfindReader(scip, READER_NAME);
2890 assert(reader != NULL);
2891
2892 readerdata = SCIPreaderGetData(reader);
2893 assert(readerdata != NULL);
2894
2895 SCIP_CALL( freeReaderdata(scip, readerdata) );
2896
2897 return SCIP_OKAY;
2898}
default Benders' decomposition plugin
Constraint handler for linear constraints in their most general form, .
#define NULL
Definition: def.h:248
#define SCIP_MAXSTRLEN
Definition: def.h:269
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:156
#define TRUE
Definition: def.h:93
#define FALSE
Definition: def.h:94
#define SCIP_CALL_TERMINATE(retcode, x, TERM)
Definition: def.h:376
#define SCIP_CALL(x)
Definition: def.h:355
#define SCIP_CALL_FINALLY(x, y)
Definition: def.h:397
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 SCIPcreateBendersDefault(SCIP *scip, SCIP **subproblems, int nsubproblems)
SCIP_Real SCIPgetRhsLinear(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPchgRhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
SCIP_Real SCIPgetLhsLinear(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPchgLhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
SCIP_RETCODE SCIPchgCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
SCIP_RETCODE SCIPgetConsCopy(SCIP *sourcescip, SCIP *targetscip, SCIP_CONS *sourcecons, SCIP_CONS **targetcons, SCIP_CONSHDLR *sourceconshdlr, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, const char *name, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode, SCIP_Bool global, SCIP_Bool *valid)
Definition: scip_copy.c:1580
void SCIPenableDebugSol(SCIP *scip)
Definition: scip_debug.c:58
int SCIPstoGetNScenarios(SCIP *scip)
Definition: reader_sto.c:2861
SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
Definition: reader_cor.c:267
SCIP_RETCODE SCIPfreeReaderdataSto(SCIP *scip)
Definition: reader_sto.c:2880
SCIP_RETCODE SCIPreadSto(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition: reader_sto.c:2822
SCIP_RETCODE SCIPincludeReaderSto(SCIP *scip)
Definition: reader_sto.c:2790
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:402
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:370
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1907
const char * SCIPgetProbName(SCIP *scip)
Definition: scip_prob.c:1242
int SCIPgetNVars(SCIP *scip)
Definition: scip_prob.c:2246
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:3274
SCIP_RETCODE SCIPdelCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:3420
SCIP_RETCODE SCIPdelVar(SCIP *scip, SCIP_VAR *var, SCIP_Bool *deleted)
Definition: scip_prob.c:2041
SCIP_RETCODE SCIPcreateProbBasic(SCIP *scip, const char *name)
Definition: scip_prob.c:182
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
SCIP_Bool SCIPhashmapExists(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:3466
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:208
#define SCIPdebugMsg
Definition: scip_message.h:78
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:120
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip_param.c:487
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:956
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip_param.c:57
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip_param.c:429
SCIP_BENDERS * SCIPfindBenders(SCIP *scip, const char *name)
Definition: scip_benders.c:493
void SCIPbendersUpdateSubproblemLowerbound(SCIP_BENDERS *benders, int probnumber, SCIP_Real lowerbound)
Definition: benders.c:6874
SCIP_RETCODE SCIPgetConsNVars(SCIP *scip, SCIP_CONS *cons, int *nvars, SCIP_Bool *success)
Definition: scip_cons.c:2621
SCIP_Bool SCIPconsIsDynamic(SCIP_CONS *cons)
Definition: cons.c:8648
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:8409
SCIP_Bool SCIPconsIsInitial(SCIP_CONS *cons)
Definition: cons.c:8558
SCIP_Bool SCIPconsIsMarkedPropagate(SCIP_CONS *cons)
Definition: cons.c:8598
SCIP_Bool SCIPconsIsChecked(SCIP_CONS *cons)
Definition: cons.c:8588
SCIP_Bool SCIPconsIsDeleted(SCIP_CONS *cons)
Definition: cons.c:8518
SCIP_RETCODE SCIPgetConsVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int varssize, SCIP_Bool *success)
Definition: scip_cons.c:2577
SCIP_Bool SCIPconsIsEnforced(SCIP_CONS *cons)
Definition: cons.c:8578
SCIP_Bool SCIPconsIsLocal(SCIP_CONS *cons)
Definition: cons.c:8628
const char * SCIPconsGetName(SCIP_CONS *cons)
Definition: cons.c:8389
SCIP_Bool SCIPconsIsModifiable(SCIP_CONS *cons)
Definition: cons.c:8638
SCIP_Bool SCIPconsIsStickingAtNode(SCIP_CONS *cons)
Definition: cons.c:8668
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1173
SCIP_Bool SCIPconsIsSeparated(SCIP_CONS *cons)
Definition: cons.c:8568
SCIP_Bool SCIPconsIsRemovable(SCIP_CONS *cons)
Definition: cons.c:8658
#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
int SCIPcalcMemGrowSize(SCIP *scip, int num)
Definition: scip_mem.c:139
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:124
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:136
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:93
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
Definition: scip_mem.h:99
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:108
#define SCIPfreeBufferArrayNull(scip, ptr)
Definition: scip_mem.h:137
#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:605
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:680
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:195
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPvarIsInitial(SCIP_VAR *var)
Definition: var.c:23514
SCIP_Bool SCIPvarIsDeleted(SCIP_VAR *var)
Definition: var.c:23534
SCIP_Real SCIPvarGetLbOriginal(SCIP_VAR *var)
Definition: var.c:24020
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:23900
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:23453
SCIP_RETCODE SCIPcreateVarImpl(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_IMPLINTTYPE impltype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
Definition: scip_var.c:225
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:23267
SCIP_Real SCIPvarGetUbOriginal(SCIP_VAR *var)
Definition: var.c:24063
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1887
SCIP_Bool SCIPvarIsRemovable(SCIP_VAR *var)
Definition: var.c:23524
SCIP_IMPLINTTYPE SCIPvarGetImplType(SCIP_VAR *var)
Definition: var.c:23463
SCIP_RETCODE SCIPchgVarObj(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
Definition: scip_var.c:5372
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10827
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
memory allocation routines
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
public methods for problem variables
#define RIGHT
Definition: rbtree.c:43
COR file reader (MPS format of the core problem for stochastic programs)
#define DEFAULT_USEBENDERS
Definition: reader_sto.c:64
static SCIP_RETCODE readStoch(SCIP *scip, STOINPUT *stoi)
Definition: reader_sto.c:1393
static SCIP_RETCODE readScenarios(STOINPUT *stoi, SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1620
static SCIP_RETCODE createScenarioData(SCIP *scip, STOSCENARIO **scenariodata)
Definition: reader_sto.c:163
static SCIP_RETCODE setScenarioProbability(SCIP *scip, STOSCENARIO *scenario, SCIP_Real probability)
Definition: reader_sto.c:467
static SCIP_Real getScenarioProbability(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:483
static void stoinputFree(SCIP *scip, STOINPUT **stoi)
Definition: reader_sto.c:1086
static void stoinputSyntaxerror(STOINPUT *stoi)
Definition: reader_sto.c:1257
StoSection
Definition: reader_sto.c:119
@ STO_INDEP
Definition: reader_sto.c:123
@ STO_SCENARIOS
Definition: reader_sto.c:121
@ STO_BLOCKS
Definition: reader_sto.c:122
@ STO_ENDATA
Definition: reader_sto.c:124
@ STO_STOCH
Definition: reader_sto.c:120
static SCIP_Bool stoinputHasError(const STOINPUT *stoi)
Definition: reader_sto.c:1195
static const char * getScenarioName(STOSCENARIO *scenario)
Definition: reader_sto.c:427
static SCIP_RETCODE removeCoreVariablesAndConstraints(SCIP *scip)
Definition: reader_sto.c:2472
static void getScenarioEntityName(char *name, const char *varname, int stagenum, int scenarionum)
Definition: reader_sto.c:1957
static SCIP_RETCODE readSto(SCIP *scip, const char *filename, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:2606
static void clearFrom(char *buf, unsigned int pos)
Definition: reader_sto.c:1270
static void stoinputSetSection(STOINPUT *stoi, STOSECTION section)
Definition: reader_sto.c:1206
static STOSTOCHINFO stoinputStochInfoType(const STOINPUT *stoi)
Definition: reader_sto.c:1107
static SCIP ** getScenarioSubproblemArray(STOSCENARIO *scenario)
Definition: reader_sto.c:307
#define STO_DEFAULT_CHILDRENSIZE
Definition: reader_sto.c:76
static int getScenarioNChildren(STOSCENARIO *scenario)
Definition: reader_sto.c:318
static SCIP_RETCODE insertScenarioInReaderdata(SCIP *scip, SCIP_READERDATA *readerdata, STOSCENARIO *scenario, char *parentname)
Definition: reader_sto.c:807
static SCIP_RETCODE readBlocks(STOINPUT *stoi, SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1455
static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1007
static const char * stoinputField5(const STOINPUT *stoi)
Definition: reader_sto.c:1173
static SCIP_RETCODE addScenarioConsToProb(SCIP *scip, SCIP *scenarioscip, STOSCENARIO *scenario, SCIP_HASHMAP *varmap, SCIP_CONS **conss, int nconss, SCIP_Bool decomp)
Definition: reader_sto.c:2144
static SCIP_DECL_READERREAD(readerReadSto)
Definition: reader_sto.c:2736
static SCIP_Real getScenarioEntryValue(STOSCENARIO *scenario, int entry)
Definition: reader_sto.c:594
static SCIP_RETCODE buildScenariosFromBlocks(SCIP *scip, STOSCENARIO ***blocks, STOSCENARIO ***scenarios, STOSCENARIO ***blocksforscen, int *numblocksforscen, int numblocks, int *numblocksperblock, int *numscenarios, int *scenariossize, const char *stage, int stagenum, int blocknum)
Definition: reader_sto.c:834
static int getScenarioStageNum(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:398
#define BLANK
Definition: reader_sto.c:78
enum StoSection STOSECTION
Definition: reader_sto.c:126
static SCIP_RETCODE createScenarioSubproblemArray(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:276
enum StoStochInfo STOSTOCHINFO
Definition: reader_sto.c:138
static void setScenarioScip(STOSCENARIO *scenario, SCIP *scip)
Definition: reader_sto.c:252
static int getScenarioNum(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:454
#define READER_DESC
Definition: reader_sto.c:61
#define STO_MAX_LINELEN
Definition: reader_sto.c:70
static SCIP_RETCODE setScenarioStageName(SCIP *scip, STOSCENARIO *scenario, const char *stagename)
Definition: reader_sto.c:353
#define STO_DEFAULT_ARRAYSIZE
Definition: reader_sto.c:73
static SCIP_Bool stoinputReadLine(STOINPUT *stoi)
Definition: reader_sto.c:1284
static const char * getScenarioEntryCol(STOSCENARIO *scenario, int entry)
Definition: reader_sto.c:581
static SCIP_RETCODE addScenarioVarsToProb(SCIP *scip, STOSCENARIO *scenario, SCIP_HASHMAP *varmap, SCIP_VAR **vars, int nvars)
Definition: reader_sto.c:1973
static SCIP_RETCODE addScenarioEntry(SCIP *scip, STOSCENARIO *scenario, const char *rowname, const char *colname, SCIP_Real value)
Definition: reader_sto.c:525
#define STO_DEFAULT_ENTRIESSIZE
Definition: reader_sto.c:74
#define READER_EXTENSION
Definition: reader_sto.c:62
#define STO_DEFAULT_BLOCKARRAYSIZE
Definition: reader_sto.c:75
static const char * stoinputField4(const STOINPUT *stoi)
Definition: reader_sto.c:1162
static void stoinputSetStochtype(STOINPUT *stoi, const char *stochtype)
Definition: reader_sto.c:1244
static SCIP_RETCODE setScenarioLowerbound(SCIP *scip, STOSCENARIO *scenario, SCIP_Real lowerbound)
Definition: reader_sto.c:496
static SCIP_RETCODE stoinputCreate(SCIP *scip, STOINPUT **stoi, SCIP_FILE *fp)
Definition: reader_sto.c:1054
static SCIP_RETCODE scenarioAddChild(SCIP *scip, STOSCENARIO **parent, STOSCENARIO *child)
Definition: reader_sto.c:679
static void addScenarioSubproblem(STOSCENARIO *scenario, SCIP *subproblem)
Definition: reader_sto.c:291
static const char * stoinputField2(const STOINPUT *stoi)
Definition: reader_sto.c:1140
static SCIP_RETCODE createScenariosFromBlocks(SCIP *scip, SCIP_READERDATA *readerdata, STOSCENARIO ***blocks, int numblocks, int *numblocksperblock, int numstages)
Definition: reader_sto.c:931
static SCIP_RETCODE buildScenarioTree(SCIP *scip, STOSCENARIO **scenariotree, STOSCENARIO ***scenarios, int *numscenarios, int numstages, int stage)
Definition: reader_sto.c:707
static STOSECTION stoinputSection(const STOINPUT *stoi)
Definition: reader_sto.c:1096
static void stoinputSetStochInfoType(STOINPUT *stoi, STOSTOCHINFO stochinfotype)
Definition: reader_sto.c:1218
static SCIP_RETCODE buildDecompProblem(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:2546
static const char * stoinputField6(const STOINPUT *stoi)
Definition: reader_sto.c:1184
static SCIP_RETCODE getScenarioDecompVar(SCIP *scip, STOSCENARIO *scenario, SCIP_VAR *consvar, SCIP_VAR **scenariovar, SCIP_Bool *varadded)
Definition: reader_sto.c:2075
static SCIP_RETCODE addScenarioVarsAndConsToProb(SCIP *scip, STOSCENARIO *scenario, SCIP_Bool decomp)
Definition: reader_sto.c:2263
StoStochInfo
Definition: reader_sto.c:130
@ STO_STOCHINFO_NORMAL
Definition: reader_sto.c:134
@ STO_STOCHINFO_NONE
Definition: reader_sto.c:131
@ STO_STOCHINFO_UNIFORM
Definition: reader_sto.c:133
@ STO_STOCHINFO_DISCRETE
Definition: reader_sto.c:132
@ STO_STOCHINFO_LINTR
Definition: reader_sto.c:136
@ STO_STOCHINFO_SUB
Definition: reader_sto.c:135
static STOSCENARIO * findScenarioInTree(STOSCENARIO *scenariotree, const char *scenname)
Definition: reader_sto.c:780
static SCIP_RETCODE copyScenario(SCIP *scip, STOSCENARIO *sourcescenario, STOSCENARIO **targetscenario, SCIP_Bool copyname)
Definition: reader_sto.c:609
#define READER_NAME
Definition: reader_sto.c:60
static SCIP_RETCODE freeReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1032
static const char * stoinputField0(const STOINPUT *stoi)
Definition: reader_sto.c:1118
static SCIP_RETCODE setScenarioName(SCIP *scip, STOSCENARIO *scenario, const char *name)
Definition: reader_sto.c:411
static SCIP_RETCODE freeScenarioTree(SCIP *scip, STOSCENARIO **scenariotree)
Definition: reader_sto.c:199
static SCIP_RETCODE readIndep(STOINPUT *stoi, SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1763
#define STO_MAX_NAMELEN
Definition: reader_sto.c:71
static SCIP_DECL_READERFREE(readerFreeSto)
Definition: reader_sto.c:2719
static void stoinputSetProbname(STOINPUT *stoi, const char *probname)
Definition: reader_sto.c:1230
static SCIP_Real computeScenarioProbability(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:1932
static SCIP_RETCODE setScenarioStageNum(SCIP *scip, STOSCENARIO *scenario, int stagenum)
Definition: reader_sto.c:382
static SCIP * getScenarioScip(STOSCENARIO *scenario)
Definition: reader_sto.c:265
static int getScenarioNEntries(STOSCENARIO *scenario)
Definition: reader_sto.c:557
static SCIP_Real getScenarioLowerbound(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:512
static SCIP_RETCODE findScenarioVar(SCIP *scip, STOSCENARIO *scenario, SCIP_VAR *consvar, SCIP_VAR **scenariovar)
Definition: reader_sto.c:2027
static STOSCENARIO * getScenarioChild(STOSCENARIO *scenario, int childnum)
Definition: reader_sto.c:329
static const char * getScenarioStageName(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:369
static SCIP_RETCODE mergeScenarios(SCIP *scip, STOSCENARIO *scenario1, STOSCENARIO **mergedscenario)
Definition: reader_sto.c:652
static SCIP_RETCODE buildFullProblem(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:2522
static SCIP_RETCODE addScenariosToReaderdata(SCIP *scip, SCIP_READERDATA *readerdata, STOSCENARIO ***scenarios, int *numscenarios, int numscenariostages)
Definition: reader_sto.c:753
static const char * stoinputField1(const STOINPUT *stoi)
Definition: reader_sto.c:1129
static SCIP_RETCODE setScenarioNum(SCIP *scip, STOSCENARIO *scenario, int scenarionum)
Definition: reader_sto.c:438
static SCIP_DECL_READERCOPY(readerCopySto)
Definition: reader_sto.c:2705
static const char * getScenarioEntryRow(STOSCENARIO *scenario, int entry)
Definition: reader_sto.c:568
static STOSCENARIO * getScenarioParent(STOSCENARIO *scenario)
Definition: reader_sto.c:342
static const char * stoinputField3(const STOINPUT *stoi)
Definition: reader_sto.c:1151
STO file reader - the stochastic information of an instance in SMPS format.
int SCIPtimFindStage(SCIP *scip, const char *stage)
Definition: reader_tim.c:1056
int SCIPtimGetStageNVars(SCIP *scip, int stagenum)
Definition: reader_tim.c:1136
SCIP_VAR ** SCIPtimGetStageVars(SCIP *scip, int stagenum)
Definition: reader_tim.c:1094
SCIP_Bool SCIPtimHasRead(SCIP_READER *reader)
Definition: reader_tim.c:958
int SCIPtimGetNStages(SCIP *scip)
Definition: reader_tim.c:975
const char * SCIPtimGetStageName(SCIP *scip, int stagenum)
Definition: reader_tim.c:994
SCIP_CONS ** SCIPtimGetStageConss(SCIP *scip, int stagenum)
Definition: reader_tim.c:1115
int SCIPtimGetStageNConss(SCIP *scip, int stagenum)
Definition: reader_tim.c:1157
const char * SCIPtimConsGetStageName(SCIP *scip, const char *consname)
Definition: reader_tim.c:1015
TIM file reader - the stage information for a stochastic programming instance in SMPS format.
public methods for constraint handler plugins and constraints
public methods for debugging
general public methods
public methods for memory management
public methods for message handling
public methods for numerical tolerances
public methods for SCIP parameter handling
public methods for global and local (sub)problems
public methods for reader plugins
public methods for SCIP variables
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
default SCIP plugins
@ RHS
char buf[STO_MAX_LINELEN]
Definition: reader_sto.c:148
const char * f4
Definition: reader_sto.c:153
const char * f1
Definition: reader_sto.c:150
char probname[STO_MAX_NAMELEN]
Definition: reader_sto.c:156
STOSTOCHINFO stochinfotype
Definition: reader_sto.c:144
const char * f5
Definition: reader_sto.c:154
int lineno
Definition: reader_sto.c:146
const char * f2
Definition: reader_sto.c:151
SCIP_Bool haserror
Definition: reader_sto.c:147
STOSECTION section
Definition: reader_sto.c:143
SCIP_FILE * fp
Definition: reader_sto.c:145
const char * f0
Definition: reader_sto.c:149
char stochtype[STO_MAX_NAMELEN]
Definition: reader_sto.c:157
const char * f3
Definition: reader_sto.c:152
const char * f6
Definition: reader_sto.c:155
SCIP_Real lowerbound
Definition: reader_sto.c:106
SCIP_Real * values
Definition: reader_sto.c:110
const char ** colnames
Definition: reader_sto.c:109
int nchildren
Definition: reader_sto.c:98
const char * name
Definition: reader_sto.c:104
int childrensize
Definition: reader_sto.c:99
SCIP ** subproblems
Definition: reader_sto.c:95
int entriessize
Definition: reader_sto.c:112
const char * stagename
Definition: reader_sto.c:103
int nsubproblems
Definition: reader_sto.c:100
SCIP * scip
Definition: reader_sto.c:94
STOSCENARIO ** children
Definition: reader_sto.c:97
int scenarionum
Definition: reader_sto.c:102
STOSCENARIO * parent
Definition: reader_sto.c:96
const char ** rownames
Definition: reader_sto.c:108
SCIP_Real probability
Definition: reader_sto.c:105
@ SCIP_PARAMSETTING_OFF
Definition: type_paramset.h:63
struct SCIP_ReaderData SCIP_READERDATA
Definition: type_reader.h:54
@ 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