def main(processes, rounds): simulation = abcEconomics.Simulation(name='logging_test_friendly_names', processes=processes) agents = simulation.build_agents(Agent, 'friendly_agent', agent_parameters=[{ 'name': 'A' }, { 'name': 'Davoud' }, { 'name': "fred" }, { 'name': "F12" }]) for rnd in range(10): for r in range(10): simulation.advance_round((r, rnd)) agents.go() agents.agg_log(variables=['i', 'r'], goods=['money']) agents.panel_log(variables=['i', 'r'], goods=['money']) simulation.finalize() compare('aggregated_friendly_agent.csv', simulation.path, 'aggregated logging test\t\t', processes) compare('aggregate_friendly_agent.csv', simulation.path, 'aggregate logging test\t\t', processes) compare('panel_friendly_agent.csv', simulation.path, 'aggregate logging test mean\t', processes)
def main(processes, rounds): simulation = abcEconomics.Simulation(processes=processes) print('build Killer') killer = simulation.build_agents(Killer, 'killer', 1) print('build Victim') victims = simulation.build_agents(Victim, 'victim', rounds) print('build Victim loudvictim') loudvictims = simulation.build_agents(Victim, 'loudvictim', rounds) print('build AddAgent') balls = simulation.build_agents(Ball, 'ball', 0) for time in range(rounds): simulation.advance_round(time) assert len(balls) == time, len(balls) deads = killer.kill_silent() victims.delete_agents(deads) deads = killer.kill_loud() loudvictims.delete_agents(deads) victims.am_I_dead() loudvictims.am_I_dead() balls.create_agents(Ball, 1) simulation.finalize() del simulation
def main(processes, rounds): sim = abcEconomics.Simulation() myagent = sim.build_agents(MyAgent, 'myagent', agent_parameters=[{'agent_parameters': 0}, {'agent_parameters': 1}, {'agent_parameters': 2}, {'agent_parameters': 3}, {'agent_parameters': 4}, {'agent_parameters': 5}, {'agent_parameters': 6}]) differentagent = sim.build_agents(DifferentAgent, 'differentagent', agent_parameters=[{'agent_parameters': 0}, {'agent_parameters': 1}, {'agent_parameters': 2}, {'agent_parameters': 3}, {'agent_parameters': 4}, {'agent_parameters': 5}, {'agent_parameters': 6}]) print('len', len(myagent)) sim.advance_round(0) myagent.call_me_maybe() print('simple') for r in range(1, 14): sim.advance_round(r) myagent[r % 7].call_me_maybe() print('--') print('two individuals') for r in range(14, 28): sim.advance_round(r) (myagent[r % 7] + myagent[3]).call_me_maybe() print('--') print('one class') for r in range(28, 42): sim.advance_round(r) myagent[r % 7, 3].call_me_maybe() print('--') print('whole class plus individual') for r in range(42, 56): sim.advance_round(r) (myagent + myagent[r % 7]).call_me_maybe() print('--') print('two classes') for r in range(56, 70): sim.advance_round(r) (myagent + differentagent).call_me_maybe() print('--') sim.finalize()
def main(processes, rounds): sim = abcEconomics.Simulation() sim.advance_round(0) myagents = sim.build_agents(MyAgent, 'myagent', number=5) for id in range(5): assert list(myagents[id].say()) == [id], (list(myagents[id].say()), [id]) sim.finalize()
def main(processes, rounds): s = abcEconomics.Simulation(processes=processes, name='unittest') endowment = s.build_agents(Endowment, 'endowment', 2, rounds=rounds) for r in range(rounds): s.time = r endowment.refresh_services('labor', derived_from='labor_endowment', units=5) endowment.Iconsume() endowment.all_tests_completed() s.finalize()
def main(processes, rounds): sim = abcEconomics.Simulation(processes=processes) myagents = sim.build_agents(MyAgent, 'myagent', number=2) for time in range(rounds): sim.time = time myagents.produce() myagents.all_tests_completed() sim.finalize()
def main(processes, rounds): sim = abcEconomics.Simulation(processes=processes) returners = sim.build_agents(Returner, 'returner', number=3) getters = sim.build_agents(Getter, 'getter', number=1) for y in range(rounds): sim.advance_round(y) ret = returners.returnit() getters.getit(ret) getters.method('a', 'b', 'c', d='d', f='f', e='e') sim.finalize() print('Returning tested \t\t\t\t\t\tOK') print('Calling with parameters tested \t\t\t\t\tOK')
def main(processes, rounds): sim = abcEconomics.Simulation( name='mysim', processes=processes, dbplugin=CustomLogging, dbpluginargs=['sqlite:///:memory:', 'sometable']) myagents = sim.build_agents(MyAgent, 'myagent', number=5) for i in range(rounds): sim.advance_round(i) myagents.write() sim.finalize()
def main(processes, rounds): s = abcEconomics.Simulation(processes=processes, name='unittest') print('build MessageA') messagea = s.build_agents(MessageA, 'messagea', 20) print('build MessageB') messageb = s.build_agents(MessageB, 'messageb', 20) for r in range(rounds): s.time = r (messagea + messageb).sendmsg() (messageb + messagea).recvmsg() print("Send_envelope and receive test:\t OK") s.finalize()
def main(processes, rounds): sim = abcEconomics.Simulation() sim.advance_round(0) myagents = sim.build_agents(MyAgent, 'myagent', agent_parameters=[{'name': 'me'}, {'name': 'you'}, {'name': 'him'}, {'name': ('firm', 0)}]) names = ['me', 'you', 'him', ('firm', 0)] for name in names: assert list(myagents.by_name(name).say()) == [name], ( list(myagents.by_name(name).say()), [name]) assert list(myagents.by_name(name).say()) == [name], ( list(myagents.by_names(names).say()), names) sim.finalize()
def main(processes, rounds): simulation = abcEconomics.Simulation(name='logging_test', processes=processes) agents = simulation.build_agents(Agent, 'agent', 10) for rnd in range(100): simulation.advance_round(rnd) agents.go() agents.agg_log(variables=['i', 'r'], goods=['money']) agents.panel_log(variables=['i', 'r'], goods=['money']) simulation.finalize() compare('aggregated_agent.csv', simulation.path, 'aggregated logging test\t\t', processes) compare('aggregate_agent.csv', simulation.path, 'aggregate logging test\t\t', processes) compare('panel_agent.csv', simulation.path, 'aggregate logging test mean\t', processes)
def main(processes, rounds): sim = abcEconomics.Simulation(processes=processes) aagents = sim.build_agents(Agent, 'aagent', number=5) bagents = sim.build_agents(Agent, 'bagent', number=5) actionA = aagents.actionA actionB = bagents.actionB for r in range(rounds): sim.advance_round(r) (aagents.actionA + aagents.actionB)() (aagents.actionA + bagents.actionB)() (aagents.actionB + bagents.actionB)() aagents.actionA() bagents.actionB() (actionA + actionB)() sim.finalize()
def main(processes, rounds=5): s = abcEconomics.Simulation(processes=processes, name='production_consumption_test') print('build ProductionMultifirm') productionmultifirm = s.build_agents(ProductionMultifirm, 'productionmultifirm', 1, rounds=rounds) print('build UtilityHousehold') utilityhousehold = s.build_agents(UtilityHousehold, 'utilityhousehold', 5, rounds=rounds) all_agents = utilityhousehold + productionmultifirm for r in range(rounds): s.advance_round(r) productionmultifirm.production() utilityhousehold.consumption() all_agents.all_tests_completed() s.finalize()
import abcEconomics as abce from ball import Ball from killer import Killer from victim import Victim rounds = num_victims = 30 simulation = abce.Simulation(processes=1) print('build Killer') killer = simulation.build_agents(Killer, 'killer', 1) print('build Victim') victims = simulation.build_agents(Victim, 'victim', num_victims) print('build Victim loudvictim') loudvictims = simulation.build_agents(Victim, 'loudvictim', num_victims) print('build AddAgent') balls = simulation.build_agents(Ball, 'ball', 0) for time in range(rounds): simulation.advance_round(time) deads = killer.kill_silent() victims.delete_agents(deads) deads = killer.kill_loud() loudvictims.delete_agents(deads) victims.am_I_dead() loudvictims.am_I_dead() balls.create_agents(Ball, 1) simulation.finalize()
def main(simulation_parameters): test_sim = abce.Simulation(name='quipu_sim', processes=1) num_agents = 5 names = list(range(0, num_agents)) money = random.sample(range(1000, 5000), num_agents) good2 = random.sample(range(100, 500), num_agents) good3 = random.sample(range(100, 500), num_agents) params = agent.build_agent_parameters(num_agents,names,'money', money,'food', good2, 'other', good3) print(params) agents = test_sim.build_agents(Agent, 'agent', agent_parameters=params) num_edges = 2*num_agents G = graph.build_graph(num_agents,names,money) node_sizes = money r = 0 while r < 10: test_sim.advance_round(r) r += 1 #Select 2 random agents buyer = random.randint(0,num_agents-1) seller = random.randint(0,num_agents-1) #Transact action = random.randint(0,1) good = random.randint(0,10) agents[buyer].buy_goods(seller,good) cost = agents[seller].sell_goods(good)[0] node_size_1 = (agents[buyer].return_curr_money()[0][0]) node_size_2 = (agents[seller].return_curr_money()[0][0]) #Buyer gets smaller, seller gets bigger node_sizes[buyer] = node_size_1/2 node_sizes[seller] = node_size_2*2 plt.clf() #Add edge between them G.add_edge(buyer,seller) print(G.nodes()) #Print nx.draw_networkx(G, pos=nx.get_node_attributes(G,'Position'), node_size=(node_sizes)) pylab.draw() plt.pause(1) test_sim.finalize()
def main(simulation_parameters): test_sim = abce.Simulation(name='tony_test', processes=1, trade_logging='individual') total_cost = 0 num_agents = 2 names = list(range(0, num_agents)) money = random.sample(range(1, 50), num_agents) good2 = random.sample(range(1, 50), num_agents) good3 = random.sample(range(1, 50), num_agents) #print(names.get_first_name()) params = agent.build_agent_parameters(num_agents, names, 'money', money, 'food', good2, 'other', good3) #agents = test_sim.build_agents(Agent, 'agent', agent_parameters=params) agents = test_sim.build_agents(Agent, 'agent', agent_parameters=[{ 'family_name': 'buyer', 'money': 100000, 'food': 0 }, { 'family_name': 'seller', 'money': 0, 'food': 100000 }]) agents[0].print_possessions() buyer = agents[0].get_name()[0][0] seller = agents[1].get_name()[0][0] #Graph inital sizes, names node_sizes = [ agents[0].return_quantity_of_good()[0][0], agents[1].return_quantity_of_good()[0][0] ] G = nx.DiGraph() G.add_node(buyer, Position=(random.randrange(0, 100), random.randrange(0, 100))) G.add_node(seller, Position=(random.randrange(0, 50), random.randrange(0, 50))) G.add_edge(buyer, seller, weight=total_cost) end = False r = 0 #Run simulation while (r < 10): test_sim.advance_round(r) r += 1 agents[0].buy_goods(1, 0) cost = agents[1].sell_goods(0)[0] #Ugly but ends the loop when the transactions are no longer possible if cost[0] == -1: end = True total_cost += cost[0] #Update node size to reflect good quantity node_size_1 = int((agents[0].return_quantity_of_good()[0][0]) * 0.1) node_size_2 = int((agents[1].return_quantity_of_good()[0][0]) * 0.1) node_sizes = [node_size_1, node_size_2] print(node_sizes) #Update weight size to reflect money transferred G.add_edge(buyer, seller, weight=(total_cost * 100)) #label the edges labels = {(buyer, seller): total_cost} plt.clf() #plot the graphs plt.title("100,000 products sold by seller") nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'Position'), node_list=[buyer, seller], node_size=node_sizes, with_labels=True) nx.draw_networkx_edge_labels(G, pos=nx.get_node_attributes(G, 'Position'), edge_labels=labels, font_size=30) pylab.draw() plt.pause(.5) test_sim.finalize()
def main(): # Read in simulation parameters f = open('sim_params_final.csv', 'r') data = np.array(list(csv.reader(f))) curr_sim = data[1, :] num_agents = int(curr_sim[1]) chance_food_inside = float(curr_sim[2]) chance_raw_inside = float(curr_sim[3]) quipu = int(curr_sim[5]) print(quipu) f.close() test_sim = abce.Simulation(name='quipu_sim', processes=1, trade_logging='off') walmart_size = 1000 names = list(range(0, num_agents + 1)) money = random.sample(range(5, 100), num_agents) money.append(walmart_size) food = random.sample(range(10, 50), num_agents) quipus = random.sample(range(5, 45), num_agents) other = random.sample(range(1, 20), num_agents) params = agent.build_agent_parameters(num_agents, names, 'money', money, 'food', food, 'quipu', quipus, 'other', other) agents = test_sim.build_agents(Quipu_Agent, 'agent', agent_parameters=params) walmart = test_sim.build_agents(Walmart, 'corporate', agent_parameters=[{ 'family_name': 'walmart', 'money': 100, 'food': 100, 'other': 100 }]) if quipu == 1: ofile = open('data_with_quipus.csv', 'w') print("here") else: ofile = open('data_just_money.csv', 'w') print("here1") writer = csv.writer(ofile, delimiter=',') writer.writerow([ 'Transaction ID', 'Buyer ID', 'Seller ID', 'Type of transaction', 'Currency', 'Failure from Buyer', 'Failure from Seller' ]) r = 0 num_rounds = 40 transaction_id = 0 while (r < num_rounds): test_sim.advance_round(r) r += 1 #Each round, select an agent and a random interaction (walmart or not) #Each round, every agent eats some food for x in range(num_agents): action = random.randint(0, 100) agents[x].eat_food() if action > (chance_food_inside * 100): buy = agents[x].buy_goods_from_walmart(0, 1)[0][0] sell = walmart[0].walmart_sell_goods(1)[0][0] writer.writerow( [transaction_id, x, -1, 'food', 'money', buy, sell]) else: #Buy from an agent that is not yourself numbers = list(range(0, x)) + list(range(x + 1, num_agents)) curr = random.choice(numbers) buy = agents[x].buy_goods(curr, 0, quipu)[0][0] sell = agents[curr].sell_goods(0, quipu)[0][0] if quipu == 1: writer.writerow( [transaction_id, x, curr, 'food', 'quipu', buy, sell]) else: writer.writerow( [transaction_id, x, curr, 'food', 'money', buy, sell]) # Buy raw materials 1x per week if r % 7 == 0: transaction_id += 1 action = random.randint(0, 100) if action > (chance_raw_inside * 100): buy = agents[x].buy_goods_from_walmart(0, 0)[0][0] sell = walmart[0].walmart_sell_goods(0)[0][0] writer.writerow( [transaction_id, x, -1, 'raw', 'money', buy, sell]) else: #Buy from an agent that is not yourself numbers = list(range(0, x)) + list(range( x + 1, num_agents)) curr = random.choice(numbers) buy = agents[x].buy_goods(curr, 1, quipu)[0][0] sell = agents[curr].sell_goods(1, quipu)[0][0] if quipu == 1: writer.writerow([ transaction_id, x, curr, 'food', 'quipu', buy, sell ]) else: writer.writerow([ transaction_id, x, curr, 'food', 'money', buy, sell ]) transaction_id += 1 #end of loop ofile.close() test_sim.finalize()
def main(): chance_raw_outside = 0.88 chance_raw_inside = 1 - chance_raw_outside chance_food_inside = 0.83 chance_food_outside = 1 - chance_food_inside test_sim = abce.Simulation(name='quipu_sim', processes=1, trade_logging='off') num_agents = 5 walmart_size = 1000 names = list(range(0, num_agents + 1)) money = random.sample(range(100, 200), num_agents) money.append(walmart_size) food = random.sample(range(10, 50), num_agents) quipu = random.sample(range(10, 50), num_agents) params = agent.build_agent_parameters(num_agents, names, 'money', money, 'food', food, 'quipu', quipu) agents = test_sim.build_agents(Quipu_Agent, 'agent', agent_parameters=params) walmart = test_sim.build_agents(Walmart, 'corporate', agent_parameters=[{ 'family_name': 'walmart', 'money': 100, 'food': 100 }]) color_map = ['red'] * (num_agents + 1) color_map[num_agents] = 'blue' G = graph.build_graph(num_agents + 1, names, money, 1, num_agents) edges = G.edges() nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'Position'), node_size=money, node_color=color_map, width=3, with_labels=True) plt.pause(1) money_over_time = [] food_over_time = [] quipus_over_time = [] r = 0 num_rounds = 10 while (r < num_rounds): test_sim.advance_round(r) r += 1 curr_money = 0 curr_quipus = 0 curr_food = 0 #Each round, select an agent and a random interaction (walmart or not) #Each round, every agent eats some food for x in range(num_agents): curr_money += agents[x].return_money()[0][0] curr_food += agents[x].return_food()[0][0] curr_quipus += agents[x].return_quipu()[0][0] colors = [] action = random.randint(0, 1) agents[x].eat_food() if action == 0: agents[x].buy_goods(0) walmart[0].sell_goods() for u, v in edges: if (u == x and v == num_agents) or (u == num_agents and v == x): colors.append('g') else: colors.append('#000000') else: numbers = list(range(0, x)) + list(range(x + 1, num_agents)) curr = random.choice(numbers) agents[x].buy_goods_with_quipu(curr) agents[curr].sell_goods_with_quipu() for u, v in edges: if (u == x and v == curr) or (u == curr and v == x): colors.append('g') else: colors.append('#000000') nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'Position'), node_size=money, node_color=color_map, edge_color=colors, with_labels=True, width=3) plt.pause(0.2) money_over_time.append(curr_money) food_over_time.append(curr_food) quipus_over_time.append(curr_quipus) plt.close() #end of loop test_sim.finalize() test_sim.graphs() r_list = list(range(0, num_rounds)) # f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=True, figsize=(15,5)) # ax1.plot(r_list,money_over_time,'r',label='money over time') # ax2.plot(r_list,food_over_time,'b',label='food over time') # ax3.plot(r_list,quipus_over_time,'g',label='quipus over time') # ax1.legend(loc='best') # ax2.legend(loc='best') # ax3.legend(loc='best') # plt.show() # plt.pause(5) # plt.close('all') sys.exit()