From 5d6e6338f43ffb563c28af490e2544dbfbe7626b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 20 Mar 2020 18:47:29 +0100
Subject: [PATCH] Block decomposition: remove unused codepaths in computation
 of normalization

---
 src/ModelTree.cc | 74 ------------------------------------------------
 src/ModelTree.hh |  4 +--
 2 files changed, 1 insertion(+), 77 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 1842532b..c8832c7c 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -270,33 +270,7 @@ ModelTree::computeNormalization(const jacob_map_t &contemporaneous_jacobian, boo
   // Compute maximum cardinality matching
   vector<int> mate_map(2*n);
 
-#if 1
   bool check = checked_edmonds_maximum_cardinality_matching(g, &mate_map[0]);
-#else // Alternative way to compute normalization, by giving an initial matching using natural normalizations
-  fill(mate_map.begin(), mate_map.end(), boost::graph_traits<BipartiteGraph>::null_vertex());
-
-  auto natural_endo2eqs = computeNormalizedEquations();
-
-  for (int i = 0; i < symbol_table.endo_nbr(); i++)
-    {
-      if (natural_endo2eqs.count(i) == 0)
-        continue;
-
-      int j = natural_endo2eqs.find(i)->second;
-
-      put(&mate_map[0], i, n+j);
-      put(&mate_map[0], n+j, i);
-    }
-
-  boost::edmonds_augmenting_path_finder<BipartiteGraph, int *, boost::property_map<BipartiteGraph, boost::vertex_index_t>::type> augmentor(g, &mate_map[0], get(boost::vertex_index, g));
-  while (augmentor.augment_matching())
-    {
-    };
-
-  augmentor.get_current_matching(&mate_map[0]);
-
-  bool check = boost::maximum_cardinality_matching_verifier<BipartiteGraph, int *, boost::property_map<BipartiteGraph, boost::vertex_index_t>::type>::verify_matching(g, &mate_map[0], get(boost::vertex_index, g));
-#endif
 
   assert(check);
 
@@ -310,29 +284,6 @@ ModelTree::computeNormalization(const jacob_map_t &contemporaneous_jacobian, boo
   endo2eq.resize(equations.size());
   transform(mate_map.begin(), mate_map.begin() + n, endo2eq.begin(), [=](int i) { return i-n; });
 
-#ifdef DEBUG
-  auto natural_endo2eqs = computeNormalizedEquations(natural_endo2eqs);
-
-  int n1 = 0, n2 = 0;
-
-  for (int i = 0; i < symbol_table.endo_nbr(); i++)
-    {
-      if (natural_endo2eqs.count(i) == 0)
-        continue;
-
-      n1++;
-
-      auto x = natural_endo2eqs.equal_range(i);
-      if (find_if(x.first, x.second, [=](auto y) { return y.second == endo2eq[i]; }) == x.second)
-        cout << "Natural normalization of variable " << symbol_table.getName(symbol_table.getID(SymbolType::endogenous, i))
-             << " not used." << endl;
-      else
-        n2++;
-    }
-
-  cout << "Used " << n2 << " natural normalizations out of " << n1 << ", for a total of " << n << " equations." << endl;
-#endif
-
   // Check if all variables are normalized
   if (auto it = find(mate_map.begin(), mate_map.begin() + n, boost::graph_traits<BipartiteGraph>::null_vertex());
       it != mate_map.begin() + n)
@@ -437,31 +388,6 @@ ModelTree::computeNonSingularNormalization(jacob_map_t &contemporaneous_jacobian
     }
 }
 
-multimap<int, int>
-ModelTree::computeNormalizedEquations() const
-{
-  multimap<int, int> endo2eqs;
-  for (size_t i = 0; i < equations.size(); i++)
-    {
-      auto lhs = dynamic_cast<VariableNode *>(equations[i]->arg1);
-      if (!lhs)
-        continue;
-
-      int symb_id = lhs->symb_id;
-      if (symbol_table.getType(symb_id) != SymbolType::endogenous)
-        continue;
-
-      set<pair<int, int>> endo;
-      equations[i]->arg2->collectEndogenous(endo);
-      if (endo.find({ symbol_table.getTypeSpecificID(symb_id), 0 }) != endo.end())
-        continue;
-
-      endo2eqs.emplace(symbol_table.getTypeSpecificID(symb_id), static_cast<int>(i));
-      cout << "Endogenous " << symbol_table.getName(symb_id) << " normalized in equation " << i+1 << endl;
-    }
-  return endo2eqs;
-}
-
 pair<ModelTree::jacob_map_t, ModelTree::jacob_map_t>
 ModelTree::evaluateAndReduceJacobian(const eval_context_t &eval_context, double cutoff, bool verbose)
 {
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 86e42b04..25cb3541 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -282,8 +282,6 @@ protected:
   void computeNonSingularNormalization(jacob_map_t &contemporaneous_jacobian, double cutoff, jacob_map_t &static_jacobian);
   //! Try to find a natural normalization if all equations are matched to an endogenous variable on the LHS
   bool computeNaturalNormalization();
-  //! Try to normalized each unnormalized equation (matched endogenous variable only on the LHS)
-  multimap<int, int> computeNormalizedEquations() const;
   //! Evaluate the jacobian (w.r.t. endogenous) and suppress all the elements below the cutoff
   /*! Returns a pair (contemporaneous_jacobian, static_jacobian). Also fills
     dynamic_jacobian. External functions are evaluated to 1. */
@@ -294,7 +292,7 @@ protected:
   tuple<vector<pair<int, int>>, lag_lead_vector_t, lag_lead_vector_t, vector<unsigned int>, vector<unsigned int>, vector<unsigned int>, vector<unsigned int>> select_non_linear_equations_and_variables(const vector<bool> &is_equation_linear);
   //! Search the equations and variables belonging to the prologue and the epilogue of the model
   void computePrologueAndEpilogue(const jacob_map_t &static_jacobian);
-  //! Determine the type of each equation of model and try to normalized the unnormalized equation using computeNormalizedEquations
+  //! Determine the type of each equation of model and try to normalize the unnormalized equation
   void equationTypeDetermination(const map<tuple<int, int, int>, expr_t> &first_order_endo_derivatives, int mfs);
   //! Compute the block decomposition and for a non-recusive block find the minimum feedback set
   /*! Returns a tuple (blocks, equation_lag_lead, variable_lag_lead, n_static,
-- 
GitLab