From 5bbd779bab8f0a4f54e5c4f003de554193a13216 Mon Sep 17 00:00:00 2001 From: sebastien Date: Mon, 18 Dec 2006 23:28:52 +0000 Subject: [PATCH] v4 parser: * added planner_objective statement, which triggers the creation of filename_objective_static.m * added computing pass * added the possibility of writing the hessian in static output file git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1126 ac1d8469-bf42-47a9-8791-bf33cf982152 --- parser.src/ComputingTasks.cc | 81 ++++++++---- parser.src/DynareBison.yy | 13 +- parser.src/DynareFlex.ll | 1 + parser.src/ModFile.cc | 6 +- parser.src/ModelTree.cc | 119 ++++++++++++------ parser.src/NumericalInitialization.cc | 8 +- parser.src/ParsingDriver.cc | 82 +++++++----- parser.src/Shocks.cc | 4 +- parser.src/SigmaeInitialization.cc | 2 +- parser.src/Statement.cc | 7 +- parser.src/include/ComputingTasks.hh | 71 +++++++---- parser.src/include/ModelTree.hh | 11 +- parser.src/include/NumericalInitialization.hh | 8 +- parser.src/include/ParsingDriver.hh | 11 ++ parser.src/include/Shocks.hh | 4 +- parser.src/include/SigmaeInitialization.hh | 2 +- parser.src/include/Statement.hh | 10 +- 17 files changed, 290 insertions(+), 150 deletions(-) diff --git a/parser.src/ComputingTasks.cc b/parser.src/ComputingTasks.cc index 5db625223..57e192a63 100644 --- a/parser.src/ComputingTasks.cc +++ b/parser.src/ComputingTasks.cc @@ -18,7 +18,7 @@ SteadyStatement::SteadyStatement(const OptionsList &options_list_arg) : } void -SteadyStatement::writeOutput(ostream &output) const +SteadyStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); output << "steady;\n"; @@ -30,7 +30,7 @@ CheckStatement::CheckStatement(const OptionsList &options_list_arg) : } void -CheckStatement::writeOutput(ostream &output) const +CheckStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); output << "check;\n"; @@ -54,7 +54,7 @@ SimulStatement::checkPass(ModFileStructure &mod_file_struct) } void -SimulStatement::writeOutput(ostream &output) const +SimulStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); output << "simul(oo_.dr);\n"; @@ -74,7 +74,7 @@ StochSimulStatement::checkPass(ModFileStructure &mod_file_struct) } void -StochSimulStatement::writeOutput(ostream &output) const +StochSimulStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -95,7 +95,7 @@ EstimationStatement::checkPass(ModFileStructure &mod_file_struct) } void -EstimationStatement::writeOutput(ostream &output) const +EstimationStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -110,7 +110,7 @@ PriorAnalysisStatement::PriorAnalysisStatement(const TmpSymbolTable &tmp_symbol_ } void -PriorAnalysisStatement::writeOutput(ostream &output) const +PriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -125,7 +125,7 @@ PosteriorAnalysisStatement::PosteriorAnalysisStatement(const TmpSymbolTable &tmp } void -PosteriorAnalysisStatement::writeOutput(ostream &output) const +PosteriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -140,7 +140,7 @@ RplotStatement::RplotStatement(const TmpSymbolTable &tmp_symbol_table_arg, } void -RplotStatement::writeOutput(ostream &output) const +RplotStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -153,7 +153,7 @@ UnitRootVarsStatement::UnitRootVarsStatement(const TmpSymbolTable &tmp_symbol_ta } void -UnitRootVarsStatement::writeOutput(ostream &output) const +UnitRootVarsStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("options_.unit_root_vars", output); } @@ -163,7 +163,7 @@ PeriodsStatement::PeriodsStatement(int periods_arg) : periods(periods_arg) } void -PeriodsStatement::writeOutput(ostream &output) const +PeriodsStatement::writeOutput(ostream &output, const string &basename) const { output << "options_.periods = " << periods << ";" << endl; output << "options_.simul = 1;" << endl; @@ -178,7 +178,7 @@ DsampleStatement::DsampleStatement(int val1_arg, int val2_arg) : val1(val1_arg), } void -DsampleStatement::writeOutput(ostream &output) const +DsampleStatement::writeOutput(ostream &output, const string &basename) const { if (val2 < 0) output << "options_.dsample = " << val1 << ";" << endl; @@ -192,7 +192,7 @@ VarobsStatement::VarobsStatement(const TmpSymbolTable &tmp_symbol_table_arg) : } void -VarobsStatement::writeOutput(ostream &output) const +VarobsStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("options_.varobs", output); } @@ -205,7 +205,7 @@ EstimatedParamsStatement::EstimatedParamsStatement(const vector::const_iterator it; @@ -312,7 +312,7 @@ EstimatedParamsBoundsStatement::EstimatedParamsBoundsStatement(const vector::const_iterator it; @@ -367,7 +367,7 @@ ObservationTrendsStatement::ObservationTrendsStatement(const trend_elements_type } void -ObservationTrendsStatement::writeOutput(ostream &output) const +ObservationTrendsStatement::writeOutput(ostream &output, const string &basename) const { output << "options_.trend_coeff_ = {};" << endl; @@ -398,7 +398,7 @@ CalibVarStatement::CalibVarStatement(const calib_var_type &calib_var_arg, } void -CalibVarStatement::writeOutput(ostream &output) const +CalibVarStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << "\n" << interfaces::comment() << "CALIB_VAR \n" @@ -495,7 +495,7 @@ CalibStatement::CalibStatement(int covar_arg) : covar(covar_arg) } void -CalibStatement::writeOutput(ostream &output) const +CalibStatement::writeOutput(ostream &output, const string &basename) const { output << "M_.Sigma_e=calib(calib_var_index,calib_targets,calib_weights," << covar << ",Sigma_e_);\n"; } @@ -506,7 +506,7 @@ OsrParamsStatement::OsrParamsStatement(const TmpSymbolTable &tmp_symbol_table_ar } void -OsrParamsStatement::writeOutput(ostream &output) const +OsrParamsStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("osr_params_", output); } @@ -525,7 +525,7 @@ OsrStatement::checkPass(ModFileStructure &mod_file_struct) } void -OsrStatement::writeOutput(ostream &output) const +OsrStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -538,7 +538,7 @@ OlrInstStatement::OlrInstStatement(const TmpSymbolTable &tmp_symbol_table_arg) : } void -OlrInstStatement::writeOutput(ostream &output) const +OlrInstStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("options_.olr_inst", output); } @@ -558,7 +558,7 @@ OlrStatement::checkPass(ModFileStructure &mod_file_struct) } void -OlrStatement::writeOutput(ostream &output) const +OlrStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); tmp_symbol_table.writeOutput("var_list_", output); @@ -578,7 +578,7 @@ OptimWeightsStatement::OptimWeightsStatement(const var_weights_type &var_weights } void -OptimWeightsStatement::writeOutput(ostream &output) const +OptimWeightsStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << "OPTIM_WEIGHTS\n\n"; output << "optim_weights_ = sparse(M_.endo_nbr,M_.endo_nbr);\n"; @@ -616,7 +616,7 @@ DynaSaveStatement::DynaSaveStatement(const TmpSymbolTable &tmp_symbol_table_arg, } void -DynaSaveStatement::writeOutput(ostream &output) const +DynaSaveStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("var_list_", output); output << "dynasave(" << filename; @@ -634,7 +634,7 @@ DynaTypeStatement::DynaTypeStatement(const TmpSymbolTable &tmp_symbol_table_arg, } void -DynaTypeStatement::writeOutput(ostream &output) const +DynaTypeStatement::writeOutput(ostream &output, const string &basename) const { tmp_symbol_table.writeOutput("var_list_", output); output << "dynatype(" << filename; @@ -651,7 +651,7 @@ ModelComparisonStatement::ModelComparisonStatement(const filename_list_type &fil } void -ModelComparisonStatement::writeOutput(ostream &output) const +ModelComparisonStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); @@ -666,3 +666,32 @@ ModelComparisonStatement::writeOutput(ostream &output) const } output << "model_comparison(ModelNames_,ModelPriors_);\n"; } + +PlannerObjectiveStatement::PlannerObjectiveStatement(ModelTree *model_tree_arg) : + model_tree(model_tree_arg) +{ +} + +PlannerObjectiveStatement::~PlannerObjectiveStatement() +{ + delete model_tree; +} + +void +PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct) +{ + model_tree->checkPass(); +} + +void +PlannerObjectiveStatement::computingPass() +{ + model_tree->computeStaticHessian = true; + model_tree->computingPass(); +} + +void +PlannerObjectiveStatement::writeOutput(ostream &output, const string &basename) const +{ + model_tree->writeStaticFile(basename + "_objective"); +} diff --git a/parser.src/DynareBison.yy b/parser.src/DynareBison.yy index eb9d7e7c7..64e8d4767 100644 --- a/parser.src/DynareBison.yy +++ b/parser.src/DynareBison.yy @@ -44,7 +44,7 @@ typedef pair ExpObj; %token DATAFILE DIAGNOSTIC DIFFUSE_D DOLLAR DR_ALGO DROP DSAMPLE DYN2VEC DYNASAVE DYNATYPE %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT %token PRIOR_ANALYSIS POSTERIOR_ANALYSIS -%token FILTERED_VARS FIRST_OBS +%token FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS %token FLOAT_NUMBER %token FORECAST FUNCTIONS %token GAMMA_PDF GRAPH @@ -59,7 +59,7 @@ typedef pair ExpObj; %token NOBS NOCORR NODIAGNOSTIC NOFUNCTIONS NOGRAPH XLS_SHEET XLS_RANGE %token NOMOMENTS NOPRINT NORMAL_PDF %token OBSERVATION_TRENDS OLR OLR_INST OLR_BETA OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS -%token PARAMETERS PERIODS PREFILTER PRESAMPLE PRINT PRIOR_TRUNC FILTER_STEP_AHEAD +%token PARAMETERS PERIODS PLANNER_OBJECTIVE PREFILTER PRESAMPLE PRINT PRIOR_TRUNC %token QZ_CRITERIUM %token RELATIVE_IRF REPLIC RESOL RPLOT %token SHOCKS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SMOOTHER SOLVE_ALGO STDERR STEADY STOCH_SIMUL @@ -129,6 +129,7 @@ typedef pair ExpObj; | olr | olr_inst | model_comparison + | planner_objective ; @@ -330,10 +331,10 @@ typedef pair ExpObj; ; model - : MODEL ';' equation_list END - | MODEL '(' o_linear ')' ';' + : MODEL ';' { driver.begin_model(); } equation_list END + | MODEL '(' o_linear ')' ';' { driver.begin_model(); } equation_list END - | MODEL '(' USE_DLL ')' ';' {driver.use_dll();} + | MODEL '(' USE_DLL ')' ';' { driver.begin_model(); driver.use_dll(); } equation_list END ; @@ -1037,6 +1038,8 @@ typedef pair ExpObj; | o_noprint ; + planner_objective : PLANNER_OBJECTIVE { driver.begin_planner_objective(); } hand_side { driver.end_planner_objective($3); } ';' + filename_list : filename {driver.add_mc_filename($1);} | filename_list COMMA filename {driver.add_mc_filename($3);} | filename '(' value ')' {driver.add_mc_filename($1, $3);} diff --git a/parser.src/DynareFlex.ll b/parser.src/DynareFlex.ll index a9f477321..8ef936b74 100644 --- a/parser.src/DynareFlex.ll +++ b/parser.src/DynareFlex.ll @@ -80,6 +80,7 @@ int sigma_e = 0; dsample {BEGIN DYNARE_STATEMENT; return token::DSAMPLE;} Sigma_e {BEGIN DYNARE_STATEMENT; sigma_e = 1; return token::SIGMA_E;} calib {BEGIN DYNARE_STATEMENT; return token::CALIB;} +planner_objective {BEGIN DYNARE_STATEMENT; return token::PLANNER_OBJECTIVE;} /* End of a Dynare statement */ ; { diff --git a/parser.src/ModFile.cc b/parser.src/ModFile.cc index 8af994d9c..6b3d133d8 100644 --- a/parser.src/ModFile.cc +++ b/parser.src/ModFile.cc @@ -56,6 +56,10 @@ ModFile::computingPass() } model_tree.computingPass(); + + for(vector::iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->computingPass(); } void @@ -140,7 +144,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) // Print statements for(vector::iterator it = statements.begin(); it != statements.end(); it++) - (*it)->writeOutput(mOutputFile); + (*it)->writeOutput(mOutputFile, basename); mOutputFile << "save('" << basename << "_results', 'oo_');" << endl; mOutputFile << "diary off" << endl; diff --git a/parser.src/ModelTree.cc b/parser.src/ModelTree.cc index f6d482e48..65cfce48d 100644 --- a/parser.src/ModelTree.cc +++ b/parser.src/ModelTree.cc @@ -39,7 +39,8 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg, num_constants(num_constants_arg), computeJacobian(false), computeJacobianExo(false), - computeHessian(false) + computeHessian(false), + computeStaticHessian(false) { } @@ -56,7 +57,7 @@ ModelTree::writeStaticMFile(const string &static_basename) exit(-1); } // Writing comments and function definition command - mStaticModelFile << "function [residual, g1] = " << static_basename << "( y, x )\n"; + mStaticModelFile << "function [residual, g1, g2] = " << static_basename << "( y, x )\n"; mStaticModelFile << interfaces::comment()+"\n"+interfaces::comment(); mStaticModelFile << "Status : Computes static model for Dynare\n" << interfaces::comment() << "\n"; mStaticModelFile << interfaces::comment(); @@ -630,13 +631,10 @@ inline NodeID ModelTree::DeriveArgument(NodeID iArg, Type iType, int iVarID) void ModelTree::writeStaticModel(ostream &StaticOutput) { - TreeIterator tree_it; - int lEquationNBR = 0; ostringstream model_output; // Used for storing model equations - ostringstream model_tmp_output;// Used for storing tmp expressions for model equations ostringstream jacobian_output; // Used for storing jacobian equations - // Used for storing tmp expressions for jacobian equations - ostringstream jacobian_tmp_output; + ostringstream hessian_output; + ostringstream lsymetric; // For symmetric elements in hessian int d = current_order; // Minimum number of times a temparary expression apears in equations // Reference count of token "0=0" is set to 0 @@ -645,23 +643,24 @@ ModelTree::writeStaticModel(ostream &StaticOutput) ZeroEqZero->reference_count.end(),0); // Writing model Equations current_order = 1; - tree_it = BeginModel; + TreeIterator tree_it = BeginModel; + int lEquationNBR = 0; for (; tree_it != mModelTree.end(); tree_it++) { if ((*tree_it)->op_code == token::EQUAL || (*tree_it)->op_code == token::ASSIGN ) { if ((*tree_it)->id1->type1 == eLocalParameter) { - model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR); + model_output << getExpression((*tree_it)->id1, eStaticEquations); model_output << " = "; - model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl; + model_output << getExpression((*tree_it)->id2, eStaticEquations) << ";" << endl; } else if (lEquationNBR < eq_nbr) { model_output << "lhs ="; - model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR) << ";" << endl; + model_output << getExpression((*tree_it)->id1, eStaticEquations) << ";" << endl; model_output << "rhs ="; - model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl; + model_output << getExpression((*tree_it)->id2, eStaticEquations) << ";" << endl; model_output << "residual" << lpar << lEquationNBR+1 << rpar << "= lhs-rhs;" << endl; lEquationNBR++; } @@ -672,7 +671,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) { if (optimize(*tree_it)) { - model_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eStaticEquations, lEquationNBR) << ";" << endl; + model_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eStaticEquations) << ";" << endl; (*tree_it)->tmp_status = 1; } else @@ -682,7 +681,6 @@ ModelTree::writeStaticModel(ostream &StaticOutput) } } - // Writing Jacobian for endogenous variables without lag for(; tree_it != mModelTree.end(); tree_it++) { if ((*tree_it)->op_code != NoOpCode @@ -691,7 +689,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) { if (optimize(*tree_it) == 1) { - jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eStaticEquations, lEquationNBR) << ";" << endl; + jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eStaticEquations) << ";" << endl; (*tree_it)->tmp_status = 1; } else @@ -701,7 +699,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) } } - lEquationNBR = 0; + // Write Jacobian w.r. to endogenous only for (unsigned int i = 0; i < mDerivativeIndex[0].size(); i++) { if (variable_table.getType(mDerivativeIndex[0][i].derivators) == eEndogenous) @@ -718,6 +716,43 @@ ModelTree::writeStaticModel(ostream &StaticOutput) } } + // Write Hessian w.r. to endogenous only + if (computeStaticHessian) + { + for (unsigned int i = 0; i < mDerivativeIndex[1].size(); i++) + { + NodeID startHessian = mDerivativeIndex[1][i].token_id; + if (startHessian != ZeroEqZero) + { + string exp = getExpression(startHessian->id1, eStaticDerivatives); + + int varID1 = mDerivativeIndex[1][i].derivators / variable_table.size(); + int varID2 = mDerivativeIndex[1][i].derivators - varID1 * variable_table.size(); + + // Keep only derivatives w.r. to endogenous variables + if (variable_table.getType(varID1) != eEndogenous + || variable_table.getType(varID2) != eEndogenous) + continue; + + int id1 = variable_table.getSymbolID(varID1); + int id2 = variable_table.getSymbolID(varID2); + + int col_nb = id1*symbol_table.endo_nbr+id2+1; + int col_nb_sym = id2*symbol_table.endo_nbr+id1+1; + + hessian_output << " g2" << lpar << mDerivativeIndex[1][i].equation_id+1 << ", " << + col_nb << rpar << " = " << exp << ";\n"; + // Treating symetric elements + if (varID1 != varID2) + lsymetric << " g2" << lpar << mDerivativeIndex[1][i].equation_id+1 << ", " << + col_nb_sym << rpar << " = " << + "g2" << lpar << mDerivativeIndex[1][i].equation_id+1 << ", " << + col_nb << rpar << ";\n"; + } + + } + } + // Writing ouputs if (offset == 1) { @@ -744,6 +779,20 @@ ModelTree::writeStaticModel(ostream &StaticOutput) StaticOutput << " g1 = real(g1)+2*imag(g1);\n"; StaticOutput << " end\n"; StaticOutput << "end\n"; + if (computeStaticHessian) + { + StaticOutput << "if nargout >= 3,\n"; + // Writing initialization instruction for matrix g2 + int ncols = symbol_table.endo_nbr * symbol_table.endo_nbr; + StaticOutput << " g2 = " << + "sparse([],[],[]," << eq_nbr << ", " << ncols << ", " << + 5*ncols << ");\n"; + StaticOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + StaticOutput << "Hessian matrix\n\t"; + StaticOutput << interfaces::comment() + "\n\n"; + StaticOutput << hessian_output.str() << lsymetric.str(); + StaticOutput << "end;\n"; + } } else { @@ -770,17 +819,10 @@ ModelTree::writeStaticModel(ostream &StaticOutput) void ModelTree::writeDynamicModel(ostream &DynamicOutput) { - TreeIterator tree_it; - int lEquationNBR = 0; - ostringstream lsymetric; // Used when writing symetric elements + ostringstream lsymetric; // Used when writing symetric elements in Hessian ostringstream model_output; // Used for storing model equations - ostringstream model_tmp_output;// Used for storing tmp expressions for model equations ostringstream jacobian_output; // Used for storing jacobian equations - // Used for storing tmp expressions for jacobian equations - ostringstream jacobian_tmp_output; ostringstream hessian_output; // Used for storing Hessian equations - // Used for storing tmp expressions for Hessian equations - ostringstream hessian_tmp_output; int d = current_order; @@ -789,32 +831,29 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) fill(ZeroEqZero->reference_count.begin(), ZeroEqZero->reference_count.end(),0); - // Clearing output string - model_output.str(""); - jacobian_output.str(""); // Getting equations from model tree // Starting from the end of equation // Searching for the next '=' operator, current_order = 1; - lEquationNBR = 0; + int lEquationNBR = 0; cout << "\tequations .. "; - tree_it = BeginModel; + TreeIterator tree_it = BeginModel; for (; tree_it != mModelTree.end(); tree_it++) { if ((*tree_it)->op_code == token::EQUAL || (*tree_it)->op_code == token::ASSIGN) { if ((*tree_it)->id1->type1 == eLocalParameter) { - model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR); + model_output << getExpression((*tree_it)->id1, eStaticEquations); model_output << " = "; - model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl; + model_output << getExpression((*tree_it)->id2, eStaticEquations) << ";" << endl; } else if (lEquationNBR < eq_nbr) { model_output << "lhs ="; - model_output << getExpression(((*tree_it)->id1), eDynamicEquations, lEquationNBR) << ";" << endl; + model_output << getExpression(((*tree_it)->id1), eDynamicEquations) << ";" << endl; model_output << "rhs ="; - model_output << getExpression(((*tree_it)->id2), eDynamicEquations, lEquationNBR) << ";" << endl; + model_output << getExpression(((*tree_it)->id2), eDynamicEquations) << ";" << endl; model_output << "residual" << lpar << lEquationNBR+1 << rpar << "= lhs-rhs;" << endl; lEquationNBR++; } @@ -825,7 +864,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) { if (optimize(*tree_it)) { - model_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations, lEquationNBR) << ";" << endl; + model_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations) << ";" << endl; (*tree_it)->tmp_status = 1; } else @@ -843,7 +882,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) { if (optimize(*tree_it) == 1) { - jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations, lEquationNBR) << ";" << endl; + jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations) << ";" << endl; (*tree_it)->tmp_status = 1; } else @@ -865,7 +904,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) { if (optimize(*tree_it) == 1) { - jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations, lEquationNBR) << ";" << endl; + jacobian_output << "T" << (*tree_it)->idx << "=" << getExpression(*tree_it, eDynamicEquations) << ";" << endl; (*tree_it)->tmp_status = 1; } else @@ -875,7 +914,6 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) } } - lEquationNBR = 0; for (unsigned int i = 0; i < mDerivativeIndex[0].size(); i++) { if (computeJacobianExo || variable_table.getType(mDerivativeIndex[0][i].derivators) == eEndogenous) @@ -899,7 +937,6 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) // Getting Hessian from model tree // Starting from the end of equation // Searching for the next '=' operator, - lEquationNBR = 0; cout << "\tHessian .. "; for (unsigned int i = 0; i < mDerivativeIndex[1].size(); i++) { @@ -995,8 +1032,8 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) current_order = d; } -//------------------------------------------------------------------------------ -inline string ModelTree::getExpression(NodeID StartID, EquationType iEquationType, int iEquationID) +string +ModelTree::getExpression(NodeID StartID, EquationType iEquationType) { // Stack of tokens @@ -1342,7 +1379,7 @@ ModelTree::computingPass() rpar = ']'; } - if (computeHessian) + if (computeHessian || computeStaticHessian) derive(2); else derive(1); diff --git a/parser.src/NumericalInitialization.cc b/parser.src/NumericalInitialization.cc index 1fa823045..1c99143a1 100644 --- a/parser.src/NumericalInitialization.cc +++ b/parser.src/NumericalInitialization.cc @@ -11,7 +11,7 @@ InitParamStatement::InitParamStatement(const string ¶m_name_arg, } void -InitParamStatement::writeOutput(ostream &output) const +InitParamStatement::writeOutput(ostream &output, const string &basename) const { int id = symbol_table.getID(param_name) + 1; output << "M_.params( " << id << " ) = " << param_value << ";\n"; @@ -53,7 +53,7 @@ InitValStatement::InitValStatement(const init_values_type &init_values_arg, } void -InitValStatement::writeOutput(ostream &output) const +InitValStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << "\n" << interfaces::comment() << "INITVAL instructions \n" << interfaces::comment() << "\n"; @@ -83,7 +83,7 @@ EndValStatement::EndValStatement(const init_values_type &init_values_arg, void -EndValStatement::writeOutput(ostream &output) const +EndValStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << "\n" << interfaces::comment() << "ENDVAL instructions\n" << interfaces::comment() << "\n"; @@ -101,7 +101,7 @@ HistValStatement::HistValStatement(const hist_values_type &hist_values_arg, } void -HistValStatement::writeOutput(ostream &output) const +HistValStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << "\n" << interfaces::comment() << "HISTVAL instructions\n" << interfaces::comment() << "\n"; diff --git a/parser.src/ParsingDriver.cc b/parser.src/ParsingDriver.cc index 4c3729a9b..185ec69da 100644 --- a/parser.src/ParsingDriver.cc +++ b/parser.src/ParsingDriver.cc @@ -120,14 +120,14 @@ ParsingDriver::add_model_constant(string *constant) { int id = mod_file->num_constants.AddConstant(*constant); delete constant; - return mod_file->model_tree.AddTerminal((NodeID) id, eNumericalConstant); + return model_tree->AddTerminal((NodeID) id, eNumericalConstant); } NodeID ParsingDriver::add_model_variable(string *name) { check_symbol_existence(*name); - NodeID id = mod_file->model_tree.AddTerminal(*name); + NodeID id = model_tree->AddTerminal(*name); delete name; return id; } @@ -145,7 +145,7 @@ ParsingDriver::add_model_variable(string *name, string *olag) << *name << " has lag " << lag << endl; } - NodeID id = mod_file->model_tree.AddTerminal(*name, lag); + NodeID id = model_tree->AddTerminal(*name, lag); delete name; delete olag; return id; @@ -296,6 +296,12 @@ ParsingDriver::end_histval() hist_values.clear(); } +void +ParsingDriver::begin_model() +{ + model_tree = &mod_file->model_tree; +} + void ParsingDriver::end_shocks() { @@ -904,159 +910,175 @@ ParsingDriver::run_model_comparison() options_list.clear(); } +void +ParsingDriver::begin_planner_objective() +{ + model_tree = new ModelTree(mod_file->symbol_table, mod_file->num_constants); +} + +void +ParsingDriver::end_planner_objective(NodeID expr) +{ + // Add equation corresponding to expression + model_tree->AddEqual(expr, model_tree->Zero); + model_tree->eq_nbr++; + + mod_file->addStatement(new PlannerObjectiveStatement(model_tree)); +} + NodeID ParsingDriver::add_model_equal(NodeID arg1, NodeID arg2) { - NodeID id = mod_file->model_tree.AddEqual(arg1, arg2); - mod_file->model_tree.eq_nbr++; + NodeID id = model_tree->AddEqual(arg1, arg2); + model_tree->eq_nbr++; return id; } NodeID ParsingDriver::add_model_equal_with_zero_rhs(NodeID arg) { - return add_model_equal(arg, mod_file->model_tree.Zero); + return add_model_equal(arg, model_tree->Zero); } void ParsingDriver::declare_and_init_local_parameter(string *name, NodeID rhs) { mod_file->symbol_table.AddSymbolDeclar(*name, eLocalParameter, *name); - NodeID id = mod_file->model_tree.AddTerminal(*name); - mod_file->model_tree.AddAssign(id, rhs); + NodeID id = model_tree->AddTerminal(*name); + model_tree->AddAssign(id, rhs); delete name; } NodeID ParsingDriver::add_model_plus(NodeID arg1, NodeID arg2) { - return mod_file->model_tree.AddPlus(arg1, arg2); + return model_tree->AddPlus(arg1, arg2); } NodeID ParsingDriver::add_model_minus(NodeID arg1, NodeID arg2) { - return mod_file->model_tree.AddMinus(arg1, arg2); + return model_tree->AddMinus(arg1, arg2); } NodeID ParsingDriver::add_model_uminus(NodeID arg1) { - return mod_file->model_tree.AddUMinus(arg1); + return model_tree->AddUMinus(arg1); } NodeID ParsingDriver::add_model_times(NodeID arg1, NodeID arg2) { - return mod_file->model_tree.AddTimes(arg1, arg2); + return model_tree->AddTimes(arg1, arg2); } NodeID ParsingDriver::add_model_divide(NodeID arg1, NodeID arg2) { - return mod_file->model_tree.AddDivide(arg1, arg2); + return model_tree->AddDivide(arg1, arg2); } NodeID ParsingDriver::add_model_power(NodeID arg1, NodeID arg2) { - return mod_file->model_tree.AddPower(arg1, arg2); + return model_tree->AddPower(arg1, arg2); } NodeID ParsingDriver::add_model_exp(NodeID arg1) { - return mod_file->model_tree.AddExp(arg1); + return model_tree->AddExp(arg1); } NodeID ParsingDriver::add_model_log(NodeID arg1) { - return mod_file->model_tree.AddLog(arg1); + return model_tree->AddLog(arg1); } NodeID ParsingDriver::add_model_log10(NodeID arg1) { - return mod_file->model_tree.AddLog10(arg1); + return model_tree->AddLog10(arg1); } NodeID ParsingDriver::add_model_cos(NodeID arg1) { - return mod_file->model_tree.AddCos(arg1); + return model_tree->AddCos(arg1); } NodeID ParsingDriver::add_model_sin(NodeID arg1) { - return mod_file->model_tree.AddSin(arg1); + return model_tree->AddSin(arg1); } NodeID ParsingDriver::add_model_tan(NodeID arg1) { - return mod_file->model_tree.AddTan(arg1); + return model_tree->AddTan(arg1); } NodeID ParsingDriver::add_model_acos(NodeID arg1) { - return mod_file->model_tree.AddACos(arg1); + return model_tree->AddACos(arg1); } NodeID ParsingDriver::add_model_asin(NodeID arg1) { - return mod_file->model_tree.AddASin(arg1); + return model_tree->AddASin(arg1); } NodeID ParsingDriver::add_model_atan(NodeID arg1) { - return mod_file->model_tree.AddATan(arg1); + return model_tree->AddATan(arg1); } NodeID ParsingDriver::add_model_cosh(NodeID arg1) { - return mod_file->model_tree.AddCosH(arg1); + return model_tree->AddCosH(arg1); } NodeID ParsingDriver::add_model_sinh(NodeID arg1) { - return mod_file->model_tree.AddSinH(arg1); + return model_tree->AddSinH(arg1); } NodeID ParsingDriver::add_model_tanh(NodeID arg1) { - return mod_file->model_tree.AddTanH(arg1); + return model_tree->AddTanH(arg1); } NodeID ParsingDriver::add_model_acosh(NodeID arg1) { - return mod_file->model_tree.AddACosH(arg1); + return model_tree->AddACosH(arg1); } NodeID ParsingDriver::add_model_asinh(NodeID arg1) { - return mod_file->model_tree.AddASinH(arg1); + return model_tree->AddASinH(arg1); } NodeID ParsingDriver::add_model_atanh(NodeID arg1) { - return mod_file->model_tree.AddATanH(arg1); + return model_tree->AddATanH(arg1); } NodeID ParsingDriver::add_model_sqrt(NodeID arg1) { - return mod_file->model_tree.AddSqRt(arg1); + return model_tree->AddSqRt(arg1); } void diff --git a/parser.src/Shocks.cc b/parser.src/Shocks.cc index 580501f81..6879ac345 100644 --- a/parser.src/Shocks.cc +++ b/parser.src/Shocks.cc @@ -120,7 +120,7 @@ ShocksStatement::ShocksStatement(const det_shocks_type &det_shocks_arg, } void -ShocksStatement::writeOutput(ostream &output) const +ShocksStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << endl << interfaces::comment(); output << "SHOCKS instructions \n"; @@ -146,7 +146,7 @@ MShocksStatement::MShocksStatement(const det_shocks_type &det_shocks_arg, } void -MShocksStatement::writeOutput(ostream &output) const +MShocksStatement::writeOutput(ostream &output, const string &basename) const { output << interfaces::comment() << endl << interfaces::comment(); output << "SHOCKS instructions \n"; diff --git a/parser.src/SigmaeInitialization.cc b/parser.src/SigmaeInitialization.cc index a87510751..d363fc873 100644 --- a/parser.src/SigmaeInitialization.cc +++ b/parser.src/SigmaeInitialization.cc @@ -40,7 +40,7 @@ SigmaeStatement::determineMatrixForm(const matrix_type &matrix) throw (MatrixFor } void -SigmaeStatement::writeOutput(ostream &output) const +SigmaeStatement::writeOutput(ostream &output, const string &basename) const { unsigned int ic, ic1; unsigned int ir, ir1; diff --git a/parser.src/Statement.cc b/parser.src/Statement.cc index 3ac46a7d4..bdc4bb492 100644 --- a/parser.src/Statement.cc +++ b/parser.src/Statement.cc @@ -16,13 +16,18 @@ Statement::checkPass(ModFileStructure &mod_file_struct) { } +void +Statement::computingPass() +{ +} + NativeStatement::NativeStatement(const string &native_statement_arg) : native_statement(native_statement_arg) { } void -NativeStatement::writeOutput(ostream &output) const +NativeStatement::writeOutput(ostream &output, const string &basename) const { output << native_statement << endl; } diff --git a/parser.src/include/ComputingTasks.hh b/parser.src/include/ComputingTasks.hh index d8be88230..0bc864a23 100644 --- a/parser.src/include/ComputingTasks.hh +++ b/parser.src/include/ComputingTasks.hh @@ -6,6 +6,7 @@ #include "TmpSymbolTable.hh" #include "SymbolTable.hh" #include "Statement.hh" +#include "ModelTree.hh" class SteadyStatement : public Statement { @@ -13,7 +14,7 @@ private: const OptionsList options_list; public: SteadyStatement(const OptionsList &options_list_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class CheckStatement : public Statement @@ -23,7 +24,7 @@ private: public: CheckStatement(const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class SimulStatement : public Statement @@ -33,7 +34,7 @@ private: public: SimulStatement(const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class StochSimulStatement : public Statement @@ -45,7 +46,7 @@ public: StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class RplotStatement : public Statement @@ -56,7 +57,7 @@ private: public: RplotStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class UnitRootVarsStatement : public Statement @@ -65,7 +66,7 @@ private: const TmpSymbolTable tmp_symbol_table; public: UnitRootVarsStatement(const TmpSymbolTable &tmp_symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class PeriodsStatement : public Statement @@ -74,7 +75,7 @@ private: const int periods; public: PeriodsStatement(int periods_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class DsampleStatement : public Statement @@ -84,7 +85,7 @@ private: public: DsampleStatement(int val1_arg); DsampleStatement(int val1_arg, int val2_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class EstimationStatement : public Statement @@ -96,7 +97,7 @@ public: EstimationStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class PriorAnalysisStatement : public Statement @@ -107,7 +108,7 @@ private: public: PriorAnalysisStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class PosteriorAnalysisStatement : public Statement @@ -118,7 +119,7 @@ private: public: PosteriorAnalysisStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class VarobsStatement : public Statement @@ -127,7 +128,7 @@ private: const TmpSymbolTable tmp_symbol_table; public: VarobsStatement(const TmpSymbolTable &tmp_symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class ObservationTrendsStatement : public Statement @@ -140,7 +141,7 @@ private: public: ObservationTrendsStatement(const trend_elements_type &trend_elements_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OsrParamsStatement : public Statement @@ -149,7 +150,7 @@ private: const TmpSymbolTable tmp_symbol_table; public: OsrParamsStatement(const TmpSymbolTable &tmp_symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OsrStatement : public Statement @@ -161,7 +162,7 @@ public: OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OlrStatement : public Statement @@ -173,7 +174,7 @@ public: OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OlrInstStatement : public Statement @@ -182,7 +183,7 @@ private: const TmpSymbolTable tmp_symbol_table; public: OlrInstStatement(const TmpSymbolTable &tmp_symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class DynaTypeStatement : public Statement @@ -194,7 +195,7 @@ private: public: DynaTypeStatement(const TmpSymbolTable &tmp_symbol_table_arg, const string &filename_arg, const string &ext_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class DynaSaveStatement : public Statement @@ -206,7 +207,7 @@ private: public: DynaSaveStatement(const TmpSymbolTable &tmp_symbol_table_arg, const string &filename_arg, const string &ext_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class ModelComparisonStatement : public Statement @@ -219,7 +220,7 @@ private: public: ModelComparisonStatement(const filename_list_type &filename_list_arg, const OptionsList &options_list_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; /*! @@ -270,7 +271,7 @@ private: public: EstimatedParamsStatement(const vector &estim_params_list_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class EstimatedParamsInitStatement : public Statement @@ -281,7 +282,7 @@ private: public: EstimatedParamsInitStatement(const vector &estim_params_list_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class EstimatedParamsBoundsStatement : public Statement @@ -292,7 +293,7 @@ private: public: EstimatedParamsBoundsStatement(const vector &estim_params_list_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OptimWeightsStatement : public Statement @@ -308,7 +309,7 @@ public: OptimWeightsStatement(const var_weights_type &var_weights_arg, const covar_weights_type &covar_weights_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class CalibStatement : public Statement @@ -317,7 +318,7 @@ private: const int covar; public: CalibStatement(int covar_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class CalibVarStatement : public Statement @@ -339,7 +340,25 @@ public: const calib_covar_type &calib_covar_arg, const calib_ac_type &calib_ac_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +/*! \todo Make model_tree a member instead of a pointer */ +class PlannerObjectiveStatement : public Statement +{ +private: + ModelTree *model_tree; +public: + //! Constructor + /*! \param model_tree_arg the model tree used to store the objective function. + It is owned by the PlannerObjectiveStatement, and will be deleted by its destructor */ + PlannerObjectiveStatement(ModelTree *model_tree_arg); + virtual ~PlannerObjectiveStatement(); + /*! \todo check there are only endogenous variables at the current period in the objective + (no exogenous, no lead/lag) */ + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void computingPass(); + virtual void writeOutput(ostream &output, const string &basename) const; }; #endif diff --git a/parser.src/include/ModelTree.hh b/parser.src/include/ModelTree.hh index aa7991bd7..e9de1bd47 100644 --- a/parser.src/include/ModelTree.hh +++ b/parser.src/include/ModelTree.hh @@ -45,11 +45,12 @@ private : /*! Gets output argument of terminal token */ inline std::string getArgument(NodeID id, Type type, EquationType iEquationType); /*! Gets expression of part of model tree */ - inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1); + std::string getExpression(NodeID StartID, EquationType iEquationType); inline int optimize(NodeID id); /*! Computes derivatives of ModelTree */ void derive(int iOrder); //! Writes the static model equations and its derivatives + /*! \todo handle hessian in C output */ void writeStaticModel(std::ostream &StaticOutput); //! Writes the dynamic model equations and its derivatives void writeDynamicModel(std::ostream &DynamicOutput); @@ -69,12 +70,14 @@ public: int eq_nbr; //! Do some checking void checkPass() const; - //! Whether Jacobian (vs endogenous) should be written + //! Whether dynamic Jacobian (w.r. to endogenous) should be written bool computeJacobian; - //! Whether Jacobian (vs endogenous and exogenous) should be written + //! Whether dynamic Jacobian (w.r. to endogenous and exogenous) should be written bool computeJacobianExo; - //! Whether Hessian (vs endogenous and exogenous) should be written + //! Whether dynamic Hessian (w.r. to endogenous and exogenous) should be written bool computeHessian; + //! Whether static Hessian (w.r. to endogenous only) should be written + bool computeStaticHessian; //! Execute computations (variable sorting + derivation) /*! You must set computeJacobian, computeJacobianExo and computeHessian to correct values before calling this function */ diff --git a/parser.src/include/NumericalInitialization.hh b/parser.src/include/NumericalInitialization.hh index 56fd8690e..9592d7eb5 100644 --- a/parser.src/include/NumericalInitialization.hh +++ b/parser.src/include/NumericalInitialization.hh @@ -18,7 +18,7 @@ private: public: InitParamStatement(const string ¶m_name_arg, const string ¶m_value_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class InitOrEndValStatement : public Statement @@ -44,7 +44,7 @@ class InitValStatement : public InitOrEndValStatement public: InitValStatement(const init_values_type &init_values_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class EndValStatement : public InitOrEndValStatement @@ -52,7 +52,7 @@ class EndValStatement : public InitOrEndValStatement public: EndValStatement(const init_values_type &init_values_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class HistValStatement : public Statement @@ -69,7 +69,7 @@ private: public: HistValStatement(const hist_values_type &hist_values_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; #endif diff --git a/parser.src/include/ParsingDriver.hh b/parser.src/include/ParsingDriver.hh index 968aaec16..1da00c1e8 100644 --- a/parser.src/include/ParsingDriver.hh +++ b/parser.src/include/ParsingDriver.hh @@ -11,6 +11,7 @@ #include "Shocks.hh" #include "SigmaeInitialization.hh" #include "NumericalInitialization.hh" +#include "ModelTree.hh" using namespace std; @@ -49,6 +50,10 @@ private: Expression expression; //! Stores temporary symbol table TmpSymbolTable *tmp_symbol_table; + + //! The model tree in which to add expressions currently parsed + ModelTree *model_tree; + //! Stores options lists OptionsList options_list; //! Stores operator table @@ -185,6 +190,8 @@ public: void end_endval(); //! Writes end of an histval block void end_histval(); + //! Begin a model block + void begin_model(); //! Writes a shocks statement void end_shocks(); //! Writes a mshocks statement @@ -280,6 +287,10 @@ public: void run_dynatype(string *arg1, string *arg2 = new string); void add_mc_filename(string *filename, string *prior = new string("1")); void run_model_comparison(); + //! Begin a planner_objective statement + void begin_planner_objective(); + //! End a planner objective statement + void end_planner_objective(NodeID expr); //! Writes token "arg1=arg2" to model tree NodeID add_model_equal(NodeID arg1, NodeID arg2); //! Writes token "arg=0" to model tree diff --git a/parser.src/include/Shocks.hh b/parser.src/include/Shocks.hh index 57494d7da..cbadc9b1f 100644 --- a/parser.src/include/Shocks.hh +++ b/parser.src/include/Shocks.hh @@ -51,7 +51,7 @@ public: const covar_and_corr_shocks_type &covar_shocks_arg, const covar_and_corr_shocks_type &corr_shocks_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class MShocksStatement : public AbstractShocksStatement @@ -63,7 +63,7 @@ public: const covar_and_corr_shocks_type &covar_shocks_arg, const covar_and_corr_shocks_type &corr_shocks_arg, const SymbolTable &symbol_table_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; #endif diff --git a/parser.src/include/SigmaeInitialization.hh b/parser.src/include/SigmaeInitialization.hh index 45ee65cc3..18a5f25e3 100644 --- a/parser.src/include/SigmaeInitialization.hh +++ b/parser.src/include/SigmaeInitialization.hh @@ -39,7 +39,7 @@ private: public : SigmaeStatement(const matrix_type &matrix_arg) throw (MatrixFormException); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; #endif diff --git a/parser.src/include/Statement.hh b/parser.src/include/Statement.hh index 1e8cdda68..f71e6975c 100644 --- a/parser.src/include/Statement.hh +++ b/parser.src/include/Statement.hh @@ -25,7 +25,13 @@ public: virtual ~Statement(); //! Do some internal check, and fill the ModFileStructure class virtual void checkPass(ModFileStructure &mod_file_struct); - virtual void writeOutput(ostream &output) const = 0; + virtual void computingPass(); + //! Write Matlab outout code + /*! + \param output is the output stream of the main matlab file + \param basename is the name of the modfile (without extension) which can be used to build auxiliary files + */ + virtual void writeOutput(ostream &output, const string &basename) const = 0; }; class NativeStatement : public Statement @@ -34,7 +40,7 @@ private: const string native_statement; public: NativeStatement(const string &native_statement_arg); - virtual void writeOutput(ostream &output) const; + virtual void writeOutput(ostream &output, const string &basename) const; }; class OptionsList