Verified Commit 9963686e authored by Houtan Bastani's avatar Houtan Bastani
Browse files

handle additive terms when PAC equation does NOT have non/optimizing agents

parent a3fc7a77
Pipeline #936 passed with stage
in 1 minute and 32 seconds
......@@ -3662,7 +3662,7 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
pair<int, int> lhs_pac_var;
int optim_share_index;
set<pair<int, pair<int, int>>> ar_params_and_vars;
pair<int, pair<vector<int>, vector<bool>>> ec_params_and_vars;
pair<int, vector<pair<int,bool>>> ec_params_and_vars;
vector<tuple<int, int, int, double>> non_optim_vars_params_and_constants, additive_vars_params_and_constants;
tie(lhs_pac_var, optim_share_index, ar_params_and_vars, ec_params_and_vars, non_optim_vars_params_and_constants, additive_vars_params_and_constants) = pit.second;
string substruct = pit.first.first + ".equations." + pit.first.second + ".";
......@@ -3677,12 +3677,30 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
output << modstruct << "pac." << substruct << "ec.params = "
<< symbol_table.getTypeSpecificID(ec_params_and_vars.first) + 1 << ";" << endl
<< modstruct << "pac." << substruct << "ec.vars = [";
for (auto it : ec_params_and_vars.second.first)
output << symbol_table.getTypeSpecificID(it) + 1 << " ";
for (auto it : ec_params_and_vars.second)
output << symbol_table.getTypeSpecificID(it.first) + 1 << " ";
output << "];" << endl
<< modstruct << "pac." << substruct << "ec.istarget = [";
for (auto it : ec_params_and_vars.second)
if (it.second)
output << "true ";
else
output << "false ";
output << "];" << endl
<< modstruct << "pac." << substruct << "ec.isendo = [";
for (auto it : ec_params_and_vars.second.second)
output << (it ? "true" : "false") << " ";
for (auto it : ec_params_and_vars.second)
switch (symbol_table.getType(it.first))
{
case SymbolType::endogenous:
output << "true ";
break;
case SymbolType::exogenous:
output << "false ";
break;
default:
cerr << "expecting endogenous or exogenous" << endl;
exit(EXIT_FAILURE);
}
output << "];" << endl
<< modstruct << "pac." << substruct << "ar.params = [";
for (auto & it : ar_params_and_vars)
......@@ -4419,7 +4437,7 @@ DynamicModel::walkPacParameters(const string &name, map<pair<string, string>, pa
for (auto & equation : equations)
{
pair<int, int> lhs (-1, -1);
pair<int, pair<vector<int>, vector<bool>>> ec_params_and_vars;
pair<int, vector<pair<int,bool>>> ec_params_and_vars;
set<pair<int, pair<int, int>>> ar_params_and_vars;
vector<tuple<int, int, int, double>> non_optim_vars_params_and_constants, additive_vars_params_and_constants;
......@@ -4451,10 +4469,24 @@ DynamicModel::walkPacParameters(const string &name, map<pair<string, string>, pa
arg2->getPacOptimizingShareAndExprNodes(lhs_symb_id, lhs_orig_symb_id);
if (optim_part == nullptr)
equation->arg2->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
{
auto bopn = dynamic_cast<BinaryOpNode *>(equation->arg2);
if (bopn == nullptr)
{
cerr << "Error in PAC equation" << endl;
exit(EXIT_FAILURE);
}
bopn->getPacAREC(lhs_symb_id, lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars, additive_vars_params_and_constants);
}
else
{
optim_part->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
auto bopn = dynamic_cast<BinaryOpNode *>(optim_part);
if (bopn == nullptr)
{
cerr << "Error in PAC equation" << endl;
exit(EXIT_FAILURE);
}
bopn->getPacAREC(lhs_symb_id, lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars, additive_vars_params_and_constants);
try
{
non_optim_vars_params_and_constants = non_optim_part->matchLinearCombinationOfVariables();
......@@ -4487,7 +4519,7 @@ DynamicModel::walkPacParameters(const string &name, map<pair<string, string>, pa
cerr << "walkPacParameters: error obtaining LHS varibale." << endl;
exit(EXIT_FAILURE);
}
if (ec_params_and_vars.second.first.empty() || ar_params_and_vars.empty())
if (ec_params_and_vars.second.empty() || ar_params_and_vars.empty())
{
cerr << "walkPacParameters: error obtaining RHS parameters." << endl;
exit(EXIT_FAILURE);
......
......@@ -487,7 +487,7 @@ public:
//! (pac_model_name, standardized_eqtag) ->
//! (lhs, optim_share_index, ar_params_and_vars, ec_params_and_vars, non_optim_vars_params_and_constants)
map<pair<string, string>,
tuple<pair<int, int>, int, set<pair<int, pair<int, int>>>, pair<int, pair<vector<int>, vector<bool>>>, vector<tuple<int, int, int, double>>, vector<tuple<int, int, int, double>>>> pac_equation_info;
tuple<pair<int, int>, int, set<pair<int, pair<int, int>>>, pair<int, vector<pair<int,bool>>>, vector<tuple<int, int, int, double>>, vector<tuple<int, int, int, double>>>> pac_equation_info;
//! Table to undiff LHS variables for pac vector z
vector<int> getUndiffLHSForPac(const string &aux_model_name,
......
......@@ -674,12 +674,6 @@ NumConstNode::isParamTimesEndogExpr() const
return false;
}
void
NumConstNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
}
bool
NumConstNode::isVarModelReferenced(const string &model_info_name) const
{
......@@ -1968,12 +1962,6 @@ VariableNode::isParamTimesEndogExpr() const
return false;
}
void
VariableNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
}
bool
VariableNode::isVarModelReferenced(const string &model_info_name) const
{
......@@ -3778,14 +3766,6 @@ UnaryOpNode::isParamTimesEndogExpr() const
return arg->isParamTimesEndogExpr();
}
void
UnaryOpNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
arg->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
}
bool
UnaryOpNode::isVarModelReferenced(const string &model_info_name) const
{
......@@ -5549,7 +5529,7 @@ BinaryOpNode::findTargetVariable(int lhs_symb_id) const
retval = arg2->findTargetVariable(lhs_symb_id);
return retval;
}
/*
void
BinaryOpNode::getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
int lhs_orig_symb_id,
......@@ -5563,6 +5543,7 @@ BinaryOpNode::getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
set<pair<int, int>> endogs;
arg2->collectDynamicVariables(SymbolType::endogenous, endogs);
arg2->collectDynamicVariables(SymbolType::exogenous, endogs);
if (endogs.size() == 1)
ar_params_and_vars.emplace(*(params.begin()), *(endogs.begin()));
else if (endogs.size() >= 2)
......@@ -5578,6 +5559,7 @@ BinaryOpNode::getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
vector<bool> order;
endogs.clear();
test_arg1->collectDynamicVariables(SymbolType::endogenous, endogs);
test_arg1->collectDynamicVariables(SymbolType::exogenous, endogs);
endog_ids.push_back(endogs.begin()->first);
if (endogs.begin()->first == lhs_orig_symb_id)
order.push_back(true);
......@@ -5586,6 +5568,7 @@ BinaryOpNode::getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
endogs.clear();
test_arg2->collectDynamicVariables(SymbolType::endogenous, endogs);
test_arg2->collectDynamicVariables(SymbolType::exogenous, endogs);
endog_ids.push_back(endogs.begin()->first);
if (endogs.begin()->first == lhs_orig_symb_id)
order.push_back(true);
......@@ -5597,21 +5580,137 @@ BinaryOpNode::getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
}
}
}
*/
pair<int, vector<pair<int,bool>>>
BinaryOpNode::getPacEC(BinaryOpNode *bopn, int lhs_symb_id, int lhs_orig_symb_id) const
{
pair<int, vector<pair<int,bool>>> ec_params_and_vars = {-1, vector<pair<int, bool>>()};
int optim_param_symb_id = -1;
expr_t optim_part = nullptr;
set<pair<int, int>> endogs;
bopn->collectDynamicVariables(SymbolType::endogenous, endogs);
int target_symb_id = getPacTargetSymbIdHelper(lhs_symb_id, lhs_orig_symb_id, endogs);
if (target_symb_id >= 0 && bopn->isParamTimesEndogExpr())
{
optim_part = bopn->arg2;
auto vn = dynamic_cast<VariableNode *>(bopn->arg1);
if (vn == nullptr || datatree.symbol_table.getType(vn->symb_id) != SymbolType::parameter)
{
optim_part = bopn->arg1;
vn = dynamic_cast<VariableNode *>(bopn->arg2);
}
if (vn == nullptr || datatree.symbol_table.getType(vn->symb_id) != SymbolType::parameter)
return ec_params_and_vars;
optim_param_symb_id = vn->symb_id;
}
if (optim_param_symb_id >= 0)
{
endogs.clear();
optim_part->collectDynamicVariables(SymbolType::endogenous, endogs);
optim_part->collectDynamicVariables(SymbolType::exogenous, endogs);
vector<pair<int,bool>> symb_ids;
for (const auto & it : endogs)
{
int id = it.first;
bool istarget = false;
while (datatree.symbol_table.isAuxiliaryVariable(id))
try
{
id = datatree.symbol_table.getOrigSymbIdForAuxVar(id);
}
catch (...)
{
break;
}
if (id == lhs_symb_id || id == lhs_orig_symb_id)
istarget = true;
symb_ids.push_back({it.first, istarget});
}
ec_params_and_vars = make_pair(optim_param_symb_id, symb_ids);
}
return ec_params_and_vars;
}
void
BinaryOpNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
BinaryOpNode::getPacAREC(int lhs_symb_id, int lhs_orig_symb_id,
pair<int, vector<pair<int,bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars,
vector<tuple<int, int, int, double>> &additive_vars_params_and_constants) const
{
if (op_code == BinaryOpcode::times)
vector<pair<expr_t, int>> terms;
decomposeAdditiveTerms(terms, 1);
for (auto it = terms.begin(); it != terms.end(); it++)
{
auto bopn = dynamic_cast<BinaryOpNode *>(it->first);
if (bopn != nullptr)
{
ec_params_and_vars = getPacEC(bopn, lhs_symb_id, lhs_orig_symb_id);
if (ec_params_and_vars.first >= 0)
{
terms.erase(it);
break;
}
}
}
if (ec_params_and_vars.first < 0)
{
int orig_ar_params_and_vars_size = ar_params_and_vars.size();
getPacOptimizingPartHelper(arg1, arg2, lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
if ((int)ar_params_and_vars.size() == orig_ar_params_and_vars_size && ec_params_and_vars.second.first.empty())
getPacOptimizingPartHelper(arg2, arg1, lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
cerr << "Error finding EC part of PAC equation" << endl;
exit(EXIT_FAILURE);
}
arg1->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
arg2->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
for (const auto & it : terms)
{
auto bopn = dynamic_cast<BinaryOpNode *>(it.first);
if (bopn != nullptr)
{
auto vn1 = dynamic_cast<VariableNode *>(bopn->arg1);
auto vn2 = dynamic_cast<VariableNode *>(bopn->arg2);
if (vn1 && vn2)
{
int pid, vid, lag;
pid = vid = lag = -1;
if (datatree.symbol_table.getType(vn1->symb_id) == SymbolType::parameter
&& (datatree.symbol_table.getType(vn2->symb_id) == SymbolType::endogenous
|| datatree.symbol_table.getType(vn2->symb_id) == SymbolType::exogenous))
{
pid = vn1->symb_id;
vid = vn2->symb_id;
lag = vn2->lag;
}
else if (datatree.symbol_table.getType(vn2->symb_id) == SymbolType::parameter
&& (datatree.symbol_table.getType(vn1->symb_id) == SymbolType::endogenous
|| datatree.symbol_table.getType(vn1->symb_id) == SymbolType::exogenous))
{
pid = vn2->symb_id;
vid = vn1->symb_id;
lag = vn1->lag;
}
if (pid > 0 && vid > 0)
{
int vidorig = vid;
while (datatree.symbol_table.isAuxiliaryVariable(vid))
try
{
vid = datatree.symbol_table.getOrigSymbIdForAuxVar(vid);
}
catch (...)
{
break;
}
if (vid == lhs_symb_id || vid == lhs_orig_symb_id)
ar_params_and_vars.insert({pid, {vidorig, lag}});
else
{
auto m = it.first->matchVariableTimesConstantTimesParam();
get<3>(m) *= it.second;
additive_vars_params_and_constants.push_back(m);
}
}
}
}
}
}
bool
......@@ -6928,15 +7027,6 @@ TrinaryOpNode::isParamTimesEndogExpr() const
|| arg3->isParamTimesEndogExpr();
}
void
TrinaryOpNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
arg1->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
arg2->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
arg3->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
}
bool
TrinaryOpNode::isVarModelReferenced(const string &model_info_name) const
{
......@@ -7485,14 +7575,6 @@ AbstractExternalFunctionNode::isParamTimesEndogExpr() const
return false;
}
void
AbstractExternalFunctionNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
for (auto argument : arguments)
argument->getPacOptimizingPart(lhs_orig_symb_id, ec_params_and_vars, ar_params_and_vars);
}
bool
AbstractExternalFunctionNode::isVarModelReferenced(const string &model_info_name) const
{
......@@ -9118,12 +9200,6 @@ VarExpectationNode::isParamTimesEndogExpr() const
return false;
}
void
VarExpectationNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
}
expr_t
VarExpectationNode::substituteStaticAuxiliaryVariable() const
{
......@@ -9596,12 +9672,6 @@ PacExpectationNode::isParamTimesEndogExpr() const
return false;
}
void
PacExpectationNode::getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const
{
}
expr_t
PacExpectationNode::substitutePacExpectation(const string & name, expr_t subexpr)
{
......
......@@ -583,10 +583,6 @@ class ExprNode
//! Returns true if model_info_name is referenced by a VarExpectationNode
virtual bool isVarModelReferenced(const string &model_info_name) const = 0;
//! Fills parameter information rerhs_symblated to PAC equation
virtual void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const = 0;
//! Matches a linear combination of variables, where scalars can be constant*parameter
/*! Returns a list of (variable_id, lag, param_id, constant)
corresponding to the terms in the expression. When there is no
......@@ -727,8 +723,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -814,8 +808,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -929,8 +921,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -995,10 +985,6 @@ public:
double eval(const eval_context_t &eval_context) const noexcept(false) override;
void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, const deriv_node_temp_terms_t &tef_terms) const override;
expr_t Compute_RHS(expr_t arg1, expr_t arg2, int op, int op_type) const;
void getPacOptimizingPartHelper(const expr_t arg1, const expr_t arg2,
int lhs_orig_symb_id,
pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars) const;
expr_t toStatic(DataTree &static_datatree) const override;
void computeXrefs(EquationInfo &ei) const override;
pair<int, expr_t> normalizeEquation(int symb_id_endo, vector<tuple<int, expr_t, expr_t>> &List_of_Op_RHS) const override;
......@@ -1062,8 +1048,11 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
pair<int, vector<pair<int,bool>>> getPacEC(BinaryOpNode *bopn, int lhs_symb_id, int lhs_orig_symb_id) const;
void getPacAREC(int lhs_symb_id, int lhs_orig_symb_id,
pair<int, vector<pair<int,bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &ar_params_and_vars,
vector<tuple<int, int, int, double>> &additive_vars_params_and_constants) const;
//! Finds the share of optimizing agents in the PAC equation,
//! the expr node associated with it,
......@@ -1182,8 +1171,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -1306,8 +1293,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -1518,8 +1503,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......@@ -1603,8 +1586,6 @@ public:
void findConstantEquations(map<VariableNode *, NumConstNode *> &table) const override;
expr_t replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const override;
bool containsPacExpectation(const string &pac_model_name = "") const override;
void getPacOptimizingPart(int lhs_orig_symb_id, pair<int, pair<vector<int>, vector<bool>>> &ec_params_and_vars,
set<pair<int, pair<int, int>>> &params_and_vars) const override;
bool isParamTimesEndogExpr() const override;
bool isVarModelReferenced(const string &model_info_name) const override;
void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment