Naprawiono algorytm, dfs do poprawy

This commit is contained in:
Anna Nowak 2019-04-23 21:45:16 +02:00
parent e2e09e4537
commit 3b6fc6a446
4 changed files with 26 additions and 34 deletions

View File

@ -33,21 +33,14 @@ class GC(Cell):
item.return_trash(self) item.return_trash(self)
self.update_image() self.update_image()
def find_houses(self,enviromnent): def find_houses(self,enviromnent, house_count):
for row in enviromnent: x = self.x
for cell in row: y = self.y
goal = [] result = []
if type(cell) == House and cell.container.is_full(): for home in range(house_count):
goal.append([cell.x, cell.y]) avalible_moves = check_moves(enviromnent, x,y)
if len(self.moves) == 0: [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]])
x = self.x self.moves.extend(result)
y = self.y
else:
x,y = self.moves[-2]
avalible_moves = check_moves(enviromnent, x,y)
self.moves.extend(DFS(enviromnent,avalible_moves,[[x,y]],goal))
self.moves.append("pick_garbage")
print(self.moves)
self.moves.reverse() self.moves.reverse()

View File

@ -4,13 +4,9 @@ from DataModels.Cell import Cell
class House(Cell): class House(Cell):
def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0): def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0):
Cell.__init__(self, x, y, max_rubbish, yellow, green, blue) Cell.__init__(self, x, y, max_rubbish, yellow, green, blue)
self.unvisited = True
def return_trash(self, collector): def return_trash(self, collector):
self.container.yellow, self.container.green, self.container.blue = collector.container.add( self.container.yellow, self.container.green, self.container.blue = collector.container.add(
[self.container.yellow, self.container.green, self.container.blue]) [self.container.yellow, self.container.green, self.container.blue])
self.update_image() self.update_image()
def is_empty(self):
if(self.container.yellow == self.container.green == self.container.blue == 0):
return True
else: return False

View File

@ -1,8 +1,7 @@
from utilities import movement,check_moves from utilities import movement,check_moves
from DataModels.House import House from DataModels.House import House
def DFS(grid, avaliable_movement, gc_moveset,goal): from DataModels.Container import Container
print("Moveset: "+str(gc_moveset)) def DFS(grid, available_movement, gc_moveset, depth=0):
possible_goals = [] possible_goals = []
a = gc_moveset[-1][0] a = gc_moveset[-1][0]
b = gc_moveset[-1][1] b = gc_moveset[-1][1]
@ -10,26 +9,29 @@ def DFS(grid, avaliable_movement, gc_moveset,goal):
possible_goals.append([a-1,b]) possible_goals.append([a-1,b])
possible_goals.append([a,b+1]) possible_goals.append([a,b+1])
possible_goals.append([a,b-1]) possible_goals.append([a,b-1])
house_in_area = False house_in_area = False
for location in possible_goals: for location in possible_goals:
try: try:
if(type(grid[location[0]][location[1]]) == House): if(type(grid[location[0]][location[1]]) == House and grid[location[0]][location[1]].container.is_full
and grid[location[0]][location[1]].unvisited):
grid[location[0]][location[1]].unvisited = False
house_in_area = True house_in_area = True
print("dupa")
break break
except: except:
continue continue
print(str(house_in_area) + " possible goals: " + str(possible_goals) + " gc_moveset: " + str(gc_moveset[-1])) if(house_in_area or len(available_movement) == 0):
if(house_in_area or len(avaliable_movement) == 0):
print("Do zwrocenia: ",gc_moveset) print("Do zwrocenia: ",gc_moveset)
return gc_moveset xy = gc_moveset[-1]
print(available_movement)
gc_moveset.append("pick_garbage")
return (xy, gc_moveset)
x,y = gc_moveset[-1] x,y = gc_moveset[-1]
for direction in avaliable_movement: for direction in available_movement:
x_next, y_next = movement(grid,x,y)[0][direction] x_next, y_next = movement(grid,x,y)[0][direction]
avaliable_movement_next = check_moves(grid, x_next,y_next,direction) available_movement_next = check_moves(grid, x_next,y_next,direction)
gc_moveset_next = gc_moveset.copy() gc_moveset_next = gc_moveset.copy()
gc_moveset_next.append([x_next,y_next]) gc_moveset_next.append([x_next,y_next])
return DFS(grid, avaliable_movement_next, gc_moveset_next,goal) return DFS(grid, available_movement_next, gc_moveset_next)

View File

@ -12,7 +12,7 @@ from DataModels.Road import Road
from DataModels.GC import GC from DataModels.GC import GC
pygame_sprites = pygame.sprite.Group() pygame_sprites = pygame.sprite.Group()
house_count=0
FPS_CLOCK = pygame.time.Clock() FPS_CLOCK = pygame.time.Clock()
GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
@ -50,6 +50,7 @@ for y in map.readlines():
elif x is 'H': elif x is 'H':
map_objects[x_coord][y_coord] = generate(x)( map_objects[x_coord][y_coord] = generate(x)(
x_coord, y_coord, HOUSE_CAPACITY, yellow, green, blue) x_coord, y_coord, HOUSE_CAPACITY, yellow, green, blue)
house_count+=1
elif x is 'B': elif x is 'B':
map_objects[x_coord][y_coord] = generate( map_objects[x_coord][y_coord] = generate(
x)(x_coord, y_coord, 100, "Dump_Blue") x)(x_coord, y_coord, 100, "Dump_Blue")
@ -90,7 +91,7 @@ while True:
elif event.key == pygame.K_SPACE: elif event.key == pygame.K_SPACE:
gc.collect(map_objects) gc.collect(map_objects)
elif event.key == pygame.K_0: elif event.key == pygame.K_0:
gc.find_houses(map_objects) gc.find_houses(map_objects,house_count)
gc.make_actions_from_list(map_objects) gc.make_actions_from_list(map_objects)
pygame_sprites.update() pygame_sprites.update()