automatyczny_kelner/src/obj/TemporaryState.py
2023-05-26 03:02:16 +02:00

99 lines
3.1 KiB
Python

from src.obj.Waiter import Waiter
import copy
class TemporaryState(Waiter):
def __init__(self, parent, cost_so_far, action="blank", cost=0, h=0):
super().__init__(copy.deepcopy(parent.position),
copy.copy(parent.orientation),
copy.copy(parent.square_size),
copy.copy(parent.screen_size),
copy.copy(parent.basket))
self.agent_role = action
self.parent = parent
self.cost = cost
self.cost_so_far = cost_so_far
self.h = h
self.change_role(action)
self.apply_transformation()
def replace(self, repl):
self.cost_so_far = copy.copy(repl.cost_so_far)
self.basket = copy.copy(repl.basket)
self.parent = repl.parent
def apply_transformation(self):
if self.agent_role == "left":
self.orientation = (self.orientation + 1) % 4
elif self.agent_role == "right":
self.orientation = (self.orientation - 1) % 4
elif self.agent_role == "front":
if self.orientation % 2: # x (1 or 3)
self.position[0] += self.orientation - 2 # x (-1 or +1)
else: # y (0 or 2)
self.position[1] += self.orientation - 1 # y (-1 or +1)
def left(self):
return TemporaryState(self, self.cost_so_far, "left", 1)
def right(self):
return TemporaryState(self, self.cost_so_far, "right", 1)
def front(self):
return TemporaryState(self, self.cost_so_far, "front", 1)
def collide_test(self) -> bool:
out_of_range = [
self.position[0] >= self.square_count,
self.position[1] >= self.square_count,
self.position[0] < 0,
self.position[1] < 0
]
return any(out_of_range)
def compare(self, state) -> bool:
conditions = [
self.position == state.position,
self.orientation == state.orientation
]
return all(conditions)
def change_cost(self, obj):
self.cost = 1 # default cost
if self.agent_role == "front":
costs = {
"kitchen": 5,
"table": 5,
"order": 20,
"wait": 10,
"done": 15,
}
if obj.agent_role in costs.keys():
self.cost = costs[obj.agent_role]
def heuristic(self, goal):
self.h = self.distance_to(goal)
return self.h
def current_cost(self):
return self.cost_so_far + self.h
def __eq__(self, __value) -> bool:
return self.current_cost() == __value.current_cost()
def __lt__(self, __value) -> bool:
return self.current_cost() < __value.current_cost()
def __le__(self, __value) -> bool:
return self.current_cost() <= __value.current_cost()
def __gt__(self, __value) -> bool:
return self.current_cost() > __value.current_cost()
def __ge__(self, __value) -> bool:
return self.current_cost() >= __value.current_cost()