def __compile_functions__(self): from dolo.compiler.function_compiler_ast import compile_function_ast from dolo.compiler.function_compiler import standard_function defs = self.symbolic.definitions # works for fg models only recipe = recipes[self.model_type] symbols = self.symbols # should match self.symbols comps = [] functions = {} for funname in recipe['specs'].keys(): spec = recipe['specs'][funname] if funname not in self.symbolic.equations: if not spec.get('optional'): raise Exception("The model doesn't contain equations of type '{}'.".format(funname)) else: continue if spec.get('target'): # keep only right-hand side # TODO: restore recursive definitions eqs = self.symbolic.equations[funname] eqs = [eq.split('=')[1] for eq in eqs] eqs = [str.strip(eq) for eq in eqs] target_spec = spec.get('target') n_output = len(self.symbols[target_spec[0]]) # target_short_name = spec.get('target')[2] target_spec[2] = 'out' else: target_spec = None if spec.get('complementarities'): comp_spec = spec.get('complementarities') comp_order = comp_spec['middle'] comp_args = comp_spec['left-right'] comps = [] eqs = [] for i,eq in enumerate(self.symbolic.equations[funname]): if '|' in eq: control = self.symbols[comp_order[0]][i] eq, comp = str.split(eq,'|') lhs, rhs = decode_complementarity(comp, control) comps.append([lhs, rhs]) else: comps.append(['-inf', 'inf']) eqs.append(eq) comp_lhs, comp_rhs = zip(*comps) fb_names = ['{}_lb'.format(funname), '{}_ub'.format(funname)] lower_bound = compile_function_ast(comp_lhs, symbols, comp_args, funname=fb_names[0], use_numexpr=False, definitions=defs) upper_bound = compile_function_ast(comp_rhs, symbols, comp_args, funname=fb_names[1], use_numexpr=False, definitions=defs) n_output = len(comp_lhs) functions[fb_names[0]] = standard_function(lower_bound, n_output ) functions[fb_names[1]] = standard_function(upper_bound, n_output ) # rewrite all equations as rhs - lhs def filter_equal(eq): if '=' in eq: lhs, rhs = str.split(eq,'=') eq = '{} - ( {} )'.format(rhs, lhs) eq = str.strip(eq) return eq else: return eq eqs = [filter_equal(eq) for eq in eqs] arg_names = recipe['specs'][funname]['eqs'] fun = compile_function_ast(eqs, symbols, arg_names, output_names=target_spec, funname=funname, use_numexpr=True, definitions=defs ) n_output = len(eqs) functions[funname] = standard_function(fun, n_output ) self.functions = functions
def __compile_functions__(self): from dolo.compiler.function_compiler_ast import compile_function_ast from dolo.compiler.function_compiler import standard_function defs = self.symbolic.definitions # works for fg models only model_type = self.model_type if 'auxiliaries' not in self.symbols: model_type += '_' else: # prepare auxiliaries auxeqs = self.symbolic.equations['auxiliary'] auxdefs = {} for time in [-1,0,1]: dd = OrderedDict() for eq in auxeqs: lhs, rhs = eq.split('=') lhs = ast.parse( str.strip(lhs) ).body[0].value rhs = ast.parse( str.strip(rhs) ).body[0].value tmp = timeshift(rhs, self.variables, time) k = timeshift(lhs, self.variables, time) k = StandardizeDatesSimple(self.variables).visit(k) v = StandardizeDatesSimple(self.variables).visit(tmp) dd[to_source(k)] = to_source(v) auxdefs[time] = dd recipe = recipes[model_type] symbols = self.symbols # should match self.symbols comps = [] functions = {} original_functions = {} original_gufunctions = {} for funname in recipe['specs'].keys(): spec = recipe['specs'][funname] if funname not in self.symbolic.equations: if not spec.get('optional'): raise Exception("The model doesn't contain equations of type '{}'.".format(funname)) else: continue if spec.get('target'): # keep only right-hand side # TODO: restore recursive definitions eqs = self.symbolic.equations[funname] eqs = [eq.split('=')[1] for eq in eqs] eqs = [str.strip(eq) for eq in eqs] target_spec = spec.get('target') n_output = len(self.symbols[target_spec[0]]) # target_short_name = spec.get('target')[2] if spec.get('recursive') is False: target_spec = None else: target_spec[2] = 'out' else: target_spec = None if spec.get('complementarities'): # TODO: Rewrite and simplify comp_spec = spec.get('complementarities') comp_order = comp_spec['middle'] comp_args = comp_spec['left-right'] comps = [] eqs = [] for i,eq in enumerate(self.symbolic.equations[funname]): if '|' in eq: control = self.symbols[comp_order[0]][i] eq, comp = str.split(eq,'|') lhs, rhs = decode_complementarity(comp, control) comps.append([lhs, rhs]) else: comps.append(['-inf', 'inf']) eqs.append(eq) comp_lhs, comp_rhs = zip(*comps) # fb_names = ['{}_lb'.format(funname), '{}_ub'.format(funname)] fb_names = ['controls_lb'.format(funname), 'controls_ub'.format(funname)] ddefs = OrderedDict() for ag in comp_args: if ag[0] == 'auxiliaries': t = ag[1] ddefs.update(auxdefs[t]) ddefs.update(defs) lower_bound, gu_lower_bound = compile_function_ast(comp_lhs, symbols, comp_args, funname=fb_names[0],definitions=defs) upper_bound, gu_upper_bound = compile_function_ast(comp_rhs, symbols, comp_args, funname=fb_names[1],definitions=defs) n_output = len(comp_lhs) functions[fb_names[0]] = standard_function(gu_lower_bound, n_output ) functions[fb_names[1]] = standard_function(gu_upper_bound, n_output ) original_functions[fb_names[0]] = lower_bound original_functions[fb_names[1]] = upper_bound original_gufunctions[fb_names[0]] = gu_lower_bound original_gufunctions[fb_names[1]] = gu_upper_bound # rewrite all equations as rhs - lhs def filter_equal(eq): if '=' in eq: lhs, rhs = str.split(eq,'=') eq = '{} - ( {} )'.format(rhs, lhs) eq = str.strip(eq) return eq else: return eq eqs = [filter_equal(eq) for eq in eqs] arg_names = recipe['specs'][funname]['eqs'] ddefs = OrderedDict() for ag in arg_names: if ag[0] == 'auxiliaries': t = ag[1] ddefs.update(auxdefs[t]) ddefs.update(defs) fun, gufun = compile_function_ast(eqs, symbols, arg_names, output_names=target_spec, funname=funname, definitions=ddefs, ) # print("So far so good !")c n_output = len(eqs) original_functions[funname] = fun functions[funname] = standard_function(gufun, n_output ) original_functions[funname] = fun original_gufunctions[funname] = gufun self.__original_functions__ = original_functions self.__original_gufunctions__ = original_gufunctions self.functions = functions
if __name__ == '__main__': from dolo import * model = yaml_import("../../../examples/models/rbc_mfga.yaml") print(model.calibration['states']) print(model.calibration_dict) print(model.markov_chain) initial_guess_symbolic = [ 'i = delta*k', 'n = 0.33' ] from dolo.compiler.function_compiler_ast import compile_function_ast from dolo.compiler.function_compiler import standard_function arg_names = [ ['markov_states',0,'m'], ['states',0,'s'], ['parameters',0,'p'] ] fun = compile_function_ast(initial_guess_symbolic, model.symbols, arg_names,'initial_guess') ff = standard_function(fun, len(initial_guess_symbolic)) sol = solve_mfg_model(model, initial_guess=ff)
return mdr if __name__ == '__main__': from dolo import * model = yaml_import("../../../examples/models/compat/rbc_mfga.yaml") print(model.calibration['states']) print(model.calibration_dict) print(model.exogenous) initial_guess_symbolic = [ 'i = delta*k', 'n = 0.33' ] from dolo.compiler.function_compiler_ast import compile_function_ast from dolo.compiler.function_compiler import standard_function arg_names = [ ['exogenous',0,'m'], ['states',0,'s'], ['parameters',0,'p'] ] fun = compile_function_ast(initial_guess_symbolic, model.symbols, arg_names,'initial_guess') ff = standard_function(fun, len(initial_guess_symbolic)) sol = time_iteration(model, initial_guess=ff)
def __compile_functions__(self): from dolo.compiler.function_compiler_ast import compile_function_ast from dolo.compiler.function_compiler import standard_function defs = self.symbolic.definitions # works for fg models only recipe = recipes[self.model_type] symbols = self.symbols # should match self.symbols comps = [] functions = {} for funname in recipe['specs'].keys(): spec = recipe['specs'][funname] if funname not in self.symbolic.equations: if not spec.get('optional'): raise Exception( "The model doesn't contain equations of type '{}'.". format(funname)) else: continue if spec.get('target'): # keep only right-hand side # TODO: restore recursive definitions eqs = self.symbolic.equations[funname] eqs = [eq.split('=')[1] for eq in eqs] eqs = [str.strip(eq) for eq in eqs] target_spec = spec.get('target') n_output = len(self.symbols[target_spec[0]]) # target_short_name = spec.get('target')[2] target_spec[2] = 'out' else: target_spec = None if spec.get('complementarities'): comp_spec = spec.get('complementarities') comp_order = comp_spec['middle'] comp_args = comp_spec['left-right'] comps = [] eqs = [] for i, eq in enumerate(self.symbolic.equations[funname]): if '|' in eq: control = self.symbols[comp_order[0]][i] eq, comp = str.split(eq, '|') lhs, rhs = decode_complementarity(comp, control) comps.append([lhs, rhs]) else: comps.append(['-inf', 'inf']) eqs.append(eq) comp_lhs, comp_rhs = zip(*comps) fb_names = ['{}_lb'.format(funname), '{}_ub'.format(funname)] lower_bound = compile_function_ast(comp_lhs, symbols, comp_args, funname=fb_names[0], use_numexpr=False, definitions=defs) upper_bound = compile_function_ast(comp_rhs, symbols, comp_args, funname=fb_names[1], use_numexpr=False, definitions=defs) n_output = len(comp_lhs) functions[fb_names[0]] = standard_function( lower_bound, n_output) functions[fb_names[1]] = standard_function( upper_bound, n_output) # rewrite all equations as rhs - lhs def filter_equal(eq): if '=' in eq: lhs, rhs = str.split(eq, '=') eq = '{} - ( {} )'.format(rhs, lhs) eq = str.strip(eq) return eq else: return eq eqs = [filter_equal(eq) for eq in eqs] arg_names = recipe['specs'][funname]['eqs'] fun = compile_function_ast(eqs, symbols, arg_names, output_names=target_spec, funname=funname, use_numexpr=True, definitions=defs) n_output = len(eqs) functions[funname] = standard_function(fun, n_output) self.functions = functions