SZI2019SmieciarzWmi/Traversal/BestFS_iter.py

97 lines
3.4 KiB
Python

from utilities import movement,check_moves
from DataModels.House import House
from DataModels.Container import Container
from config import GRID_WIDTH, GRID_HEIGHT
from math import sqrt
INF = float('Inf')
def CalculateDistance(gc, houses_list):
min_distance_goal = ['-',INF]
for h in houses_list:
distance = sqrt(pow(h[1][0]-gc[0],2)+pow(h[1][1]-gc[1],2))
if(min_distance_goal[1] > distance):
min_distance_goal = [h[1], distance]
return min_distance_goal
def BestFS(grid, gc_moveset, houses_list, result, available_movement, mode = "House"):
#result = [gc_moveset]
if(len(houses_list) == 0 and depth > 100):
return result
print(gc_moveset)
x, y = gc_moveset[0], gc_moveset[1]
available_movement = check_moves(grid, x, y)
constraint = 100
while(len(houses_list) > 0 and constraint > 0):
print("================")
print("iteracja: "+str(100-constraint))
print("GC: "+str([x,y]))
print(houses_list)
#calculate distance to the nearest object
min_distance_goal = CalculateDistance([x,y], houses_list)
print(min_distance_goal)
#set preffered directions based on the closest object
preffered_directions = []
discouraged_directions = []
if(min_distance_goal[1] == 1):
result.append("pick_garbage")
cell = grid[min_distance_goal[0][0]][min_distance_goal[0][1]]
print("***"+str([cell,min_distance_goal[0]])+"***")
houses_list.remove([cell,min_distance_goal[0]])
if(len(houses_list)==0):
break
available_movement = check_moves(grid, x, y)
min_distance_goal = CalculateDistance([x,y], houses_list)
print(min_distance_goal)
print(min_distance_goal[0])
if(min_distance_goal[0][0] > x):
preffered_directions.append("right")
if(min_distance_goal[0][0] < x):
preffered_directions.append("left")
if(min_distance_goal[0][1] > y):
preffered_directions.append("down")
if(min_distance_goal[0][1] < y):
preffered_directions.append("up")
if(len(preffered_directions) == 1):
discouraged_directions.append(movement(grid, x, y)[1][preffered_directions[0]])
print("Preferred: "+str(preffered_directions))
print("Discouraged: "+str(discouraged_directions))
print("Available: "+str(available_movement))
if(len(available_movement) == 0):
available_movement = check_moves(grid, x, y)
if(len(available_movement)>0):
next_move = available_movement[0]
for move in available_movement:
if (move not in discouraged_directions):
next_move = move
break
for move in preffered_directions:
if(move in available_movement):
next_move = move
break
print("Next move: "+str(next_move))
x_next, y_next = movement(grid, x, y)[0][next_move]
print("Next moving to "+str(x_next)+" "+str(y_next))
result.append([x_next,y_next])
x, y = x_next, y_next
available_movement = check_moves(grid, x, y, next_move)
print("------------------------------")
constraint -= 1
return result