SI_InteligentnyWozekWidlowy/ForkliftAgent.py

163 lines
6.8 KiB
Python
Raw Normal View History

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
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-05-25 14:33:37 +02:00
def __init__(self, model, game_constants, client_delivery: PatchAgent, drop_off: PatchAgent,
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.current_order: Order = None
self.current_item = None
self.item_station_completed = False
self.provided_items: List[Item] = []
2022-05-27 00:03:24 +02:00
self.ready_for_execution = False
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:
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:
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:
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:
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-05-22 16:27:36 +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-05-22 16:27:36 +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-05-22 16:27:36 +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-05-22 16:27:36 +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-05-25 14:33:37 +02:00
def plan_actions(self):
if len(self.current_order.items) > 0:
i = self.current_order.items.pop(0)
if self.current_item is None:
self.provided_items.clear()
self.current_item = i
print("PLAN MOVEMENT")
# get item
pathFinder = PathFinderOnStates(
self.game_constants,
self.drop_off.location,
PathFinderState(self.current_position,
self.current_rotation,
0,
Action(ActionType.NONE),
[])
)
actions = pathFinder.get_action_list()
self.queue_movement_actions(actions)
elif not self.item_station_completed:
# go through station
packing_station: GridLocation = None
stations = dict(self.graph.packingStations)
2022-05-25 23:51:29 +02:00
if i.real_type == ItemType.SHELF:
2022-06-09 22:24:46 +02:00
packing_station = stations[PatchType.packingShelf]
2022-06-02 11:13:21 +02:00
elif i.real_type == ItemType.REFRIGERATOR:
2022-06-09 22:24:46 +02:00
packing_station = stations[PatchType.packingRefrigerator]
2022-05-25 23:51:29 +02:00
elif i.real_type == ItemType.DOOR:
2022-05-25 14:33:37 +02:00
packing_station = stations[PatchType.packingC]
pathFinder = PathFinderOnStates(
self.game_constants,
packing_station,
PathFinderState(self.current_position,
self.current_rotation,
0,
Action(
desired_item=i,
action_type=ActionType.PICK_ITEM
),
[])
)
actions = pathFinder.get_action_list()
self.queue_movement_actions(actions)
self.item_station_completed = True
else:
# go to client delivery area
pathFinder = PathFinderOnStates(
self.game_constants,
self.client_delivery.location,
PathFinderState(self.current_position,
self.current_rotation,
0,
Action(ActionType.NONE),
[])
)
actions = pathFinder.get_action_list()
self.queue_movement_actions(actions)
self.queue_movement_actions(
[Action(ActionType.DROP_ITEM)]
)
self.current_item = None
self.provided_items.append(self.current_item)
self.item_station_completed = False
2022-04-28 02:09:41 +02:00
def step(self) -> None:
2022-05-25 14:33:37 +02:00
if len(self.action_queue) > 0:
self.move()
2022-05-27 00:03:24 +02:00
elif self.ready_for_execution and len(self.orderList) > 0:
2022-05-25 14:33:37 +02:00
if (self.current_order is not None and len(self.current_order.items)) == 0:
self.fulfilled_orders.append(self.current_order)
self.current_order = None
if self.current_order is None:
self.current_order = self.orderList.pop(0)
self.plan_actions()
2022-04-16 14:55:25 +02:00
def creation_log(self):
print("Created Forklift Agent [id: {}]".format(self.unique_id))