From 5c50c54e4689a09d05cf835b7ce3700630907523 Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Mon, 13 May 2019 13:16:36 +0200 Subject: [PATCH 1/4] Dodano BFS --- DataModels/GC.py | 16 ++++++++++----- Traversal/BFS.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ Traversal/DFS.py | 21 ++++++++----------- main.py | 4 +++- 4 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 Traversal/BFS.py diff --git a/DataModels/GC.py b/DataModels/GC.py index fcde0ab..20c7d9e 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -5,11 +5,10 @@ from DataModels.Dump import Dump from config import GRID_WIDTH, GRID_HEIGHT, DELAY from utilities import movement, check_moves from Traversal.DFS import DFS +from Traversal.BFS import BFS import pygame - class GC(Cell): moves_made = 0 - def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0): Cell.__init__(self, x, y, max_rubbish, yellow, green, blue) self.moves = [] @@ -38,17 +37,24 @@ class GC(Cell): def get_moves_made(self): return self.moves_made - def find_houses(self,enviromnent, house_count,dump_count): + + def find_houses(self,enviromnent, house_count,dump_count, mode): x = self.x y = self.y result = [] for home in range(house_count): avalible_moves = check_moves(enviromnent, x,y) - [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],"House") + if mode == "DFS": + [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House) + elif mode == "BFS": + [x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House) self.moves.extend(result) for dump in range(dump_count): avalible_moves = check_moves(enviromnent, x,y) - [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],"Dump") + if mode == "DFS": + [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump) + elif mode == "BFS": + [x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump) self.moves.extend(result) self.moves.reverse() diff --git a/Traversal/BFS.py b/Traversal/BFS.py new file mode 100644 index 0000000..1f44342 --- /dev/null +++ b/Traversal/BFS.py @@ -0,0 +1,52 @@ +from utilities import movement,check_moves +from DataModels.House import House +from DataModels.Dump import Dump +from DataModels.Container import Container +from config import GRID_WIDTH, GRID_HEIGHT +from collections import deque + +def BFS(grid, available_movement, gc_moveset, mode): + queue = deque() + visited_nodes=[] + for x in range(GRID_WIDTH): + visited_nodes.append([]) + for y in range(GRID_HEIGHT): + visited_nodes[-1].append(0) + visited_nodes[gc_moveset[-1][0]][gc_moveset[-1][1]] = 1 + queue.append([available_movement, gc_moveset, visited_nodes]) + + + while queue: + possible_goals = [] + state = queue.popleft() + avalible_movement_state = state[0] + gc_moveset_state = state[1] + visited_nodes_state = state[2] + a = gc_moveset_state[-1][0] + b = gc_moveset_state[-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(type(cell) == mode and cell.unvisited): + cell.unvisited = False + object_in_area = True + break + + x,y = gc_moveset_state[-1] + if(object_in_area): + gc_moveset_state.append("pick_garbage") + return ([x,y], gc_moveset_state) + + for direction in avalible_movement_state: + x_next, y_next = movement(grid,x,y)[0][direction] + if visited_nodes_state[x_next][y_next]==0: + available_movement_next = check_moves(grid, x_next,y_next,direction) + gc_moveset_next = gc_moveset_state.copy() + gc_moveset_next.append([x_next,y_next]) + visited_nodes_state[x_next][y_next]=1 + queue.append([available_movement_next, gc_moveset_next,visited_nodes_state]) diff --git a/Traversal/DFS.py b/Traversal/DFS.py index 9e71834..0bdf85e 100644 --- a/Traversal/DFS.py +++ b/Traversal/DFS.py @@ -5,6 +5,7 @@ from DataModels.Container import Container from config import GRID_WIDTH, GRID_HEIGHT def DFS(grid, available_movement, gc_moveset, mode,depth=0): + possible_goals = [] a = gc_moveset[-1][0] b = gc_moveset[-1][1] @@ -16,24 +17,18 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0): 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 - break - elif mode == "Dump": - if(type(cell) == Dump and cell.unvisited): - cell.unvisited = False - object_in_area = True - break + if(type(cell) == mode and cell.unvisited): + cell.unvisited = False + object_in_area = True + break + + x,y = gc_moveset[-1] if(object_in_area): - xy = gc_moveset[-1] gc_moveset.append("pick_garbage") - return (xy, gc_moveset) + return ([x,y], gc_moveset) if len(available_movement) == 0 or depth>30: return - x,y = gc_moveset[-1] 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) diff --git a/main.py b/main.py index 04761d1..45d8a22 100755 --- a/main.py +++ b/main.py @@ -99,7 +99,9 @@ while True: elif event.key == pygame.K_SPACE: gc.collect(map_objects) elif event.key == pygame.K_0: - gc.find_houses(map_objects,house_count,dump_count) + gc.find_houses(map_objects,house_count,dump_count, "DFS") + elif event.key == pygame.K_8: + gc.find_houses(map_objects,house_count,dump_count, "BFS") gc.make_actions_from_list(map_objects) pygame_sprites.update() From a605f7a87e5b7dc02867795685240dfd5b90cca5 Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Tue, 14 May 2019 22:18:38 +0200 Subject: [PATCH 2/4] Poprawiono liczenie krokow, usunieto bug z powtarzaniem algorytmu --- DataModels/GC.py | 16 ++++++++++++---- Traversal/BFS.py | 3 ++- Traversal/DFS.py | 3 ++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/DataModels/GC.py b/DataModels/GC.py index 20c7d9e..61ca3f5 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -42,24 +42,32 @@ class GC(Cell): x = self.x y = self.y result = [] + element_list=[] + house_count_after_search=house_count for home in range(house_count): avalible_moves = check_moves(enviromnent, x,y) if mode == "DFS": - [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House) + house,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House) elif mode == "BFS": - [x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House) + house,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House) self.moves.extend(result) + element_list.append(house) + for dump in range(dump_count): avalible_moves = check_moves(enviromnent, x,y) if mode == "DFS": - [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump) + dump,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump) elif mode == "BFS": - [x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump) + dump,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump) self.moves.extend(result) + element_list.append(dump) + for x in element_list: + x.unvisited = True self.moves.reverse() + def make_actions_from_list(self,environment): now = pygame.time.get_ticks() if len(self.moves)==0 or now - self.old_time <= DELAY: diff --git a/Traversal/BFS.py b/Traversal/BFS.py index 1f44342..e43a947 100644 --- a/Traversal/BFS.py +++ b/Traversal/BFS.py @@ -40,7 +40,8 @@ def BFS(grid, available_movement, gc_moveset, mode): x,y = gc_moveset_state[-1] if(object_in_area): gc_moveset_state.append("pick_garbage") - return ([x,y], gc_moveset_state) + gc_moveset_state=gc_moveset_state[1::] + return (cell,[x,y], gc_moveset_state) for direction in avalible_movement_state: x_next, y_next = movement(grid,x,y)[0][direction] diff --git a/Traversal/DFS.py b/Traversal/DFS.py index 0bdf85e..68f59ad 100644 --- a/Traversal/DFS.py +++ b/Traversal/DFS.py @@ -25,7 +25,7 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0): x,y = gc_moveset[-1] if(object_in_area): gc_moveset.append("pick_garbage") - return ([x,y], gc_moveset) + return (cell,[x,y], gc_moveset) if len(available_movement) == 0 or depth>30: return @@ -35,5 +35,6 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0): gc_moveset_next = gc_moveset.copy() gc_moveset_next.append([x_next,y_next]) result = DFS(grid, available_movement_next, gc_moveset_next,mode, depth+1) + result[2]=result[2][1::] if result!= None: return result From 36b8bf702a89e4cd1e3c0268485e1317902dbe9b Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Tue, 14 May 2019 22:31:30 +0200 Subject: [PATCH 3/4] naprawiono bug z DFS --- DataModels/GC.py | 1 + Traversal/BFS.py | 1 - Traversal/DFS.py | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/DataModels/GC.py b/DataModels/GC.py index 61ca3f5..46f930d 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -50,6 +50,7 @@ class GC(Cell): house,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House) elif mode == "BFS": house,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House) + result = result[1::] self.moves.extend(result) element_list.append(house) diff --git a/Traversal/BFS.py b/Traversal/BFS.py index e43a947..e6fcc07 100644 --- a/Traversal/BFS.py +++ b/Traversal/BFS.py @@ -40,7 +40,6 @@ def BFS(grid, available_movement, gc_moveset, mode): x,y = gc_moveset_state[-1] if(object_in_area): gc_moveset_state.append("pick_garbage") - gc_moveset_state=gc_moveset_state[1::] return (cell,[x,y], gc_moveset_state) for direction in avalible_movement_state: diff --git a/Traversal/DFS.py b/Traversal/DFS.py index 68f59ad..353b17b 100644 --- a/Traversal/DFS.py +++ b/Traversal/DFS.py @@ -25,7 +25,7 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0): x,y = gc_moveset[-1] if(object_in_area): gc_moveset.append("pick_garbage") - return (cell,[x,y], gc_moveset) + return [cell,[x,y], gc_moveset] if len(available_movement) == 0 or depth>30: return @@ -35,6 +35,5 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0): gc_moveset_next = gc_moveset.copy() gc_moveset_next.append([x_next,y_next]) result = DFS(grid, available_movement_next, gc_moveset_next,mode, depth+1) - result[2]=result[2][1::] if result!= None: return result From 2ccc2b41b3a66d49e0b85959b4722b9532ac9670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Starski?= Date: Tue, 14 May 2019 21:20:40 +0000 Subject: [PATCH 4/4] Update 'Raports/SI_Raport_2.md' --- Raports/SI_Raport_2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Raports/SI_Raport_2.md b/Raports/SI_Raport_2.md index 6ee5132..2c021a1 100644 --- a/Raports/SI_Raport_2.md +++ b/Raports/SI_Raport_2.md @@ -1,6 +1,6 @@ # Sztuczna inteligencja 2019 - Raport 2 -**Czas trwania opisywanych prac:** 06.03.2018 - 26.03.2018 +**Czas trwania opisywanych prac:** 06.03.2019 - 26.03.2019 **Członkowie zespołu:** Anna Nowak, Magdalena Wilczyńska, Konrad Pierzyński, Michał Starski