From 8188cbbadfa7510d0c45d38cbad4195f238332c6 Mon Sep 17 00:00:00 2001 From: s464943 Date: Mon, 7 Mar 2022 22:27:34 +0100 Subject: [PATCH] env update - added PatchAgent und resolved viz(grid) problems --- ForkliftAgent.py | 2 +- ForkliftModel.py | 40 ++++++++++++++++++++++++++++++++-------- PatchAgent.py | 11 +++++++++++ PatchType.py | 7 +++++++ main.py | 36 +++++++++++++++++++++++++++++++++--- 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 PatchAgent.py create mode 100644 PatchType.py diff --git a/ForkliftAgent.py b/ForkliftAgent.py index 94789f4..d762986 100644 --- a/ForkliftAgent.py +++ b/ForkliftAgent.py @@ -5,4 +5,4 @@ class ForkliftAgent(Agent): def __init__(self, unique_id, model): super().__init__(unique_id, model) - print("Created forklift Agent with ID: {}".format(unique_id)) + print("Created forklift Agent with ID: {}".format(unique_id)) \ No newline at end of file diff --git a/ForkliftModel.py b/ForkliftModel.py index 22e34d1..abc590c 100644 --- a/ForkliftModel.py +++ b/ForkliftModel.py @@ -5,13 +5,15 @@ from mesa.time import RandomActivation from mesa.space import MultiGrid from mesa.datacollection import DataCollector +from PatchType import PatchType from ForkliftAgent import ForkliftAgent +from PatchAgent import PatchAgent class ForkliftModel(Model): def __init__(self, width, height): - self.num_agents = 1 + # self.num_agents = 5 self.running = True self.grid = MultiGrid(height, width, True) self.schedule = RandomActivation(self) @@ -20,14 +22,36 @@ class ForkliftModel(Model): # agent_reporters={"Wealth": lambda a: a.wealth} # ) - # Create agents - for i in range(self.num_agents): - a = ForkliftAgent(1, self) + agent = ForkliftAgent(0, self) + self.schedule.add(agent) + # Add the agent to a random grid cell + x = 3 + y = 2 + self.grid.place_agent(agent, (x, y)) + + agent = PatchAgent(1, self, PatchType.pickUp) + self.schedule.add(agent) + self.grid.place_agent(agent, (0, self.grid.height-1)) + + agent = PatchAgent(2, self, PatchType.dropOff) + self.schedule.add(agent) + self.grid.place_agent(agent, (self.grid.width-1, self.grid.height-1)) + + for i in range(3): + a = PatchAgent(i+3, self, PatchType.item) 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.grid.place_agent(a, (i, 0)) + + # Create patch agents + # for i in range(self.num_agents): + # a = PatchAgent(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)) + + def step(self): # self.datacollector.collect(self) diff --git a/PatchAgent.py b/PatchAgent.py new file mode 100644 index 0000000..4ebee70 --- /dev/null +++ b/PatchAgent.py @@ -0,0 +1,11 @@ +from mesa import Agent, Model + +from PatchType import PatchType + + +class PatchAgent(Agent): + + def __init__(self, unique_id, model, type: PatchType): + super().__init__(unique_id, model) + self.type = type + print("Created Patch Agent with ID: {}".format(unique_id)) \ No newline at end of file diff --git a/PatchType.py b/PatchType.py new file mode 100644 index 0000000..49f3bad --- /dev/null +++ b/PatchType.py @@ -0,0 +1,7 @@ +import enum + + +class PatchType(enum.Enum): + dropOff = 1 + pickUp = 2 + item = 3 \ No newline at end of file diff --git a/main.py b/main.py index 22404af..3ebe508 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,50 @@ +import random + from mesa.visualization.modules import CanvasGrid from mesa.visualization.ModularVisualization import ModularServer from ForkliftModel import ForkliftModel +from ForkliftAgent import ForkliftAgent +from PatchAgent import PatchAgent +from PatchType import PatchType +colors = [ + 'blue', 'cyan', 'orange', 'yellow', 'magenta', 'purple', '#103d3e', '#9fc86c', + '#b4c2ed', '#31767d', '#31a5fa', '#ba96e0', '#fef3e4', '#6237ac', '#f9cacd', '#1e8123' +] def agent_portrayal(agent): - portrayal = {"Shape": "image.png", "scale": 1.0, "Layer": 0} + + if isinstance(agent, ForkliftAgent): + portrayal = {"Shape": "image.png", "scale": 1.0, "Layer": 0} + + if isinstance(agent, PatchAgent): + color = colors[0] + if agent.type == PatchType.dropOff: + color = colors[1] + elif agent.type == PatchType.pickUp: + color = colors[2] + else: + color = colors[random.randrange(13)+3] + portrayal = {"Shape": "rect", + "Filled": "true", + "Layer": 0, + "Color": color, + "w": 1, + "h": 1} return portrayal +base = 512 +gridWidth = 6 +gridHeight = 4 +scale = base/gridWidth -grid = CanvasGrid(agent_portrayal, 10, 10, 512, 512) +grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale*gridWidth, scale*gridHeight) server = ModularServer(ForkliftModel, [grid], "Automatyczny Wózek Widłowy", - {"width": 10, "height": 10}) + {"width": gridHeight, "height": gridWidth}) server.port = 8888 server.launch()