From fe6f8ee25bf878db27cb8c8b40814252a6023664 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 30 Jun 2014 18:23:51 +0200
Subject: [PATCH] Ramsey policy: compute FOC *after* removing leads/lags of two
 or more.

Otherwise, on big models with lots of leads/lags (like GPM7), the Lagrangian
can be so huge that the preprocessor requires an insane amount of memory.
---
 preprocessor/ModFile.cc | 41 +++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 8916121463..b2a66eea28 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -330,26 +330,6 @@ ModFile::transformPass(bool nostrict)
       dynamic_model.removeTrendVariableFromEquations();
     }
 
-  if (mod_file_struct.ramsey_model_present)
-    {
-      StaticModel *planner_objective = NULL;
-      for (vector<Statement *>::iterator it = statements.begin(); it != statements.end(); it++)
-        {
-          PlannerObjectiveStatement *pos = dynamic_cast<PlannerObjectiveStatement *>(*it);
-          if (pos != NULL)
-            planner_objective = pos->getPlannerObjective();
-        }
-      assert(planner_objective != NULL);
-      ramsey_model_orig_eqn_nbr = dynamic_model.equation_number();
-
-      /*
-        clone the model then clone the new equations back to the original because
-        we have to call computeDerivIDs (in computeRamseyPolicyFOCs and computingPass)
-       */
-      dynamic_model.cloneDynamic(ramsey_FOC_equations_dynamic_model);
-      ramsey_FOC_equations_dynamic_model.computeRamseyPolicyFOCs(*planner_objective);
-      ramsey_FOC_equations_dynamic_model.replaceMyEquations(dynamic_model);
-    }
 
   if (mod_file_struct.stoch_simul_present
       || mod_file_struct.estimation_present
@@ -371,6 +351,27 @@ ModFile::transformPass(bool nostrict)
       dynamic_model.substituteEndoLagGreaterThanTwo(true);
     }
 
+  if (mod_file_struct.ramsey_model_present)
+    {
+      StaticModel *planner_objective = NULL;
+      for (vector<Statement *>::iterator it = statements.begin(); it != statements.end(); it++)
+        {
+          PlannerObjectiveStatement *pos = dynamic_cast<PlannerObjectiveStatement *>(*it);
+          if (pos != NULL)
+            planner_objective = pos->getPlannerObjective();
+        }
+      assert(planner_objective != NULL);
+      ramsey_model_orig_eqn_nbr = dynamic_model.equation_number();
+
+      /*
+        clone the model then clone the new equations back to the original because
+        we have to call computeDerivIDs (in computeRamseyPolicyFOCs and computingPass)
+       */
+      dynamic_model.cloneDynamic(ramsey_FOC_equations_dynamic_model);
+      ramsey_FOC_equations_dynamic_model.computeRamseyPolicyFOCs(*planner_objective);
+      ramsey_FOC_equations_dynamic_model.replaceMyEquations(dynamic_model);
+    }
+
   if (differentiate_forward_vars)
     dynamic_model.differentiateForwardVars(differentiate_forward_vars_subset);
 
-- 
GitLab