From 827a105f179eda9b3cbffe2b71ecd24c2dcd36d2 Mon Sep 17 00:00:00 2001 From: MonoYuku Date: Wed, 23 Jun 2021 11:40:06 +0200 Subject: [PATCH] astar --- __pycache__/granny.cpython-39.pyc | Bin 0 -> 855 bytes __pycache__/pool.cpython-39.pyc | Bin 0 -> 843 bytes __pycache__/trash.cpython-39.pyc | Bin 2270 -> 2270 bytes astar.py | 150 ++++++++++++++++++++++++++++++ granny.py | 14 +++ houses.txt | 24 ++--- img/granny.png | Bin 0 -> 2497 bytes main.py | 23 +++-- pool.py | 14 +++ trash.py | 4 +- 10 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 __pycache__/granny.cpython-39.pyc create mode 100644 __pycache__/pool.cpython-39.pyc create mode 100644 astar.py create mode 100644 granny.py create mode 100644 img/granny.png create mode 100644 pool.py diff --git a/__pycache__/granny.cpython-39.pyc b/__pycache__/granny.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..40b09871b71f06fcc8d23c8f7f502dd9456ca699 GIT binary patch literal 855 zcmZ8f!H&}~5cN1tnyu*4!=8Wx*Gi=<;)2i$A$G-z1HG&eq6#um>~_6PlhjV7-72}z zy&`@Aj{FW6BwzUpgaqS+qQY1+@tg5X#`7HadR>C@?dR8b_bDOY9oQ8Q!BbRo3(YAJ zM8GxbhE9l}!n-CD5bTT$`~>6w2v%z|4{#&hIWnOF1jQUrc$mX%aH``hyGq<&w&M0K z_ZZdm(Ns_oPAZBPYhWIX2h@5+ZY(ouEe9TcWjeJXx>Tw1%U;(u%i;3QYb2lH+IT>+{sUslL+sG4H>edL zF~NAh79Qf9SYAk^OlGTv)Nv`B zyV@Ww&@Bd`4)Ag|Y$#CD=zRw}DbgV1rDdtgr0tylfuWl6rUM(aoc%Xf+e@^>fABWy uV=d#}CjkciW`#hZ|BKNf=-cSWr`Gh-fSqFQQE4}S8y>aXKKV=6KKl*pQOG9% literal 0 HcmV?d00001 diff --git a/__pycache__/pool.cpython-39.pyc b/__pycache__/pool.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d641363e28b25a5f1ebf60081d63bf64ad1c4118 GIT binary patch literal 843 zcmZ8fPm2>V6i+glcD8Jn9`@uP@LH&4^&}#TxCQZG5f%~4NE{|5qysO0CQAEprhhoIUv^B5F`=g5RfDjDPiGJqWJ`HP~+QoLhFsAJ|C zu)JNdbP8Yd9m>Dev2EP!~h8jOcXAk3- zRv8;VPg1Qe?~JvQYppUn*YQZ%xhu*z(ee2+QJUgX&7H6dtx}zsc@me<AI!$+i)50NG7*dm=&~#j?N_ zZ;5DQr|8%bo{f1Rvv?Ia;v|9g#OrVpaz=2b(wc3D{xXs#-Ff zGc$0k3M+Cyy#}r*fYKVT zI~T>uDm!#n&Qr4vS8jUu0irY2nerUQpnRnAw2(TVd3qn=FG>Y)seH9iCUL5Ht8XPG zxMe>wEqI&{as1XCq2A5_0jaJ$&vdSQ`{Iuo>8WV0_Z(0EH&TC?8U^J=Qz?W(7XKun poc5X(QAT^eI2%xN6a4VOnob&HuNHnKgOm5sqb;hZe}OvUzX7w1!u|jN literal 0 HcmV?d00001 diff --git a/__pycache__/trash.cpython-39.pyc b/__pycache__/trash.cpython-39.pyc index fb38fe91b372b8bd1969ea63ce9a3b85a249301a..dcaeda95dfc6b51dcb11312c9d90c945d454edce 100644 GIT binary patch delta 41 wcmca7cu$Z!k(ZZ?0SG=ZT;9lifrXzbg?S-U4MRL<3Ht($6sFCuSiUd=0P9@~ 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, 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, 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() diff --git a/granny.py b/granny.py new file mode 100644 index 0000000..cdb77f4 --- /dev/null +++ b/granny.py @@ -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 \ No newline at end of file diff --git a/houses.txt b/houses.txt index 1f4bcbf..0b5a875 100644 --- a/houses.txt +++ b/houses.txt @@ -1,16 +1,16 @@ 11111111111111111111111111111111 -10100011110000111100010000011101 -10100000000000011000000111000001 +10100011110000111100010003011101 +10100020000000011002000111000001 10111111110001011010001110011001 -10000000000001000010000100000001 -11111111110001000010000000001111 -11110000000111000100001111111111 -10000000000000010001100000000001 -10001110000011100010000000000001 +10000300002001000010000100000001 +11111111110001300010000002001111 +11110320000111200100001111111111 +10000000020000010301100300000001 +10001110000011120010200200000001 11111011110111001010111111111011 -10001000001000011010000001110001 -10000010000001001000111100000011 -10011111111001000010011001111011 -10000110001000000001110000000011 -10010000100010111100000001100001 +10001030001000011010000001110001 +10200010000001001000111100200011 +10011111111001020010011001111011 +10000112001000000001110000000011 +10012000130010111100200001100001 11111111111111111111111111111111 \ No newline at end of file diff --git a/img/granny.png b/img/granny.png new file mode 100644 index 0000000000000000000000000000000000000000..64dd1ee94be9bc84ed1610a33eb7ce48d84d96b1 GIT binary patch literal 2497 zcmV;y2|o6TP)EX>4Tx04R}tkv&MmKpe$i(@I4uf>sc9$WWauh>D1lRIvyaN?V~-2a`*`ph-iL z;^HW{799LotU9+0Yt2!bCVZf;JBE>hzEl0u7E503ls?%w0>9U#<7Of`MufT~$W zA{r6XnN`vM3Ll2hi+;>X%+zDa#T43(uX}j-dKczd?a%!=x|OWS0FOvK$8^IY-XNad zv~ zc3I)P#aS&?SmU1jg@LTLlH@wgVMMWn7~&8iqkA{&2mgcL-I}?{2{$Pa0Xkl6`(p&~?*jFzZGRuzcKrnKJ_A=;(_gLuGoPea zn_B1y=-CD?uA7>?2VCv|15dhSNRH&CDdcj%`x$*x2I#v5y4Kv@8v8hX08-S|@(pls z2#n?_d%exOJ6n7E_e`U|AIxiV%l5iNPXGV_24YJ`L;ywrMgT^|SOY--000SaNLh0L z04^f{04^f|c%?sf00007bV*G`2jv7C5*Q3@%JriF000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}000M=Nkl z5StVNVJS`sq)DSzv=32RP!Y;QD>YFnD1B)vRY_B+%Tv{qG>THSDO5#Kq%JZoL2Us$ zKv-vzgV~-ccGoIycrw;?JZDtGxJD+r=t8>ry-T(c6>$yTH#luJ}7N_}Kt1@&Q z1qi@#92PCC5#ewF4=eh^8i>c^s_FO%{60UP=OU%VvMeeqDpfdKARgAfm<)rbK0T#8 zJU;%<5e!|&^Awt{A%)A7xwX=&-#rX1k8i*1w?6|r&MQ)s$DMMcHyz_(gjTB@#Izd_?C$En}2 zft63J5W~X>73u5aN=GM3DNNIkZW#3R-29DeJCv4|PMwS2|HB`ZrfJ0Daipf&&k2y>`o+;NGX{;YZk8SqLe}bmSv&qI$hn}q>?G5l(?=-GMU6M4D$0s^bZV>N~I_a zhjBw8lv22!M_+&cKF{;&XO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KRHh9A`9qy0UYYP&dv7Li2*uxZKU%&Lwz^y4`4|F6ou}tCB^xfqMLhY zRCirZu3Dusj}*DCmX>A(sFf?fFyX$sy1F}mJjU1@y;iemaZVdqBdz}FdOYP#+>677 ztXaL9FK*m8;l53qHc?()j^A$*>5Gi4Z`tx3exHHuy3{PJ$*KQ8f`e@>@O8zF00000 LNkvXXu0mjfqqUkz literal 0 HcmV?d00001 diff --git a/main.py b/main.py index d7ffd43..b80aef1 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,8 @@ from truck import Truck from trash import Trash from TSP import tsp, tspmove from bfs import bfs, distance - +from pool import create_pools +from granny import create_granny model = load_model("model.h5") @@ -70,10 +71,14 @@ def game_keys(truck, multi_trash, houses, auto=False): break -def update_images(gameDisplay, truck, multi_trash, houses): +def update_images(gameDisplay, truck, multi_trash, houses,pools,grannies): gameDisplay.fill(gray) for house in houses: 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: gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos) gameDisplay.blit(truck.image, truck.pos) @@ -93,6 +98,8 @@ def game_loop(): truck = Truck(game_w, game_h, grid_size) houses = create_houses(grid_size) + pools = create_pools(grid_size) + grannies = create_granny(grid_size) for _ in range(7): trash = Trash(game_w, game_h, grid_size) @@ -118,7 +125,9 @@ def game_loop(): if (event.key == pygame.K_k): print(":>") 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) print(dis_dump, dis_trash, truck.mass, truck.space, trash.mass, trash.space) decision = tree.making_decision(decision_tree, @@ -144,7 +153,7 @@ def game_loop(): pygame.KEYDOWN, {'key': action})) game_keys(truck, multi_trash, houses, True) - update_images(gameDisplay, truck, multi_trash, houses) + update_images(gameDisplay, truck, multi_trash, houses,pools,grannies) else: truck.space=0 truck.mass=0 @@ -165,7 +174,7 @@ def game_loop(): pygame.KEYDOWN, {'key': action})) 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): solution = tsp(multi_trash, truck) actions = tspmove(solution, truck, multi_trash) @@ -184,7 +193,7 @@ def game_loop(): pygame.KEYDOWN, {'key': action})) 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.dir_control = 0 truck.image = pygame.image.load('./img/truck.png') @@ -196,7 +205,7 @@ def game_loop(): else: pygame.event.post(event) 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) diff --git a/pool.py b/pool.py new file mode 100644 index 0000000..0cd1b64 --- /dev/null +++ b/pool.py @@ -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 \ No newline at end of file diff --git a/trash.py b/trash.py index 7370c45..dd243ce 100644 --- a/trash.py +++ b/trash.py @@ -37,8 +37,8 @@ class Trash: self.size = grid_size self.content = draw_trash(filenames)[0] self.names = draw_trash(filenames)[1] - self.mass = random.randint(0, 25) - self.space = random.randint(0, 25) + self.mass = random.randint(1, 25) + self.space = random.randint(1, 25) def new_pos(self, truck_pos, houses, multi): self.trash_content, self.trash_names = draw_trash(filenames)