-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmacros.gms
187 lines (155 loc) · 7.44 KB
/
macros.gms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
*-------------------------------------------------------------------------------
* macros.gms
*
* Define macros to simplify complex and repetitive expressions
*-------------------------------------------------------------------------------
* Build a polynomial expression
$if not defined polydeg set polydeg 'Set of polynomial degrees' / deg0*deg11 /;
$macro poly(x,coeff) sum(polydeg$(coeff), (coeff)*(x)**(ord(polydeg)-1))
* Sum expression, defined with the set 'ss', over the subset 'sd'
$macro cast(expression,ss,sd) sum(&ss$(sameas(&ss,&sd)), &expression)
* Return true (>0), if the set 'small' belongs to the set 'big'
$macro xiny(small,big) sum(&big$sameas(&big,&small),1)
*-------------------------------------------------------------------------------
* Specific macros
* Sum child in the CES tree, in post_tables.gms
$macro sumjchild(x,jset,anode) sum(j$map_j(&anode,j), cast(&x,&jset,j))
* store model results, in solve_macros.gms
$macro savereport(reg) solrep('®','objval')=witch_®.objval;\
solrep('®','solvestat')=witch_®.solvestat;\
solrep('®','modelstat')=witch_®.modelstat;\
solrep('®','iterusd')=witch_®.iterusd;\
solrep('®','resusd')=witch_®.resusd;\
solrep('®','numvar')=witch_®.numvar;\
solrep('®','numequ')=witch_®.numequ;\
solrep('®','numnz')=witch_®.numnz;\
solrep('®','numvarproj')=witch_®.numvarproj;\
solrep('®','numinfes')=witch_®.numinfes;\
solrep('®','numnopt')=witch_®.numnopt;\
solrep('®','domusd')=witch_®.domusd;
* Select numiter iterations
$macro last_iter(numiter) ((ord(ssiter) le ord(siter)) and (ord(ssiter) ge (ord(siter) - (&numiter - 1))))
*-------------------------------------------------------------------------------
* Load variables from startgdx
* Read and set the variable level from the start GDX,
* if the level is not found in the gdx, default is used
$macro loadvar(var,setdep,default) \
prevexecerrors = execerror; last_load_went_wrong=0; \
execute_load '%startgdx%', &var; \
if(execerror gt prevexecerrors, \
&var.l&&setdep = &default; \
execerror = prevexecerrors; \
last_load_went_wrong=1)
* Load variable level and specify bounds
* if the level is not found in the gdx, default is set as level
$macro loadvarbnd(var,setdep,default,bndlo,bndup) \
loadvar(var,setdep,default); \
&var.lo&&setdep = &bndlo; \
&var.up&&setdep = &bndup; \
&var.l&&setdep$(&var.l&&setdep<&bndlo) = &bndlo; \
&var.l&&setdep$(&var.l&&setdep>&bndup) = &bndup;
$macro loadvarbndcond(var,setdep,cond,default,bndlo,bndup) \
loadvar(var,'&&setdep$(&&cond)',default); \
&var.lo&&setdep$(&&cond) = &bndlo; \
&var.up&&setdep$(&&cond) = &bndup; \
&var.l&&setdep$((&&cond) and (&var.l&&setdep<&bndlo)) = &bndlo; \
&var.l&&setdep$((&&cond) and (&var.l&&setdep>&bndup)) = &bndup;
* Load from the same parameter but with first ssp index. Name starts with ssp_
$macro load_from_ssp(par,idx,ssp,suxfile) \
parameter ssp_&par(*,&&idx); \
execute_loaddc '%datapath%data_&suxfile' ssp_∥ \
&par(&&idx) = ssp_&par('&ssp',&&idx);
* Tracking error parameters
parameter prevexecerrors, last_load_went_wrong;
*-------------------------------------------------------------------------------
* log some message
file output;
$macro putlog(msg) \
put output; \
put_utility 'log' / &msg; \
putclose output;
*-------------------------------------------------------------------------------
* Load parameters from startgdx
$ifthen.st not set startgdx2
$macro loadpar(par,setdep,default) \
prevexecerrors = execerror; last_load_went_wrong=0; \
execute_load '%startgdx%', ∥ \
if(execerror gt prevexecerrors, &par&&setdep = &default; execerror = prevexecerrors; last_load_went_wrong=1)
$else.st
$macro loadpar(par,setdep,default) \
prevexecerrors = execerror; last_load_went_wrong=0; \
execute_load '%startgdx%', ∥ \
if(execerror gt prevexecerrors, \
execerror = prevexecerrors; \
execute_load '%startgdx2%', ∥ \
if(execerror gt prevexecerrors, \
&par&&setdep = &default; execerror = prevexecerrors; last_load_went_wrong=1))
$endif.st
*-------------------------------------------------------------------------------
* coalition
$macro mapn(clt) map_clt_n(&clt,n)
$macro mapnn(clt) map_clt_n(&clt,nn)
* time horizon
$macro mapn_th(clt) (map_clt_n(&clt,n) and (not tfix(t)) and (year(t) le yeoh))
$macro mapn_th1(clt) (map_clt_n(&clt,n) and (not tfix(tp1)) and pre(t,tp1) and (year(t) lt yeoh))
$macro mapn_th2(clt) (map_clt_n(&clt,n) and (not tfix(tp2)) and pre(t,tp1) and pre(tp1,tp2) and (year(t) lt yeoh))
$macro mapn_th1_last(clt) (map_clt_n(&clt,n) and \
(((not tfix(tp1)) and pre(t,tp1) and (year(t) lt yeoh)) or (year(t) eq yeoh and sameas(t,tp1))))
$macro time_th1_last (((not tfix(tp1)) and pre(t,tp1) and (year(t) lt yeoh)) or (year(t) eq yeoh and sameas(t,tp1)))
* cooperation
$macro cooprd (sum(clt$(map_clt_n(clt,n) and rd_cooperation(rd,clt)),1) eq 1)
$macro nocooprd (sum(clt$(map_clt_n(clt,n) and rd_cooperation(rd,clt)),1) ne 1)
*-------------------------------------------------------------------------------
* Depreciation Rate [TW/TW/yr]
* Let
* A := integral over time of linear 1% depreciation until end of life (than full depreciation)
* = lifetime - 0.01/2 lifetime**2
* B := integral over time of exponential depreciation
* = int((1-delta_en)^x, dx) = - 1/log(1-delta_en)
* We find delta_en such that A = B
$macro depreciation_rate(tech) 1 - exp( 1 / ( - lifetime(&tech,n) + (0.01/2) * lifetime(&tech,n)**2) )
*-------------------------------------------------------------------------------
* Production functions
$macro cobb(out,fac1id,fac1exp,fac2id,fac2exp) \
q0(&out,n)*( \
(&fac1exp/q0(&fac1id,n))**(alpha(&fac1id,n))* \
(&fac2exp/q0(&fac2id,n))**(alpha(&fac2id,n)) \
)
$macro ces(out,fac1id,fac1exp,fac2id,fac2exp) \
q0(&out,n)*( \
alpha(&fac1id,n)*(&fac1exp/q0(&fac1id,n))**rho(&out,t)+ \
alpha(&fac2id,n)*(&fac2exp/q0(&fac2id,n))**rho(&out,t) \
)**(1/rho(&out,t))
$macro ces3(out,fac1id,fac1exp,fac2id,fac2exp,fac3id,fac3exp) \
q0(&out,n)*( \
alpha(&fac1id,n)*(&fac1exp/q0(&fac1id,n))**rho(&out,t)+ \
alpha(&fac2id,n)*(&fac2exp/q0(&fac2id,n))**rho(&out,t)+ \
alpha(&fac3id,n)*(&fac3exp/q0(&fac3id,n))**rho(&out,t) \
)**(1/rho(&out,t))
$macro lin(out,fac1id,fac1exp,fac2id,fac2exp) \
q0(&out,n)*( \
alpha(&fac1id,n)*(&fac1exp/q0(&fac1id,n))+ \
alpha(&fac2id,n)*(&fac2exp/q0(&fac2id,n)) \
)
$macro lin3(out,fac1id,fac1exp,fac2id,fac2exp,fac3id,fac3exp) \
q0(&out,n)*( \
alpha(&fac1id,n)*(&fac1exp/q0(&fac1id,n))+ \
alpha(&fac2id,n)*(&fac2exp/q0(&fac2id,n))+ \
alpha(&fac3id,n)*(&fac3exp/q0(&fac3id,n)) \
)
*-------------------------------------------------------------------------------
* Solve model
$macro savereport_with_loadhandle(clt) \
witch_&clt.handle = remh('&clt'); \
execute_loadhandle witch_&clt; \
savereport(&clt)
$macro checkforproblems(clt) \
if((solrep(&clt,'solvestat') ne 1) or (solrep(&clt,'modelstat') ne 2), \
cproblem(&clt) = yes; \
else cproblem(&clt) = no;)
*-------------------------------------------------------------------------------
* Frozen variables
$macro freezevar(name,idx) \
&name.fx&&idx = &name.l&&idx;
$macro nullvar(name,idx) \
&name.fx&&idx = 0;