bug fix regarding obstacles

This commit is contained in:
MarRac 2024-06-09 22:09:36 +02:00
parent 0f92ffd53f
commit 0e2d63fbbf
5 changed files with 40 additions and 4 deletions

View File

@ -91,7 +91,7 @@ def heuristic(current_x, current_y, end_x, end_y):
# actions(string): move, rotate_to_left, rotate_to_right
# main search function:
def a_star(istate, succ, goaltest, tractor):
def a_star(istate, succ_astar, goaltest):
fringe = []
explored = set()
node = Node(0, istate.get_x(), istate.get_y(), istate.get_direction(), None, None, 0)
@ -109,7 +109,7 @@ def a_star(istate, succ, goaltest, tractor):
explored.add(elem)
for (action, state) in succ(temp, tractor):
for (action, state) in succ_astar(temp):
fringe_tuple = []
explored_tuple = []

View File

@ -159,6 +159,42 @@ def succ(elem, tractor):
return actions_states
#its the copy of successor function for A* only - tractor can ride through stones if there is no other way:
def succ_astar(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
#returns list of actions
def get_moves(elem):

View File

@ -10,7 +10,7 @@ from area.field import tiles, fieldX, fieldY
from area.field import get_tile_coordinates, get_tile_index
from ground import Dirt
from plant import Plant
from bfs import graphsearch, Istate, succ
from bfs import graphsearch, Istate, succ_astar, succ
from astar import a_star
from NN.neural_network import load_model, load_image, guess_image, display_image, display_result, clear_text_area
from PIL import Image
@ -71,7 +71,7 @@ def main():
goaltest[1]=tile_y
#moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
moves = (a_star(istate, succ, goaltest, tractor))
moves = (a_star(istate, succ_astar, goaltest))
print(moves)