env update - added PatchAgent und resolved viz(grid) problems
This commit is contained in:
parent
2c986832f6
commit
8188cbbadf
@ -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)
|
||||
self.schedule.add(a)
|
||||
agent = ForkliftAgent(0, self)
|
||||
self.schedule.add(agent)
|
||||
# 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))
|
||||
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.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)
|
||||
|
11
PatchAgent.py
Normal file
11
PatchAgent.py
Normal 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
7
PatchType.py
Normal file
@ -0,0 +1,7 @@
|
||||
import enum
|
||||
|
||||
|
||||
class PatchType(enum.Enum):
|
||||
dropOff = 1
|
||||
pickUp = 2
|
||||
item = 3
|
34
main.py
34
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):
|
||||
|
||||
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user