Route_planning_bfs #2
BIN
source/__pycache__/bfs.cpython-311.pyc
Normal file
BIN
source/__pycache__/bfs.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
from crop_protection_product import CropProtectionProduct
|
from crop_protection_product import CropProtectionProduct
|
||||||
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, GREY
|
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT
|
||||||
from area.field import fieldX,fieldY
|
from area.field import fieldX,fieldY
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
@ -23,8 +23,6 @@ class Tractor:
|
|||||||
|
|
||||||
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
||||||
|
|
||||||
# self.previous_x = x
|
|
||||||
# self.previous_y = y
|
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
self.image = pygame.image.load('resources/images/tractor_right.png').convert_alpha()
|
self.image = pygame.image.load('resources/images/tractor_right.png').convert_alpha()
|
||||||
|
|
||||||
@ -112,7 +110,19 @@ class Tractor:
|
|||||||
self.direction -= 1
|
self.direction -= 1
|
||||||
self.image = pygame.transform.rotate(self.image, 90)
|
self.image = pygame.transform.rotate(self.image, 90)
|
||||||
|
|
||||||
|
#sprawdza czy mozna sie dalej poruszyc, czy nie wyjdziemy poza mape - do funkcji succ w bfs
|
||||||
|
def can_it_move_node(node):
|
||||||
|
if node.get_direction() == DIRECTION_EAST and node.get_x() + TILE_SIZE < FIELD_WIDTH:
|
||||||
|
return "move east"
|
||||||
|
elif node.get_direction() == DIRECTION_WEST and node.get_x() - TILE_SIZE >=0:
|
||||||
|
return "move west"
|
||||||
|
elif node.get_direction() == DIRECTION_NORTH and node.get_y() - TILE_SIZE >=0:
|
||||||
|
return "move north"
|
||||||
|
elif node.get_direction() == DIRECTION_SOUTH and node.get_y() + TILE_SIZE < FIELD_HEIGHT:
|
||||||
|
return "move south"
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def draw_tractor(self, win):
|
def draw_tractor(self, win):
|
||||||
|
|
||||||
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
||||||
|
179
source/bfs.py
Normal file
179
source/bfs.py
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
from queue import Queue
|
||||||
|
from area.constants import DIRECTION_EAST, DIRECTION_WEST, DIRECTION_NORTH, DIRECTION_SOUTH, TILE_SIZE
|
||||||
|
from area.field import get_tile_coordinates, tiles, fieldX, fieldY
|
||||||
|
import copy
|
||||||
|
from area.tractor import Tractor
|
||||||
|
|
||||||
|
class Istate:
|
||||||
|
|
||||||
|
def __init__(self, x, y, direction ):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.direction = direction
|
||||||
|
|
||||||
|
|
||||||
|
def get_x(self):
|
||||||
|
return self.x
|
||||||
|
|
||||||
|
def set_x(self, _x):
|
||||||
|
self.x = _x
|
||||||
|
|
||||||
|
def get_y(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
|
def set_y(self, _y):
|
||||||
|
self.y = _y
|
||||||
|
|
||||||
|
def get_direction(self):
|
||||||
|
return self.direction
|
||||||
|
|
||||||
|
def set_direction(self, _direction):
|
||||||
|
self.parent = _direction
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
|
||||||
|
def __init__(self, x, y, direction, parent, action):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.direction = direction
|
||||||
|
self.action = action
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#getters and setters:
|
||||||
|
|
||||||
|
def get_parent(self):
|
||||||
|
return self.parent
|
||||||
|
|
||||||
|
def set_parent(self, _parent):
|
||||||
|
self.parent = _parent
|
||||||
|
|
||||||
|
def get_action(self):
|
||||||
|
return self.action
|
||||||
|
|
||||||
|
def set_action(self, _action):
|
||||||
|
self.parent = _action
|
||||||
|
|
||||||
|
def get_x(self):
|
||||||
|
return self.x
|
||||||
|
|
||||||
|
def set_x(self, _x):
|
||||||
|
self.x = _x
|
||||||
|
|
||||||
|
def get_y(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
|
def set_y(self, _y):
|
||||||
|
self.y = _y
|
||||||
|
|
||||||
|
def get_direction(self):
|
||||||
|
return self.direction
|
||||||
|
|
||||||
|
def set_direction(self, _direction):
|
||||||
|
self.parent = _direction
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
return copy.copy(self)
|
||||||
|
|
||||||
|
#State:
|
||||||
|
def get_state(self):
|
||||||
|
x = self.get_x()
|
||||||
|
y = self.get_y()
|
||||||
|
direct = self.get_direction()
|
||||||
|
state = (x, y, direct)
|
||||||
|
return state #returns state as a tuple (x,y,direction)
|
||||||
|
|
||||||
|
|
||||||
|
def goal_test(elem, goalstate):
|
||||||
|
if elem.get_x() == goalstate[0] and elem.get_y() == goalstate[1]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
#actions(string): move, rotate_to_left, rotate_to_right
|
||||||
|
|
||||||
|
#main search function:
|
||||||
|
def graphsearch(istate, succ, goaltest):
|
||||||
|
|
||||||
|
fringe = []
|
||||||
|
explored = []
|
||||||
|
node = Node(istate.get_x(), istate.get_y(), istate.get_direction(), None, None)
|
||||||
|
fringe.append(node)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
if not fringe:
|
||||||
|
return False
|
||||||
|
|
||||||
|
elem = fringe.pop(0)
|
||||||
|
temp = copy.copy(elem)
|
||||||
|
if goal_test(elem, goaltest) is True: #jesli True zwroc ciag akcji
|
||||||
|
return get_moves(elem)
|
||||||
|
|
||||||
|
explored.append(elem)
|
||||||
|
|
||||||
|
for (action, state) in succ(temp): #dla wszystkich mozliwych stanow i akcjach otrzymanych dla danego wierzcholka
|
||||||
|
fringe_tuple = []
|
||||||
|
explored_tuple = []
|
||||||
|
|
||||||
|
for node in fringe:
|
||||||
|
fringe_tuple.append((node.get_x(), node.get_y(), node.get_direction()))
|
||||||
|
for node in explored:
|
||||||
|
explored_tuple.append((node.get_x(), node.get_y(), node.get_direction()))
|
||||||
|
|
||||||
|
|
||||||
|
if state not in fringe_tuple and state not in explored_tuple:
|
||||||
|
x = Node(state[0], state[1], state[2], elem, action)
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
|
||||||
|
#funkcja nastepnika - jakie akcje sa mozlwie na danym polu i jaki bedzie stan po wykonaniu tych akcji
|
||||||
|
def succ(elem):
|
||||||
|
actions_states = []
|
||||||
|
temp = copy.copy(elem.get_direction())
|
||||||
|
|
||||||
|
if temp == 1:
|
||||||
|
temp = 4
|
||||||
|
else:
|
||||||
|
temp -= 1
|
||||||
|
actions_states.append(("rotate_left", (elem.get_x(), elem.get_y(), temp)))
|
||||||
|
|
||||||
|
temp = copy.copy(elem.get_direction())
|
||||||
|
if temp == 4:
|
||||||
|
temp = 1
|
||||||
|
else:
|
||||||
|
temp += 1
|
||||||
|
actions_states.append(("rotate_right", (elem.get_x(), elem.get_y(), temp)))
|
||||||
|
|
||||||
|
temp_move_east = elem.get_x() + TILE_SIZE
|
||||||
|
temp_move_west = elem.get_x() - TILE_SIZE
|
||||||
|
temp_move_north = elem.get_y() - TILE_SIZE
|
||||||
|
temp_move_south = elem.get_y() + TILE_SIZE
|
||||||
|
|
||||||
|
if Tractor.can_it_move_node(elem) == "move east":
|
||||||
|
actions_states.append(("move", (temp_move_east, elem.get_y(), elem.get_direction())))
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move west":
|
||||||
|
actions_states.append(("move", (temp_move_west, elem.get_y(), elem.get_direction())))
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move north":
|
||||||
|
actions_states.append(("move", (elem.get_x(), temp_move_north, elem.get_direction())))
|
||||||
|
elif Tractor.can_it_move_node(elem) == "move south":
|
||||||
|
actions_states.append(("move", (elem.get_x(), temp_move_south, elem.get_direction())))
|
||||||
|
return actions_states
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_moves(elem):
|
||||||
|
move_list = []
|
||||||
|
while (elem.get_parent() != None):
|
||||||
|
move_list.append(elem.get_action())
|
||||||
|
elem = elem.get_parent()
|
||||||
|
move_list.reverse()
|
||||||
|
return move_list
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#Testy
|
Loading…
Reference in New Issue
Block a user