recurse.gms

$TITLE GAMS Template for Report Writing with a Recursive Model sets r Regions /A, B/, t Time period (annual time steps) /0*10/; alias(t,tt); parameters delta Capital depreciation rate /0.04/ i0(r) Base year investment /A 0.5, B 0.5/ c0(r) Base year consumption /A 0.5, B 0.5/ b(r) Capital value share /A 0.3, B 0.3/ phi(r) Cobb Douglas scale parameter L(r) Labour supply k0(r) Base year capital stock, s0(r) Saving rate gl(r,t) Labor supply growth rates; * Assume random growth over the model horizon: gl(r,t) = uniform(0.8,1.2) * 0.02; * Labor supply in the base year: L(r) = (1-b(r))*(c0(r)+i0(r)); k0(r) = i0(r)/(gl(r,"0")+delta); * Calibration of production scale parameter: phi(r) = (c0(r)+i0(r)) / k0(r)**b(r) * L(r)**(1-b(r)); s0(r) = i0(r) / (c0(r) + i0(r)); variables K(r) capital stock Y(r) production S(r) savings I(r) investment; equations prof(r) cobb douglas production function savr(r) saving rate capm(r) capital market; prof(r).. Y(r) =e= phi(r) * K(r)**b(r) * L(r)**(1-b(r)); savr(r).. S(r) =e= s0(r) * Y(r); capm(r).. S(r) =e= I(r); model grm_mcp /prof.y, savr.s, capm.i/; * (1) check initial equilibrium I.l(r) = i0(r); S.l(r) = i0(r); K.fx(r) = k0(r); Y.l(r) = c0(r) + i0(r); I.lo(r) = 1e-5; * Save solutions using naming convention modelname_psolvenumber.gdx option savepoint = 2; grm_mcp.iterlim = 0; solve grm_mcp using mcp; * evolution of model along time path grm_mcp.iterlim = 1000; * We have run one solution already, so the solution for the first * time period t is stored in grm_mcp_p2.gdx and so on: loop(t, K.fx(r) = K.l(r)*(1-delta) + I.l(r); solve grm_mcp using mcp; L(r) = L(r) * (1 + gl(r,t)); ); * We are finished -- generate the report: execute 'gams report'
report.gms
$TITLE Retrieve information from savepoint files for reporting sets r /A,B/ t /0*10/; parameters Y_R(t,r) Output level over time, I_R(t,r) Investment level over time, K_R(t,r) Capital stock over time; variables Y(r) Output level an intra-temporal equilibrium, I(r) Investment level in an intra-temporal equilibrium, K(r) Capital stock in an intra-temporal equilibrium; * Need to initialize to keep the GAMS compiler happy: Y.L(r) = 0; I.L(r) = 0; K.L(r) = 0; * Initialize the loadpoint utility (declare files): $batinclude loadpoint * Note that recurse.gms generates 12 solution files, * corresponding to the base year and time periods 0 to 10. * One solve is executed prior to entering the loop, so * we use (ord(t)+1) to map the first period (t=0) * to solution file "grm_mcp_p2.gdx". loop(t, * Load a solution: $batinclude loadpoint grm_mcp (ord(t)+1) * Transfer solution values into time-indexed arrays: Y_R(t,r) = Y.l(r); I_R(t,r) = I.l(r); K_R(t,r) = K.l(r); ); display Y_R, I_R, K_R; $setglobal domain t $setglobal labels t $libinclude plot Y_R
loadpoint.gms
$offlisting * * 1. Store the command file temporarily in the GAMS * scratch directory to avoid clobbering user files. * Delete the file after each command. * * 2. Use the POSIX utilities (cp and mv) from the GAMS * gbin directory to improve portability. $if not defined kcp file kcp /%gams.scrdir%kcp.bat/; kcp.nw=0; kcp.nd=0; $if "%1"=="" $exit $setargs mdl index putclose kcp,'@echo off'/'cp %mdl%_p'%index%'.gdx %mdl%_p.gdx'/; execute "%gams.scrdir%kcp.bat"; execute 'rm "%gams.scrdir%kcp.bat"'; execute_loadpoint "%mdl%_p.gdx";
altreport.gms
$ontext Tom, thanks for the modified version of you loadpoint-with-varying-index utility. It works fine. I must admit that I was first a bit overwhelmed from the complexity of the solution (first write an $batinclude file from GAMS, then let this $batinclude file write a bat file, which renames an existing file, which is then read in from the original GAMS code), so it took me some time to find my way through that. I assembled below some kind of "minimal code" from the core lines of your example that does the job without any further precaution. Perhaps it's also helpful for other people around here who want to understand what's going on. Stefan $offtext $TITLE Retrieve information from savepoint files for reporting sets r /A,B/ t /0*10/; parameters Y_R(t,r) Output level over time, I_R(t,r) Investment level over time, K_R(t,r) Capital stock over time; variables Y(r) Output level an intra-temporal equilibrium, I(r) Investment level in an intra-temporal equilibrium, K(r) Capital stock in an intra-temporal equilibrium; * Need to initialize to keep the GAMS compiler happy: Y.L(r) = 0; I.L(r) = 0; K.L(r) = 0; file kcp /kcp.bat/; kcp.nw=0; kcp.nd=0; loop(t, * Load a solution: putclose kcp, 'rm grm_mcp_p.gdx'/'cp grm_mcp_p', (ord(t)+1),'.gdx grm_mcp_p.gdx'/; execute "kcp.bat"; execute_loadpoint "grm_mcp_p.gdx"; * Transfer solution values into time-indexed arrays: Y_R(t,r) = Y.l(r); I_R(t,r) = I.l(r); K_R(t,r) = K.l(r); ); display Y_R, I_R, K_R; $setglobal domain t $setglobal labels t $libinclude plot Y_R