Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mesa.visualization.ModularVisualization import ModularServer
- from mesa.visualization.UserParam import UserSettableParameter
- from mesa.visualization.modules import ChartModule
- from mesa.visualization.modules import NetworkModule
- from .model import BoltzmannWealthModelNetwork
- def network_portrayal(G):
- # The model ensures there is 0 or 1 agent per node
- portrayal = dict()
- portrayal['nodes'] = [{'id': node_id,
- 'size': 3 if agents else 1,
- 'color': '#CC0000' if not agents or agents[0].wealth == 0 else '#007959',
- 'label': None if not agents else 'Agent:{} Wealth:{}'.format(agents[0].unique_id,
- agents[0].wealth),
- }
- for (node_id, agents) in G.nodes.data('agent')]
- portrayal['edges'] = [{'id': edge_id,
- 'source': source,
- 'target': target,
- 'color': '#140078',
- }
- for edge_id, (source, target) in enumerate(G.edges)]
- return portrayal
- grid = NetworkModule(network_portrayal, 500, 500, library='sigma')
- chart = ChartModule([
- {"Label": "Gini", "Color": "Black"}],
- data_collector_name='datacollector'
- )
- model_params = {
- "num_agents": UserSettableParameter('slider', "Number of agents", 7, 2, 10, 1,
- description="Choose how many agents to include in the model"),
- "num_nodes": UserSettableParameter('slider', "Number of nodes", 10, 3, 12, 1,
- description="Choose how many nodes to include in the model, with at "
- "least the same number of agents")#,
- #"wealth": UserSettableParameter('slider', "Initial Wealth", 1000,1,10,1,
- # description="Choose how much starting wealth "
- # "least the same number of agents")
- }
- server = ModularServer(BoltzmannWealthModelNetwork, [grid, chart], "Money Model", model_params)
- server.port = 8521
- from mesa import Agent, Model
- from mesa.time import RandomActivation
- from mesa.datacollection import DataCollector
- import networkx as nx
- import random
- from mesa.space import NetworkGrid
- def compute_gini(model):
- agent_wealths = [agent.wealth for agent in model.schedule.agents]
- x = sorted(agent_wealths)
- N = model.num_agents
- B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
- return 1 + (1 / N) - 2 * B
- class BoltzmannWealthModelNetwork(Model):
- """A model with some number of agents."""
- def __init__(self, num_agents=7, num_nodes=10):
- #self.wealth = wealth
- self.num_agents = num_agents
- self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents
- self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)
- self.grid = NetworkGrid(self.G)
- self.schedule = RandomActivation(self)
- self.datacollector = DataCollector(
- model_reporters={"Gini": compute_gini},
- agent_reporters={"Wealth": lambda _: _.wealth}
- )
- list_of_random_nodes = self.random.sample(self.G.nodes(), self.num_agents)
- # Create agents
- for i in range(self.num_agents):
- a = MoneyAgent(i, self)
- self.schedule.add(a)
- # Add the agent to a random node
- self.grid.place_agent(a, list_of_random_nodes[i])
- self.running = True
- self.datacollector.collect(self)
- def step(self):
- self.schedule.step()
- # collect data
- self.datacollector.collect(self)
- def run_model(self, n):
- for i in range(n):
- self.step()
- class MoneyAgent(Agent):
- """ An agent with fixed initial wealth."""
- def __init__(self, unique_id, model):
- super().__init__(unique_id, model)
- self.wealth = 1
- def move(self):
- possible_steps = [node for node in self.model.grid.get_neighbors(self.pos, include_center=False) if
- self.model.grid.is_cell_empty(node)]
- if len(possible_steps) > 0:
- new_position = self.random.choice(possible_steps)
- self.model.grid.move_agent(self, new_position)
- def give_money(self):
- neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
- neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
- if len(neighbors) > 0:
- other = self.random.choice(neighbors)
- newval = random.uniform(0, 1)
- newval = (newval*100)/100
- #newval = format(newval, '.2f')
- other.wealth += newval
- self.wealth -= newval
- def step(self):
- self.move()
- if self.wealth > 0:
- self.give_money()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement