def create_world(mp: MasParams): world = World(w=mp.G, h=mp.G, torus_enabled=True) n_ores = round(mp.G**2 * mp.D) for _ in range(n_ores): world.add_agent(Ore()) positions = [] for x in range(mp.G): for y in range(mp.G): positions.append(Vec2D(x, y)) base_positions = random.sample(positions, mp.N) if mp.M == 1: # cooperation company_ids = [0] * mp.N else: # competitive company_ids = list(range(mp.N)) for base_pos, comp_id in zip(base_positions, company_ids): base = Base(mp, comp_id) world.add_agent(base, base_pos) for _ in range(mp.X): world.add_agent(Explorer(base, mp, comp_id), pos=base_pos) for _ in range(mp.Y): world.add_agent(Transporter(base, mp, comp_id), pos=base_pos) return world
def draw_sample(T): count_logger = AgentCountLogger() RandomlyDyingAgentLog = count_logger.bind(RandomlyDyingAgent) world = World(100, 100, max_steps=T) for _ in range(100): world.add_agent(RandomlyDyingAgentLog()) agent_counts = [] while not world.ended: agent_counts.append(count_logger.count) world.step() return agent_counts
def draw_sample(T): #world = World(200, 200, max_steps=T) world = World(w=mining.G, h=mining.G, torus_enabled=True, max_steps=T) # Add the agents to the world. base_logger = AgentCountLogger() BaseLog = base_logger.bind(mining.Base) for _ in range(mining.N): world.add_agent(BaseLog(), pos = Vec2D(random.randint(0,world.h), random.randint(0,world.h))) # Dispurse robots equally amongst bases initially explorer_logger = AgentCountLogger() ExplorerLog = explorer_logger.bind(mining.Explorer) for _ in range(mining.X): for j in range(mining.N): world.add_agent(ExplorerLog(), pos = world.agents[j].pos()) transporter_logger = AgentCountLogger() TransporterLog = transporter_logger.bind(mining.Transporter) for _ in range(mining.Y): for j in range(mining.N): world.add_agent(TransporterLog(), pos = world.agents[j].pos()) # Add ore on the map (as agents) ore_total = world.w * world.h * mining.D # The density of ores is multiplied with the mapsize to make the correct amount of ore ore_logger = AgentCountLogger() OreLog = ore_logger.bind(mining.Ore) for _ in range(int(ore_total)): world.add_agent(OreLog()) explorer_counts = [] transporter_counts = [] base_counts = [] ore_counts = [] while not world.ended: explorer_counts.append(explorer_logger.count) transporter_counts.append(transporter_logger.count) base_counts.append(base_logger.count) ore_counts.append(ore_total - ore_logger.count) world.step() #vis = Visualizer(world, target_speed=25) #vis.start() return [explorer_counts, transporter_counts, ore_counts] # [explorer_counts, transporter_counts, base_counts]
from pygridmas import World, Agent, Vec2D, Colors, Visualizer import colorsys import math brush_radius = 5 size = 100 world = World(w=size, h=size, torus_enabled=True) class Canvas(Agent): color = Colors.BLACK def initialize(self): # If there are many agents with no step method, # performance can be increased by deactivating them. self.deactivate() def step(self): # Will not be called because of deactivation. pass def receive_event(self, event_type, data): if event_type == "PAINT": emit_pos = data dir = -self.vec_to(emit_pos) dist = dir.magnitude() dir_angle = (dir.angle() + (self.world.time * 0.1)) % (math.pi * 2) hue = dir_angle / (2 * math.pi) if dist < brush_radius: self.color = colorsys.hsv_to_rgb(hue, 1, 1)
from pygridmas import World, Agent, Vec2D, Colors, Visualizer import random import math size = 200 world = World(w=size, h=size, torus_enabled=True) class Repulser(Agent): color = Colors.YELLOW start_target: Vec2D = None reached_target = False def initialize(self): # Create a target a bit off center to view the torus effect # self.world is set on the agent when added to a world, # so the world can be accessed within the agent methods self.start_target = Vec2D( math.floor(self.world.w * 0.25), math.floor(self.world.h * 0.25) ) def step(self): if not self.reached_target: self.move_towards(self.start_target) self.reached_target = self.pos() == self.start_target else: near_agents = self.box_scan(10, sort=False) if near_agents: self.color = Colors.RED if random.random() < 0.2:
G = 200 # Gridsize of map M = 1 # Coordination mode (1 = cooperative, 0 = competetive) N = 3 # Number of bases # Setting up the robots I = int(G/5) # Communication scope of robots P = int(G/20) # Perception scope of robots Q = 1 # Cost of a move-action X = 10 # Number of Explorers per base Y = 10 # Number of Transporters per base S = X + Y - 1 # Memory capacity of robots W = 5 # Maximum inventory capacity of a Transporter # Setting up simulation T = 10000 # Maximum number of cycles world = World(w=G, h=G, torus_enabled=True, max_steps=T) # create world, torus or not class Base(Agent): group_ids = {0} ore_capacity = C def initialize(self): # Called once when the agent enters a world. # After the agent is added to the world, a reference to the # world is stored in 'self.world'. self.color = Colors.YELLOW self.ore_in_storage = [] pass
from pygridmas import Agent, World, Visualizer, Colors import random world = World(100, 100) comm_rng = 10 class LonelyAgent(Agent): state = "LONELY" friend = None agent_group_id = None friendship_patience = None def initialize(self): self.agent_group_id = 'AGENT:{}'.format(self.idx) self.group_ids.add(self.agent_group_id) def step(self): if self.state == "LONELY": self.color = Colors.RED if random.randint(0, 20) == 0: self.emit_event(comm_rng, ("ANYONE THERE?", self.agent_group_id)) self.color = Colors.MAGENTA elif self.state == "HOPEFUL": self.color = Colors.BLUE self.friendship_patience -= 1 if self.friendship_patience == 0: self.friend = None self.state = "LONELY"
def main(): world = World(w=G, h=G, torus_enabled=True, max_steps=T) # create world, torus or not # Add the agents to the world. for _ in range(N): world.add_agent(Base(), pos = Vec2D(random.randint(0,world.h), random.randint(0,world.h))) # Dispurse robots equally amongst bases initially for _ in range(X): for j in range(N): world.add_agent(Explorer(), pos = world.agents[j].pos()) for _ in range(Y): for j in range(N): world.add_agent(Transporter(), pos = world.agents[j].pos()) # Add ore on the map (as agents) ore_total = world.w * world.h * D # The density of ores is multiplied with the mapsize to make the correct amount of ore for _ in range(int(ore_total)): world.add_agent(Ore()) # The world proceeds by calling 'world.step()' while not world.ended: world.step()
from pygridmas import World, Agent, Vec2D, Colors, Visualizer size = 100 world = World(w=size, h=size, torus_enabled=True) class Wall(Agent): color = Colors.WHITE group_ids = {0} # A python set class Mover(Agent): color = Colors.BLUE group_ids = {1} group_collision_ids = { 0, 1 } # Not able to enter wall tiles or other Mover tiles def step(self): self.move_rel(Vec2D.random_grid_dir()) # Create walls x1, x2 = round(size * 0.3), round(size * 0.7) y1, y2 = round(size * 0.6), round(size * 0.8) for x in (x1, x2): for y in range(y1 + 1): world.add_agent(Wall(), Vec2D(x, y)) for y in range(y2, size): world.add_agent(Wall(), Vec2D(x, y))