def solveACycle(agent, solution_last): ''' Solve one "cycle" of the dynamic model for one agent type. This function iterates over the periods within an agent's cycle, updating the time-varying parameters and passing them to the single period solver(s). ''' # Calculate number of periods per cycle, defaults to 1 if all variables are time invariant if len(agent.time_vary) > 0: name = agent.time_vary[0] T = len(eval('agent.' + name)) else: T = 1 # Check whether the same solution method is used in all periods always_same_solver = 'solveAPeriod' not in agent.time_vary if always_same_solver: solveAPeriod = agent.solveAPeriod these_args = getArgNames(solveAPeriod) # Construct a dictionary to be passed to the solver time_inv_string = '' for name in agent.time_inv: time_inv_string += ' \'' + name + '\' : agent.' + name + ',' time_vary_string = '' for name in agent.time_vary: time_vary_string += ' \'' + name + '\' : None,' solve_dict = eval('{' + time_inv_string + time_vary_string + '}') # Initialize the solution for this cycle, then iterate on periods solution_cycle = [] solution_tp1 = solution_last for t in range(T): # Update which single period solver to use if not always_same_solver: solveAPeriod = agent.solveAPeriod[t] these_args = getArgNames(solveAPeriod) # Update time-varying single period inputs for name in agent.time_vary: if name in these_args: solve_dict[name] = eval('agent.' + name + '[t]') solve_dict['solution_tp1'] = solution_tp1 # Make a temporary dictionary for this period temp_dict = {name: solve_dict[name] for name in these_args} # Solve one period, add it to the solution, and move to the next period solution_t = solveAPeriod(**temp_dict) solution_cycle.append(solution_t) solution_tp1 = solution_t # Return the list of per-period solutions return solution_cycle
def solveACycle(agent,solution_last): ''' Solve one "cycle" of the dynamic model for one agent type. This function iterates over the periods within an agent's cycle, updating the time-varying parameters and passing them to the single period solver(s). ''' # Calculate number of periods per cycle, defaults to 1 if all variables are time invariant if len(agent.time_vary) > 0: name = agent.time_vary[0] T = len(eval('agent.' + name)) else: T = 1 # Check whether the same solution method is used in all periods always_same_solver = 'solveAPeriod' not in agent.time_vary if always_same_solver: solveAPeriod = agent.solveAPeriod these_args = getArgNames(solveAPeriod) # Construct a dictionary to be passed to the solver time_inv_string = '' for name in agent.time_inv: time_inv_string += ' \'' + name + '\' : agent.' +name + ',' time_vary_string = '' for name in agent.time_vary: time_vary_string += ' \'' + name + '\' : None,' solve_dict = eval('{' + time_inv_string + time_vary_string + '}') # Initialize the solution for this cycle, then iterate on periods solution_cycle = [] solution_tp1 = solution_last for t in range(T): # Update which single period solver to use if not always_same_solver: solveAPeriod = agent.solveAPeriod[t] these_args = getArgNames(solveAPeriod) # Update time-varying single period inputs for name in agent.time_vary: if name in these_args: solve_dict[name] = eval('agent.' + name + '[t]') solve_dict['solution_tp1'] = solution_tp1 # Make a temporary dictionary for this period temp_dict = {name: solve_dict[name] for name in these_args} # Solve one period, add it to the solution, and move to the next period solution_t = solveAPeriod(**temp_dict) solution_cycle.append(solution_t) solution_tp1 = solution_t # Return the list of per-period solutions return solution_cycle
def updateDynamics(self): ''' Calculates a new "aggregate dynamic rule" using the history of variables named in track_vars, and distributes this rule to AgentTypes in agents. Parameters ---------- none Returns ------- dynamics : instance The new "aggregate dynamic rule" that agents believe in and act on. Should have attributes named in dyn_vars. ''' # Make a dictionary of inputs for the dynamics calculator history_vars_string = '' arg_names = list(getArgNames(self.calcDynamics)) if 'self' in arg_names: arg_names.remove('self') for name in arg_names: history_vars_string += ' \'' + name + '\' : self.' + name + '_hist,' update_dict = eval('{' + history_vars_string + '}') # Calculate a new dynamic rule and distribute it to the agents in agent_list dynamics = self.calcDynamics(**update_dict) # User-defined dynamics calculator for var_name in self.dyn_vars: this_obj = getattr(dynamics,var_name) for this_type in self.agents: setattr(this_type,var_name,this_obj) return dynamics
def updateDynamics(self): ''' Calculates a new "aggregate dynamic rule" using the history of variables named in track_vars, and distributes this rule to AgentTypes in agents. Parameters ---------- none Returns ------- dynamics : instance The new "aggregate dynamic rule" that agents believe in and act on. Should have attributes named in dyn_vars. ''' # Make a dictionary of inputs for the dynamics calculator history_vars_string = '' arg_names = list(getArgNames(self.calcDynamics)) if 'self' in arg_names: arg_names.remove('self') for name in arg_names: history_vars_string += ' \'' + name + '\' : self.' + name + '_hist,' update_dict = eval('{' + history_vars_string + '}') # Calculate a new dynamic rule and distribute it to the agents in agent_list dynamics = self.calcDynamics( **update_dict) # User-defined dynamics calculator for var_name in self.dyn_vars: this_obj = getattr(dynamics, var_name) for this_type in self.agents: setattr(this_type, var_name, this_obj) return dynamics
def solveOneCycle(agent,solution_last): ''' Solve one "cycle" of the dynamic model for one agent type. This function iterates over the periods within an agent's cycle, updating the time-varying parameters and passing them to the single period solver(s). Parameters ---------- agent : AgentType The microeconomic AgentType whose dynamic problem is to be solved. solution_last : Solution A representation of the solution of the period that comes after the end of the sequence of one period problems. This might be the term- inal period solution, a "pseudo terminal" solution, or simply the solution to the earliest period from the succeeding cycle. Returns ------- solution_cycle : [Solution] A list of one period solutions for one "cycle" of the AgentType's microeconomic model. Returns in reverse chronological order. ''' # Calculate number of periods per cycle, defaults to 1 if all variables are time invariant if len(agent.time_vary) > 0: name = agent.time_vary[0] T = len(eval('agent.' + name)) else: T = 1 # Check whether the same solution method is used in all periods always_same_solver = 'solveOnePeriod' not in agent.time_vary if always_same_solver: solveOnePeriod = agent.solveOnePeriod these_args = getArgNames(solveOnePeriod) # Construct a dictionary to be passed to the solver time_inv_string = '' for name in agent.time_inv: time_inv_string += ' \'' + name + '\' : agent.' +name + ',' time_vary_string = '' for name in agent.time_vary: time_vary_string += ' \'' + name + '\' : None,' solve_dict = eval('{' + time_inv_string + time_vary_string + '}') # Initialize the solution for this cycle, then iterate on periods solution_cycle = [] solution_next = solution_last for t in range(T): # Update which single period solver to use (if it depends on time) if not always_same_solver: solveOnePeriod = agent.solveOnePeriod[t] these_args = getArgNames(solveOnePeriod) # Update time-varying single period inputs for name in agent.time_vary: if name in these_args: solve_dict[name] = eval('agent.' + name + '[t]') solve_dict['solution_next'] = solution_next # Make a temporary dictionary for this period temp_dict = {name: solve_dict[name] for name in these_args} # Solve one period, add it to the solution, and move to the next period solution_t = solveOnePeriod(**temp_dict) solution_cycle.append(solution_t) solution_next = solution_t # Return the list of per-period solutions return solution_cycle
def solveOneCycle(agent,solution_last): ''' Solve one "cycle" of the dynamic model for one agent type. This function iterates over the periods within an agent's cycle, updating the time-varying parameters and passing them to the single period solver(s). Parameters ---------- agent : AgentType The microeconomic AgentType whose dynamic problem is to be solved. solution_last : Solution A representation of the solution of the period that comes after the end of the sequence of one period problems. This might be the term- inal period solution, a "pseudo terminal" solution, or simply the solution to the earliest period from the succeeding cycle. Returns ------- solution_cycle : [Solution] A list of one period solutions for one "cycle" of the AgentType's microeconomic model. Returns in reverse chronological order. ''' # Calculate number of periods per cycle, defaults to 1 if all variables are time invariant if len(agent.time_vary) > 0: name = agent.time_vary[0] T = len(eval('agent.' + name)) else: T = 1 # Check whether the same solution method is used in all periods always_same_solver = 'solveOnePeriod' not in agent.time_vary if always_same_solver: solveOnePeriod = agent.solveOnePeriod these_args = getArgNames(solveOnePeriod) # Construct a dictionary to be passed to the solver time_inv_string = '' for name in agent.time_inv: time_inv_string += ' \'' + name + '\' : agent.' +name + ',' time_vary_string = '' for name in agent.time_vary: time_vary_string += ' \'' + name + '\' : None,' solve_dict = eval('{' + time_inv_string + time_vary_string + '}') # Initialize the solution for this cycle, then iterate on periods solution_cycle = [] solution_next = solution_last for t in range(T): # Update which single period solver to use (if it depends on time) if not always_same_solver: solveOnePeriod = agent.solveOnePeriod[t] these_args = getArgNames(solveOnePeriod) # Update time-varying single period inputs for name in agent.time_vary: if name in these_args: solve_dict[name] = eval('agent.' + name + '[t]') solve_dict['solution_next'] = solution_next # Make a temporary dictionary for this period temp_dict = {name: solve_dict[name] for name in these_args} #PNG addition 2016-06-30 settings.t_curr = t #temp_dict['t_curr'] = t # Solve one period, add it to the solution, and move to the next period solution_t = solveOnePeriod(**temp_dict) solution_cycle.append(solution_t) solution_next = solution_t # Return the list of per-period solutions return solution_cycle