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-2020 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 numstages; /* the number of stages */
752  int i;
753 
754  assert(scip != NULL);
755  assert(readerdata != NULL);
756  assert(scenarios != NULL);
757 
758  numstages = SCIPtimGetNStages(scip);
759  if( numstages != numscenariostages + 1 )
760  assert(FALSE);
761 
762  SCIP_CALL( buildScenarioTree(scip, &readerdata->scenariotree, scenarios, numscenarios, numscenariostages, 0) );
763 
764  /* setting the number of scenarios per stage in the TIME reader data */
765  for( i = 0; i < numscenariostages; i++ )
766  readerdata->numscenarios += numscenarios[i];
767 
768  return SCIP_OKAY;
769 }
770 
771 
772 /** finds a scenario with a given name */
773 static
775  STOSCENARIO* scenariotree, /**< the scenario tree to search */
776  const char* scenname /**< the name of the scenario to search */
777  )
778 {
779  STOSCENARIO* retscen;
780  int i;
781 
782  if( strcmp(getScenarioName(scenariotree), scenname) == 0 )
783  return scenariotree;
784  else
785  {
786  retscen = NULL;
787  for( i = 0; i < getScenarioNChildren(scenariotree); i++ )
788  {
789  retscen = findScenarioInTree(scenariotree->children[i], scenname);
790  if( retscen != NULL )
791  return retscen;
792  }
793  }
794 
795  return NULL;
796 }
797 
798 
799 /** inserts a scenario into the reader data scenario tree */
800 static
802  SCIP* scip, /**< the SCIP data structure */
803  SCIP_READERDATA* readerdata, /**< the reader data */
804  STOSCENARIO* scenario, /**< the scenario to insert in the scenario tree */
805  char* parentname /**< the parent scenario for the inserting scenario */
806  )
807 {
808  STOSCENARIO* parentscen;
809 
810  assert(scip != NULL);
811  assert(readerdata != NULL);
812  assert(scenario != NULL);
813 
814  /* searching for the parent scenario in the tree */
815  parentscen = findScenarioInTree(readerdata->scenariotree, parentname);
816 
817  /* adding the scenario as a child of the parent scenario */
818  SCIP_CALL( scenarioAddChild(scip, &parentscen, scenario) );
819 
820  readerdata->numscenarios++;
821 
822  return SCIP_OKAY;
823 }
824 
825 
826 /* builds the scenarios from the blocks for a given stage */
827 static
829  SCIP* scip, /**< the SCIP data structure */
830  STOSCENARIO*** blocks, /**< the block that form the scenarios */
831  STOSCENARIO*** scenarios, /**< the array to store the scenarios */
832  STOSCENARIO*** blocksforscen, /**< the blocks that will form the scenario */
833  int* numblocksforscen, /**< the number of blocks that form the scenario */
834  int numblocks, /**< the number of blocks */
835  int* numblocksperblock, /**< the number of blocks for a given block */
836  int* numscenarios, /**< the number of scenarios */
837  int* scenariossize, /**< the size of scenarios array */
838  const char* stage, /**< the stage for this scenario */
839  int stagenum, /**< the number of the stage */
840  int blocknum /**< the block number */
841  )
842 {
843  SCIP_Bool processed;
844  int i;
845  int j;
846 
847  assert(scip != NULL);
848  assert(blocks != NULL);
849  assert(scenarios != NULL);
850  assert(blocksforscen != NULL);
851 
852  processed = FALSE;
853  i = blocknum + 1;
854  while( !processed && i < numblocks )
855  {
856  /* it is only necessary to process the next block in the list the belongs to the given stage. */
857  if( strcmp(getScenarioStageName(scip, blocks[i][0]), stage) == 0 )
858  {
859  processed = TRUE;
860 
861  for( j = 0; j < numblocksperblock[i]; j++ )
862  {
863  /* adding the blocks that will build the scenario */
864  (*blocksforscen)[(*numblocksforscen)] = blocks[i][j];
865  (*numblocksforscen)++;
866  SCIP_CALL( buildScenariosFromBlocks(scip, blocks, scenarios, blocksforscen, numblocksforscen, numblocks,
867  numblocksperblock, numscenarios, scenariossize, stage, stagenum + 1, i) );
868 
869  /* the last block needs to be removed so that a new block can be used in its place */
870  (*numblocksforscen)--;
871  }
872  }
873  else
874  {
875  /* the index is only incremented if no block is processed. This is necessary because the value of i is used in
876  * the next if statement for identifying whether all blocks have been processed.
877  */
878  i++;
879  }
880  }
881 
882  /* when all blocks have been inspected, then it is possible to build the scenario */
883  if( i == numblocks )
884  {
885  char scenarioname[SCIP_MAXSTRLEN];
886 
887  /* ensuring the correct amount of memory is available */
888  if( (*numscenarios) + 1 > (*scenariossize) )
889  {
890  int newsize;
891  newsize = SCIPcalcMemGrowSize(scip, (*numscenarios) + 1);
892  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, scenarios, (*scenariossize), newsize) );
893  (*scenariossize) = newsize;
894  }
895 
896  SCIP_CALL( createScenarioData(scip, &(*scenarios)[(*numscenarios)]) );
897 
898  /* setting the scenario name */
899  (void) SCIPsnprintf(scenarioname, SCIP_MAXSTRLEN, "Scenario_%s_%d", stage, (*numscenarios));
900  SCIP_CALL( setScenarioName(scip, (*scenarios)[(*numscenarios)], scenarioname) );
901  SCIP_CALL( setScenarioStageName(scip, (*scenarios)[(*numscenarios)], stage) );
902  SCIP_CALL( setScenarioNum(scip, (*scenarios)[(*numscenarios)], (*numscenarios)) );
903  SCIP_CALL( setScenarioStageNum(scip, (*scenarios)[(*numscenarios)], stagenum) );
904 
905  /* if there is only a single block for the scenario, then we simply copy the block.
906  * Otherwise, the blocks are merged into a single scenario */
907  if( (*numblocksforscen) == 1 )
908  SCIP_CALL( copyScenario(scip, (*blocksforscen)[0], &(*scenarios)[(*numscenarios)], FALSE) );
909  else
910  {
911  SCIP_CALL( copyScenario(scip, (*blocksforscen)[0], &(*scenarios)[(*numscenarios)], FALSE) );
912  for( i = 1; i < (*numblocksforscen); i++ )
913  SCIP_CALL( mergeScenarios(scip, (*blocksforscen)[i], &(*scenarios)[(*numscenarios)]) );
914  }
915 
916  (*numscenarios)++;
917  }
918 
919  return SCIP_OKAY;
920 }
921 
922 
923 /* creates the scenarios from the blocks */
924 static
926  SCIP* scip, /**< the SCIP data structure */
927  SCIP_READERDATA* readerdata, /**< the reader data */
928  STOSCENARIO*** blocks, /**< the block that form the scenarios */
929  int numblocks, /**< the number of blocks */
930  int* numblocksperblock, /**< the number of blocks for each block type */
931  int numstages /**< the number of stages */
932  )
933 {
934  STOSCENARIO*** scenarios;
935  STOSCENARIO** blocksforscen;
936  int* numscenarios;
937  int* scenariossize;
938  int numblocksforscen;
939  int stagenum;
940  char periods[SCIP_MAXSTRLEN];
941  int i;
942  int j;
943 
944  assert(scip != NULL);
945  assert(blocks != NULL);
946 
947  /* allocating the memory for the scenarios array */
948  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &scenarios, numstages) );
949  SCIP_CALL( SCIPallocBufferArray(scip, &numscenarios, numstages) );
950  SCIP_CALL( SCIPallocBufferArray(scip, &scenariossize, numstages) );
951  for( i = 0; i < numstages; i++ )
952  {
953  scenariossize[i] = STO_DEFAULT_BLOCKARRAYSIZE;
954  numscenarios[i] = 0;
955  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &scenarios[i], scenariossize[i]) );
956  }
957 
958  /* allocating the memory for the block for scenario array */
959  SCIP_CALL( SCIPallocBufferArray(scip, &blocksforscen, numblocks) );
960 
961  (void) SCIPsnprintf(periods, SCIP_MAXSTRLEN, "");
962 
963  stagenum = 0;
964  for( i = 0; i < numblocks; i++ )
965  {
966  numblocksforscen = 0;
967  if( strstr(periods, getScenarioStageName(scip, blocks[i][0])) == NULL )
968  {
969  /* recording the stage name as processed */
970  (void) SCIPsnprintf(periods, SCIP_MAXSTRLEN, "%s_%s", periods, getScenarioStageName(scip, blocks[i][0]));
971 
972  SCIP_CALL( buildScenariosFromBlocks(scip, blocks, &scenarios[stagenum], &blocksforscen, &numblocksforscen,
973  numblocks, numblocksperblock, &numscenarios[stagenum], &scenariossize[stagenum],
974  getScenarioStageName(scip, blocks[i][0]), stagenum, i - 1) );
975 
976  stagenum++;
977  }
978  }
979 
980  /* adding the scenarios to the reader data */
981  SCIP_CALL( setScenarioNum(scip, readerdata->scenariotree, 0) );
982  SCIP_CALL( setScenarioStageNum(scip, readerdata->scenariotree, 0) );
983  SCIP_CALL( addScenariosToReaderdata(scip, readerdata, scenarios, numscenarios, numstages) );
984 
985  SCIPfreeBufferArray(scip, &blocksforscen);
986  for( i = numstages - 1; i >= 0; i-- )
987  {
988  for( j = numscenarios[i] - 1; j >= 0; j-- )
989  SCIP_CALL( freeScenarioTree(scip, &scenarios[i][j]) );
990  SCIPfreeBlockMemoryArray(scip, &scenarios[i], scenariossize[i]);
991  }
992  SCIPfreeBufferArray(scip, &scenariossize);
993  SCIPfreeBufferArray(scip, &numscenarios);
994  SCIPfreeBlockMemoryArray(scip, &scenarios, numstages);
995 
996  return SCIP_OKAY;
997 }
998 
999 /** creates the reader data */
1000 static
1002  SCIP* scip, /**< SCIP data structure */
1003  SCIP_READERDATA* readerdata /**< the reader data */
1004  )
1005 {
1006  assert(scip != NULL);
1007  assert(readerdata != NULL);
1008 
1009  /* creating the initial scenario */
1010  SCIP_CALL( createScenarioData(scip, &readerdata->scenariotree) );
1011 
1012  /* setting the scenario name and stage name */
1013  SCIP_CALL( setScenarioName(scip, readerdata->scenariotree, "ROOT") );
1014  SCIP_CALL( setScenarioStageName(scip, readerdata->scenariotree, SCIPtimGetStageName(scip, 0)) );
1015 
1016  return SCIP_OKAY;
1017 }
1018 
1019 /** frees the reader data */
1020 static
1022  SCIP* scip, /**< the SCIP data structure */
1023  SCIP_READERDATA* readerdata /**< the reader data */
1024  )
1025 {
1026  assert(scip != NULL);
1027  assert(readerdata != NULL);
1028 
1029  /* freeing the scenario tree */
1030  if( readerdata->scenariotree != NULL )
1031  SCIP_CALL( freeScenarioTree(scip, &readerdata->scenariotree) );
1032 
1033  SCIPfreeBlockMemory(scip, &readerdata);
1034 
1035  return SCIP_OKAY;
1036 }
1037 
1038 /** creates the sto input structure */
1039 static
1041  SCIP* scip, /**< SCIP data structure */
1042  STOINPUT** stoi, /**< sto input structure */
1043  SCIP_FILE* fp /**< file object for the input file */
1044  )
1045 {
1046  assert(stoi != NULL);
1047  assert(fp != NULL);
1048 
1049  SCIP_CALL( SCIPallocBlockMemory(scip, stoi) );
1050 
1051  (*stoi)->section = STO_STOCH;
1052  (*stoi)->stochinfotype = STO_STOCHINFO_NONE;
1053  (*stoi)->fp = fp;
1054  (*stoi)->lineno = 0;
1055  (*stoi)->haserror = FALSE;
1056  (*stoi)->buf [0] = '\0';
1057  (*stoi)->probname[0] = '\0';
1058  (*stoi)->stochtype[0] = '\0';
1059  (*stoi)->f0 = NULL;
1060  (*stoi)->f1 = NULL;
1061  (*stoi)->f2 = NULL;
1062  (*stoi)->f3 = NULL;
1063  (*stoi)->f4 = NULL;
1064  (*stoi)->f5 = NULL;
1065  (*stoi)->f6 = NULL;
1066 
1067  return SCIP_OKAY;
1068 }
1069 
1070 /** free the sto input structure */
1071 static
1073  SCIP* scip, /**< SCIP data structure */
1074  STOINPUT** stoi /**< sto input structure */
1075  )
1076 {
1077  SCIPfreeBlockMemory(scip, stoi);
1078 }
1079 
1080 /** returns the current section */
1081 static
1083  const STOINPUT* stoi /**< sto input structure */
1084  )
1085 {
1086  assert(stoi != NULL);
1087 
1088  return stoi->section;
1089 }
1090 
1091 /** returns the stochastic information type */
1092 static
1094  const STOINPUT* stoi /**< sto input structure */
1095  )
1096 {
1097  assert(stoi != NULL);
1098 
1099  return stoi->stochinfotype;
1100 }
1101 
1102 /** return the current value of field 0 */
1103 static
1104 const char* stoinputField0(
1105  const STOINPUT* stoi /**< sto input structure */
1106  )
1107 {
1108  assert(stoi != NULL);
1109 
1110  return stoi->f0;
1111 }
1112 
1113 /** return the current value of field 1 */
1114 static
1115 const char* stoinputField1(
1116  const STOINPUT* stoi /**< sto input structure */
1117  )
1118 {
1119  assert(stoi != NULL);
1120 
1121  return stoi->f1;
1122 }
1123 
1124 /** return the current value of field 2 */
1125 static
1126 const char* stoinputField2(
1127  const STOINPUT* stoi /**< sto input structure */
1128  )
1129 {
1130  assert(stoi != NULL);
1131 
1132  return stoi->f2;
1133 }
1134 
1135 /** return the current value of field 3 */
1136 static
1137 const char* stoinputField3(
1138  const STOINPUT* stoi /**< sto input structure */
1139  )
1140 {
1141  assert(stoi != NULL);
1142 
1143  return stoi->f3;
1144 }
1145 
1146 /** return the current value of field 4 */
1147 static
1148 const char* stoinputField4(
1149  const STOINPUT* stoi /**< sto input structure */
1150  )
1151 {
1152  assert(stoi != NULL);
1153 
1154  return stoi->f4;
1155 }
1156 
1157 /** return the current value of field 5 */
1158 static
1159 const char* stoinputField5(
1160  const STOINPUT* stoi /**< sto input structure */
1161  )
1162 {
1163  assert(stoi != NULL);
1164 
1165  return stoi->f5;
1166 }
1167 
1168 /** return the current value of field 6 */
1169 static
1170 const char* stoinputField6(
1171  const STOINPUT* stoi /**< sto input structure */
1172  )
1173 {
1174  assert(stoi != NULL);
1175 
1176  return stoi->f6;
1177 }
1178 
1179 /** returns if an error was detected */
1180 static
1182  const STOINPUT* stoi /**< sto input structure */
1183  )
1184 {
1185  assert(stoi != NULL);
1186 
1187  return stoi->haserror;
1188 }
1189 
1190 /** set the section in the sto input structure to given section */
1191 static
1193  STOINPUT* stoi, /**< sto input structure */
1194  STOSECTION section /**< section that is set */
1195  )
1196 {
1197  assert(stoi != NULL);
1198 
1199  stoi->section = section;
1200 }
1201 
1202 /** set the stochastic info type in the sto input structure */
1203 static
1205  STOINPUT* stoi, /**< sto input structure */
1206  STOSTOCHINFO stochinfotype /**< the stochastic infomation type */
1207  )
1208 {
1209  assert(stoi != NULL);
1210 
1211  stoi->stochinfotype = stochinfotype;
1212 }
1213 
1214 /** set the problem name in the sto input structure to given problem name */
1215 static
1217  STOINPUT* stoi, /**< sto input structure */
1218  const char* probname /**< name of the problem to set */
1219  )
1220 {
1221  assert(stoi != NULL);
1222  assert(probname != NULL);
1223  assert(strlen(probname) < sizeof(stoi->probname));
1224 
1225  (void)SCIPmemccpy(stoi->probname, probname, '\0', STO_MAX_NAMELEN - 1);
1226 }
1227 
1228 /** set the type name in the sto input structure to given objective name */
1229 static
1231  STOINPUT* stoi, /**< sto input structure */
1232  const char* stochtype /**< name of the scenario type */
1233  )
1234 {
1235  assert(stoi != NULL);
1236  assert(stochtype != NULL);
1237  assert(strlen(stochtype) < sizeof(stoi->stochtype));
1238 
1239  (void)SCIPmemccpy(stoi->stochtype, stochtype, '\0', STO_MAX_NAMELEN - 1);
1240 }
1241 
1242 static
1244  STOINPUT* stoi /**< sto input structure */
1245  )
1246 {
1247  assert(stoi != NULL);
1248 
1249  SCIPerrorMessage("Syntax error in line %d\n", stoi->lineno);
1250  stoi->section = STO_ENDATA;
1251  stoi->haserror = TRUE;
1252 }
1253 
1254 /** fill the line from \p pos up to column 80 with blanks. */
1255 static
1257  char* buf, /**< buffer to clear */
1258  unsigned int pos /**< position to start the clearing process */
1259  )
1260 {
1261  unsigned int i;
1262 
1263  for(i = pos; i < 80; i++)
1264  buf[i] = BLANK;
1265  buf[80] = '\0';
1266 }
1267 
1268 /** read a sto format data line and parse the fields. */
1269 static
1271  STOINPUT* stoi /**< sto input structure */
1272  )
1273 {
1274  unsigned int len;
1275  unsigned int i;
1276  char* s;
1277  SCIP_Bool is_marker;
1278  SCIP_Bool is_empty;
1279  char* nexttok;
1280 
1281  do
1282  {
1283  stoi->f0 = stoi->f1 = stoi->f2 = stoi->f3 = stoi->f4 = stoi->f5 = stoi->f6 = 0;
1284  is_marker = FALSE;
1285 
1286  /* Read until we have not a comment line. */
1287  do
1288  {
1289  stoi->buf[STO_MAX_LINELEN-1] = '\0';
1290  if( NULL == SCIPfgets(stoi->buf, (int) sizeof(stoi->buf), stoi->fp) )
1291  return FALSE;
1292  stoi->lineno++;
1293  }
1294  while( *stoi->buf == '*' );
1295 
1296  /* Normalize line */
1297  len = (unsigned int) strlen(stoi->buf);
1298 
1299  for( i = 0; i < len; i++ )
1300  {
1301  if( (stoi->buf[i] == '\t') || (stoi->buf[i] == '\n') || (stoi->buf[i] == '\r') )
1302  stoi->buf[i] = BLANK;
1303  }
1304 
1305  if( len < 80 )
1306  clearFrom(stoi->buf, len);
1307 
1308  SCIPdebugMessage("line %d: <%s>\n", stoi->lineno, stoi->buf);
1309 
1310  assert(strlen(stoi->buf) >= 80);
1311 
1312  /* Look for new section */
1313  if( *stoi->buf != BLANK )
1314  {
1315  stoi->f0 = SCIPstrtok(&stoi->buf[0], " ", &nexttok);
1316 
1317  assert(stoi->f0 != 0);
1318 
1319  stoi->f1 = SCIPstrtok(NULL, " ", &nexttok);
1320 
1321  return TRUE;
1322  }
1323 
1324  s = &stoi->buf[1];
1325 
1326  /* At this point it is not clear if we have a indicator field.
1327  * If there is none (e.g. empty) f1 will be the first name field.
1328  * If there is one, f2 will be the first name field.
1329  *
1330  * Initially comment marks '$' are only allowed in the beginning
1331  * of the 2nd and 3rd name field. We test all fields but the first.
1332  * This makes no difference, since if the $ is at the start of a value
1333  * field, the line will be erroneous anyway.
1334  */
1335  do
1336  {
1337  if( NULL == (stoi->f1 = SCIPstrtok(s, " ", &nexttok)) )
1338  break;
1339 
1340  if( (NULL == (stoi->f2 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f2 == '$') )
1341  {
1342  stoi->f2 = 0;
1343  break;
1344  }
1345 
1346  if( (NULL == (stoi->f3 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f3 == '$') )
1347  {
1348  stoi->f3 = 0;
1349  break;
1350  }
1351 
1352  if( (NULL == (stoi->f4 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f4 == '$') )
1353  {
1354  stoi->f4 = 0;
1355  break;
1356  }
1357 
1358  if( (NULL == (stoi->f5 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f5 == '$') )
1359  {
1360  stoi->f5 = 0;
1361  break;
1362  }
1363 
1364  if( (NULL == (stoi->f6 = SCIPstrtok(NULL, " ", &nexttok))) || (*stoi->f6 == '$') )
1365  stoi->f6 = 0;
1366  }
1367  while( FALSE );
1368 
1369  /* check for empty lines */
1370  is_empty = (stoi->f0 == NULL && stoi->f1 == NULL);
1371  }
1372  while( is_marker || is_empty );
1373 
1374  return TRUE;
1375 }
1376 
1377 /** Process STOCH section. */
1378 static
1380  SCIP* scip, /**< SCIP data structure */
1381  STOINPUT* stoi /**< sto input structure */
1382  )
1383 {
1384  assert(stoi != NULL);
1385 
1386  SCIPdebugMsg(scip, "read problem name\n");
1387 
1388  /* This has to be the Line with the NAME section. */
1389  if( !stoinputReadLine(stoi) || stoinputField0(stoi) == NULL || strcmp(stoinputField0(stoi), "STOCH") )
1390  {
1391  stoinputSyntaxerror(stoi);
1392  return SCIP_OKAY;
1393  }
1394 
1395  /* Sometimes the name is omitted. */
1396  stoinputSetProbname(stoi, (stoinputField1(stoi) == 0) ? "_STO_" : stoinputField1(stoi));
1397 
1398  /* This hat to be a new section */
1399  if( !stoinputReadLine(stoi) || (stoinputField0(stoi) == NULL) )
1400  {
1401  stoinputSyntaxerror(stoi);
1402  return SCIP_OKAY;
1403  }
1404 
1405  /* setting the stochatic information section */
1406  if( !strncmp(stoinputField0(stoi), "BLOCKS", 6) )
1408  else if( !strncmp(stoinputField0(stoi), "SCENARIOS", 9) )
1410  else if( !strncmp(stoinputField0(stoi), "INDEP", 5) )
1412  else
1413  {
1414  stoinputSyntaxerror(stoi);
1415  return SCIP_OKAY;
1416  }
1417 
1418  /* setting the stochastic information type */
1419  if( !strncmp(stoinputField1(stoi), "DISCRETE", 8) )
1421  else if( !strncmp(stoinputField1(stoi), "UNIFORM", 7) )
1423  else if( !strncmp(stoinputField1(stoi), "NORMAL", 6) )
1425  else if( !strncmp(stoinputField1(stoi), "SUB", 3) )
1427  else if( !strncmp(stoinputField1(stoi), "LINTR", 5) )
1429  else
1430  {
1431  stoinputSyntaxerror(stoi);
1432  return SCIP_OKAY;
1433  }
1434 
1435  return SCIP_OKAY;
1436 }
1437 
1438 /** Process BLOCKS section. */
1439 static
1441  STOINPUT* stoi, /**< sto input structure */
1442  SCIP* scip, /**< SCIP data structure */
1443  SCIP_READERDATA* readerdata /**< the reader data */
1444  )
1445 {
1446  STOSCENARIO*** blocks;
1447  int numblocks;
1448  int* numblocksperblock;
1449  int blockssize;
1450  int* blocksperblocksize;
1451  char BL[] = "BL";
1452  int blocknum;
1453  int blockindex;
1454  int i;
1455  int j;
1456  char stagenames[SCIP_MAXSTRLEN];
1457  int numstages;
1458 
1459  SCIPdebugMsg(scip, "read Blocks\n");
1460 
1461  /* This has to be the Line with the name. */
1462  if( stoinputField1(stoi) == NULL )
1463  {
1464  stoinputSyntaxerror(stoi);
1465  return SCIP_OKAY;
1466  }
1467 
1468  stoinputSetStochtype(stoi, stoinputField1(stoi));
1469 
1470  /* initializing the block data */
1471  numblocks = 0;
1472  blockssize = STO_DEFAULT_ARRAYSIZE;
1474  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &numblocksperblock, STO_DEFAULT_ARRAYSIZE) );
1475  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocksperblocksize, STO_DEFAULT_ARRAYSIZE) );
1476 
1477  blockindex = 0;
1478  blocknum = 0;
1479 
1480  /* initializing the stage names record */
1481  numstages = 0;
1482  (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "");
1483 
1484  while( stoinputReadLine(stoi) )
1485  {
1486  if( stoinputField0(stoi) != NULL )
1487  {
1488  if( !strcmp(stoinputField0(stoi), "BLOCKS") )
1489  {
1491  if( strcmp(stoinputField1(stoi), "DISCRETE") )
1492  {
1493  SCIPerrorMessage("Sorry, %s blocks stucture is not currently supported.\n", stoinputField1(stoi));
1494  SCIPerrorMessage("Only DISCRETE blocks are supported.\n");
1495  goto TERMINATE;
1496  }
1497  }
1498  else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1499  {
1500  SCIP_CALL( createScenariosFromBlocks(scip, readerdata, blocks, numblocks, numblocksperblock, numstages) );
1502  }
1503  else
1504  stoinputSyntaxerror(stoi);
1505 
1506  goto TERMINATE;
1507  }
1508 
1509  if( strcmp(stoinputField1(stoi), BL) == 0 )
1510  {
1511  SCIP_Bool foundblock = FALSE;
1512 
1513  /* checking whether the stage has been added previously */
1514  if( strstr(stagenames, stoinputField3(stoi)) == NULL )
1515  {
1516  /* recording the stage name as processed */
1517  (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "%s_%s", stagenames, stoinputField3(stoi));
1518  numstages++;
1519  }
1520 
1521  /* determining whether a block name has previously been added */
1522  for( i = 0; i < numblocks; i++ )
1523  {
1524  if( strcmp(getScenarioName(blocks[i][0]), stoinputField2(stoi)) == 0 )
1525  {
1526  foundblock = TRUE;
1527  break;
1528  }
1529  }
1530  blocknum = i;
1531 
1532  /* if the block is found, then the memory for the blocks array must be ensured */
1533  if( foundblock )
1534  {
1535  /* ensuring enough memory is available for the blocks */
1536  if( numblocksperblock[blocknum] + 1 > blocksperblocksize[blocknum] )
1537  {
1538  int newsize;
1539  newsize = SCIPcalcMemGrowSize(scip, numblocksperblock[blocknum] + 1);
1540  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum], newsize) ); /*lint !e866*/
1541  blocksperblocksize[blocknum] = newsize;
1542  }
1543  }
1544  else
1545  {
1546  /* ensuring enough memory is available for the blocks */
1547  if( numblocks + 1 > blockssize )
1548  {
1549  int newsize;
1550  newsize = SCIPcalcMemGrowSize(scip, numblocks + 1);
1551  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks, blockssize, newsize) );
1552  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &numblocksperblock, blockssize, newsize) );
1553  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocksperblocksize, blockssize, newsize) );
1554  blockssize = newsize;
1555  }
1556 
1557  blocksperblocksize[blocknum] = STO_DEFAULT_BLOCKARRAYSIZE;
1558  numblocksperblock[blocknum] = 0;
1559  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum]) );
1560  }
1561 
1562  blockindex = numblocksperblock[blocknum];
1563 
1564  /* creating the scenario data structure */
1565  SCIP_CALL( createScenarioData(scip, &blocks[blocknum][blockindex]) );
1566 
1567  SCIP_CALL( setScenarioName(scip, blocks[blocknum][blockindex], stoinputField2(stoi)) );
1568  SCIP_CALL( setScenarioStageName(scip, blocks[blocknum][blockindex], stoinputField3(stoi)) );
1569  SCIP_CALL( setScenarioProbability(scip, blocks[blocknum][blockindex], atof(stoinputField4(stoi))) );
1570  numblocksperblock[blocknum]++;
1571 
1572  if( !foundblock )
1573  numblocks++;
1574  }
1575  else
1576  {
1577  SCIP_CALL( addScenarioEntry(scip, blocks[blocknum][blockindex], stoinputField2(stoi), stoinputField1(stoi),
1578  atof(stoinputField3(stoi))) );
1579  }
1580  }
1581  stoinputSyntaxerror(stoi);
1582 
1583 TERMINATE:
1584 
1585  /* releasing the scenario data */
1586  for( i = numblocks - 1; i >= 0; i-- )
1587  {
1588  for( j = numblocksperblock[i] - 1; j >= 0; j-- )
1589  SCIP_CALL( freeScenarioTree(scip, &blocks[i][j]) );
1590  }
1591 
1592  for( i = numblocks - 1; i >= 0; i-- )
1593  SCIPfreeBlockMemoryArray(scip, &blocks[i], blocksperblocksize[i]);
1594  SCIPfreeBlockMemoryArray(scip, &blocksperblocksize, blockssize);
1595  SCIPfreeBlockMemoryArray(scip, &numblocksperblock, blockssize);
1596  SCIPfreeBlockMemoryArray(scip, &blocks, blockssize);
1597 
1598  return SCIP_OKAY;
1599 }
1600 
1601 
1602 /** Process SCENARIOS section. */
1603 static
1605  STOINPUT* stoi, /**< sto input structure */
1606  SCIP* scip, /**< SCIP data structure */
1607  SCIP_READERDATA* readerdata /**< the reader data */
1608  )
1609 {
1610  STOSCENARIO* scenario;
1611  char SC[] = "SC";
1612  char wrongroot[] = "\'ROOT\'";
1613  char parentname[SCIP_MAXSTRLEN];
1614  char scennames[SCIP_MAXSTRLEN];
1615  char tmpname[SCIP_MAXSTRLEN];
1616  int numscenarios;
1617  SCIP_Bool addscenario;
1618 
1619  SCIPdebugMsg(scip, "read SCENARIOS\n");
1620 
1621  /* This has to be the Line with the name. */
1622  if( stoinputField1(stoi) == NULL )
1623  {
1624  stoinputSyntaxerror(stoi);
1625  return SCIP_OKAY;
1626  }
1627 
1628  stoinputSetStochtype(stoi, stoinputField1(stoi));
1629 
1630  /* initializing the scen names record */
1631  numscenarios = 0;
1632  (void) SCIPsnprintf(scennames, SCIP_MAXSTRLEN, "ROOT");
1633 
1634  scenario = NULL;
1635  addscenario = FALSE;
1636 
1637  /* initializing the root scenario in the reader data */
1638  SCIP_CALL( setScenarioNum(scip, readerdata->scenariotree, 0) );
1639  SCIP_CALL( setScenarioStageNum(scip, readerdata->scenariotree, 0) );
1640 
1641  while( stoinputReadLine(stoi) )
1642  {
1643  if( stoinputField0(stoi) != NULL )
1644  {
1645  /* if a scenario has been created that needs to be added to the scenario tree */
1646  if( addscenario )
1647  {
1648  SCIP_CALL( insertScenarioInReaderdata(scip, readerdata, scenario, parentname) );
1649 
1650  /* freeing the scenario */
1651  SCIP_CALL( freeScenarioTree(scip, &scenario) );
1652  }
1653 
1654  if( !strcmp(stoinputField0(stoi), "SCENARIOS") )
1655  {
1657  if( strcmp(stoinputField1(stoi), "DISCRETE") )
1658  {
1659  SCIPerrorMessage("Sorry, %s scenarios is not currently supported.\n", stoinputField1(stoi));
1660  SCIPerrorMessage("Only DISCRETE scenarios are supported.\n");
1661  goto TERMINATE;
1662  }
1663  }
1664  else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1666  else
1667  stoinputSyntaxerror(stoi);
1668 
1669  goto TERMINATE;
1670  }
1671 
1672  if( strcmp(stoinputField1(stoi), SC) == 0 )
1673  {
1674  int stagenum;
1675 
1676  /* if a scenario has been created that needs to be added to the scenario tree */
1677  if( addscenario )
1678  {
1679  SCIP_CALL( insertScenarioInReaderdata(scip, readerdata, scenario, parentname) );
1680 
1681  /* freeing the scenario */
1682  SCIP_CALL( freeScenarioTree(scip, &scenario) );
1683  assert(scenario == NULL);
1684  }
1685 
1686  if( strcmp(wrongroot, stoinputField3(stoi)) == 0 )
1687  (void) SCIPsnprintf(parentname, SCIP_MAXSTRLEN, "%s", "ROOT");
1688  else
1689  (void) SCIPsnprintf(parentname, SCIP_MAXSTRLEN, "%s", stoinputField3(stoi));
1690 
1691  /* checking whether the stage has been added previously */
1692  if( strstr(scennames, stoinputField2(stoi)) == NULL )
1693  {
1694  /* recording the stage name as processed */
1695  (void) SCIPsnprintf(tmpname, SCIP_MAXSTRLEN, "%s_%s", scennames, stoinputField2(stoi));
1696  (void) SCIPsnprintf(scennames, SCIP_MAXSTRLEN, "%s", tmpname);
1697  }
1698 
1699  /* checking whether the "common" scenario has been added yet */
1700  if( strstr(scennames, parentname) == NULL )
1701  {
1702  SCIPerrorMessage("Scenario <%s> needs to be read before scenario <%s>\n", parentname, stoinputField2(stoi));
1703  stoinputSyntaxerror(stoi);
1704  goto TERMINATE;
1705  }
1706 
1707  /* the "common" scenario has been added before, so a child can be added to the scenario tree */
1708  SCIP_CALL( createScenarioData(scip, &scenario) );
1709 
1710  SCIP_CALL( setScenarioName(scip, scenario, stoinputField2(stoi)) );
1711  SCIP_CALL( setScenarioStageName(scip, scenario, stoinputField5(stoi)) );
1712  SCIP_CALL( setScenarioNum(scip, scenario, numscenarios) );
1713 
1714  stagenum = SCIPtimFindStage(scip, stoinputField5(stoi));
1715  if( stagenum < 0 )
1716  {
1717  stoinputSyntaxerror(stoi);
1718  goto TERMINATE;
1719  }
1720  SCIP_CALL( setScenarioStageNum(scip, scenario, stagenum) );
1721  SCIP_CALL( setScenarioProbability(scip, scenario, atof(stoinputField4(stoi))) );
1722  if( stoinputField6(stoi) != NULL )
1723  {
1724  SCIP_CALL( setScenarioLowerbound(scip, scenario, atof(stoinputField6(stoi))) );
1725  }
1726 
1727  numscenarios++;
1728  addscenario = TRUE;
1729  }
1730  else if( addscenario )
1731  {
1732  SCIP_CALL( addScenarioEntry(scip, scenario, stoinputField2(stoi), stoinputField1(stoi),
1733  atof(stoinputField3(stoi))) );
1734  }
1735  }
1736  stoinputSyntaxerror(stoi);
1737 
1738 TERMINATE:
1739 
1740  return SCIP_OKAY;
1741 }
1742 
1743 
1744 /** Process INDEP section. */
1745 static
1747  STOINPUT* stoi, /**< sto input structure */
1748  SCIP* scip, /**< SCIP data structure */
1749  SCIP_READERDATA* readerdata /**< the reader data */
1750  )
1751 {
1752  STOSCENARIO*** blocks;
1753  int numblocks;
1754  int* numblocksperblock;
1755  int blockssize;
1756  int* blocksperblocksize;
1757  int blocknum;
1758  int blockindex;
1759  int i;
1760  int j;
1761  char stagenames[SCIP_MAXSTRLEN];
1762  int numstages;
1763  SCIP_Bool foundblock;
1764 
1765  SCIP_Real probability;
1766  char currstagename[SCIP_MAXSTRLEN];
1767 
1768  SCIPdebugMsg(scip, "read Indep\n");
1769 
1770  /* This has to be the Line with the name. */
1771  if( stoinputField1(stoi) == NULL )
1772  {
1773  stoinputSyntaxerror(stoi);
1774  return SCIP_OKAY;
1775  }
1776 
1777  stoinputSetStochtype(stoi, stoinputField1(stoi));
1778 
1779  /* initializing the block data */
1780  numblocks = 0;
1781  blockssize = STO_DEFAULT_ARRAYSIZE;
1783  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &numblocksperblock, STO_DEFAULT_ARRAYSIZE) );
1784  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocksperblocksize, STO_DEFAULT_ARRAYSIZE) );
1785 
1786  /* initializing the stage names record */
1787  numstages = 0;
1788  (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "");
1789 
1790  while( stoinputReadLine(stoi) )
1791  {
1792  if( stoinputField0(stoi) != NULL )
1793  {
1794  if( !strcmp(stoinputField0(stoi), "INDEP") )
1795  {
1797  }
1798  else if( !strcmp(stoinputField0(stoi), "ENDATA") )
1799  {
1800  SCIP_CALL( createScenariosFromBlocks(scip, readerdata, blocks, numblocks, numblocksperblock, numstages) );
1802  }
1803  else
1804  stoinputSyntaxerror(stoi);
1805 
1806  goto TERMINATE;
1807  }
1808 
1809  /* if the 5th input is NULL, then the 4th input is the probability. Otherwise, the 4th input is the stage name and
1810  * the 5th input is the probability. The stage name is redundant information, but sometimes included for more
1811  * information.
1812  */
1813  if( stoinputField5(stoi) == NULL )
1814  {
1815  probability = atof(stoinputField4(stoi));
1816  (void) SCIPsnprintf(currstagename, SCIP_MAXSTRLEN, "%s", SCIPtimConsGetStageName(scip, stoinputField2(stoi)));
1817  }
1818  else
1819  {
1820  probability = atof(stoinputField5(stoi));
1821  (void) SCIPsnprintf(currstagename, SCIP_MAXSTRLEN, "%s", stoinputField4(stoi));
1822  }
1823 
1824  /* checking whether the stage has been added previously */
1825  if( strstr(stagenames, currstagename) == NULL )
1826  {
1827  /* recording the stage name as processed */
1828  (void) SCIPsnprintf(stagenames, SCIP_MAXSTRLEN, "%s_%s", stagenames, currstagename);
1829 
1830  numstages++;
1831  }
1832 
1833  foundblock = FALSE;
1834 
1835  /* determining whether a block name has previously been added */
1836  for( i = 0; i < numblocks; i++ )
1837  {
1838  if( strcmp(getScenarioName(blocks[i][0]), stoinputField2(stoi)) == 0 )
1839  {
1840  foundblock = TRUE;
1841  break;
1842  }
1843  }
1844  blocknum = i;
1845 
1846  /* if the block is found, then the memory for the blocks array must be ensured */
1847  if( foundblock )
1848  {
1849  /* ensuring enough memory is available for the blocks */
1850  if( numblocksperblock[blocknum] + 1 > blocksperblocksize[blocknum] )
1851  {
1852  int newsize;
1853  newsize = SCIPcalcMemGrowSize(scip, numblocksperblock[blocknum] + 1);
1854  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum], newsize) ); /*lint !e866*/
1855  blocksperblocksize[blocknum] = newsize;
1856  }
1857  }
1858  else
1859  {
1860  /* ensuring enough memory is available for the blocks */
1861  if( numblocks + 1 > blockssize )
1862  {
1863  int newsize;
1864  newsize = SCIPcalcMemGrowSize(scip, numblocks + 1);
1865  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocks, blockssize, newsize) );
1866  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &numblocksperblock, blockssize, newsize) );
1867  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &blocksperblocksize, blockssize, newsize) );
1868  blockssize = newsize;
1869  }
1870 
1871  blocksperblocksize[blocknum] = STO_DEFAULT_BLOCKARRAYSIZE;
1872  numblocksperblock[blocknum] = 0;
1873  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &blocks[blocknum], blocksperblocksize[blocknum]) );
1874  }
1875 
1876  blockindex = numblocksperblock[blocknum];
1877 
1878  /* creating the scenario data structure */
1879  SCIP_CALL( createScenarioData(scip, &blocks[blocknum][blockindex]) );
1880 
1881  SCIP_CALL( setScenarioName(scip, blocks[blocknum][blockindex], stoinputField2(stoi)) );
1882  SCIP_CALL( setScenarioStageName(scip, blocks[blocknum][blockindex], currstagename) );
1883  SCIP_CALL( setScenarioProbability(scip, blocks[blocknum][blockindex], probability) );
1884  numblocksperblock[blocknum]++;
1885 
1886  if( !foundblock )
1887  numblocks++;
1888 
1889  SCIP_CALL( addScenarioEntry(scip, blocks[blocknum][blockindex], stoinputField2(stoi), stoinputField1(stoi),
1890  atof(stoinputField3(stoi))) );
1891  }
1892  stoinputSyntaxerror(stoi);
1893 
1894 TERMINATE:
1895 
1896  /* releasing the scenario data */
1897  for( i = numblocks - 1; i >= 0; i-- )
1898  {
1899  for( j = numblocksperblock[i] - 1; j >= 0; j-- )
1900  SCIP_CALL( freeScenarioTree(scip, &blocks[i][j]) );
1901  }
1902 
1903  for( i = numblocks - 1; i >= 0; i-- )
1904  SCIPfreeBlockMemoryArray(scip, &blocks[i], blocksperblocksize[i]);
1905  SCIPfreeBlockMemoryArray(scip, &blocksperblocksize, blockssize);
1906  SCIPfreeBlockMemoryArray(scip, &numblocksperblock, blockssize);
1907  SCIPfreeBlockMemoryArray(scip, &blocks, blockssize);
1908 
1909  return SCIP_OKAY;
1910 }
1911 
1912 
1913 /** computes the probability of a scenario */
1914 static
1916  SCIP* scip, /**< the SCIP data structure */
1917  STOSCENARIO* scenario /**< the current scenario */
1918  )
1919 {
1920  STOSCENARIO* checkscen;
1921  SCIP_Real probability;
1922 
1923  assert(scip != NULL);
1924  assert(scenario != NULL);
1925 
1926  /* computing the probability for the scenario */
1927  checkscen = scenario;
1928  probability = 1;
1929  while( checkscen != NULL )
1930  {
1931  probability *= getScenarioProbability(scip, checkscen);
1932  checkscen = getScenarioParent(checkscen);
1933  }
1934 
1935  return probability;
1936 }
1937 
1938 /** gets the variable name */
1939 static
1941  char* name, /**< the name to be returned */
1942  const char* varname, /**< the root of the variable name */
1943  int stagenum, /**< the stage number */
1944  int scenarionum /**< the scenario number */
1945  )
1946 {
1947  if( stagenum < 0 )
1948  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_00_%d", varname, scenarionum);
1949  else
1950  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_%d", varname, stagenum, scenarionum);
1951 }
1952 
1953 
1954 /** add variables to the scenario */
1955 static
1957  SCIP* scip, /**< the SCIP data structure */
1958  STOSCENARIO* scenario, /**< the current scenario */
1959  SCIP_HASHMAP* varmap, /**< the variable map from the original to the subproblem variables */
1960  SCIP_VAR** vars, /**< the variables of the core problem associated with this scenario */
1961  int nvars /**< the number of variables for this scenario */
1962  )
1963 {
1964  SCIP_Real probability;
1965  int i;
1966  char name[SCIP_MAXSTRLEN];
1967 
1968  assert(scip != NULL);
1969  assert(scenario != NULL);
1970  assert(vars != NULL);
1971 
1972  /* computing the probability for the scenario */
1973  probability = computeScenarioProbability(scip, scenario);
1974 
1975  for( i = 0; i < nvars; i++ )
1976  {
1977  SCIP_VAR* var;
1978  SCIP_Real obj;
1979  SCIP_VARTYPE vartype;
1980 
1981  SCIPdebugMessage("Original problem variable <%s> is being duplicated for scenario %d\n", SCIPvarGetName(vars[i]),
1982  getScenarioNum(scip, scenario));
1983 
1984  if( SCIPvarIsDeleted(vars[i]) )
1985  continue;
1986 
1987  obj = SCIPvarGetObj(vars[i])*probability;
1988 
1989  vartype = SCIPvarGetType(vars[i]);
1990 #if 0
1991  if( getScenarioStageNum(scip, scenario) == 0 )
1992  vartype = SCIPvarGetType(vars[i]);
1993  else
1994  vartype = SCIP_VARTYPE_CONTINUOUS;
1995 #endif
1996 
1997  /* creating a variable as a copy of the original variable. */
1998  getScenarioEntityName(name, SCIPvarGetName(vars[i]), getScenarioStageNum(scip, scenario), getScenarioNum(scip, scenario));
1999  SCIP_CALL( SCIPcreateVar(scip, &var, name, SCIPvarGetLbOriginal(vars[i]), SCIPvarGetUbOriginal(vars[i]),
2000  obj, vartype, SCIPvarIsInitial(vars[i]), SCIPvarIsRemovable(vars[i]), NULL, NULL, NULL,
2001  NULL, NULL) );
2002 
2003  SCIPdebugMessage("Adding variable <%s>\n", name);
2004 
2005  SCIP_CALL( SCIPaddVar(scip, var) );
2006 
2007  /* inserting the scenario variable into the hashmap */
2008  SCIP_CALL( SCIPhashmapInsert(varmap, vars[i], var) );
2009 
2010  SCIP_CALL( SCIPreleaseVar(scip, &var) );
2011  }
2012 
2013  return SCIP_OKAY;
2014 }
2015 
2016 
2017 /** finds the scenario variable to add to a constraint */
2018 static
2020  SCIP* scip, /**< the SCIP data structure */
2021  STOSCENARIO* scenario, /**< the current scenario */
2022  SCIP_VAR* consvar, /**< the variable in the constraint that is being searched for */
2023  SCIP_VAR** scenariovar /**< pointer to return the variable to be added to the constraint */
2024  )
2025 {
2026  STOSCENARIO* checkscen;
2027  char varname[SCIP_MAXSTRLEN];
2028 
2029  assert(scip != NULL);
2030  assert(scenario != NULL);
2031  assert(consvar != NULL);
2032  assert(scenariovar != NULL);
2033 
2034  (*scenariovar) = NULL;
2035 
2036  checkscen = scenario;
2037 
2038  /* NOTE: if the variable does not exist, then we need to search the preceding scenarios. In the case of
2039  * decomposition, then we only check the preceding scenario. As such, a check count is used to limit the number
2040  * of scenario checks. */
2041  while( (*scenariovar) == NULL )
2042  {
2043  assert(checkscen != NULL);
2044  if( getScenarioStageNum(scip, checkscen) == 0 )
2045  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(consvar));
2046  else
2047  getScenarioEntityName(varname, SCIPvarGetName(consvar), getScenarioStageNum(scip, checkscen),
2048  getScenarioNum(scip, checkscen));
2049 
2050  (*scenariovar) = SCIPfindVar(scip, varname);
2051 
2052  checkscen = getScenarioParent(checkscen);
2053  }
2054 
2055  if( (*scenariovar) == NULL )
2056  {
2057  SCIPerrorMessage("There is no scenario variable could be found.\n");
2058  return SCIP_READERROR;
2059  }
2060 
2061  return SCIP_OKAY;
2062 }
2063 
2064 
2065 /** create variable for the decomposed scenario */
2066 static
2068  SCIP* scip, /**< the SCIP data structure */
2069  STOSCENARIO* scenario, /**< the current scenario */
2070  SCIP_VAR* consvar, /**< the variable in the constraint that is being searched for */
2071  SCIP_VAR** scenariovar, /**< pointer to return the variable to be added to the constraint */
2072  SCIP_Bool* varadded /**< pointer to indicate whether a variable has been added */
2073  )
2074 {
2075  STOSCENARIO* checkscen;
2076  SCIP_VAR* searchvar;
2077  int checkcount;
2078  char varname[SCIP_MAXSTRLEN];
2079 
2080  assert(scip != NULL);
2081  assert(scenario != NULL);
2082  assert(consvar != NULL);
2083 
2084  (*varadded) = FALSE;
2085 
2086  /* finding the scenario that the consvar belongs to */
2087  checkscen = scenario;
2088  searchvar = NULL;
2089  checkcount = 0;
2090  while( searchvar == NULL && checkcount < 2 )
2091  {
2092  assert(checkscen != NULL);
2093  if( getScenarioStageNum(scip, checkscen) == 0 )
2094  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(consvar));
2095  else
2096  getScenarioEntityName(varname, SCIPvarGetName(consvar), getScenarioStageNum(scip, checkscen),
2097  getScenarioNum(scip, checkscen));
2098 
2099  /* first checking whether the variable is included in the scenario */
2100  searchvar = SCIPfindVar(scip, varname);
2101  if( searchvar != NULL )
2102  {
2103  (*scenariovar) = searchvar;
2104  return SCIP_OKAY;
2105  }
2106 
2107  searchvar = SCIPfindVar(getScenarioScip(checkscen), varname);
2108 
2109  checkscen = getScenarioParent(checkscen);
2110  checkcount++;
2111  }
2112 
2113  if( searchvar != NULL )
2114  {
2115  SCIP_VAR* var;
2116  /* creating a variable as a copy of the original variable. */
2117  SCIP_CALL( SCIPcreateVar(scip, &var, varname, SCIPvarGetLbOriginal(searchvar), SCIPvarGetUbOriginal(searchvar),
2118  0.0, SCIPvarGetType(searchvar), SCIPvarIsInitial(searchvar), SCIPvarIsRemovable(searchvar), NULL, NULL,
2119  NULL, NULL, NULL) );
2120 
2121  SCIP_CALL( SCIPaddVar(scip, var) );
2122 
2123  (*scenariovar) = var;
2124  (*varadded) = TRUE;
2125  }
2126 
2127  return SCIP_OKAY;
2128 }
2129 
2130 
2131 /** adds the constraint to the scenario problem */
2132 static
2134  SCIP* scip, /**< the SCIP data structure */
2135  SCIP* scenarioscip, /**< the scenario SCIP data structure */
2136  STOSCENARIO* scenario, /**< the current scenario */
2137  SCIP_HASHMAP* varmap, /**< the variable map from the original to the subproblem variables */
2138  SCIP_CONS** conss, /**< the constraints of the core problem associated with this scenario */
2139  int nconss, /**< the number of constraints for this scenario */
2140  SCIP_Bool decomp /**< is the problem being decomposed */
2141  )
2142 {
2143  int i;
2144  int j;
2145  char name[SCIP_MAXSTRLEN];
2146  SCIP_Bool varadded;
2147 
2148  assert(scip != NULL);
2149  assert(scenarioscip != NULL);
2150  assert(scenario != NULL);
2151  assert(conss != NULL);
2152 
2153  /* Add constraints */
2154  /* NOTE: It is assumed that the problems only have linear constraints */
2155  for( i = 0; i < nconss; i++ )
2156  {
2157  SCIP_CONS* cons;
2158  SCIP_VAR** consvars = NULL;
2159  int nconsvars;
2160  SCIP_Bool success;
2161 
2162  if( SCIPconsIsDeleted(conss[i]) )
2163  continue;
2164 
2165  /* getting the number of variables in the constraints */
2166  SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &nconsvars, &success) );
2167 
2168  if( success )
2169  {
2170  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nconsvars) );
2171  SCIP_CALL( SCIPgetConsVars(scip, conss[i], consvars, nconsvars, &success) );
2172  }
2173 
2174  /* if the get variable callback is not implemented for the constraint, then the success flag will be returned as
2175  * FALSE. In this case, it is not possible to build the stochastic program, so an error will be returned
2176  */
2177  if( !success )
2178  {
2179  SCIPerrorMessage("It is not possible to copy constraint <%s>. The stochastic program can not be built.\n",
2180  SCIPconsGetName(conss[i]));
2181 
2182  /* freeing buffer memory */
2183  SCIPfreeBufferArrayNull(scip, consvars);
2184 
2185  return SCIP_READERROR;
2186  }
2187 
2188  assert(consvars != NULL);
2189  for( j = 0; j < nconsvars; j++ )
2190  {
2191  SCIP_VAR* scenariovar;
2192 
2193  scenariovar = NULL;
2194 
2195  varadded = FALSE;
2196 
2197  if( decomp )
2198  SCIP_CALL( getScenarioDecompVar(scenarioscip, scenario, consvars[j], &scenariovar, &varadded) );
2199  else
2200  SCIP_CALL( findScenarioVar(scenarioscip, scenario, consvars[j], &scenariovar) );
2201 
2202  if( scenariovar != NULL )
2203  {
2204  /* checking whether the variable is in the variable hashmap. If it doesn't exist, then it is added to the
2205  * variable hashmap
2206  */
2207  if( !SCIPhashmapExists(varmap, consvars[j]) )
2208  {
2209  SCIP_CALL( SCIPhashmapInsert(varmap, consvars[j], scenariovar) );
2210  }
2211  }
2212 
2213  if( varadded )
2214  {
2215  SCIP_CALL( SCIPreleaseVar(scenarioscip, &scenariovar) );
2216  }
2217  }
2218 
2219  /* creating a linear constraint as a copy of the original constraint. */
2220  getScenarioEntityName(name, SCIPconsGetName(conss[i]), getScenarioStageNum(scip, scenario), getScenarioNum(scip, scenario));
2221 
2222  /* copying the constraint from the original SCIP to the stochastic program */
2223  SCIP_CALL( SCIPgetConsCopy(scip, scenarioscip, conss[i], &cons, SCIPconsGetHdlr(conss[i]), varmap, NULL, name,
2224  SCIPconsIsInitial(conss[i]), SCIPconsIsSeparated(conss[i]), SCIPconsIsEnforced(conss[i]),
2225  SCIPconsIsChecked(conss[i]), SCIPconsIsMarkedPropagate(conss[i]), SCIPconsIsLocal(conss[i]),
2226  SCIPconsIsModifiable(conss[i]), SCIPconsIsDynamic(conss[i]), SCIPconsIsRemovable(conss[i]),
2227  SCIPconsIsStickingAtNode(conss[i]), TRUE, &success) );
2228 
2229  /* freeing the cons vars buffer array */
2230  SCIPfreeBufferArray(scip, &consvars);
2231 
2232  /* if the copy failed, then the scenarios can not be created. */
2233  if( !success )
2234  {
2235  SCIPerrorMessage("It is not possible to copy constraint <%s>. The stochastic program can not be built.\n",
2236  SCIPconsGetName(conss[i]));
2237  return SCIP_READERROR;
2238  }
2239 
2240  SCIP_CALL( SCIPaddCons(scenarioscip, cons) );
2241  SCIP_CALL( SCIPreleaseCons(scenarioscip, &cons) );
2242  }
2243 
2244  return SCIP_OKAY;
2245 }
2246 
2247 /** add variables and constraint to problem */
2248 static
2250  SCIP* scip, /**< the SCIP data structure of master problem */
2251  STOSCENARIO* scenario, /**< the current scenario */
2252  SCIP_Bool decomp /**< is the problem being decomposed */
2253  )
2254 {
2255  SCIP* scenarioscip;
2256  SCIP_BENDERS* benders;
2257  SCIP_HASHMAP* varmap;
2258  SCIP_CONS** conss;
2259  SCIP_VAR** vars;
2260  SCIP_Real probability;
2261  int nconss;
2262  int nvars;
2263  int nmastervars;
2264  int nentries;
2265  int stagenum;
2266  int i;
2267  char name[SCIP_MAXSTRLEN];
2268 
2269  assert(scip != NULL);
2270  assert(scenario != NULL);
2271 
2272  stagenum = SCIPtimFindStage(scip, getScenarioStageName(scip, scenario));
2273  if( stagenum < 0 || stagenum >= SCIPtimGetNStages(scip) )
2274  {
2275  SCIPerrorMessage("Unable to build stochastic program - stage <%s> was not found\n",
2276  getScenarioStageName(scip, scenario));
2277  return SCIP_READERROR;
2278  }
2279 
2280  SCIPdebugMessage("Creating scenario at stage <%d>. Scenario: %d Stage: %d\n", stagenum, getScenarioNum(scip, scenario),
2281  getScenarioStageNum(scip, scenario));
2282 
2283  conss = SCIPtimGetStageConss(scip, stagenum);
2284  nconss = SCIPtimGetStageNConss(scip, stagenum);
2285  vars = SCIPtimGetStageVars(scip, stagenum);
2286  nvars = SCIPtimGetStageNVars(scip, stagenum);
2287 
2288  nmastervars = SCIPgetNVars(scip);
2289 
2290  /* this if 0 will be removed when the stochastic reader is merged with the Benders' branch */
2291  if( decomp )
2292  {
2293  SCIP_CALL( SCIPcreate(&scenarioscip) );
2294 
2295  getScenarioEntityName(name, SCIPgetProbName(scip), getScenarioStageNum(scip, scenario), getScenarioNum(scip, scenario));
2296 
2297  /* creating the problem */
2298  SCIP_CALL( SCIPcreateProbBasic(scenarioscip, name) );
2299 
2300  /* we explicitly enable the use of a debug solution for this main SCIP instance */
2301  SCIPenableDebugSol(scenarioscip);
2302 
2303  /* include default SCIP plugins */
2304  SCIP_CALL( SCIPincludeDefaultPlugins(scenarioscip) );
2305 
2306  /* activating the Benders' constraint handler for the scenario stages.
2307  * TODO: consider whether the two-phase method should be activated by default in the scenario stages.
2308  */
2309  SCIP_CALL( SCIPsetBoolParam(scenarioscip, "constraints/benders/active", TRUE) );
2310 
2311  /* allocating memory for the subproblems */
2312  if( getScenarioNChildren(scenario) > 0 )
2313  SCIP_CALL( createScenarioSubproblemArray(scip, scenario) );
2314  }
2315  else
2316  scenarioscip = scip;
2317 
2318  /* adding the scenarioscip to the scenario */
2319  setScenarioScip(scenario, scenarioscip);
2320 
2321  /* creating the variable hashmap to copy the constraints */
2322  SCIP_CALL( SCIPhashmapCreate(&varmap, SCIPblkmem(scenarioscip), nmastervars) );
2323 
2324  /* adding the variables to the scenario */
2325  SCIP_CALL( addScenarioVarsToProb(scenarioscip, scenario, varmap, vars, nvars) );
2326 
2327  /* adding the constraints to the scenario */
2328  SCIP_CALL( addScenarioConsToProb(scip, scenarioscip, scenario, varmap, conss, nconss, decomp) );
2329 
2330  /* destroying the hashmap */
2331  SCIPhashmapFree(&varmap);
2332 
2333  /* add the variables and constraints of the child scenarios */
2334  for( i = 0; i < getScenarioNChildren(scenario); i++ )
2335  {
2336  /* the master SCIP is always passed to the recursive function. The scenario SCIP instances are generated in the
2337  * function call. */
2338  SCIP_CALL( addScenarioVarsAndConsToProb(scip, getScenarioChild(scenario, i), decomp) );
2339  if( decomp )
2340  addScenarioSubproblem(scenario, getScenarioScip(getScenarioChild(scenario, i)));
2341  }
2342 
2343  /* adding the Benders' decomposition */
2344  if( decomp && getScenarioNChildren(scenario) > 0 )
2345  {
2347 
2348  /* getting the default Benders' decomposition */
2349  benders = SCIPfindBenders(scenarioscip, "default");
2350 
2351  /* updating the lower bounds for the subproblems */
2352  for( i = 0; i < getScenarioNChildren(scenario); i++ )
2354  getScenarioLowerbound(scenarioscip, getScenarioChild(scenario, i)));
2355  }
2356 
2357  /* computing the probability for the scenario */
2358  probability = computeScenarioProbability(scenarioscip, scenario);
2359 
2360  /* change the constraints for the given scenario */
2361  nentries = getScenarioNEntries(scenario);
2362  for( i = 0; i < nentries; i++ )
2363  {
2364  SCIP_CONS* cons;
2365  SCIP_VAR* var;
2366  char RHS[] = "RHS";
2367  char rhs[] = "rhs";
2368  char RIGHT[] = "RIGHT";
2369  char MINI[] = "MINI";
2370  char obj[] = "obj";
2371  char OBJ[] = "OBJ";
2372 
2373  /* finding the constraint associated with the row */
2374  getScenarioEntityName(name, getScenarioEntryRow(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2375  getScenarioNum(scenarioscip, scenario));
2376  cons = SCIPfindCons(scenarioscip, name);
2377 
2378  if( strncmp(getScenarioEntryCol(scenario, i), RHS, 3) == 0 ||
2379  strncmp(getScenarioEntryCol(scenario, i), rhs, 3) == 0 ||
2380  strcmp(getScenarioEntryCol(scenario, i), RIGHT) == 0 )
2381  {
2382  /* if the constraint is NULL, then it is not possible to make any changes to the scenario */
2383  if( cons == NULL )
2384  {
2385  SCIPerrorMessage("There is no constraint <%s> in the current scenario.\n", name);
2386  return SCIP_READERROR;
2387  }
2388 
2389  /* if the constraint is an equality constraint, then the LHS must also be changed */
2390  if( SCIPgetLhsLinear(scenarioscip, cons) >= SCIPgetRhsLinear(scenarioscip, cons) )
2391  {
2392  SCIP_CALL( SCIPchgLhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2393  SCIP_CALL( SCIPchgRhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2394  }
2395  else if( SCIPisLT(scenarioscip, SCIPgetRhsLinear(scenarioscip, cons), SCIPinfinity(scenarioscip)) )
2396  SCIP_CALL( SCIPchgRhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2397  else if( SCIPisLT(scenarioscip, SCIPgetLhsLinear(scenarioscip, cons), SCIPinfinity(scenarioscip)) )
2398  SCIP_CALL( SCIPchgLhsLinear(scenarioscip, cons, getScenarioEntryValue(scenario, i)) );
2399  }
2400  else if( strstr(getScenarioEntryRow(scenario, i), MINI) != NULL ||
2401  strstr(getScenarioEntryRow(scenario, i), obj) != NULL ||
2402  strstr(getScenarioEntryRow(scenario, i), OBJ) != NULL )
2403  {
2404  /* finding the variable associated with the column */
2405  getScenarioEntityName(name, getScenarioEntryCol(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2406  getScenarioNum(scenarioscip, scenario));
2407  var = SCIPfindVar(scenarioscip, name);
2408 
2409  /* changing the coefficient for the variable */
2410  if( var == NULL )
2411  {
2412  SCIPerrorMessage("There is no variable <%s> in the current scenario.\n", name);
2413  return SCIP_READERROR;
2414  }
2415  else
2416  {
2417  SCIP_CALL( SCIPchgVarObj(scenarioscip, var, getScenarioEntryValue(scenario, i)*probability) );
2418  }
2419  }
2420  else
2421  {
2422  /* if the constraint is NULL, then it is not possible to make any changes to the scenario */
2423  if( cons == NULL )
2424  {
2425  SCIPerrorMessage("There is no constraint <%s> in the current scenario.\n", name);
2426  return SCIP_READERROR;
2427  }
2428 
2429  /* finding the variable associated with the column */
2430  getScenarioEntityName(name, getScenarioEntryCol(scenario, i), getScenarioStageNum(scenarioscip, scenario),
2431  getScenarioNum(scenarioscip, scenario));
2432  var = SCIPfindVar(scenarioscip, name);
2433 
2434  if( var == NULL )
2435  {
2436  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s", getScenarioEntryCol(scenario, i));
2437  var = SCIPfindVar(scenarioscip, name);
2438  }
2439 
2440  /* changing the coefficient for the variable */
2441  if( var == NULL )
2442  {
2443  SCIPerrorMessage("There is no variable <%s> in the current scenario.\n", name);
2444  return SCIP_READERROR;
2445  }
2446  else
2447  {
2448  SCIP_CALL( SCIPchgCoefLinear(scenarioscip, cons, var, getScenarioEntryValue(scenario, i)) );
2449  }
2450  }
2451  }
2452 
2453  return SCIP_OKAY;
2454 }
2455 
2456 /** removes the core variables and constriants for stage 2 and lower */
2457 static
2459  SCIP* scip /**< the SCIP data structure */
2460  )
2461 {
2462  SCIP_CONS** conss;
2463  SCIP_VAR** vars;
2464  int nconss;
2465  int nvars;
2466  int numstages;
2467  int i;
2468  int j;
2469  SCIP_Bool deleted;
2470 
2471  assert(scip != NULL);
2472 
2473  numstages = SCIPtimGetNStages(scip);
2474 
2475  /* looping through all stages to remove the variables and constraints. The first stage is not removed as these are
2476  * part of the complete problem */
2477  for( i = 1; i < numstages; i++ )
2478  {
2479  conss = SCIPtimGetStageConss(scip, i);
2480  vars = SCIPtimGetStageVars(scip, i);
2481  nconss = SCIPtimGetStageNConss(scip, i);
2482  nvars = SCIPtimGetStageNVars(scip, i);
2483 
2484  /* removing constriants */
2485  for( j = 0; j < nconss; j++ )
2486  {
2487  if( !SCIPconsIsDeleted(conss[j]) )
2488  SCIP_CALL( SCIPdelCons(scip, conss[j]) );
2489  }
2490 
2491  /* removing variables */
2492  for( j = 0; j < nvars; j++ )
2493  {
2494  if( !SCIPvarIsDeleted(vars[j]) )
2495  {
2496  SCIP_CALL( SCIPdelVar(scip, vars[j], &deleted) );
2497  assert(deleted);
2498  }
2499  }
2500  }
2501 
2502  return SCIP_OKAY;
2503 }
2504 
2505 
2506 /* build the stochastic program completely as a MIP, i.e. no decomposition */
2507 static
2509  SCIP* scip, /**< the SCIP data structure */
2510  SCIP_READERDATA* readerdata /**< the reader data */
2511  )
2512 {
2513  int i;
2514 
2515  assert(scip != NULL);
2516  assert(readerdata != NULL);
2517 
2518  /* adding all variables and constraints for stages below the first stage.
2519  * The first stage is covered by the original problem. */
2520  for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2521  SCIP_CALL( addScenarioVarsAndConsToProb(scip, getScenarioChild(readerdata->scenariotree, i), FALSE) );
2522 
2523  /* removing the variable and constraints that were included as part of the core file */
2525 
2526  return SCIP_OKAY;
2527 }
2528 
2529 
2530 /** builds the stochastic program using Benders' decomposition */
2531 static
2533  SCIP* scip, /**< the SCIP data structure */
2534  SCIP_READERDATA* readerdata /**< the reader data */
2535  )
2536 {
2537  SCIP_BENDERS* benders;
2538  int i;
2539 
2540  assert(scip != NULL);
2541  assert(readerdata != NULL);
2542 
2543  SCIP_CALL( createScenarioSubproblemArray(scip, readerdata->scenariotree) );
2544 
2545  /* activating the Benders' constraint handler. The two-phase method is activated by default. If the user desires not
2546  * to use the two-phase method, then the setting in cons_benderslp must be explicitly changed.
2547  */
2548  SCIP_CALL( SCIPsetBoolParam(scip, "constraints/benders/active", TRUE) );
2549 
2550  setScenarioScip(readerdata->scenariotree, scip);
2551 
2552  /* adding all variables and constraints for stages below the first stage.
2553  * The first stage is covered by the original problem. */
2554  for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2555  {
2556  SCIP_CALL( addScenarioVarsAndConsToProb(scip, getScenarioChild(readerdata->scenariotree, i), TRUE) );
2557  addScenarioSubproblem(readerdata->scenariotree, getScenarioScip(getScenarioChild(readerdata->scenariotree, i)));
2558  }
2559 
2560  /* creating the Benders' decomposition */
2561  SCIP_CALL( SCIPcreateBendersDefault(scip, getScenarioSubproblemArray(readerdata->scenariotree),
2562  getScenarioNChildren(readerdata->scenariotree)) );
2563 
2564  /* getting the default Benders' decomposition */
2565  benders = SCIPfindBenders(scip, "default");
2566 
2567  /* updating the lower bounds for the subproblems */
2568  for( i = 0; i < getScenarioNChildren(readerdata->scenariotree); i++ )
2569  {
2571  getScenarioLowerbound(scip, getScenarioChild(readerdata->scenariotree, i)));
2572  }
2573 
2574  /* removing the variable and constraints that were included as part of the core file */
2576 
2577  /* changing settings that are required for Benders' decomposition */
2579  SCIP_CALL( SCIPsetIntParam(scip, "propagating/maxrounds", 0) );
2580  SCIP_CALL( SCIPsetIntParam(scip, "propagating/maxroundsroot", 0) );
2581  SCIP_CALL( SCIPsetIntParam(scip, "heuristics/trysol/freq", 1) );
2582 
2583  /* disabling aggregation since it can affect the mapping between the master and subproblem variables */
2584  SCIP_CALL( SCIPsetBoolParam(scip, "presolving/donotaggr", TRUE) );
2585  SCIP_CALL( SCIPsetBoolParam(scip, "presolving/donotmultaggr", TRUE) );
2586 
2587  return SCIP_OKAY;
2588 }
2589 
2590 /** Read the stochastic information of an SMPS file instance in "STO File Format". */
2591 static
2593  SCIP* scip, /**< SCIP data structure */
2594  const char* filename, /**< name of the input file */
2595  SCIP_READERDATA* readerdata /**< the reader data */
2596  )
2597 {
2598  SCIP_FILE* fp;
2599  STOINPUT* stoi;
2600  SCIP_RETCODE retcode;
2601  SCIP_Bool error = TRUE;
2602  SCIP_Bool unsupported = FALSE;
2603 
2604  assert(scip != NULL);
2605  assert(filename != NULL);
2606 
2607  fp = SCIPfopen(filename, "r");
2608  if( fp == NULL )
2609  {
2610  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
2611  SCIPprintSysError(filename);
2612  return SCIP_NOFILE;
2613  }
2614 
2615  SCIP_CALL_FINALLY( stoinputCreate(scip, &stoi, fp), SCIPfclose(fp) );
2616  SCIP_CALL_TERMINATE( retcode, createReaderdata(scip, readerdata), TERMINATE );
2617 
2618  SCIP_CALL_TERMINATE( retcode, readStoch(scip, stoi), TERMINATE );
2619 
2620  /* checking for supported stochastic information types */
2622  {
2623  SCIPinfoMessage(scip, NULL, "\nSorry, currently only STO files with the stochastic information as DISCRETE are supported.\n\n");
2624  SCIPinfoMessage(scip, NULL, "NOTE: The problem provided by the COR file is loaded without stochastic information.\n\n");
2625  unsupported = TRUE;
2626  }
2627  else
2628  {
2629  if( stoinputSection(stoi) == STO_BLOCKS )
2630  {
2631  SCIP_CALL_TERMINATE( retcode, readBlocks(stoi, scip, readerdata), TERMINATE );
2632  }
2633 
2634  if( stoinputSection(stoi) == STO_SCENARIOS )
2635  {
2636  /* if there are more than two stages, then the sto file is not read. */
2637  if( SCIPtimGetNStages(scip) > 2 )
2638  {
2639  SCIPinfoMessage(scip, NULL, "\nThe scenarios for the stochastic programs are defined in <%s> as SCENARIOS\n", filename);
2640  SCIPinfoMessage(scip, NULL, "Sorry, currently only two-stage stochastic programs are supported when scenarios are defined as SCENARIOS.\n\n");
2641  SCIPinfoMessage(scip, NULL, "NOTE: The problem provided by the COR file is loaded without stochastic information.\n\n");
2642  unsupported = TRUE;
2643  }
2644  else
2645  {
2646  SCIP_CALL_TERMINATE( retcode, readScenarios(stoi, scip, readerdata), TERMINATE );
2647  }
2648  }
2649 
2650  if( stoinputSection(stoi) == STO_INDEP )
2651  {
2652  SCIP_CALL_TERMINATE( retcode, readIndep(stoi, scip, readerdata), TERMINATE );
2653  }
2654  }
2655 
2656  if( !unsupported && stoinputSection(stoi) != STO_ENDATA )
2657  stoinputSyntaxerror(stoi);
2658 
2659  error = stoinputHasError(stoi);
2660 
2661  if( !error && !unsupported )
2662  {
2663  if( readerdata->usebenders )
2664  {
2665  SCIP_CALL_TERMINATE( retcode, buildDecompProblem(scip, readerdata), TERMINATE );
2666  }
2667  else
2668  {
2669  SCIP_CALL_TERMINATE( retcode, buildFullProblem(scip, readerdata), TERMINATE );
2670  }
2671  }
2672 
2673 /* cppcheck-suppress unusedLabel */
2674 TERMINATE:
2675  stoinputFree(scip, &stoi);
2676  SCIPfclose(fp);
2677 
2678  if( error || retcode != SCIP_OKAY )
2679  return SCIP_READERROR;
2680  else
2681  return SCIP_OKAY;
2682 }
2683 
2684 
2685 /*
2686  * Callback methods of reader
2687  */
2688 
2689 /** copy method for reader plugins (called when SCIP copies plugins) */
2690 static
2691 SCIP_DECL_READERCOPY(readerCopySto)
2692 { /*lint --e{715}*/
2693  assert(scip != NULL);
2694  assert(reader != NULL);
2695  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2696 
2697  /* call inclusion method of reader */
2699 
2700  return SCIP_OKAY;
2701 }
2702 
2703 /** destructor of reader to free user data (called when SCIP is exiting) */
2704 static
2705 SCIP_DECL_READERFREE(readerFreeSto)
2706 {
2707  SCIP_READERDATA* readerdata;
2708 
2709  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2710  readerdata = SCIPreaderGetData(reader);
2711  assert(readerdata != NULL);
2712 
2713  SCIP_CALL( freeReaderdata(scip, readerdata) );
2714 
2715  return SCIP_OKAY;
2716 }
2717 
2718 /** problem reading method of reader */
2719 static
2720 SCIP_DECL_READERREAD(readerReadSto)
2721 { /*lint --e{715}*/
2722  SCIP_READER* correader;
2723  SCIP_READER* timreader;
2724 
2725  assert(reader != NULL);
2726  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2727 
2728  correader = SCIPfindReader(scip, "correader");
2729  timreader = SCIPfindReader(scip, "timreader");
2730 
2731  if( correader == NULL )
2732  {
2733  SCIPwarningMessage(scip, "It is necessary to include the \"cor\" reader\n");
2734  (*result) = SCIP_DIDNOTRUN;
2735  return SCIP_OKAY;
2736  }
2737 
2738  if( timreader == NULL )
2739  {
2740  SCIPwarningMessage(scip, "It is necessary to include the \"tim\" reader\n");
2741  (*result) = SCIP_DIDNOTRUN;
2742  return SCIP_OKAY;
2743  }
2744 
2745  /* checking whether the cor file has been read */
2746  if( !SCIPcorHasRead(correader) )
2747  {
2748  SCIPwarningMessage(scip, "The core file must be read before the time and stochastic files.\n");
2749  (*result) = SCIP_DIDNOTRUN;
2750  return SCIP_OKAY;
2751  }
2752 
2753  /* checking whether the tim file has been read */
2754  if( !SCIPtimHasRead(timreader) )
2755  {
2756  SCIPwarningMessage(scip, "The time file must be read before the stochastic files.\n");
2757  (*result) = SCIP_DIDNOTRUN;
2758  return SCIP_OKAY;
2759  }
2760 
2761  SCIP_CALL( SCIPreadSto(scip, filename, result) );
2762 
2763  return SCIP_OKAY;
2764 }
2765 
2766 /*
2767  * sto file reader specific interface methods
2768  */
2769 
2770 /** includes the sto file reader in SCIP */
2772  SCIP* scip /**< SCIP data structure */
2773  )
2774 {
2775  SCIP_READERDATA* readerdata;
2776  SCIP_READER* reader;
2777 
2778  /* create reader data */
2779  SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
2780  readerdata->scenariotree = NULL;
2781  readerdata->numscenarios = 0;
2782 
2783  /* include reader */
2784  SCIP_CALL( SCIPincludeReaderBasic(scip, &reader, READER_NAME, READER_DESC, READER_EXTENSION, readerdata) );
2785 
2786  /* set non fundamental callbacks via setter functions */
2787  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySto) );
2788  SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeSto) );
2789  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSto) );
2790 
2791  /* add decomposition parameters */
2793  "reading/" READER_NAME "/usebenders",
2794  "should Benders' decomposition be used?",
2795  &readerdata->usebenders, FALSE, DEFAULT_USEBENDERS, NULL, NULL) );
2796 
2797  return SCIP_OKAY;
2798 }
2799 
2800 
2801 /** reads the stochastic information for a stochastic program that is in SMPS format */
2803  SCIP* scip, /**< SCIP data structure */
2804  const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
2805  SCIP_RESULT* result /**< pointer to store the result of the file reading call */
2806  )
2807 {
2808  SCIP_READER* reader;
2809  SCIP_READERDATA* readerdata;
2810  SCIP_RETCODE retcode;
2811 
2812  assert(scip != NULL);
2813  assert(result != NULL);
2814 
2815  reader = SCIPfindReader(scip, READER_NAME);
2816  assert(reader != NULL);
2817  readerdata = SCIPreaderGetData(reader);
2818 
2819  retcode = readSto(scip, filename, readerdata);
2820 
2821  if( retcode == SCIP_PLUGINNOTFOUND )
2822  retcode = SCIP_READERROR;
2823 
2824  if( retcode == SCIP_NOFILE || retcode == SCIP_READERROR )
2825  return retcode;
2826 
2827  SCIP_CALL( retcode );
2828 
2829  *result = SCIP_SUCCESS;
2830 
2831  return SCIP_OKAY;
2832 }
2833 
2834 /** returns the total number of scenarios added to the problem */
2836  SCIP* scip /**< SCIP data structure */
2837  )
2838 {
2839  SCIP_READER* reader;
2840  SCIP_READERDATA* readerdata;
2841 
2842  reader = SCIPfindReader(scip, READER_NAME);
2843 
2844  assert(reader != NULL);
2845  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
2846 
2847  readerdata = SCIPreaderGetData(reader);
2848  assert(readerdata != NULL);
2849 
2850  return readerdata->numscenarios;
2851 }
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:97
static const char * stoinputField5(const STOINPUT *stoi)
Definition: reader_sto.c:1159
SCIP_Real * values
Definition: reader_sto.c:100
SCIP_EXPORT const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
Definition: scip_mem.h:86
SCIP_RETCODE SCIPhashmapInsert(SCIP_HASHMAP *hashmap, void *origin, void *image)
Definition: misc.c:3095
enum StoStochInfo STOSTOCHINFO
Definition: reader_sto.c:128
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:80
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:2019
public methods for SCIP parameter handling
SCIP_Real SCIPgetLhsLinear(SCIP *scip, SCIP_CONS *cons)
static SCIP_Real computeScenarioProbability(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:1915
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
SCIP_Bool SCIPconsIsLocal(SCIP_CONS *cons)
Definition: cons.c:8328
public methods for memory management
static const char * getScenarioEntryRow(STOSCENARIO *scenario, int entry)
Definition: reader_sto.c:558
void SCIPbendersUpdateSubproblemLowerbound(SCIP_BENDERS *benders, int probnumber, SCIP_Real lowerbound)
Definition: benders.c:6685
SCIP_Bool SCIPconsIsChecked(SCIP_CONS *cons)
Definition: cons.c:8288
SCIP_EXPORT SCIP_READERDATA * SCIPreaderGetData(SCIP_READER *reader)
Definition: reader.c:483
static STOSTOCHINFO stoinputStochInfoType(const STOINPUT *stoi)
Definition: reader_sto.c:1093
#define SCIP_MAXSTRLEN
Definition: def.h:273
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:123
int childrensize
Definition: reader_sto.c:89
static SCIP_RETCODE addScenarioVarsToProb(SCIP *scip, STOSCENARIO *scenario, SCIP_HASHMAP *varmap, SCIP_VAR **vars, int nvars)
Definition: reader_sto.c:1956
int stagenum
Definition: reader_sto.c:91
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:406
enum StoSection STOSECTION
Definition: reader_sto.c:116
static SCIP_RETCODE setScenarioLowerbound(SCIP *scip, STOSCENARIO *scenario, SCIP_Real lowerbound)
Definition: reader_sto.c:486
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:1534
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:186
int SCIPgetNVars(SCIP *scip)
Definition: scip_prob.c:1986
static void stoinputSetStochtype(STOINPUT *stoi, const char *stochtype)
Definition: reader_sto.c:1230
#define FALSE
Definition: def.h:73
static SCIP_RETCODE addScenarioVarsAndConsToProb(SCIP *scip, STOSCENARIO *scenario, SCIP_Bool decomp)
Definition: reader_sto.c:2249
SCIP_EXPORT SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17510
#define DEFAULT_USEBENDERS
Definition: reader_sto.c:55
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:138
static SCIP_Real getScenarioLowerbound(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:502
static SCIP_RETCODE addScenarioEntry(SCIP *scip, STOSCENARIO *scenario, const char *rowname, const char *colname, SCIP_Real value)
Definition: reader_sto.c:515
SCIP_EXPORT SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:17177
#define TRUE
Definition: def.h:72
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_RETCODE SCIPchgLhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
#define STO_DEFAULT_ARRAYSIZE
Definition: reader_sto.c:64
SCIP_Bool SCIPconsIsInitial(SCIP_CONS *cons)
Definition: cons.c:8258
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
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
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
static SCIP_DECL_READERCOPY(readerCopySto)
Definition: reader_sto.c:2691
static const char * stoinputField1(const STOINPUT *stoi)
Definition: reader_sto.c:1115
#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:965
static void stoinputSetProbname(STOINPUT *stoi, const char *probname)
Definition: reader_sto.c:1216
char * SCIPstrtok(char *s, const char *delim, char **ptrptr)
Definition: misc.c:10548
static const char * getScenarioName(STOSCENARIO *scenario)
Definition: reader_sto.c:417
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:123
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
static SCIP_RETCODE readStoch(SCIP *scip, STOINPUT *stoi)
Definition: reader_sto.c:1379
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:2067
static SCIP_Bool stoinputReadLine(STOINPUT *stoi)
Definition: reader_sto.c:1270
static void getScenarioEntityName(char *name, const char *varname, int stagenum, int scenarionum)
Definition: reader_sto.c:1940
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_RETCODE SCIPgetConsNVars(SCIP *scip, SCIP_CONS *cons, int *nvars, SCIP_Bool *success)
Definition: scip_cons.c:2558
const char ** colnames
Definition: reader_sto.c:99
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
SCIP_RETCODE SCIPdelCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2837
#define READER_DESC
Definition: reader_sto.c:52
SCIP_BENDERS * SCIPfindBenders(SCIP *scip, const char *name)
Definition: scip_benders.c:484
SCIP_EXPORT SCIP_Bool SCIPvarIsInitial(SCIP_VAR *var)
Definition: var.c:17213
static void addScenarioSubproblem(STOSCENARIO *scenario, SCIP *subproblem)
Definition: reader_sto.c:281
SCIP_RETCODE SCIPincludeReaderSto(SCIP *scip)
Definition: reader_sto.c:2771
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:144
STOSECTION section
Definition: reader_sto.c:133
SCIP_Bool SCIPhashmapExists(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:3362
#define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
Definition: scip_mem.h:92
SCIP_Bool SCIPtimHasRead(SCIP_READER *reader)
Definition: reader_tim.c:908
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:283
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:774
static SCIP_RETCODE removeCoreVariablesAndConstraints(SCIP *scip)
Definition: reader_sto.c:2458
SCIP * scip
Definition: reader_sto.c:84
static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1001
const char * f6
Definition: reader_sto.c:145
SCIP_EXPORT const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:17012
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:1440
static SCIP_Real getScenarioEntryValue(STOSCENARIO *scenario, int entry)
Definition: reader_sto.c:584
#define SCIPerrorMessage
Definition: pub_message.h:55
static const char * stoinputField0(const STOINPUT *stoi)
Definition: reader_sto.c:1104
const char * f0
Definition: reader_sto.c:139
static SCIP_RETCODE createScenarioSubproblemArray(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:266
static const char * stoinputField3(const STOINPUT *stoi)
Definition: reader_sto.c:1137
const char ** rownames
Definition: reader_sto.c:98
static const char * stoinputField4(const STOINPUT *stoi)
Definition: reader_sto.c:1148
#define SCIPfreeBufferArrayNull(scip, ptr)
Definition: scip_mem.h:124
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
void SCIPenableDebugSol(SCIP *scip)
Definition: scip_debug.c:48
SCIP_Real SCIPgetRhsLinear(SCIP *scip, SCIP_CONS *cons)
int nchildren
Definition: reader_sto.c:88
static SCIP_Real getScenarioProbability(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:473
static STOSECTION stoinputSection(const STOINPUT *stoi)
Definition: reader_sto.c:1082
int SCIPtimGetNStages(SCIP *scip)
Definition: reader_tim.c:925
static SCIP_RETCODE setScenarioName(SCIP *scip, STOSCENARIO *scenario, const char *name)
Definition: reader_sto.c:401
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:2532
SCIP_RETCODE SCIPsetReaderFree(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERFREE((*readerfree)))
Definition: scip_reader.c:162
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:364
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:942
#define SCIPensureBlockMemoryArray(scip, ptr, arraysizeptr, minsize)
Definition: scip_mem.h:94
static SCIP_Bool stoinputHasError(const STOINPUT *stoi)
Definition: reader_sto.c:1181
#define STO_MAX_LINELEN
Definition: reader_sto.c:61
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_READER * SCIPfindReader(SCIP *scip, const char *name)
Definition: scip_reader.c:226
STOSCENARIO ** children
Definition: reader_sto.c:87
SCIP_Bool SCIPconsIsRemovable(SCIP_CONS *cons)
Definition: cons.c:8358
const char * stagename
Definition: reader_sto.c:93
static const char * stoinputField2(const STOINPUT *stoi)
Definition: reader_sto.c:1126
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:111
struct SCIP_ReaderData SCIP_READERDATA
Definition: type_reader.h:44
int SCIPcalcMemGrowSize(SCIP *scip, int num)
Definition: scip_mem.c:130
SCIP_Real SCIPinfinity(SCIP *scip)
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:1006
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
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
const char * SCIPgetProbName(SCIP *scip)
Definition: scip_prob.c:1065
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:3013
SCIP_CONS * SCIPfindCons(SCIP *scip, const char *name)
Definition: scip_prob.c:2941
static void stoinputSetSection(STOINPUT *stoi, STOSECTION section)
Definition: reader_sto.c:1192
STOSTOCHINFO stochinfotype
Definition: reader_sto.c:134
SCIP_CONS ** SCIPtimGetStageConss(SCIP *scip, int stagenum)
Definition: reader_tim.c:1065
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 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 SCIP_RETCODE readScenarios(STOINPUT *stoi, SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1604
#define READER_EXTENSION
Definition: reader_sto.c:53
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
SCIP_Bool SCIPconsIsSeparated(SCIP_CONS *cons)
Definition: cons.c:8268
static const char * getScenarioStageName(SCIP *scip, STOSCENARIO *scenario)
Definition: reader_sto.c:359
SCIP_EXPORT SCIP_Bool SCIPvarIsDeleted(SCIP_VAR *var)
Definition: var.c:17233
SCIP_Bool haserror
Definition: reader_sto.c:137
Constraint handler for linear constraints in their most general form, .
SCIP_EXPORT SCIP_Real SCIPvarGetLbOriginal(SCIP_VAR *var)
Definition: var.c:17608
static SCIP_RETCODE createScenariosFromBlocks(SCIP *scip, SCIP_READERDATA *readerdata, STOSCENARIO ***blocks, int numblocks, int *numblocksperblock, int numstages)
Definition: reader_sto.c:925
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1666
int scenarionum
Definition: reader_sto.c:92
int SCIPstoGetNScenarios(SCIP *scip)
Definition: reader_sto.c:2835
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
SCIP_RETCODE SCIPchgCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
SCIP_Bool SCIPconsIsDynamic(SCIP_CONS *cons)
Definition: cons.c:8348
#define STO_DEFAULT_ENTRIESSIZE
Definition: reader_sto.c:65
int SCIPtimGetStageNVars(SCIP *scip, int stagenum)
Definition: reader_tim.c:1086
static SCIP_RETCODE readIndep(STOINPUT *stoi, SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:1746
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip_prob.c:2679
int nsubproblems
Definition: reader_sto.c:90
static void clearFrom(char *buf, unsigned int pos)
Definition: reader_sto.c:1256
static const char * stoinputField6(const STOINPUT *stoi)
Definition: reader_sto.c:1170
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:1021
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_RETCODE SCIPchgVarObj(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
Definition: scip_var.c:4514
SCIP_Bool SCIPconsIsEnforced(SCIP_CONS *cons)
Definition: cons.c:8278
static SCIP_DECL_READERFREE(readerFreeSto)
Definition: reader_sto.c:2705
#define STO_DEFAULT_BLOCKARRAYSIZE
Definition: reader_sto.c:66
public methods for message output
SCIP_RETCODE SCIPcreateBendersDefault(SCIP *scip, SCIP **subproblems, int nsubproblems)
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10590
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:3047
const char * SCIPconsGetName(SCIP_CONS *cons)
Definition: cons.c:8089
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1252
SCIP_Bool SCIPconsIsDeleted(SCIP_CONS *cons)
Definition: cons.c:8218
SCIP_RETCODE SCIPdelVar(SCIP *scip, SCIP_VAR *var, SCIP_Bool *deleted)
Definition: scip_prob.c:1784
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:828
SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
Definition: reader_cor.c:208
#define SCIP_Real
Definition: def.h:163
int lineno
Definition: reader_sto.c:136
static SCIP_RETCODE readSto(SCIP *scip, const char *filename, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:2592
public methods for input file readers
#define SCIP_CALL_TERMINATE(retcode, x, TERM)
Definition: def.h:385
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:8109
default Benders&#39; decomposition plugin
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
public methods for message handling
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:1040
void SCIPprintSysError(const char *message)
Definition: misc.c:10499
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip_param.c:439
SCIP_Bool SCIPconsIsStickingAtNode(SCIP_CONS *cons)
Definition: cons.c:8368
#define STO_DEFAULT_CHILDRENSIZE
Definition: reader_sto.c:67
SCIP ** subproblems
Definition: reader_sto.c:85
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2764
static SCIP_RETCODE createScenarioData(SCIP *scip, STOSCENARIO **scenariodata)
Definition: reader_sto.c:153
enum SCIP_Vartype SCIP_VARTYPE
Definition: type_var.h:60
static void stoinputSetStochInfoType(STOINPUT *stoi, STOSTOCHINFO stochinfotype)
Definition: reader_sto.c:1204
SCIP_RETCODE SCIPreadSto(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition: reader_sto.c:2802
static STOSCENARIO * getScenarioChild(STOSCENARIO *scenario, int childnum)
Definition: reader_sto.c:319
SCIP_Bool SCIPconsIsMarkedPropagate(SCIP_CONS *cons)
Definition: cons.c:8298
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:315
SCIP_EXPORT SCIP_Real SCIPvarGetUbOriginal(SCIP_VAR *var)
Definition: var.c:17628
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:199
SCIP_VAR ** SCIPtimGetStageVars(SCIP *scip, int stagenum)
Definition: reader_tim.c:1044
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:223
SCIP_RETCODE SCIPchgRhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
const char * f4
Definition: reader_sto.c:143
char buf[STO_MAX_LINELEN]
Definition: reader_sto.c:138
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1110
static SCIP_RETCODE buildFullProblem(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_sto.c:2508
SCIP_EXPORT SCIP_Bool SCIPvarIsRemovable(SCIP_VAR *var)
Definition: var.c:17223
int SCIPtimGetStageNConss(SCIP *scip, int stagenum)
Definition: reader_tim.c:1107
public methods for reader plugins
SCIP_RETCODE SCIPgetConsVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int varssize, SCIP_Bool *success)
Definition: scip_cons.c:2514
public methods for global and local (sub)problems
default SCIP plugins
static void setScenarioScip(STOSCENARIO *scenario, SCIP *scip)
Definition: reader_sto.c:242
int SCIPmemccpy(char *dest, const char *src, char stop, unsigned int cnt)
Definition: misc.c:10474
const char * SCIPtimGetStageName(SCIP *scip, int stagenum)
Definition: reader_tim.c:944
static SCIP_RETCODE insertScenarioInReaderdata(SCIP *scip, SCIP_READERDATA *readerdata, STOSCENARIO *scenario, char *parentname)
Definition: reader_sto.c:801
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:2133
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip_param.c:497
static void stoinputFree(SCIP *scip, STOINPUT **stoi)
Definition: reader_sto.c:1072
SCIP_FILE * fp
Definition: reader_sto.c:135
SCIP_RETCODE SCIPcreateProbBasic(SCIP *scip, const char *name)
Definition: scip_prob.c:170
static SCIP_DECL_READERREAD(readerReadSto)
Definition: reader_sto.c:2720
SCIP_Bool SCIPconsIsModifiable(SCIP_CONS *cons)
Definition: cons.c:8338
static void stoinputSyntaxerror(STOINPUT *stoi)
Definition: reader_sto.c:1243
memory allocation routines