From 61c731f92532f6fafadc8cdf00e5a42ccaeb8b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Tue, 14 May 2024 18:13:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20MCP=20solver=20in=20the=20?= =?UTF-8?q?presence=20of=20multiple=20complementarity=20conditions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The solver relies on a reordering of equations (eq_index) so that reordered equations appear at the index of the endogenous to which they are associated through the complementarity condition. However, this reordering could be computed incorrectly in the presence of multiple complementarity conditions (technically, eq_index was not necessarily a permutation). (cherry picked from commit 1fea7f3e966b5c7803d4905ced29207440d13d57) --- matlab/lmmcp/get_complementarity_conditions.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/matlab/lmmcp/get_complementarity_conditions.m b/matlab/lmmcp/get_complementarity_conditions.m index 5d3f961f59..9aed55f4d3 100644 --- a/matlab/lmmcp/get_complementarity_conditions.m +++ b/matlab/lmmcp/get_complementarity_conditions.m @@ -12,7 +12,7 @@ function [lb,ub,eq_index] = get_complementarity_conditions(M_,ramsey_policy) % from complementarity setup used in % perfect_foresight_mcp_problem.m -% Copyright © 2014-2018 Dynare Team +% Copyright © 2014-2024 Dynare Team % % This file is part of Dynare. % @@ -60,7 +60,9 @@ for i=1:size(etags,1) error('Complementarity condition %s: variable %s is not recognized', etags{i,3}, strtrim(str(1:kop-1))) end ub(k) = str2num(str(kop+1:end)); - eq_index(eq_nbr) = k; + swapped_eq_nbr = find(eq_index == eq_nbr); + assert(length(swapped_eq_nbr) == 1); + eq_index(swapped_eq_nbr) = eq_index(k); eq_index(k) = eq_nbr; else kop = strfind(etags{i,3},'>'); @@ -70,7 +72,9 @@ for i=1:size(etags,1) error('Complementarity condition %s: variable %s is not recognized', etags{i,3}, strtrim(str(1:kop-1))) end lb(k) = str2num(str(kop+1:end)); - eq_index(eq_nbr) = k; + swapped_eq_nbr = find(eq_index == eq_nbr); + assert(length(swapped_eq_nbr) == 1); + eq_index(swapped_eq_nbr) = eq_index(k); eq_index(k) = eq_nbr; else error('Complementarity condition %s can''t be parsed',etags{i,3}) -- GitLab