def main(parameters): simulation = Simulation(processes=1) firms = simulation.build_agents(Firm, 'firm', number=parameters['num_firms']) households = simulation.build_agents(Household, 'household', number=1, parameters=parameters) try: for rnd in range(parameters['rounds']): simulation.advance_round(rnd) households.refresh_services('labor', derived_from='adult', units=1) households.sell_labor() firms.buy_labor() firms.production() firms.panel_log(possessions=['money', 'GOOD'], variables=['price', 'inventory']) firms.quotes() households.buy_goods() firms.sell_goods() households.agg_log(possessions=['money', 'GOOD'], variables=['current_utiliy']) households.consumption() firms.adjust_price() except Exception as e: print(e) simulation.finalize()
def main(): """ Demonstration of the simulation.advance_round function, which can take arbitrary values """ simulation = Simulation() agents = simulation.build_agents(Agent, 'agent', number=1) weekday = 0 for year in range(2000, 2010): for month in range(12): for day in range(30): simulation.advance_round((year, month, day)) weekday = (weekday + 1) % 7 print(weekday) if weekday == 3: agents.wednessday() if day == 1: agents.first() if month == 12 and day == 31: agents.newyearseve() if day <= 7 and weekday == 5: agents.firstfriday() if day == 15: agents.fiveteens() agents.panel_log(goods=['money']) agents.agg_log(goods=['labor']) simulation.finalize()
def main(): s = Simulation() a = s.build_agents(MyAgent, 'myagent', 10000) for r in range(50): s.advance_round(r) a.do("compute") s.finalize()
def main(): s = Simulation(processes=1) myagents = s.build_agents(MyAgent, 'myagent', 500000) youragents = s.build_agents(YourAgent, 'youragent', 500000) for r in range(100): print('Round: %i' % r) s.advance_round(r) # (myagents+youragents).do('compute') youragents.s() myagents.g() s.finalize()
def main(simulation_parameters): s = Simulation() firms = s.build_agents(Firm, 'firm', 10) households = s.build_agents(Household, 'household', 10) for r in range(int(simulation_parameters['rounds'])): s.advance_round(r) firms.panel_log(possessions=['cookies']) firms.quote() households.buying() firms.selling() households.panel_log(possessions=['cookies']) households.consumption() s.finalize()
def main(): simulation_parameters = {'name': 'direct_optimization', 'random_seed': None, 'rounds': 500, 'trade_logging': 'off', 'num_firms': 250, 'alpha': 0.3, 'gamma': 0.8, 'price_stickiness': 0.0, 'network_weight_stickiness': 0.0, 'wage_stickiness': 0.0, 'dividends_percent': 0.0, 'percentage_beneficiaries': 0.25, 'percentage_injection': 0.25, 'time_of_intervention': 250} s = Simulation(name=simulation_parameters['name']) network = create_network(simulation_parameters['num_firms']) network = [{'neighbors': network.neighbors(neighbor)} for neighbor in range(simulation_parameters['num_firms'])] firms = s.build_agents(Firm, 'firm', **simulation_parameters, agent_parameters=[n for n in network]) household = s.build_agents(Household, 'household', 1, num_firms=simulation_parameters['num_firms'], wage_stickiness=simulation_parameters['wage_stickiness']) centralbank = s.build_agents(CentralBank, 'centralbank', 1, time_of_intervention=simulation_parameters['time_of_intervention'], percentage_injection=simulation_parameters['percentage_injection'], percentage_beneficiaries=simulation_parameters['percentage_beneficiaries']) for rnd in range(simulation_parameters['rounds']): s.advance_round(rnd) (firms + household).send_demand() (firms + household).selling() (firms + household).buying() firms.production() firms.dividends() household.consuming() firms.change_weights() firms.stats() centralbank.intervention() household.agg_log(goods=['money'], variables=['utility', 'rationing']) firms.agg_log(goods=['money'], variables=['produced', 'profit', 'price', 'dead', 'inventory', 'rationing']) (household + firms).refresh_services('labor', 'labor_endowment', units=1) s.finalize()
def main(simulation_parameters): s = Simulation(name=simulation_parameters['name']) network = create_network(simulation_parameters['num_firms']) network = [ network.neighbors(neighbor) for neighbor in range(simulation_parameters['num_firms']) ] firms = s.build_agents(Firm, 'firm', parameters=simulation_parameters, agent_parameters=network) household = s.build_agents(Household, 'household', 1, parameters=simulation_parameters) centralbank = s.build_agents(CentralBank, 'centralbank', 1, parameters=simulation_parameters) for rnd in range(simulation_parameters['rounds']): s.advance_round(rnd) (firms + household).send_demand() (firms + household).selling() (firms + household).buying() firms.production() firms.dividends() household.consuming() firms.change_weights() firms.stats() centralbank.intervention() household.agg_log(possessions=['money'], variables=['utility', 'rationing']) firms.agg_log(possessions=['money'], variables=[ 'produced', 'profit', 'price', 'dead', 'inventory', 'rationing' ]) (household + firms).refresh_services('labor', 'labor_endowment', units=1) s.finalize()
def main(simulation_parameters): s = Simulation() firms = s.build_agents(Firm, 'firm', parameters=simulation_parameters, number=1) market = s.build_agents(Market, 'market', parameters=simulation_parameters, number=1) for r in range(simulation_parameters['rounds']): s.advance_round(r) firms.my_production() firms.selling() market.buying() firms.adjust_price() firms.adjust_quantity() market.consumption() s.finalize()
def main(processes, rounds): s = Simulation(processes=processes, name='unittest') print('build Buy') buy = s.build_agents(Buy, 'buy', 1000, rounds=rounds) print('build Sell') sell = s.build_agents(Sell, 'sell', 1000, rounds=rounds) print('build Give') give = s.build_agents(Give, 'give', 2, rounds=rounds) print('build LoggerTest') loggertest = s.build_agents( LoggerTest, 'loggertest', 1, rounds=rounds) all = buy + sell + give + loggertest for r in range(rounds): s.advance_round(r) for _ in range(5): buy.one() buy.two() buy.three() buy.clean_up() buy.panel_log(variables=['price']) for _ in range(5): sell.one() sell.two() sell.three() sell.clean_up() for _ in range(5): give.one() give.two() give.three() give.clean_up() for _ in range(5): loggertest.one() loggertest.two() loggertest.three() loggertest.clean_up() all.all_tests_completed() s.finalize()
def main(processes, rounds): s = Simulation(processes=processes, name='unittest', trade_logging='group') print('build Buy') buy = s.build_agents(Buy, 'buy', 1000, rounds=rounds) print('build Sell') sell = s.build_agents(Sell, 'sell', 1000, rounds=rounds) print('build Give') give = s.build_agents(Give, 'give', 2, rounds=rounds) print('build LoggerTest') loggertest = s.build_agents(LoggerTest, 'loggertest', 1, rounds=rounds) all = buy + sell + give + loggertest for r in range(rounds): s.advance_round(r) for _ in range(5): buy.one() buy.two() buy.three() buy.clean_up() buy.panel_log(variables=['price']) for _ in range(5): sell.one() sell.two() sell.three() sell.clean_up() for _ in range(5): give.one() give.two() give.three() give.clean_up() for _ in range(5): loggertest.one() loggertest.two() loggertest.three() loggertest.clean_up() all.all_tests_completed() s.finalize()
def main(simulation_parameters): w = Simulation() firms = w.build_agents(Firm, 'firm', 2) households = w.build_agents(Household, 'household', 2) for r in range(simulation_parameters['rounds']): w.advance_round(r) households.refresh_services('labor', derived_from='labor_endowment', units=5) # to access round, just get the value of w.round # to access its datetime version, use w._round # todo, better naming households.sell_labor() firms.buy_inputs() firms.production() firms.panel_log(goods=['consumption_good', 'intermediate_good']) firms.sell_intermediary_goods() households.buy_intermediary_goods() households.panel_log(goods=['consumption_good']) households.consumption() w.finalize()
def main(): s = Simulation(processes=1) grid = MultiGrid(50, 50, True) # build sugar and spice sugar_distribution = pylab.genfromtxt("sugar-map.txt") spice_distribution = sugar_distribution.T sugars = [] spices = [] for _, x, y in grid.coord_iter(): max_sugar = sugar_distribution[x, y] max_spice = spice_distribution[x, y] sugar = SugarPatch((x, y), max_sugar) spice = SpicePatch((x, y), max_spice) sugars.append(sugar) spices.append(spice) grid.place_agent(sugar, (x, y)) grid.place_agent(spice, (x, y)) # build agents agents = s.build_agents(SsAgent, 'SsAgent', 100, parameters={'grid': grid}) # prices = [] for r in range(100): s.advance_round(r) for sugar in sugars: sugar.step() for spice in spices: spice.step() agents.move() agents.eat() print(',', len(agents.trade_with_neighbors())) agents.trade() agents.agg_log(possessions=['sugar', 'spice']) s.finalize()
def main(simulation_parameters): sam = Sam( 'climate_square.sam.csv', inputs=[ 'col', 'ele', 'gas', 'o_g', 'oil', 'eis', 'trn', 'roe', 'lab', 'cap' ], outputs=['col', 'ele', 'gas', 'o_g', 'oil', 'eis', 'trn', 'roe'], output_tax='tax', consumption=['col', 'ele', 'gas', 'o_g', 'oil', 'eis', 'trn', 'roe'], consumers=['hoh']) """ reads the social accounting matrix and returns coefficients of a cobb-douglas model """ carbon_prod = defaultdict(float) carbon_prod.update({ 'col': 2112 * 1e-4, 'oil': 2439.4 * 1e-4, 'gas': 1244.3 * 1e-4 }) """ this is the co2 output per sector at the base year """ print(sam.output_tax_shares()) simulation_parameters.update({ 'name': 'cce', 'random_seed': None, 'num_household': 1, 'num_firms': 1, 'endowment_FFcap': sam.endowment('cap'), 'endowment_FFlab': sam.endowment('lab'), 'final_goods': sam.consumption, 'capital_types': ['cap', 'lab'], 'dividends_percent': 0.0, 'production_functions': sam.production_functions(), 'consumption_functions': sam.utility_function(), 'output_tax_shares': sam.output_tax_shares(), 'money': 2691.2641884030372, 'inputs': sam.inputs, 'outputs': sam.outputs, 'balance_of_payment': sam.balance_of_payment('nx', 'inv'), 'sam': sam, 'carbon_prod': carbon_prod, 'wage_stickiness': 0.5, 'price_stickiness': 0.5, 'network_weight_stickiness': 0.5 }) simulation = Simulation(trade_logging='group', processes=1) declare_service('endowment_FFcap', 1, 'cap') simulation.declare_service('endowment_FFlab', 1, 'lab') """ every round for every endowment_FFcap the owner gets one good of lab similar for cap""" firms = { good: simulation.build_agents(Firm, number=simulation_parameters['num_firms'], group_name=good, parameters=simulation_parameters) for good in sam.outputs } household = simulation.build_agents(Household, 'household', simulation_parameters['num_household'], parameters=simulation_parameters) netexport = simulation.build_agents(NetExport, 'netexport', 1, parameters=simulation_parameters) government = simulation.build_agents(Government, 'government', 1, parameters=simulation_parameters) firms_and_household = sum(firms.values()) + household all_firms = sum(firms.values()) try: for r in range(simulation_parameters['rounds']): simulation.advance_round(r) all_firms.taxes_intervention() firms_and_household.send_demand() firms_and_household.selling() firms_and_household.buying() household.money_to_nx() all_firms.production() all_firms.carbon_taxes() all_firms.sales_tax() government.taxes_to_household() all_firms.international_trade() all_firms.invest() netexport.invest() household.sales_accounting() all_firms.dividends() all_firms.change_weights() all_firms.stats() household.agg_log(variables=['welfare']) (firms['col'] + firms['gas'] + firms['oil']).agg_log(variables=['price', 'produced', 'co2']) (firms['ele'] + firms['o_g'] + firms['eis'] + firms['trn'] + firms['roe']).agg_log(variables=['price', 'produced']) household.consuming() except Exception as e: print(e) simulation.finalize() # raise # put raise for full traceback but no graphs in case of error iotable.to_iotable(simulation.path, [99, simulation_parameters['rounds'] - 1]) mean_price = iotable.average_price(simulation.path, 99) print('mean price', mean_price) # simulation.graphs() return mean_price