moje podejscie
This commit is contained in:
parent
976d285e4b
commit
b2463050bc
BIN
__pycache__/agent.cpython-37.pyc
Normal file
BIN
__pycache__/agent.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/attributes.cpython-37.pyc
Normal file
BIN
__pycache__/attributes.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/warehouse.cpython-37.pyc
Normal file
BIN
__pycache__/warehouse.cpython-37.pyc
Normal file
Binary file not shown.
169
agent.py
169
agent.py
@ -1,96 +1,91 @@
|
|||||||
from warehouse import Coordinates, Tile, Pack
|
from warehouse import Coordinates, Tile, Pack
|
||||||
from attributes import PackStatus, TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self, coord_x, coord_y):
|
||||||
|
self.x = coord_x
|
||||||
|
self.y = coord_y
|
||||||
|
self.parent = None
|
||||||
|
self.g_cost = 0
|
||||||
|
self.h_cost = 0
|
||||||
|
def __eq__(self, other):
|
||||||
|
if isinstance(other, Node):
|
||||||
|
return self.x == other.x and self.y == self.y
|
||||||
|
return False
|
||||||
|
|
||||||
class Agent:
|
class Agent:
|
||||||
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
||||||
self.x = start_x
|
self.x = start_x
|
||||||
self.y = start_y
|
self.y = start_y
|
||||||
self.direction = 'down'
|
self.radius = radius
|
||||||
self.radius = radius
|
self.warehouse = assigned_warehouse
|
||||||
self.assigned_warehouse = assigned_warehouse
|
self.is_loaded = False
|
||||||
self.is_loaded = False
|
self.transported_package = None
|
||||||
self.transported_package = None
|
self.dest = Node(0, 0)
|
||||||
|
self.closed = list()
|
||||||
|
self.open = list()
|
||||||
|
self.path = list()
|
||||||
|
|
||||||
def demo_agent_move(self, demo_agent_step, demo_step_max=5):
|
def find_path(self):
|
||||||
demo_agent_sign = 0
|
start_node = Node(self.x, self.y)
|
||||||
next_coords = self.get_next_move_coordinates()
|
self.open.append(start_node)
|
||||||
can_move = self.check_if_can_move(next_coords)
|
while self.open:
|
||||||
if demo_agent_step >= demo_step_max:
|
current_node = self.open.pop()
|
||||||
demo_agent_sign = -1
|
self.closed.append(current_node)
|
||||||
self.direction = 'up'
|
if current_node.x == self.dest.x and current_node.y == self.dest.y:
|
||||||
elif demo_agent_step == 0:
|
while current_node != start_node:
|
||||||
demo_agent_sign = 1
|
path.append(current_node)
|
||||||
self.direction = 'down'
|
current_node = current_node.parent
|
||||||
if not can_move:
|
return True
|
||||||
alternative_move = ('left' if self.check_if_can_move(self.move_left()) else None) or ('right' if self.check_if_can_move(self.move_right()) else self.direction)
|
neighbour_list = self.get_neighbours(current_node)
|
||||||
self.direction = alternative_move
|
for neighbour in neighbour_list:
|
||||||
else:
|
if neighbour in self.closed:
|
||||||
self.x = next_coords.x
|
continue
|
||||||
self.y = next_coords.y
|
cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour)
|
||||||
|
if cost < neighbour.g_cost or neighbour not in self.open:
|
||||||
|
neighbour.g_cost = cost
|
||||||
|
neighbour.h_cost = self.heur_cost_est(neighbour, self.dest)
|
||||||
|
neighbour.parent = current_node
|
||||||
|
if neighbour not in self.open:
|
||||||
|
self.open.append(neighbour);
|
||||||
|
return False
|
||||||
|
|
||||||
def turn_left(self):
|
def heur_cost_est(self, nodeA: Node, nodeB: Node):
|
||||||
new_direction = TURN_LEFT_DIRECTIONS.get(self.direction, self.direction)
|
deltaX = abs(nodeA.x - nodeB.x)
|
||||||
self.direction = new_direction
|
deltaY = abs(nodeA.y - nodeB.y)
|
||||||
|
if deltaX > deltaY:
|
||||||
|
return (14 * deltaY) + (10 * (deltaX - deltaY))
|
||||||
|
return (14 * deltaX) + (10 *(deltaY - deltaX))
|
||||||
|
|
||||||
def turn_right(self):
|
def get_neighbours(self, node: Node):
|
||||||
new_direction = TURN_RIGHT_DIRECTIONS.get(self.direction, self.direction)
|
neighbours = list()
|
||||||
self.direction = new_direction
|
#if self.check_if_can_move(Coordinates(node.x + 1, node.y)):
|
||||||
|
neighbours.append(Node(node.x + 1, node.y))
|
||||||
|
#if self.check_if_can_move(Coordinates(node.x - 1, node.y)):
|
||||||
|
neighbours.append(Node(node.x - 1, node.y))
|
||||||
|
#if self.check_if_can_move(Coordinates(node.x, node.y + 1)):
|
||||||
|
neighbours.append(Node(node.x, node.y + 1))
|
||||||
|
#if self.check_if_can_move(Coordinates(node.x, node.y - 1)):
|
||||||
|
neighbours.append(Node(node.x, node.y - 1))
|
||||||
|
return neighbours
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
next_coords = self.get_next_move_coordinates()
|
if not self.path:
|
||||||
can_move = self.check_if_can_move(next_coords)
|
if not self.find_path():
|
||||||
if can_move:
|
return
|
||||||
self.x = next_coords.x
|
else:
|
||||||
self.y = next_coords.y
|
next = self.path.pop()
|
||||||
|
self.x = next.x
|
||||||
|
self.y = next.y
|
||||||
|
|
||||||
def get_next_move_coordinates(self):
|
def check_if_can_move(self, next_coords: Coordinates):
|
||||||
direction_moves = {
|
next_tile = self.warehouse.tiles[next_coords.x][next_coords.y]
|
||||||
'up': self.move_up(),
|
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
|
||||||
'down': self.move_down(),
|
tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height
|
||||||
'left': self.move_left(),
|
return tile_passable and tile_on_map
|
||||||
'right': self.move_right()
|
|
||||||
}
|
|
||||||
next_coords = direction_moves.get(self.direction, Coordinates(self.x, self.y))
|
|
||||||
return next_coords
|
|
||||||
|
|
||||||
def check_if_can_move(self, next_coords: Coordinates):
|
def pick_up_package(self, pack):
|
||||||
next_tile = self.assigned_warehouse.tiles[next_coords.x][next_coords.y]
|
tile = pack.lays_on_field
|
||||||
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
|
self.assigned_warehouse.tiles[tile.x_position][tile.y_position] = tile
|
||||||
tile_on_map = 0 <= next_coords.x < self.assigned_warehouse.width and 0 <= next_coords.y < self.assigned_warehouse.height
|
self.is_loaded = True
|
||||||
return tile_passable and tile_on_map
|
pack.lays_on_field = None
|
||||||
|
self.transported_package = pack
|
||||||
def move_right(self):
|
|
||||||
pos_x = self.x + 1
|
|
||||||
if pos_x > self.assigned_warehouse.width - 1:
|
|
||||||
pos_x = self.assigned_warehouse.width - 1
|
|
||||||
return Coordinates(x=pos_x, y=self.y)
|
|
||||||
|
|
||||||
def move_left(self):
|
|
||||||
pos_x = self.x - 1
|
|
||||||
if pos_x < 0:
|
|
||||||
pos_x = 0
|
|
||||||
return Coordinates(x=pos_x, y=self.y)
|
|
||||||
|
|
||||||
def move_down(self):
|
|
||||||
pos_y = self.y + 1
|
|
||||||
if pos_y > self.assigned_warehouse.height - 1:
|
|
||||||
pos_y = self.assigned_warehouse.height - 1
|
|
||||||
return Coordinates(x=self.x, y=pos_y)
|
|
||||||
|
|
||||||
def move_up(self):
|
|
||||||
pos_y = self.y - 1
|
|
||||||
if pos_y < 0:
|
|
||||||
pos_y = 0
|
|
||||||
return Coordinates(x=self.x, y=pos_y)
|
|
||||||
|
|
||||||
def package_ahead(self):
|
|
||||||
next_coords = self.get_next_move_coordinates()
|
|
||||||
potential_package = self.assigned_warehouse.tiles[next_coords.x][next_coords.y]
|
|
||||||
return isinstance(potential_package, Pack) and potential_package.status != PackStatus.STORED
|
|
||||||
|
|
||||||
def pick_up_package(self, pack):
|
|
||||||
tile = pack.lays_on_field
|
|
||||||
self.assigned_warehouse.tiles[tile.x_position][tile.y_position] = tile
|
|
||||||
self.is_loaded = True
|
|
||||||
pack.lays_on_field = None
|
|
||||||
self.transported_package = pack
|
|
||||||
|
@ -26,18 +26,3 @@ COLORS = {
|
|||||||
'lightgreen': (70, 238, 70),
|
'lightgreen': (70, 238, 70),
|
||||||
'red': (255, 0, 0)
|
'red': (255, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TURN_LEFT_DIRECTIONS = {
|
|
||||||
"left": "down",
|
|
||||||
"down": "right",
|
|
||||||
"right": "up",
|
|
||||||
"up": "left"
|
|
||||||
}
|
|
||||||
|
|
||||||
TURN_RIGHT_DIRECTIONS = {
|
|
||||||
"left": "up",
|
|
||||||
"up": "right",
|
|
||||||
"right": "down",
|
|
||||||
"down": "left"
|
|
||||||
}
|
|
10
main.py
10
main.py
@ -25,8 +25,6 @@ class MainGameFrame:
|
|||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
demo_agent_step = 1
|
|
||||||
demo_agent_sign = 1
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
@ -35,13 +33,7 @@ class MainGameFrame:
|
|||||||
self.draw_floor()
|
self.draw_floor()
|
||||||
self.draw_packages()
|
self.draw_packages()
|
||||||
self.draw_agent()
|
self.draw_agent()
|
||||||
self.agent.demo_agent_move(demo_agent_step, 8) #linijka w celu zademonstrowania poruszania się agenta
|
self.agent.move()
|
||||||
# self.agent.move() #oryginalna linijka
|
|
||||||
demo_agent_step += (1*demo_agent_sign)
|
|
||||||
if demo_agent_step >= 8:
|
|
||||||
demo_agent_sign = -1
|
|
||||||
if demo_agent_step == 0:
|
|
||||||
demo_agent_sign = 1
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
self.clock.tick(5)
|
self.clock.tick(5)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user