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