Merged with master

This commit is contained in:
Magdalena Wilczyńska 2019-05-14 23:45:35 +02:00
commit 431d130379
5 changed files with 84 additions and 20 deletions

View File

@ -6,11 +6,10 @@ from config import GRID_WIDTH, GRID_HEIGHT, DELAY
from utilities import movement, check_moves
from Traversal.DFS import DFS
from Traversal.BestFS import BestFS
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 = []
@ -39,18 +38,33 @@ class GC(Cell):
def get_moves_count(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 = []
element_list=[]
house_count_after_search=house_count
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":
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)
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":
dump,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump)
elif mode == "BFS":
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()
@ -89,6 +103,7 @@ class GC(Cell):
self.moves.extend(result[1:])
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:

View File

@ -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

52
Traversal/BFS.py Normal file
View File

@ -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 (cell,[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])

View File

@ -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):
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 [cell,[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)

View File

@ -99,9 +99,11 @@ 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_9:
gc.find_houses_BestFS(map_objects)
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()