SI_InteligentnyWozekWidlowy/ForkliftAgent.py

148 lines
6.5 KiB
Python
Raw Permalink Normal View History

2022-06-10 01:16:43 +02:00
from copy import deepcopy
2022-04-28 02:09:41 +02:00
from typing import Tuple, List
2022-04-08 00:43:25 +02:00
2022-04-16 14:55:25 +02:00
from AgentBase import AgentBase
2022-05-25 14:33:37 +02:00
from PatchAgent import PatchAgent
from PatchType import PatchType
2022-06-10 01:16:43 +02:00
from data.GameConstants import GameConstants
2022-05-25 14:33:37 +02:00
from data.Item import Item
from data.Order import Order
2022-05-11 19:05:44 +02:00
from data.enum.Direction import Direction
2022-05-25 14:33:37 +02:00
from data.enum.ItemType import ItemType
2022-05-22 16:27:36 +02:00
from decision.Action import Action
2022-04-28 02:09:41 +02:00
from decision.ActionType import ActionType
2022-05-25 14:33:37 +02:00
from pathfinding.PathFinderState import PathFinderState
from pathfinding.PathfinderOnStates import PathFinderOnStates
from util.PathDefinitions import GridLocation, GridWithWeights
2022-03-06 22:16:21 +01:00
2022-04-16 14:55:25 +02:00
class ForkliftAgent(AgentBase):
2022-03-06 22:16:21 +01:00
2022-06-10 01:16:43 +02:00
def __init__(self, model, game_constants: GameConstants, client_delivery: PatchAgent, drop_off: PatchAgent,
2022-05-25 14:33:37 +02:00
graph: GridWithWeights):
2022-04-16 14:55:25 +02:00
super().__init__(model)
2022-05-22 16:27:36 +02:00
self.action_queue: List[Action] = []
2022-04-28 02:09:41 +02:00
self.current_position = Tuple[int, int]
2022-04-08 00:43:25 +02:00
self.current_rotation = Direction.right
2022-05-25 14:33:37 +02:00
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
2022-06-09 21:33:57 +02:00
self.current_order_delivered_items: List[Item] = []
2022-05-27 00:03:24 +02:00
self.ready_for_execution = False
2022-06-10 01:16:43 +02:00
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
2022-04-08 00:43:25 +02:00
2022-05-22 16:27:36 +02:00
def queue_movement_actions(self, movement_actions: List[Action]):
self.action_queue.extend(movement_actions)
2022-04-28 02:09:41 +02:00
def move(self):
2022-05-22 16:27:36 +02:00
if len(self.action_queue) > 0:
action = self.action_queue.pop(0)
action_type = action.action_type
2022-04-28 02:09:41 +02:00
2022-05-22 16:27:36 +02:00
if action_type == ActionType.ROTATE_UP:
2022-06-10 01:16:43 +02:00
# print("rotate {} --> {}".format(self.current_rotation, action_type))
2022-04-28 02:09:41 +02:00
self.current_rotation = Direction.top
2022-05-22 16:27:36 +02:00
elif action_type == ActionType.ROTATE_RIGHT:
2022-06-10 01:16:43 +02:00
# print("rotate {} --> {}".format(self.current_rotation, action_type))
2022-04-28 02:09:41 +02:00
self.current_rotation = Direction.right
2022-05-22 16:27:36 +02:00
elif action_type == ActionType.ROTATE_DOWN:
2022-06-10 01:16:43 +02:00
# print("rotate {} --> {}".format(self.current_rotation, action_type))
2022-04-28 02:09:41 +02:00
self.current_rotation = Direction.down
2022-05-22 16:27:36 +02:00
elif action_type == ActionType.ROTATE_LEFT:
2022-06-10 01:16:43 +02:00
# print("rotate {} --> {}".format(self.current_rotation, action_type))
2022-04-28 02:09:41 +02:00
self.current_rotation = Direction.left
2022-05-22 16:27:36 +02:00
elif action_type == ActionType.MOVE:
2022-04-28 02:09:41 +02:00
if self.current_rotation == Direction.top:
2022-06-10 01:16:43 +02:00
# print("move {} --> {}".format(self.current_position, action_type))
2022-04-28 02:09:41 +02:00
self.current_position = (self.current_position[0], self.current_position[1] + 1)
elif self.current_rotation == Direction.down:
2022-06-10 01:16:43 +02:00
# print("move {} --> {}".format(self.current_position, action_type))
2022-04-28 02:09:41 +02:00
self.current_position = (self.current_position[0], self.current_position[1] - 1)
elif self.current_rotation == Direction.right:
2022-06-10 01:16:43 +02:00
# print("move {} --> {}".format(self.current_position, action_type))
2022-04-28 02:09:41 +02:00
self.current_position = (self.current_position[0] + 1, self.current_position[1])
elif self.current_rotation == Direction.left:
2022-06-10 01:16:43 +02:00
# print("move {} --> {}".format(self.current_position, action_type))
2022-04-28 02:09:41 +02:00
self.current_position = (self.current_position[0] - 1, self.current_position[1])
2022-06-10 01:16:43 +02:00
def step(self) -> None:
if len(self.action_queue) > 0:
self.move()
elif self.ready_for_execution:
if self.current_position != self.goal:
2022-05-25 14:33:37 +02:00
pathFinder = PathFinderOnStates(
self.game_constants,
2022-06-10 01:16:43 +02:00
self.goal,
2022-05-25 14:33:37 +02:00
PathFinderState(self.current_position,
self.current_rotation,
0,
Action(
2022-06-10 01:16:43 +02:00
desired_item=None,
2022-05-25 14:33:37 +02:00
action_type=ActionType.PICK_ITEM
),
[])
)
actions = pathFinder.get_action_list()
self.queue_movement_actions(actions)
else:
2022-06-10 01:16:43 +02:00
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)
2022-04-16 14:55:25 +02:00
def creation_log(self):
print("Created Forklift Agent [id: {}]".format(self.unique_id))