SI_InteligentnyWozekWidlowy/ForkliftAgent.py
2022-06-10 01:16:43 +02:00

148 lines
6.5 KiB
Python

from copy import deepcopy
from typing import Tuple, List
from AgentBase import AgentBase
from PatchAgent import PatchAgent
from PatchType import PatchType
from data.GameConstants import GameConstants
from data.Item import Item
from data.Order import Order
from data.enum.Direction import Direction
from data.enum.ItemType import ItemType
from decision.Action import Action
from decision.ActionType import ActionType
from pathfinding.PathFinderState import PathFinderState
from pathfinding.PathfinderOnStates import PathFinderOnStates
from util.PathDefinitions import GridLocation, GridWithWeights
class ForkliftAgent(AgentBase):
def __init__(self, model, game_constants: GameConstants, client_delivery: PatchAgent, drop_off: PatchAgent,
graph: GridWithWeights):
super().__init__(model)
self.action_queue: List[Action] = []
self.current_position = Tuple[int, int]
self.current_rotation = Direction.right
self.orderList = []
self.fulfilled_orders: List[Order] = []
self.client_delivery: PatchAgent = client_delivery
self.drop_off: PatchAgent = drop_off
self.graph = graph
self.game_constants = game_constants
self.item_station_completed = False
self.current_order_delivered_items: List[Item] = []
self.ready_for_execution = False
self.last_delviered_item = None
self.current_item: Item = None
self.current_order = None
self.base: GridLocation = None
self.goal: GridLocation = None
def set_base(self, drop_off: PatchAgent):
self.drop_off = drop_off
self.base = self.drop_off.location
self.goal = self.base
def queue_movement_actions(self, movement_actions: List[Action]):
self.action_queue.extend(movement_actions)
def move(self):
if len(self.action_queue) > 0:
action = self.action_queue.pop(0)
action_type = action.action_type
if action_type == ActionType.ROTATE_UP:
# print("rotate {} --> {}".format(self.current_rotation, action_type))
self.current_rotation = Direction.top
elif action_type == ActionType.ROTATE_RIGHT:
# print("rotate {} --> {}".format(self.current_rotation, action_type))
self.current_rotation = Direction.right
elif action_type == ActionType.ROTATE_DOWN:
# print("rotate {} --> {}".format(self.current_rotation, action_type))
self.current_rotation = Direction.down
elif action_type == ActionType.ROTATE_LEFT:
# print("rotate {} --> {}".format(self.current_rotation, action_type))
self.current_rotation = Direction.left
elif action_type == ActionType.MOVE:
if self.current_rotation == Direction.top:
# print("move {} --> {}".format(self.current_position, action_type))
self.current_position = (self.current_position[0], self.current_position[1] + 1)
elif self.current_rotation == Direction.down:
# print("move {} --> {}".format(self.current_position, action_type))
self.current_position = (self.current_position[0], self.current_position[1] - 1)
elif self.current_rotation == Direction.right:
# print("move {} --> {}".format(self.current_position, action_type))
self.current_position = (self.current_position[0] + 1, self.current_position[1])
elif self.current_rotation == Direction.left:
# print("move {} --> {}".format(self.current_position, action_type))
self.current_position = (self.current_position[0] - 1, self.current_position[1])
def step(self) -> None:
if len(self.action_queue) > 0:
self.move()
elif self.ready_for_execution:
if self.current_position != self.goal:
pathFinder = PathFinderOnStates(
self.game_constants,
self.goal,
PathFinderState(self.current_position,
self.current_rotation,
0,
Action(
desired_item=None,
action_type=ActionType.PICK_ITEM
),
[])
)
actions = pathFinder.get_action_list()
self.queue_movement_actions(actions)
else:
if self.current_order is not None and self.goal == self.base:
self.current_item = self.current_order.items.pop(0)
packing_station: GridLocation = None
stations = dict(self.graph.packingStations)
if self.current_item.real_type == ItemType.SHELF:
packing_station = stations[PatchType.packingShelf]
elif self.current_item.real_type == ItemType.REFRIGERATOR:
packing_station = stations[PatchType.packingRefrigerator]
elif self.current_item.real_type == ItemType.DOOR:
packing_station = stations[PatchType.packingDoor]
self.goal = packing_station
elif self.goal in [i[1] for i in self.graph.packingStations]:
self.goal = self.client_delivery.location
elif self.goal == self.client_delivery.location:
if self.current_order is not None and len(self.current_order.items) == 0:
self.current_order_delivered_items.append(self.current_item)
self.current_order.items = deepcopy(self.current_order_delivered_items)
self.fulfilled_orders.append(self.current_order)
self.current_item = None
self.current_order = None
self.goal = self.base
else:
self.current_order_delivered_items.append(self.current_item)
self.goal = self.base
self.current_item = None
elif self.goal == self.base and self.current_order is None:
self.current_order_delivered_items.clear()
self.current_order = self.orderList.pop(0)
def creation_log(self):
print("Created Forklift Agent [id: {}]".format(self.unique_id))