Why u do da bestfs with bugz

This commit is contained in:
Magdalena Wilczynska 2019-05-13 22:01:33 +02:00
parent e39b82b4a8
commit 5790a923f0
4 changed files with 304 additions and 64 deletions

View File

@ -59,6 +59,7 @@ class GC(Cell):
result = []
houses_list = []
dump_list = []
a = 0
for row in enviromnent:
b = 0
@ -66,13 +67,18 @@ class GC(Cell):
print(col)
if (type(col) is House):
houses_list.append([col,[a,b]])
if (type(col) is Dump):
dump_list.append([col,[a,b]])
b += 1
a += 1
for home in range(house_count):
avalible_moves = check_moves(enviromnent, x,y)
[x,y],result = BestFS(enviromnent,avalible_moves,[[x,y]],houses_list)
self.moves.extend(result)
available_movement = check_moves(enviromnent, x, y)
result, [x, y] = BestFS(enviromnent, [x, y], houses_list)
self.moves.extend(result)
available_movement = check_moves(enviromnent, x, y)
result, [x, y] = BestFS(enviromnent, [x, y], dump_list)
print("IM ALIVEEEEEEEEEEEEEEEE")
self.moves.extend(result[1:])
self.moves.reverse()

View File

@ -1,72 +1,126 @@
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, goal):
result = sqrt(pow(goal[0]-gc[0],2)+pow(goal[1]-gc[1],2))
return result
def BestFS(grid, available_movement, gc_moveset, houses_list, depth=0):
possible_goals = []
a = gc_moveset[-1][0]
b = gc_moveset[-1][1]
possible_goals.append([a+1,b])
possible_goals.append([a-1,b])
possible_goals.append([a,b+1])
possible_goals.append([a,b-1])
house_in_area = False
for location in possible_goals:
if location[0]>=0 and location[1]>=0:
try:
cell = grid[location[0]][location[1]]
if(type(cell) == House and cell.container.is_full and cell.unvisited):
cell.unvisited = False
house_in_area = True
break
except:
continue
if(house_in_area):
xy = gc_moveset[-1]
gc_moveset.append("pick_garbage")
return (xy, gc_moveset)
if len(available_movement) == 0 or depth>30:
return
x,y = gc_moveset[-1]
print([x,y])
#calculate distance to the nearest object
def CalculateDistance(gc, object_list):
min_distance_goal = ['-',INF]
for h in houses_list:
distance = CalculateDistance([a,b],h[1])
for h in object_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, object_list):
if(len(object_list) == 0):
return result
result = [gc_moveset]
print(gc_moveset)
x, y = gc_moveset[0], gc_moveset[1]
available_movement = check_moves(grid, x, y)
decision_stack = []
constraint = 100
while(len(object_list) > 0 and constraint > 0):
print("================")
print("iteracja: "+str(100-constraint))
print("GC: "+str([x,y]))
print(object_list)
#calculate distance to the nearest object
min_distance_goal = CalculateDistance([x,y], object_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")
decision_stack = []
cell = grid[min_distance_goal[0][0]][min_distance_goal[0][1]]
print("***"+str([cell,min_distance_goal[0]])+"***")
object_list.remove([cell,min_distance_goal[0]])
if(len(object_list)==0):
break
available_movement = check_moves(grid, x, y)
min_distance_goal = CalculateDistance([x,y], object_list)
print(min_distance_goal)
#set preffered directions based on the closest object
preffered_directions = []
if(min_distance_goal[1] == 1):
preffered_directions.append("pick_garbage")
if(min_distance_goal[0][0] >= a):
preffered_directions.append("right")
if(min_distance_goal[0][0] <= a):
preffered_directions.append("left")
if(min_distance_goal[0][1] >= b):
preffered_directions.append("down")
if(min_distance_goal[0][1] <= b):
preffered_directions.append("up")
print(preffered_directions)
print("------------------------------")
for direction in available_movement:
x_next, y_next = movement(grid,x,y)[0][direction]
available_movement_next = check_moves(grid, x_next,y_next,direction)
gc_moveset_next = gc_moveset.copy()
gc_moveset_next.append([x_next,y_next])
result = BestFS(grid, available_movement_next, gc_moveset_next, houses_list, depth+1)
if result!= None:
return result
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))
possible_routes = len([i for i in available_movement if i in preffered_directions ])
print("Preferred to available count: "+str(possible_routes))
#HOTFIX
if(possible_routes > 1):
if(len(decision_stack) > 0):
if(decision_stack[0] == [x,y]):
preffered_directions.pop(1)
else:
decision_stack = [[x,y]]
preffered_directions.pop(0)
print("Decision stack: "+str(decision_stack))
""" if(possible_routes > 1):
for move in available_movement:
if (move in preffered_directions):
x_next, y_next = movement(grid, x, y)[0][move]
route = BestFS(grid, [x_next,y_next], houses_list, [[x_next, y_next]], check_moves(grid, x_next, y_next, move), "House", depth + 1)
print("DIRECTION: "+str(move)+", GIVEN "+str(len(houses_list))+" HOUSES, RECURSION ON DEPTH "+str(depth+1)+" returned "+str(route))
if (route == None):
break
if (route.count("pick_garbage") - 1 == len(houses_list)):
print(str(route.count("pick_garbage"))+" / "+str(len(houses_list)))
print("ROUTE ADDED")
result.extend(route)
break
break """
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, [x,y]

View File

@ -0,0 +1,84 @@
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, goal):
result = sqrt(pow(goal[0]-gc[0],2)+pow(goal[1]-gc[1],2))
return result
def BestFS(grid, available_movement, gc_moveset, houses_list, mode = "House", depth=0):
possible_goals = []
a = gc_moveset[-1][0]
b = gc_moveset[-1][1]
possible_goals.append([a+1,b])
possible_goals.append([a-1,b])
possible_goals.append([a,b+1])
possible_goals.append([a,b-1])
object_in_area = False
for location in possible_goals:
if GRID_WIDTH>location[0]>=0 and GRID_HEIGHT>location[1]>=0:
cell = grid[location[0]][location[1]]
if mode == "House":
if(type(cell) == House and cell.container.is_full and cell.unvisited):
cell.unvisited = False
object_in_area = True
print("***"+str([cell,location])+"***")
houses_list.remove([cell,location])
break
elif mode == "Dump":
if(type(cell) == Dump and cell.unvisited):
cell.unvisited = False
object_in_area = True
break
if(object_in_area):
xy = gc_moveset[-1]
gc_moveset.append("pick_garbage")
return (xy, gc_moveset)
if len(available_movement) == 0 or depth>30:
return
x,y = gc_moveset[-1]
print([x,y])
print(houses_list)
#calculate distance to the nearest object
min_distance_goal = ['-',INF]
for h in houses_list:
distance = CalculateDistance([a,b],h[1])
if(min_distance_goal[1] > distance):
min_distance_goal = [h[1], distance]
print(min_distance_goal)
#set preffered directions based on the closest object
preffered_directions = []
if(min_distance_goal[0][0] >= a):
preffered_directions.append("right")
if(min_distance_goal[0][0] <= a):
preffered_directions.append("left")
if(min_distance_goal[0][1] >= b):
preffered_directions.append("down")
if(min_distance_goal[0][1] <= b):
preffered_directions.append("up")
print(preffered_directions)
print(available_movement)
print("=")
output_list = available_movement.copy()
output_list = output_list.sort(key=lambda x: preffered_directions.index(x))
print(output_list)
print("------------------------------")
for direction in available_movement:
x_next, y_next = movement(grid,x,y)[0][direction]
available_movement_next = check_moves(grid, x_next,y_next,direction)
gc_moveset_next = gc_moveset.copy()
gc_moveset_next.append([x_next,y_next])
result = BestFS(grid, available_movement_next, gc_moveset_next, houses_list, "House", depth+1)
if result!= None:
return result

96
Traversal/BestFS_iter.py Normal file
View File

@ -0,0 +1,96 @@
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