env update - added PatchAgent und resolved viz(grid) problems

This commit is contained in:
s464943 2022-03-07 22:27:34 +01:00
parent 2c986832f6
commit 8188cbbadf
5 changed files with 84 additions and 12 deletions

View File

@ -5,4 +5,4 @@ class ForkliftAgent(Agent):
def __init__(self, unique_id, model): def __init__(self, unique_id, model):
super().__init__(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))

View File

@ -5,13 +5,15 @@ from mesa.time import RandomActivation
from mesa.space import MultiGrid from mesa.space import MultiGrid
from mesa.datacollection import DataCollector from mesa.datacollection import DataCollector
from PatchType import PatchType
from ForkliftAgent import ForkliftAgent from ForkliftAgent import ForkliftAgent
from PatchAgent import PatchAgent
class ForkliftModel(Model): class ForkliftModel(Model):
def __init__(self, width, height): def __init__(self, width, height):
self.num_agents = 1 # self.num_agents = 5
self.running = True self.running = True
self.grid = MultiGrid(height, width, True) self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self) self.schedule = RandomActivation(self)
@ -20,14 +22,36 @@ class ForkliftModel(Model):
# agent_reporters={"Wealth": lambda a: a.wealth} # agent_reporters={"Wealth": lambda a: a.wealth}
# ) # )
# Create agents agent = ForkliftAgent(0, self)
for i in range(self.num_agents): self.schedule.add(agent)
a = ForkliftAgent(1, self) # 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) self.schedule.add(a)
# Add the agent to a random grid cell self.grid.place_agent(a, (i, 0))
x = random.randrange(self.grid.width)
y = random.randrange(self.grid.height) # Create patch agents
self.grid.place_agent(a, (x, y)) # 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): def step(self):
# self.datacollector.collect(self) # self.datacollector.collect(self)

11
PatchAgent.py Normal file
View File

@ -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))

7
PatchType.py Normal file
View File

@ -0,0 +1,7 @@
import enum
class PatchType(enum.Enum):
dropOff = 1
pickUp = 2
item = 3

36
main.py
View File

@ -1,20 +1,50 @@
import random
from mesa.visualization.modules import CanvasGrid from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer from mesa.visualization.ModularVisualization import ModularServer
from ForkliftModel import ForkliftModel 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): 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 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, server = ModularServer(ForkliftModel,
[grid], [grid],
"Automatyczny Wózek Widłowy", "Automatyczny Wózek Widłowy",
{"width": 10, "height": 10}) {"width": gridHeight, "height": gridWidth})
server.port = 8888 server.port = 8888
server.launch() server.launch()