scenred.gms 3.91 KB
Newer Older
1
2
3
4
5
* Ensure scenario reduction setting are given
if(mSettings(mSolve, 'red_num_leaves')
   or mSettings(mSolve, 'red_percentage'),

* Get probabilitiesfor samples
6
Option clear = p_sProbability;
7
p_sProbability(s_active(s)) = p_msProbability(mSolve, s);
8
9

* SCENRED2 parameters
10
ScenRedParms('red_num_leaves') = min(mSettings(mSolve, 'red_num_leaves'),
11
                                     mSettings(mSolve, 'scenarios'));
12
ScenRedParms('red_percentage') = mSettings(mSolve, 'red_percentage');
13
14
ScenRedParms('scen_red') = 1$mSettings(mSolve, 'red_num_leaves'); // Reduce scenarios
ScenRedParms('tree_con') = 1$mSettings(mSolve, 'red_percentage');  // Tree construction
15
16
*ScenRedParms('report_level') = 2;
ScenRedParms('run_time_limit') = 30;
17
$ifthene %debug%>1
18
19
20
21
ScenRedParms('visual_init') = 1;
ScenRedParms('visual_red') = 1;
$endif

22
23
24
25
26
27
28
29
30
31
32
33
34
35
* Calculate data for scenario reduction
Option clear = ts_energy_;
ts_energy_(s)
    = sum(sft(s, f, t)$mf_central(mSolve, f),
          sum(gn(grid, node),
              ts_influx_(grid, node, s, f, t)
               + sum((flowNode(flow, node), flowUnit(flow, unit))
                      $gnu_output(grid, node, unit),
                     ts_cf_(flow, node, s, f, t)
                      * p_gnu(grid, node, unit, 'maxGen')
                 )
          ) * p_stepLength(mSolve, f, t)
      );

Erkka Rinne's avatar
Erkka Rinne committed
36
* Export data
37
execute_unload 'srin.gdx', ScenRedParms,
38
                           s_active, ss, p_sProbability,
39
                           ts_energy_;
Erkka Rinne's avatar
Erkka Rinne committed
40
41
42
43
44
* Choose right null device
$ifthen %system.filesys% == 'MSNT' $set nuldev NUL
$else $set nuldev /dev/null
$endif
* Execute SCENRED2 and load data
Erkka Rinne's avatar
Erkka Rinne committed
45
put log "Executing SCENRED2"/; putclose log;
Erkka Rinne's avatar
Erkka Rinne committed
46
execute 'scenred2 inc/scenred.cmd > %nuldev%';
47

48
49
50
if(errorLevel,
    put log "!!! Scenario reduction (SCENRED2) failed. ";
    put log "See file 'sr.log' for details."/;
51
    put_utility log, 'Click' / 'sr.log';
52
53
54
55
56
57
58
59
    putclose;
    execError = execError + 1;
else
    execute_load 'srout.gdx', ScenRedReport,
                              ss=red_ancestor,
                              p_sProbability=red_prob;

    // Update probabilities
60
61
62
    // Account for small errors from SCENRED2 where the probability of a single
    // scenario can be slightly above one.
    p_msProbability(mSolve, s) = min(p_sProbability(s), 1);
63

64
    // Update scenarios
65
66
67
68
    loop(mft_lastSteps(mSolve, f, t), // Select last time step
        // Select each scenario and the leaf sample of the scenario (using non-reduced msft)
        loop((scenario, msft(mSolve, s_, f, t))$s_scenario(s_, scenario),
            // Drop scenarios with zero probability
69
            s_scenario(s, scenario)$(not p_msProbability(mSolve, s_)) = no;
70
71
            // Build scenarios starting from the leaf samples
            Option clear = s_prev; s_prev(s_) = yes;
72
            tmp = 0;
73
74
            // Re-construct scenarios left from reduction
            while(not tmp and p_msProbability(mSolve, s_),
75
                loop(ss(s__, s)$s_prev(s__),
76
                    s_scenario(s, scenario) = yes$p_msProbability(mSolve, s);
77
78
79
                    Option clear = s_prev; s_prev(s) = yes;
                    tmp = ms_initial(mSolve, s);
                );
80
            );
81
82
        );
    );
Erkka Rinne's avatar
Erkka Rinne committed
83

84
85
86
87
88
89
90
91
    $$ifthene.debug %debug%>0
    put log "Used scenarios are:"/;
    loop((ms_initial(mSolve, s), s_scenario(s, scenario)),
        put log scenario.tl/;
    );
    putclose log;
    $$endif.debug

92
93
94
95
96
97
    // Update sets
    ms(mSolve, s)$ms(mSolve, s) = yes$p_msProbability(mSolve, s);
    msf(mSolve, s, f)$msf(mSolve, s, f) = ms(mSolve, s);
    msft(mSolve, s, f, t)$msft(mSolve, s, f, t) = msf(mSolve, s, f);
    sft(s, f, t)$sft(s, f, t) = yes$p_msProbability(mSolve, s);

98
    // Clear data from removed samples
99
100
    ts_influx_(gn, s_active(s), ft)$(not p_sProbability(s)) = 0;
    ts_cf_(flowNode, s_active(s), ft)$(not p_sProbability(s)) = 0;
101
102
103

    s_active(s)$s_active(s) = yes$p_sProbability(s);
);
Erkka Rinne's avatar
Erkka Rinne committed
104

105
106
107
else
    put log "!!! No scenario reduction setting given, skipping scenario reduction!"/;
);