SI_InteligentnyWozekWidlowy/GameModel.py

99 lines
3.1 KiB
Python
Raw Normal View History

2022-04-16 14:55:25 +02:00
from typing import List
2022-04-28 01:50:56 +02:00
from mesa import Model
2022-04-08 00:43:25 +02:00
from mesa.space import MultiGrid
from mesa.time import RandomActivation
2022-04-16 14:55:25 +02:00
from AgentBase import AgentBase
2022-04-08 00:43:25 +02:00
from ForkliftAgent import ForkliftAgent
2022-04-28 01:50:56 +02:00
from InitialStateFactory import InitialStateFactory
2022-04-08 00:43:25 +02:00
from PatchAgent import PatchAgent
from PatchType import PatchType
2022-04-28 01:50:56 +02:00
from data.GameConstants import GameConstants
from decision.ActionType import ActionType
2022-04-28 21:22:19 +02:00
from pathfinding.PathfinderOnStates import PathFinderOnStates, PathFinderState
2022-04-28 01:50:56 +02:00
from util.PathDefinitions import GridLocation, GridWithWeights
2022-04-08 00:43:25 +02:00
class GameModel(Model):
2022-04-28 14:03:53 +02:00
def __init__(self, width, height, graph: GridWithWeights, graph2: GridWithWeights):
2022-04-08 00:43:25 +02:00
# self.num_agents = 5
self.running = True
self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self)
2022-04-16 14:55:25 +02:00
self.agents = [AgentBase]
2022-04-08 00:43:25 +02:00
2022-04-16 14:55:25 +02:00
self.forklift_agent = ForkliftAgent(self)
2022-04-08 00:43:25 +02:00
self.schedule.add(self.forklift_agent)
self.agents.append(self.forklift_agent)
2022-04-28 01:50:56 +02:00
initial_state_factory = InitialStateFactory()
self.game_constants = GameConstants(
width,
height,
2022-04-28 14:03:53 +02:00
graph.walls,
graph2.puddles
2022-04-28 01:50:56 +02:00
)
2022-04-08 00:43:25 +02:00
# Add the agent to a random grid cell
x = 5
y = 5
self.grid.place_agent(self.forklift_agent, (x, y))
self.forklift_agent.current_position = (x, y)
2022-04-28 21:22:19 +02:00
start, goal = (x, y), (1, 3)
2022-04-28 01:50:56 +02:00
pathFinder = PathFinderOnStates(
self.game_constants,
goal,
PathFinderState(self.forklift_agent.current_position, self.forklift_agent.current_rotation, 0,
ActionType.NONE, [])
)
2022-04-16 14:55:25 +02:00
self.place_patch_agents()
self.place_walls_agents(graph.walls)
2022-04-28 14:03:53 +02:00
self.place_puddles(graph2.puddles)
2022-04-16 14:55:25 +02:00
2022-04-28 21:22:19 +02:00
actions = pathFinder.get_action_list()
2022-04-28 01:50:56 +02:00
print("PATHFINDING")
print(actions)
2022-04-28 02:09:41 +02:00
self.forklift_agent.queue_movement_actions(actions)
2022-04-28 01:50:56 +02:00
2022-04-16 14:55:25 +02:00
def place_patch_agents(self):
agent = PatchAgent(self, PatchType.pickUp)
self.schedule.add(agent)
2022-04-08 00:43:25 +02:00
self.grid.place_agent(agent, (self.grid.width - 1, self.grid.height - 1))
self.agents.append(agent)
2022-04-16 14:55:25 +02:00
agent = PatchAgent(self, PatchType.dropOff)
2022-04-08 00:43:25 +02:00
# self.schedule.add(agent)
self.grid.place_agent(agent, (0, self.grid.height - 1))
self.agents.append(agent)
for i in range(3):
2022-04-16 14:55:25 +02:00
a = PatchAgent(self, PatchType.item)
2022-04-08 00:43:25 +02:00
self.agents.append(a)
self.grid.place_agent(a, (i, 0))
2022-04-16 14:55:25 +02:00
def place_walls_agents(self, walls: List[GridLocation]):
for w in walls:
agent = PatchAgent(self, PatchType.wall)
self.agents.append(agent)
self.grid.place_agent(agent, w)
2022-04-28 14:03:53 +02:00
def place_puddles(self, puddles: List[GridLocation]):
for p in puddles:
agent = PatchAgent(self, PatchType.diffTerrain)
self.agents.append(agent)
self.grid.place_agent(agent, p)
2022-04-08 00:43:25 +02:00
def step(self):
self.schedule.step()
print("update multiGrid")
self.grid.remove_agent(self.forklift_agent)
self.grid.place_agent(self.forklift_agent, self.forklift_agent.current_position)