Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, agent_lifetime=1, n_agents=10):
super().__init__()
self.agent_lifetime = agent_lifetime
self.n_agents = n_agents
# keep track of the the remaining life of an agent and
# how many ticks it has seen
self.datacollector = DataCollector(
agent_reporters={"remaining_life": lambda a: a.remaining_life,
"steps": lambda a: a.steps})
self.current_ID = 0
self.schedule = RandomActivation(self)
for _ in range(n_agents):
self.schedule.add(FiniteLifeAgent(self.next_id(),
self.agent_lifetime,
self))
def __init__(self, N=100, width=10, height=10):
self.num_agents = N
self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self)
self.datacollector = DataCollector(
model_reporters={"Gini": compute_gini},
agent_reporters={"Wealth": "wealth"}
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
self.schedule.add(a)
# Add the agent to a random grid cell
x = random.randrange(self.grid.width)
y = random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))
self.running = True
self.datacollector.collect(self)
from mesa import Model
from mesa.time import BaseScheduler, RandomActivation, SimultaneousActivation
from mesa.space import SingleGrid
from mesa.datacollection import DataCollector
from .agent import PDAgent
class PdGrid(Model):
''' Model class for iterated, spatial prisoner's dilemma model. '''
schedule_types = {"Sequential": BaseScheduler,
"Random": RandomActivation,
"Simultaneous": SimultaneousActivation}
# This dictionary holds the payoff for this agent,
# keyed on: (my_move, other_move)
payoff = {("C", "C"): 1,
("C", "D"): 0,
("D", "C"): 1.6,
("D", "D"): 0}
def __init__(self, height=50, width=50, schedule_type="Random", payoffs=None):
'''
Create a new Spatial Prisoners' Dilemma Model.
Args:
height, width: Grid size. There will be one agent per grid cell.
def __init__(self, num_nodes=10, avg_node_degree=3, initial_outbreak_size=1, virus_spread_chance=0.4,
virus_check_frequency=0.4, recovery_chance=0.3, gain_resistance_chance=0.5):
self.num_nodes = num_nodes
prob = avg_node_degree / self.num_nodes
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
self.grid = NetworkGrid(self.G)
self.schedule = RandomActivation(self)
self.initial_outbreak_size = initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
self.virus_spread_chance = virus_spread_chance
self.virus_check_frequency = virus_check_frequency
self.recovery_chance = recovery_chance
self.gain_resistance_chance = gain_resistance_chance
self.datacollector = DataCollector({"Infected": number_infected,
"Susceptible": number_susceptible,
"Resistant": number_resistant})
# Create agents
for i, node in enumerate(self.G.nodes()):
a = VirusAgent(i, self, State.SUSCEPTIBLE, self.virus_spread_chance, self.virus_check_frequency,
self.recovery_chance, self.gain_resistance_chance)
self.schedule.add(a)
# Add the agent to the node
Create a new Flockers model.
Args:
population: Number of Boids
width, height: Size of the space.
speed: How fast should the Boids move.
vision: How far around should each Boid look for its neighbors
separation: What's the minimum distance each Boid will attempt to
keep from any other
cohere, separate, match: factors for the relative importance of
the three drives. '''
self.population = population
self.vision = vision
self.speed = speed
self.separation = separation
self.schedule = RandomActivation(self)
self.space = ContinuousSpace(width, height, True)
self.factors = dict(cohere=cohere, separate=separate, match=match)
self.make_agents()
self.running = True
import random
from collections import defaultdict
from mesa.time import RandomActivation
class RandomActivationByBreed(RandomActivation):
'''
A scheduler which activates each type of agent once per step, in random
order, with the order reshuffled every step.
This is equivalent to the NetLogo 'ask breed...' and is generally the
default behavior for an ABM.
Assumes that all agents have a step() method.
'''
agents_by_breed = defaultdict(list)
def __init__(self, model):
super().__init__(model)
self.agents_by_breed = defaultdict(list)
def add(self, agent):
def __init__(self, height=grid_h, width=grid_w, init_people=2, rich_threshold=10,
reserve_percent=50,):
self.height = height
self.width = width
self.init_people = init_people
self.schedule = RandomActivation(self)
self.grid = MultiGrid(self.width, self.height, torus=True)
# rich_threshold is the amount of savings a person needs to be considered "rich"
self.rich_threshold = rich_threshold
self.reserve_percent = reserve_percent
# see datacollector functions above
self.datacollector = DataCollector(model_reporters={
"Rich": get_num_rich_agents,
"Poor": get_num_poor_agents,
"Middle Class": get_num_mid_agents,
"Savings": get_total_savings,
"Wallets": get_total_wallets,
"Money": get_total_money,
"Loans": get_total_loans},
agent_reporters={
"Wealth": lambda x: x.wealth})
def __init__(self, N=2, width=20, height=10):
self.N = N # num of agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
self.grid = SingleGrid(width, height, torus=False)
self.schedule = RandomActivation(self)
self.make_walker_agents()
self.running = True
def __init__(self, num_agents=7, num_nodes=10):
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 = 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 __init__(self, height=grid_h, width=grid_w, init_people=2, rich_threshold=10,
reserve_percent=50,):
self.uid = next(self.id_gen)
self.height = height
self.width = width
self.init_people = init_people
self.schedule = RandomActivation(self)
self.grid = MultiGrid(self.width, self.height, torus=True)
# rich_threshold is the amount of savings a person needs to be considered "rich"
self.rich_threshold = rich_threshold
self.reserve_percent = reserve_percent
# see datacollector functions above
self.datacollector = DataCollector(model_reporters={
"Rich": get_num_rich_agents,
"Poor": get_num_poor_agents,
"Middle Class": get_num_mid_agents,
"Savings": get_total_savings,
"Wallets": get_total_wallets,
"Money": get_total_money,
"Loans": get_total_loans,
"Model Params": track_params,
"Run": track_run},
agent_reporters={