This commit is contained in:
MonoYuku 2021-06-23 11:40:06 +02:00
parent 89d1aa7802
commit 827a105f17
10 changed files with 208 additions and 21 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

150
astar.py Normal file
View File

@ -0,0 +1,150 @@
from truck import Truck
class Node():
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def astar(maze, start, end):
start_node = Node(position=start)
end_node = Node(position=end)
x=0
open_list = []
closed_list = []
open_list.append(start_node)
while len(open_list) > 0:
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
if (maze[current.position[0]][current.position[1]] == 1):
x = x + 1
if (maze[current.position[0]][current.position[1]] == 2):
x = x + 3
if (maze[current.position[0]][current.position[1]] == 6):
x = x + 20
if (maze[current.position[0]][current.position[1]] == 7):
x = x + 5
if (maze[current.position[0]][current.position[1]] == 8):
x = x + 1
current = current.parent
return path[::-1]
#return x
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
node_position = (
current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) - 1) or node_position[1] < 0:
continue
if Node(current_node, node_position) in closed_list:
continue
if maze[node_position[0]][node_position[1]] == 9:
continue
new_node = Node(current_node, node_position)
children.append(new_node)
for child in children:
for closed_child in closed_list:
if child == closed_child:
continue
if(maze[child.position[0]][child.position[1]]==1):
child.g = 1
if(maze[child.position[0]][child.position[1]]==2):
child.g = 3
if(maze[child.position[0]][child.position[1]]==6):
child.g = 20
if(maze[child.position[0]][child.position[1]]==7):
child.g = 5
if(maze[child.position[0]][child.position[1]]==8):
child.g = 1
child.h = ((child.position[0] - end_node.position[0]) **
2) + ((child.position[1] - end_node.position[1]) ** 2)
child.f = child.g + child.h
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
open_list.append(child)
def main():
maze = [[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1,
1, 9, 9, 1, 1, 1, 9, 9, 1, 1, 9, 1, 1, 9, 9],
[9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 9, 1, 1,
8, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 2, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 9,
1, 0, 1, 7, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 9, 9,
9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9],
[9, 9, 9, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 2, 1,
1, 1, 1, 9, 6, 6, 9, 1, 1, 2, 1, 1, 9, 1, 9],
[9, 1, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 9, 9, 9,
9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[9, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1,
1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9, 1, 1, 1, 9,
9, 9, 7, 6, 9, 9, 9, 9, 6, 2, 1, 9, 9, 9, 9],
[9, 1, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 9, 9, 9, 9, 9, 1, 1, 9, 9, 9, 1, 1, 9,
9, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 1, 1, 9, 9, 1, 9, 1, 1, 7, 1, 1, 9],
[9, 1, 1, 1, 1, 1, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9],
[9, 1, 1, 9, 1, 1, 9, 9, 9, 1, 9, 1, 1, 1, 9, 9, 9,
1, 1, 1, 1, 1, 9, 1, 1, 1, 9, 9, 1, 1, 1, 9],
[9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 1, 1, 2, 1,
1, 1, 1, 9, 9, 9, 9, 1, 1, 1, 2, 1, 1, 9, 9],
[9, 1, 1, 1, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 9, 1, 1,
1, 9, 1, 1, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]]
start = (13, 7)
end = (3, 18)
path = astar(maze, start, end)
print(path)
return path
if __name__ == '__main__':
main()

14
granny.py Normal file
View File

@ -0,0 +1,14 @@
class Granny:
def __init__(self, position, grid_size):
self.pos = position
self.size = grid_size
def create_granny(grid_size):
grannies = []
with open(file='houses.txt', mode='r', encoding='utf-8') as file:
for l_index, line in enumerate(file):
for h_index, if_house in enumerate(line):
if if_house == '3':
granny = Granny(
[h_index*grid_size, l_index*grid_size], grid_size)
grannies.append(granny)
return grannies

View File

@ -1,16 +1,16 @@
11111111111111111111111111111111 11111111111111111111111111111111
10100011110000111100010000011101 10100011110000111100010003011101
10100000000000011000000111000001 10100020000000011002000111000001
10111111110001011010001110011001 10111111110001011010001110011001
10000000000001000010000100000001 10000300002001000010000100000001
11111111110001000010000000001111 11111111110001300010000002001111
11110000000111000100001111111111 11110320000111200100001111111111
10000000000000010001100000000001 10000000020000010301100300000001
10001110000011100010000000000001 10001110000011120010200200000001
11111011110111001010111111111011 11111011110111001010111111111011
10001000001000011010000001110001 10001030001000011010000001110001
10000010000001001000111100000011 10200010000001001000111100200011
10011111111001000010011001111011 10011111111001020010011001111011
10000110001000000001110000000011 10000112001000000001110000000011
10010000100010111100000001100001 10012000130010111100200001100001
11111111111111111111111111111111 11111111111111111111111111111111

BIN
img/granny.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

23
main.py
View File

@ -18,7 +18,8 @@ from truck import Truck
from trash import Trash from trash import Trash
from TSP import tsp, tspmove from TSP import tsp, tspmove
from bfs import bfs, distance from bfs import bfs, distance
from pool import create_pools
from granny import create_granny
model = load_model("model.h5") model = load_model("model.h5")
@ -70,10 +71,14 @@ def game_keys(truck, multi_trash, houses, auto=False):
break break
def update_images(gameDisplay, truck, multi_trash, houses): def update_images(gameDisplay, truck, multi_trash, houses,pools,grannies):
gameDisplay.fill(gray) gameDisplay.fill(gray)
for house in houses: for house in houses:
gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos) gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos)
for pool in pools:
gameDisplay.blit(pygame.image.load('./img/wet.png'), pool.pos)
for granny in grannies:
gameDisplay.blit(pygame.image.load('./img/granny.png'), granny.pos)
for trash in multi_trash: for trash in multi_trash:
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos) gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
gameDisplay.blit(truck.image, truck.pos) gameDisplay.blit(truck.image, truck.pos)
@ -93,6 +98,8 @@ def game_loop():
truck = Truck(game_w, game_h, grid_size) truck = Truck(game_w, game_h, grid_size)
houses = create_houses(grid_size) houses = create_houses(grid_size)
pools = create_pools(grid_size)
grannies = create_granny(grid_size)
for _ in range(7): for _ in range(7):
trash = Trash(game_w, game_h, grid_size) trash = Trash(game_w, game_h, grid_size)
@ -118,7 +125,9 @@ def game_loop():
if (event.key == pygame.K_k): if (event.key == pygame.K_k):
print(":>") print(":>")
trash = multi_trash[0] trash = multi_trash[0]
dis_dump = distance(truck.pos,[80,80]) dis = [120,120]
print(truck.pos)
dis_dump = distance(dis,truck.pos)
dis_trash = distance(truck.pos, trash.pos) dis_trash = distance(truck.pos, trash.pos)
print(dis_dump, dis_trash, truck.mass, truck.space, trash.mass, trash.space) print(dis_dump, dis_trash, truck.mass, truck.space, trash.mass, trash.space)
decision = tree.making_decision(decision_tree, decision = tree.making_decision(decision_tree,
@ -144,7 +153,7 @@ def game_loop():
pygame.KEYDOWN, {'key': action})) pygame.KEYDOWN, {'key': action}))
game_keys(truck, multi_trash, houses, True) game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, multi_trash, houses) update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
else: else:
truck.space=0 truck.space=0
truck.mass=0 truck.mass=0
@ -165,7 +174,7 @@ def game_loop():
pygame.KEYDOWN, {'key': action})) pygame.KEYDOWN, {'key': action}))
game_keys(truck, multi_trash, houses, True) game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, multi_trash, houses) update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
if (event.key == pygame.K_t): if (event.key == pygame.K_t):
solution = tsp(multi_trash, truck) solution = tsp(multi_trash, truck)
actions = tspmove(solution, truck, multi_trash) actions = tspmove(solution, truck, multi_trash)
@ -184,7 +193,7 @@ def game_loop():
pygame.KEYDOWN, {'key': action})) pygame.KEYDOWN, {'key': action}))
game_keys(truck, multi_trash, houses, True) game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, multi_trash, houses) update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
truck.direction = [1, 0] truck.direction = [1, 0]
truck.dir_control = 0 truck.dir_control = 0
truck.image = pygame.image.load('./img/truck.png') truck.image = pygame.image.load('./img/truck.png')
@ -196,7 +205,7 @@ def game_loop():
else: else:
pygame.event.post(event) pygame.event.post(event)
game_keys(truck, multi_trash, houses) game_keys(truck, multi_trash, houses)
update_images(gameDisplay, truck, multi_trash, houses) update_images(gameDisplay, truck, multi_trash, houses,pools,grannies)
clock.tick(60) clock.tick(60)

14
pool.py Normal file
View File

@ -0,0 +1,14 @@
class Pool:
def __init__(self, position, grid_size):
self.pos = position
self.size = grid_size
def create_pools(grid_size):
pools = []
with open(file='houses.txt', mode='r', encoding='utf-8') as file:
for l_index, line in enumerate(file):
for h_index, if_house in enumerate(line):
if if_house == '2':
pool = Pool(
[h_index*grid_size, l_index*grid_size], grid_size)
pools.append(pool)
return pools

View File

@ -37,8 +37,8 @@ class Trash:
self.size = grid_size self.size = grid_size
self.content = draw_trash(filenames)[0] self.content = draw_trash(filenames)[0]
self.names = draw_trash(filenames)[1] self.names = draw_trash(filenames)[1]
self.mass = random.randint(0, 25) self.mass = random.randint(1, 25)
self.space = random.randint(0, 25) self.space = random.randint(1, 25)
def new_pos(self, truck_pos, houses, multi): def new_pos(self, truck_pos, houses, multi):
self.trash_content, self.trash_names = draw_trash(filenames) self.trash_content, self.trash_names = draw_trash(filenames)