From 1e881497893df16b3ccf2538423e4e8910cb8eea Mon Sep 17 00:00:00 2001 From: Mateusz Date: Wed, 19 Apr 2023 21:16:40 +0200 Subject: [PATCH 01/12] created successor function, adjustet truck class --- garbage_truck.py | 6 ++++-- succ.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 succ.py diff --git a/garbage_truck.py b/garbage_truck.py index 45f611a..82a7112 100644 --- a/garbage_truck.py +++ b/garbage_truck.py @@ -8,10 +8,12 @@ class Engine: self.power = power #HP class GarbageTruck: - def __init__(self, dump_location, fuel_capacity, start_pos): + def __init__(self, dump_location, fuel_capacity, xpos, ypos, orientation): self.dump_location = dump_location self.tank = GarbageTank(15, 18000) self.engine = Engine(400) self.fuel = fuel_capacity - self.pos = start_pos + self.xpos = xpos + self.ypos = ypos + self.orientation = orientation self.houses = [] #lista domów do odwiedzenia \ No newline at end of file diff --git a/succ.py b/succ.py new file mode 100644 index 0000000..600471e --- /dev/null +++ b/succ.py @@ -0,0 +1,28 @@ +def succ(xpos, ypos, orientation): + successors = [] + + if orientation == 'N': + successors.append(['LEFT', xpos, ypos, 'W']) + successors.append(['RIGHT', xpos, ypos, 'E']) + if ypos > 0: + successors.append(['FORWARD', xpos, ypos - 50, 'N']) + + if orientation == 'S': + successors.append(['LEFT', xpos, ypos, 'E']) + successors.append(['RIGHT', xpos, ypos, 'W']) + if ypos < 750: + successors.append(['FORWARD', xpos, ypos + 50, 'S']) + + if orientation == 'W': + successors.append(['LEFT', xpos, ypos, 'S']) + successors.append(['RIGHT', xpos, ypos, 'N']) + if xpos > 0: + successors.append(['FORWARD', xpos - 50, ypos, 'W']) + + if orientation == 'E': + successors.append(['LEFT',xpos, ypos, 'N']) + successors.append(['RIGHT', xpos, ypos, 'S']) + if xpos < 750: + successors.append(['FORWARD', xpos + 50, ypos, 'E']) + + return successors -- 2.20.1 From c91e01629443559f3a8c502d6ccf2886dd5c4615 Mon Sep 17 00:00:00 2001 From: Mateusz Date: Wed, 19 Apr 2023 21:40:40 +0200 Subject: [PATCH 02/12] added state class --- state.py | 6 ++++++ succ.py | 44 +++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 state.py diff --git a/state.py b/state.py new file mode 100644 index 0000000..40a3133 --- /dev/null +++ b/state.py @@ -0,0 +1,6 @@ +class State: + def __init__(self, parent, xpos, ypos, orientation): + self.parent = parent + self.xpos = xpos + self.ypos = ypos + self.orientation = orientation \ No newline at end of file diff --git a/succ.py b/succ.py index 600471e..69a8df0 100644 --- a/succ.py +++ b/succ.py @@ -1,28 +1,30 @@ -def succ(xpos, ypos, orientation): +from state import State + +def succ(st: State): successors = [] - if orientation == 'N': - successors.append(['LEFT', xpos, ypos, 'W']) - successors.append(['RIGHT', xpos, ypos, 'E']) - if ypos > 0: - successors.append(['FORWARD', xpos, ypos - 50, 'N']) + if st.orientation == 'N': + successors.append(['LEFT', State(st, st.xpos, st.ypos, 'W')]) + successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'E')]) + if st.ypos > 0: + successors.append(['FORWARD', State(st, st.xpos, st.ypos - 50, 'N')]) - if orientation == 'S': - successors.append(['LEFT', xpos, ypos, 'E']) - successors.append(['RIGHT', xpos, ypos, 'W']) - if ypos < 750: - successors.append(['FORWARD', xpos, ypos + 50, 'S']) + if st.orientation == 'S': + successors.append(['LEFT', State(st, st.xpos, st.ypos, 'E')]) + successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'W')]) + if st.ypos < 750: + successors.append(['FORWARD', State(st, st.xpos, st.ypos + 50, 'S')]) - if orientation == 'W': - successors.append(['LEFT', xpos, ypos, 'S']) - successors.append(['RIGHT', xpos, ypos, 'N']) - if xpos > 0: - successors.append(['FORWARD', xpos - 50, ypos, 'W']) + if st.orientation == 'W': + successors.append(['LEFT', State(st, st.xpos, st.ypos, 'S')]) + successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'N')]) + if st.xpos > 0: + successors.append(['FORWARD', State(st, st.xpos - 50, st.ypos, 'W')]) - if orientation == 'E': - successors.append(['LEFT',xpos, ypos, 'N']) - successors.append(['RIGHT', xpos, ypos, 'S']) - if xpos < 750: - successors.append(['FORWARD', xpos + 50, ypos, 'E']) + if st.orientation == 'E': + successors.append(['LEFT', State(st, st.xpos, st.ypos, 'N')]) + successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'S')]) + if st.xpos < 750: + successors.append(['FORWARD', State(st, st.xpos + 50, st.ypos, 'E')]) return successors -- 2.20.1 From bdb0ed2d3cb5268b2b0693ed974a9c5746b1d349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Miko=C5=82ajczak?= Date: Wed, 19 Apr 2023 22:12:02 +0200 Subject: [PATCH 03/12] basic bfs implementation --- bfs.py | 15 +++++++++++++++ state.py | 5 +++-- succ.py | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 bfs.py diff --git a/bfs.py b/bfs.py new file mode 100644 index 0000000..b0d3da5 --- /dev/null +++ b/bfs.py @@ -0,0 +1,15 @@ +from succ import succ as successors + +def bfs(istate, goalstate): + fringe = [istate] + explored = [] + while(fringe): + state = fringe.pop(0) + if state == goalstate : + return + element = successors(state) + explored.append(state) + for value in element : + if value not in explored : + fringe.append(value) + return False diff --git a/state.py b/state.py index 40a3133..a6b5e2b 100644 --- a/state.py +++ b/state.py @@ -1,6 +1,7 @@ class State: - def __init__(self, parent, xpos, ypos, orientation): + def __init__(self, parent, action, xpos, ypos, orientation): self.parent = parent self.xpos = xpos self.ypos = ypos - self.orientation = orientation \ No newline at end of file + self.orientation = orientation + self.action = action \ No newline at end of file diff --git a/succ.py b/succ.py index 69a8df0..70b5f0f 100644 --- a/succ.py +++ b/succ.py @@ -4,27 +4,27 @@ def succ(st: State): successors = [] if st.orientation == 'N': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'W')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'E')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W')) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E')) if st.ypos > 0: - successors.append(['FORWARD', State(st, st.xpos, st.ypos - 50, 'N')]) + successors.append(State(st, 'FORWARD', st.xpos, st.ypos - 50, 'N')) if st.orientation == 'S': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'E')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'W')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E')) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W')) if st.ypos < 750: - successors.append(['FORWARD', State(st, st.xpos, st.ypos + 50, 'S')]) + successors.append(State(st, 'FORWARD', st.xpos, st.ypos + 50, 'S')) if st.orientation == 'W': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'S')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'N')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S')) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N')) if st.xpos > 0: - successors.append(['FORWARD', State(st, st.xpos - 50, st.ypos, 'W')]) + successors.append(State(st, 'FORWARD', st.xpos - 50, st.ypos, 'W')) if st.orientation == 'E': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'N')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'S')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N')) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S')) if st.xpos < 750: - successors.append(['FORWARD', State(st, st.xpos + 50, st.ypos, 'E')]) + successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E')) return successors -- 2.20.1 From 9406038dcbcd23a0e418c8a096002fb5753dae4b Mon Sep 17 00:00:00 2001 From: Mateusz Date: Wed, 19 Apr 2023 23:51:24 +0200 Subject: [PATCH 04/12] implemented movement planning --- bfs.py | 14 ++++++++++---- main.py | 55 +++++++++++++++++++++++++++++++++++++++++-------------- succ.py | 1 - 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/bfs.py b/bfs.py index b0d3da5..0e29f49 100644 --- a/bfs.py +++ b/bfs.py @@ -1,15 +1,21 @@ from succ import succ as successors -def bfs(istate, goalstate): +def bfs(istate, goalx, goaly): fringe = [istate] explored = [] + steps = [] while(fringe): state = fringe.pop(0) - if state == goalstate : - return + if state.xpos == goalx and state.ypos == goaly: + steps.insert(0, state) + while(state.parent != None): + state = state.parent + steps.insert(0, state) + return steps + element = successors(state) explored.append(state) for value in element : - if value not in explored : + if value not in explored and value not in fringe: fringe.append(value) return False diff --git a/main.py b/main.py index 2beb086..73ebbd7 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ import pygame import random +from bfs import bfs +from state import State +import time pygame.init() WIDTH, HEIGHT = 800, 800 @@ -17,6 +20,10 @@ COBBLE_IMG = pygame.image.load("cobble.jpeg") COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50)) FPS = 10 +class Agent: + def __init__(self, rect, direction): + self.rect = rect + self.direction = direction def randomize_map(): # tworzenie mapy z losowymi polami fields_list = [DIRT, GRASS, SAND, COBBLE] @@ -34,37 +41,57 @@ def draw_window(agent, fields): for i in range(16): for j in range(16): window.blit(fields[i][j], (i * 50, j * 50)) - window.blit(AGENT, (agent.x, agent.y)) # wyswietlanie agenta + window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta pygame.display.update() -def agent_movement(keys_pressed, agent): # sterowanie - if keys_pressed[pygame.K_LEFT] and agent.x > 0: - agent.x -= 50 - if keys_pressed[pygame.K_RIGHT] and agent.x < 750: - agent.x += 50 - if keys_pressed[pygame.K_UP] and agent.y > 0: - agent.y -= 50 - if keys_pressed[pygame.K_DOWN] and agent.y < 750: - agent.y += 50 +#def agent_movement(keys_pressed, agent): # sterowanie +# if keys_pressed[pygame.K_LEFT] and agent.x > 0: +# agent.x -= 50 +# if keys_pressed[pygame.K_RIGHT] and agent.x < 750: +# agent.x += 50 +# if keys_pressed[pygame.K_UP] and agent.y > 0: +# agent.y -= 50 +# if keys_pressed[pygame.K_DOWN] and agent.y < 750: +# agent.y += 50 def main(): clock = pygame.time.Clock() run = True - agent = pygame.Rect(0, 0, 50, 50) # tworzenie pola dla agenta + agent = Agent(pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta fields = randomize_map() while run: clock.tick(FPS) for event in pygame.event.get(): # przechwycanie zamknięcia okna if event.type == pygame.QUIT: run = False - keys_pressed = pygame.key.get_pressed() - draw_window(agent, fields) - agent_movement(keys_pressed, agent) + #keys_pressed = pygame.key.get_pressed() + + steps = bfs(State(None, None, 0, 0, 'E'), 450, 100) + for interm in steps: + if interm.action == 'LEFT': + agent.direction = (agent.direction - 1) % 4 + if interm.action == 'RIGHT': + agent.direction = (agent.direction + 1) % 4 + if interm.action == 'FORWARD': + if agent.direction == 0: + agent.rect.x += 50 + elif agent.direction == 1: + agent.rect.y += 50 + elif agent.direction == 2: + agent.rect.x -= 50 + else: + agent.rect.y -= 50 + draw_window(agent, fields) + time.sleep(1) + + while True: + pass pygame.quit() + if __name__ == "__main__": main() diff --git a/succ.py b/succ.py index 70b5f0f..b7309dd 100644 --- a/succ.py +++ b/succ.py @@ -26,5 +26,4 @@ def succ(st: State): successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S')) if st.xpos < 750: successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E')) - return successors -- 2.20.1 From d503ee4f0cedb2801cf8ee6c20675415c39f0eba Mon Sep 17 00:00:00 2001 From: Mateusz Date: Wed, 19 Apr 2023 23:53:36 +0200 Subject: [PATCH 05/12] implemented path finding --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 73ebbd7..90191dc 100644 --- a/main.py +++ b/main.py @@ -67,8 +67,8 @@ def main(): if event.type == pygame.QUIT: run = False #keys_pressed = pygame.key.get_pressed() - - steps = bfs(State(None, None, 0, 0, 'E'), 450, 100) + draw_window(agent, fields) + steps = bfs(State(None, None, 0, 0, 'E'), 250, 100) for interm in steps: if interm.action == 'LEFT': agent.direction = (agent.direction - 1) % 4 -- 2.20.1 From fc588a4ad52b266d4fa64420004e49b0eaee970a Mon Sep 17 00:00:00 2001 From: Mateusz Szlachetka Date: Thu, 20 Apr 2023 10:01:48 +0200 Subject: [PATCH 06/12] fixed search --- bfs.py | 5 +++-- main.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bfs.py b/bfs.py index 0e29f49..3f219da 100644 --- a/bfs.py +++ b/bfs.py @@ -14,8 +14,9 @@ def bfs(istate, goalx, goaly): return steps element = successors(state) - explored.append(state) + explored.append((state.xpos, state.ypos, state.orientation)) for value in element : - if value not in explored and value not in fringe: + val = (value.xpos, value.ypos, value.orientation) + if val not in explored: fringe.append(value) return False diff --git a/main.py b/main.py index 90191dc..b9b865a 100644 --- a/main.py +++ b/main.py @@ -68,7 +68,7 @@ def main(): run = False #keys_pressed = pygame.key.get_pressed() draw_window(agent, fields) - steps = bfs(State(None, None, 0, 0, 'E'), 250, 100) + steps = bfs(State(None, None, 0, 0, 'E'), 250, 700) for interm in steps: if interm.action == 'LEFT': agent.direction = (agent.direction - 1) % 4 -- 2.20.1 From 15abf755b9a03399abf90ca48959cbac70ca965d Mon Sep 17 00:00:00 2001 From: Mateusz Szlachetka Date: Fri, 21 Apr 2023 14:31:17 +0200 Subject: [PATCH 07/12] added constants, created agent methods --- bfs.py | 2 +- garbage_truck.py | 25 +++++++++++++++++++++---- main.py | 22 +++++++++------------- succ.py | 13 +++++++------ 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/bfs.py b/bfs.py index 3f219da..58d7f4e 100644 --- a/bfs.py +++ b/bfs.py @@ -17,6 +17,6 @@ def bfs(istate, goalx, goaly): explored.append((state.xpos, state.ypos, state.orientation)) for value in element : val = (value.xpos, value.ypos, value.orientation) - if val not in explored: + if val not in explored and value not in fringe: fringe.append(value) return False diff --git a/garbage_truck.py b/garbage_truck.py index 82a7112..9998400 100644 --- a/garbage_truck.py +++ b/garbage_truck.py @@ -1,3 +1,5 @@ +FIELDWIDTH = 50 + class GarbageTank: def __init__(self, volume_capacity, mass_capacity): self.vcapacity = volume_capacity #m^3 @@ -8,12 +10,27 @@ class Engine: self.power = power #HP class GarbageTruck: - def __init__(self, dump_location, fuel_capacity, xpos, ypos, orientation): + def __init__(self, dump_location, fuel_capacity, rect, orientation): self.dump_location = dump_location self.tank = GarbageTank(15, 18000) self.engine = Engine(400) self.fuel = fuel_capacity - self.xpos = xpos - self.ypos = ypos + self.rect = rect self.orientation = orientation - self.houses = [] #lista domów do odwiedzenia \ No newline at end of file + self.houses = [] #lista domów do odwiedzenia + + def turn_left(self): + self.orientation = (self.orientation - 1) % 4 + + def turn_right(self): + self.orientation = (self.orientation + 1) % 4 + + def forward(self): + if self.orientation == 0: + self.rect.x += FIELDWIDTH + elif self.orientation == 1: + self.rect.y += FIELDWIDTH + elif self.orientation == 2: + self.rect.x -= FIELDWIDTH + else: + self.rect.y -= FIELDWIDTH \ No newline at end of file diff --git a/main.py b/main.py index b9b865a..b33ffb2 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import random from bfs import bfs from state import State import time +from garbage_truck import GarbageTruck pygame.init() WIDTH, HEIGHT = 800, 800 @@ -19,6 +20,8 @@ SAND = pygame.transform.scale(SAND_IMG, (50, 50)) COBBLE_IMG = pygame.image.load("cobble.jpeg") COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50)) FPS = 10 +FIELDCOUNT = 16 +FIELDWIDTH = 50 class Agent: def __init__(self, rect, direction): @@ -59,7 +62,7 @@ def draw_window(agent, fields): def main(): clock = pygame.time.Clock() run = True - agent = Agent(pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta + agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta fields = randomize_map() while run: clock.tick(FPS) @@ -68,23 +71,16 @@ def main(): run = False #keys_pressed = pygame.key.get_pressed() draw_window(agent, fields) - steps = bfs(State(None, None, 0, 0, 'E'), 250, 700) + steps = bfs(State(None, None, 0, 0, 'E'), 450, 600) for interm in steps: if interm.action == 'LEFT': - agent.direction = (agent.direction - 1) % 4 + agent.turn_left() if interm.action == 'RIGHT': - agent.direction = (agent.direction + 1) % 4 + agent.turn_right() if interm.action == 'FORWARD': - if agent.direction == 0: - agent.rect.x += 50 - elif agent.direction == 1: - agent.rect.y += 50 - elif agent.direction == 2: - agent.rect.x -= 50 - else: - agent.rect.y -= 50 + agent.forward() draw_window(agent, fields) - time.sleep(1) + time.sleep(0.5) while True: pass diff --git a/succ.py b/succ.py index b7309dd..8ebe570 100644 --- a/succ.py +++ b/succ.py @@ -1,4 +1,5 @@ from state import State +FIELDWIDTH, FIELDCOUNT = 50, 16 def succ(st: State): successors = [] @@ -7,23 +8,23 @@ def succ(st: State): successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W')) successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E')) if st.ypos > 0: - successors.append(State(st, 'FORWARD', st.xpos, st.ypos - 50, 'N')) + successors.append(State(st, 'FORWARD', st.xpos, st.ypos - FIELDWIDTH , 'N')) if st.orientation == 'S': successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E')) successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W')) - if st.ypos < 750: - successors.append(State(st, 'FORWARD', st.xpos, st.ypos + 50, 'S')) + if st.ypos < FIELDWIDTH * (FIELDCOUNT - 1): + successors.append(State(st, 'FORWARD', st.xpos, st.ypos + FIELDWIDTH , 'S')) if st.orientation == 'W': successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S')) successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N')) if st.xpos > 0: - successors.append(State(st, 'FORWARD', st.xpos - 50, st.ypos, 'W')) + successors.append(State(st, 'FORWARD', st.xpos - FIELDWIDTH , st.ypos, 'W')) if st.orientation == 'E': successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N')) successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S')) - if st.xpos < 750: - successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E')) + if st.xpos < FIELDWIDTH * (FIELDCOUNT - 1): + successors.append(State(st, 'FORWARD', st.xpos + FIELDWIDTH , st.ypos, 'E')) return successors -- 2.20.1 From 674393a4d9ead32365b6a2f4e1df7f751c532a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Miko=C5=82ajczak?= Date: Wed, 3 May 2023 12:07:13 +0200 Subject: [PATCH 08/12] added priority for blocks --- __pycache__/bfs.cpython-310.pyc | Bin 0 -> 667 bytes __pycache__/garbage_truck.cpython-310.pyc | Bin 0 -> 1744 bytes __pycache__/main.cpython-310.pyc | Bin 0 -> 2616 bytes __pycache__/state.cpython-310.pyc | Bin 0 -> 519 bytes __pycache__/succ.cpython-310.pyc | Bin 0 -> 930 bytes bfs.py | 4 ++-- main.py | 13 +++++++------ state.py | 3 ++- succ.py | 2 +- 9 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 __pycache__/bfs.cpython-310.pyc create mode 100644 __pycache__/garbage_truck.cpython-310.pyc create mode 100644 __pycache__/main.cpython-310.pyc create mode 100644 __pycache__/state.cpython-310.pyc create mode 100644 __pycache__/succ.cpython-310.pyc diff --git a/__pycache__/bfs.cpython-310.pyc b/__pycache__/bfs.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f9f8ace0b0cf92919487d8962cd4d0c931f02ae7 GIT binary patch literal 667 zcmZuvy^a$x5cb&K-{b@Zx}&EG1p!fl&`H2aG*@ul33M{Cv-q;RUhK^&k-QQTJOGc7 zl)MIy;g*V)k^(VaC{i%id^4WUGag%Gj*d!#^5x-cnK44X$IYQo+?=Aiy9kmp$$#s$8OnCjLk+lRChR<(U7f-QB$&~&9 DW@)J% literal 0 HcmV?d00001 diff --git a/__pycache__/garbage_truck.cpython-310.pyc b/__pycache__/garbage_truck.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d4dcb18111ae34ac264d3c6cc7cf81f6f0a09965 GIT binary patch literal 1744 zcma)6OK;Oa5Z<*nj*})WkI?cGh}Xg5QHcvZAXEuTse}R&NJzO@#&%OTew4M-(ki#| z1Na-g@q6~lDd*le@y)t%leS1~w3C^go!QxMKTph>w`dMg}AjePTzv>lmuB>w7z%9~Jwm zkxW#_R3Z=KO|`I_#ls|YCl1w4ygaYWBPI!$KWocdSrRTM-XPykqB!!>Dw8WpY!j9E^?;!S?gnY!Y$$(HVsp#7Ri=y-6RW!G3hPW{WkRC(j!~QqKljobF@bI&^)GdY}Vp>#fLF7CT}1~0p8YH+KUVPKuV|yWJ%c_u4+5kUML+y)?v%5BxqwPbUjpP12d;gxb;GL(CLL`y9ziH zdIb)foR>+E_0MbMPvAXIC1Ph!l)UWKf_h-Xwt@yI)>N$t{`5|M4GAWLbizTPIWy3toomvU{BJ!&?ZLat$Z(>c} zB5?*%dJV!mpNFt`3p}*+4=;0V8jtFId=y;`(kn*L*E?d{XbIQR5EkjuXcG7U&F+{s zj)@~jT-Sj=&5+r0RHGQm)Q!WF4#5*@&Zt~1|g!o2E2DpyyC|M=~&8#2)(8wh*mh1$-59^LJefl&rJ`n_;=NOyRr8E&6kZgpWi9If8p)xo0;76 zWN;fHnhs6g0UgTOF;3f)Zeu5#yG~sabtJJ`Qu{4C(F4*Rk$6nv6lksKMnH5S`sK7_ tGn?BD+iaPV*1L|74*vDWpg#k>d{G`{zbsqgE8+ik~5Cdo`PS(Xe8AcH0Wg!TatFq7K^4H-y+kPN8RaaBC+wA)T~ zWrsv<3CaA6eQ_T7OZBwUKILtn_l5mV*_jJetE$eW>U7n2F4dozi5Ob&;eqh$jQxk0 zSDOdsEA--1&X{0=XRI}ud5eR0GNd>rCHLF(hH!;(CLo`}{7qhK5cbS+I^S?1M zKXO`Y;+9zWjkWHHMX`kWEpc1i!Th#Z7Au(V%Xh@xQ%C(v-XGCz`JwI2)oVg^zsZez zd{SWY4|Sp?i281#j+2f&Qs?c{rgOdKGNybizIOj_oe z0^xe@W^WtBYs~@o6?(CVK{E1w%Mn~SxV0RFG=(D!G#IqIL%WX$E&baP;>`8;+GA3@bw$zKJJ2* zdD4?{Y@#^s^~HHcbUlvG&Xa7SQ5G#al|9;Im?gG~;V!!UL&)mfN?yaX0yR0w0wX|Z zrnz>s+wq31!-gE)(VpNVzQumNH*{an3qw!)vwqr^#!qu0e=$Ka7|2{SE2egm%1jh-mKNH~SSC)CN-pA$US~fxJT<&)7A6)q zt-YrwkFtLI)XWZ4+E=N*mzz@V%iIfiE9B2u^2(mF@~E8)iqii{NNPl|q#}i_Sb+r`?iu zy)Mwl7)*4!(hbK|TjLPcO>cs43ApiS@`IQE|#?>I{}Qf7C1|Hy{9;lCJu zVS>R$2Z3iQA}t140>Sw}P_00Ip-$x51QXoV8kxRF!^;??@G~``lIQW{KMx=%D1iv7 zce(S&veV#d1A;ak?WPe;-NHnemAHhWnqZALxmv%WU>K2B<1KHVVFkx**`;gby+O zCDuhDx*1d%cLcZThMvV^1gGNjN4^MzJ93dcK5R6RIzboD z4J>?zB$_7|<_lI9LiQtMx2&vcWmU)oWD6vF!DDeCsNVco2%FYJ3&(e!m;Ac|x z{v4Ga72!u%SA2pYLbl$eoOL4JKxlg0`85Q+?1VO>DXSw+be%hngTSr3>TCK7Dl=J% z6gy%!w-nPRV}{y}SJrjKN7AIT|p^a2?FhrO+%$0m5ZP1vgstmQ&#s`XO6 zLu`%Mb~2y`3DvKM>@!~0PlT!3tc~}+*#&y2PX(~*;qUDvlj=Qdipnc3Q1o7!s;#q@ zU$SZ{xR#|_W}->uwSqeks86kQXK(w-*5=di_m50uX?te-mRR3UJWAPSkx}zRy{rY6 yvIQaheDZ>QK_OUtfgxlefUN^ybU;35Ida77-GgI1-f%)cY=pN$H@pYO{qcXU1!#Q$ literal 0 HcmV?d00001 diff --git a/__pycache__/state.cpython-310.pyc b/__pycache__/state.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b39f4348da82751c4e5be3a998b9224ff7861b84 GIT binary patch literal 519 zcmY+By-ve05P;9lk3wk~5Kq9CrFj8Bl^7692fAgktk{6n#3^Wd$(S2uM?#E}hi^U93zVjy)F@PW1OeMn23e{gC9zcO&N7PSVg5n#aP$QN~WOXiYb1CGEx!d#39*qveQ+H5htNFrQW0ZYedg_5YR_R*2hTRC&;Mh$XEdu zV(UB!=}tOryAZ$cS|8#A@w{~geX=vH4XLci=4pt%uAd!san#Pya4se!h%ay%n%~>v zwQY3K$gMv#rZ&v+CWNu36N379y4{qG-;8}+ z41+IEA#1z~jJLAULWG$RO{?}bt>;3#?PWdwaa3hE+yrOj!zTk$Pg7(tXM<;*k9HDG SlQ~KW+0EGGZpfoUUcw(m>~8Y_ literal 0 HcmV?d00001 diff --git a/__pycache__/succ.cpython-310.pyc b/__pycache__/succ.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6927dcf0b724af09de00d341c5983c48bed9782c GIT binary patch literal 930 zcmZva&x;c=6vtmO$+V8UAYHL#4;~i=#EKw$S;S@Ax`m|+ZKY5I8Jf6`ogXEc1qFRL*eZ9)YP%anO~yN@ zT-JeTA|)!dDoUYM@GDBeM@q&sSKRBsLs&vhrABKbp+<9%_U#T@QFEJCoz>aisoK4z z*!%2s#deM(<@X(wLhClA;A4+>?o_GdwDx~m*&TdsZ)C{Gu#5g+YRJs+@$mIIdVPhD z38pc)9M4Y2JwqlN8}J+EeyT#5;Lixt#JIs~B@?0SpD^sxD%FOqt@Q@O{zY##>`l*e z|LpS3XzDu(zbTl#RiL#LgHT2Si}m$lA+=7G{t4<47W_Twen?~4jf16L$6*u(Nms|A zT!evI2Hjzj$tb+SVS3BwyMtX&22NG)as811(w7hyVZp literal 0 HcmV?d00001 diff --git a/bfs.py b/bfs.py index 58d7f4e..dc4a11f 100644 --- a/bfs.py +++ b/bfs.py @@ -1,6 +1,6 @@ from succ import succ as successors -def bfs(istate, goalx, goaly): +def bfs(istate, goalx, goaly, passedFields): fringe = [istate] explored = [] steps = [] @@ -13,7 +13,7 @@ def bfs(istate, goalx, goaly): steps.insert(0, state) return steps - element = successors(state) + element = successors(state, passedFields) explored.append((state.xpos, state.ypos, state.orientation)) for value in element : val = (value.xpos, value.ypos, value.orientation) diff --git a/main.py b/main.py index b33ffb2..e22a4e9 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,9 @@ def randomize_map(): # tworzenie mapy z losowymi polami field_array_2 = [] for i in range(16): for j in range(16): - field_array_2.append(random.choice(fields_list)) + randomChoiceOfBlock = random.choice(fields_list) + priorityOfBlock = fields_list.index(randomChoiceOfBlock) + field_array_2.append([randomChoiceOfBlock,priorityOfBlock]) field_array_1.append(field_array_2) field_array_2 = [] return field_array_1 @@ -43,7 +45,7 @@ def randomize_map(): # tworzenie mapy z losowymi polami def draw_window(agent, fields): for i in range(16): for j in range(16): - window.blit(fields[i][j], (i * 50, j * 50)) + window.blit(fields[i][j][0], (i * 50, j * 50)) window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta pygame.display.update() @@ -58,7 +60,6 @@ def draw_window(agent, fields): # if keys_pressed[pygame.K_DOWN] and agent.y < 750: # agent.y += 50 - def main(): clock = pygame.time.Clock() run = True @@ -71,13 +72,13 @@ def main(): run = False #keys_pressed = pygame.key.get_pressed() draw_window(agent, fields) - steps = bfs(State(None, None, 0, 0, 'E'), 450, 600) + steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields) for interm in steps: if interm.action == 'LEFT': agent.turn_left() - if interm.action == 'RIGHT': + elif interm.action == 'RIGHT': agent.turn_right() - if interm.action == 'FORWARD': + elif interm.action == 'FORWARD': agent.forward() draw_window(agent, fields) time.sleep(0.5) diff --git a/state.py b/state.py index a6b5e2b..56e67df 100644 --- a/state.py +++ b/state.py @@ -4,4 +4,5 @@ class State: self.xpos = xpos self.ypos = ypos self.orientation = orientation - self.action = action \ No newline at end of file + self.action = action + # self.priority = priority \ No newline at end of file diff --git a/succ.py b/succ.py index 8ebe570..a01b49a 100644 --- a/succ.py +++ b/succ.py @@ -1,7 +1,7 @@ from state import State FIELDWIDTH, FIELDCOUNT = 50, 16 -def succ(st: State): +def succ(st: State, passedFields): successors = [] if st.orientation == 'N': -- 2.20.1 From a9e8d1b83d8e85c9a714c3874cb1bc17e711bd0b Mon Sep 17 00:00:00 2001 From: Tomasz Torchalski Date: Thu, 4 May 2023 18:21:09 +0200 Subject: [PATCH 09/12] Changed map generating --- bfs.py | 18 ++++++++++-------- main.py | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/bfs.py b/bfs.py index dc4a11f..1288543 100644 --- a/bfs.py +++ b/bfs.py @@ -1,21 +1,23 @@ from succ import succ as successors +from queue import PriorityQueue + def bfs(istate, goalx, goaly, passedFields): fringe = [istate] explored = [] steps = [] - while(fringe): + while fringe: state = fringe.pop(0) - if state.xpos == goalx and state.ypos == goaly: + if state.xpos == goalx and state.ypos == goaly: + steps.insert(0, state) + while (state.parent != None): + state = state.parent steps.insert(0, state) - while(state.parent != None): - state = state.parent - steps.insert(0, state) - return steps - + return steps + element = successors(state, passedFields) explored.append((state.xpos, state.ypos, state.orientation)) - for value in element : + for value in element: val = (value.xpos, value.ypos, value.orientation) if val not in explored and value not in fringe: fringe.append(value) diff --git a/main.py b/main.py index e22a4e9..a94873f 100644 --- a/main.py +++ b/main.py @@ -28,49 +28,55 @@ class Agent: self.rect = rect self.direction = direction + def randomize_map(): # tworzenie mapy z losowymi polami - fields_list = [DIRT, GRASS, SAND, COBBLE] field_array_1 = [] field_array_2 = [] + field_priority = [] for i in range(16): + temp_priority = [] for j in range(16): - randomChoiceOfBlock = random.choice(fields_list) - priorityOfBlock = fields_list.index(randomChoiceOfBlock) - field_array_2.append([randomChoiceOfBlock,priorityOfBlock]) + if i in (0, 1) and j in (0, 1): + field_array_2.append(GRASS) + temp_priority.append(1) + else: + prob = random.uniform(0, 100) + if 0 <= prob <= 10: + field_array_2.append(COBBLE) + temp_priority.append(3) + if 10 < prob <= 20: + field_array_2.append(SAND) + temp_priority.append(2) + else: + field_array_2.append(GRASS) + temp_priority.append(1) field_array_1.append(field_array_2) field_array_2 = [] - return field_array_1 + field_priority.append(temp_priority) + return field_array_1, field_priority def draw_window(agent, fields): for i in range(16): for j in range(16): - window.blit(fields[i][j][0], (i * 50, j * 50)) + window.blit(fields[i][j], (i * 50, j * 50)) window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta pygame.display.update() -#def agent_movement(keys_pressed, agent): # sterowanie -# if keys_pressed[pygame.K_LEFT] and agent.x > 0: -# agent.x -= 50 -# if keys_pressed[pygame.K_RIGHT] and agent.x < 750: -# agent.x += 50 -# if keys_pressed[pygame.K_UP] and agent.y > 0: -# agent.y -= 50 -# if keys_pressed[pygame.K_DOWN] and agent.y < 750: -# agent.y += 50 - def main(): clock = pygame.time.Clock() run = True agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta - fields = randomize_map() + fields, priority_array = randomize_map() + print(priority_array) + print(priority_array[4][8]) while run: clock.tick(FPS) - for event in pygame.event.get(): # przechwycanie zamknięcia okna + for event in pygame.event.get(): if event.type == pygame.QUIT: run = False - #keys_pressed = pygame.key.get_pressed() + # keys_pressed = pygame.key.get_pressed() draw_window(agent, fields) steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields) for interm in steps: @@ -89,6 +95,5 @@ def main(): pygame.quit() - if __name__ == "__main__": main() -- 2.20.1 From ba2ed3705d123d8489ec0b40613e6e5aa429ace8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Miko=C5=82ajczak?= Date: Thu, 4 May 2023 20:44:35 +0200 Subject: [PATCH 10/12] implemented a* instead of bfs and heuristic function --- __pycache__/astar.cpython-310.pyc | Bin 0 -> 990 bytes __pycache__/bfs.cpython-310.pyc | Bin 667 -> 710 bytes __pycache__/heuristicfn.cpython-310.pyc | Bin 0 -> 340 bytes __pycache__/state.cpython-310.pyc | Bin 519 -> 697 bytes __pycache__/succ.cpython-310.pyc | Bin 930 -> 1456 bytes astar.py | 61 ++++++++++++++++++++++++ bfs.py | 24 ---------- heuristicfn.py | 2 + main.py | 7 +-- state.py | 7 ++- succ.py | 27 ++++++----- 11 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 __pycache__/astar.cpython-310.pyc create mode 100644 __pycache__/heuristicfn.cpython-310.pyc create mode 100644 astar.py delete mode 100644 bfs.py create mode 100644 heuristicfn.py diff --git a/__pycache__/astar.cpython-310.pyc b/__pycache__/astar.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..42aa76a6d268a4bb9cc8921e9b9f384a169d91f7 GIT binary patch literal 990 zcmZuwO=}b}7*1xA`Pj}@i-Kq`ew@~W+oOnzpcM2_D!na82|MX_c4u~`Nw#z$14{ch zgf5=^E&hXCJ@qFjRD3cm*n=7Jemw7I@@DFOzeljne(mKyI3Yj1a&rYx-eBu3keuYS zBo!G^>gAves&Eu`a#*s8k2qwO^D{Dv@@S6?;tNEP0d-6-#$)jQt}1F(nB~WXTu1X@uE26)FvlVG$8&A2Vvp~Io z6xpDYqVqVXHeLmk08Mf-3)OQk!T&!t{nVm$liJvZmfGNywTb2Dy)L)U8c)}-vjgUP zvB^8~X{SwXipnXhDXgitwmsXk{Fo?lwCnvndXKbgrjN|&bRGL&gpav(8n^JWYKv?$$6* z7=$V6xl5Ys`6@qA7m0BPr-yQ(3T=w9<44`(`$(+)=pCQbqCB}`OP4i5Yng8svdmHZ zKo#?ebcsA^%38_XacyLyJ@c`#EM?`p;>V&~NEd=RHmq-tQG1JScC<`$k>Z*|ALa3P-zMmUe784|#i~Sq8KsEKOSvuZH_)m!528n2oMwHP6 z-(8w@8t5Mut_Ke~ETI8#Z})!evd3T#9zuiVb8wkluub&@sdfgj^8cFhuV3Bq#1BYq idgAZ0^Xd+9gFJ@8B`>QwUzGB-pR7(mGLoTc7W@T@(*rC3 literal 0 HcmV?d00001 diff --git a/__pycache__/bfs.cpython-310.pyc b/__pycache__/bfs.cpython-310.pyc index f9f8ace0b0cf92919487d8962cd4d0c931f02ae7..6cd41fc78b2a22ce17a38a9563ed9dc2ade24751 100644 GIT binary patch delta 190 zcmbQudW@AfpO=@50SJ!V4Nl>j$Sdoi0_3DHq%h_%O~zX+#ihx~K$av7o+85vTTQkYv9qL@-xf*CYf zUxL(XGTve-E=^9}Sk%qPC^q>YqbnoxWEUo5N044WP39sdAYH@(B0wTVVyJ{Tkb8^6 WCO1E&G$+-L5y&fM0TMh6JWK!{JsL*< diff --git a/__pycache__/heuristicfn.cpython-310.pyc b/__pycache__/heuristicfn.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a8d87758d95a6736e9434ed9cb5aa29c7638047f GIT binary patch literal 340 zcmd1j<>g`kg7$C0DfK}5F^Gc(44TZzEI=U`0I^wt z*cph6b$~<(Lk&>16hjRoh?D^8X=Vb7F@eRH5n|#DAhY~l8UgjZ1lgv^c#F9pzg&~$ z7F%&iVo^y2l&-wRnx3DSQvsnWS27fF09F5T*3Zb#P1Vm$%r36X&CJP6%+oK<%}hVn&qysT$}BF)Ois(wE2zB1jl^dJnpzBUhz?^BC~W*R bAvD-JB!UwtcZ77i; literal 0 HcmV?d00001 diff --git a/__pycache__/state.cpython-310.pyc b/__pycache__/state.cpython-310.pyc index b39f4348da82751c4e5be3a998b9224ff7861b84..927e172c331ed437c286156d87409a0f0fb83ba6 100644 GIT binary patch literal 697 zcmY+C&2HN;41g(HcHGoyft>dOz9cWO76o<~cFD!JTm->V8#Vq(B?V{--PS$E9-yzJ z>z<*Poklvyn$qAWks?is_Ar}G0p<65t$s0pKV+OsipCwPnxfYe2*_JX z1&&NYhD<|(yR)bPeu)g_>p@{8Re7DAZbcp>QG?h28!k7vqWuEsenPGJqjorL!P2UemH z;1P%R2wPr*rIU({9ao8^ZymG2Z?qj7KZ25CF2nEos?x!#%Bas?c3Oxy z6`~u|zNLLB#M553#}hllTzgHR$((_zSp3C+)X^LZC|FsHz7_UJ{Rx=_y^7UlDhx^ delta 234 zcmdnV+Rnn8&&$ij00b8`gHo6$^7`9|06D1)QH&`JQA{a}DNHR4QOqgK!3>%#w^)Nq z5=&B(*+A-{0K{ejVrL*OR+_j#ku!w>sFu-BlXc=XO;t^{TP($?IcY^8HANuBMa&?A z1w^m{iIogR9Fv_H16e?hm^_D3irG(-WwI`lss~7*hy#`21aTRG1PB*{4B=qlVFZi# OX+mfWEt3~ANdN!`$s=F@ diff --git a/__pycache__/succ.cpython-310.pyc b/__pycache__/succ.cpython-310.pyc index 6927dcf0b724af09de00d341c5983c48bed9782c..d3465fc75de02eb6e777170701bdbb8ce625f5c4 100644 GIT binary patch literal 1456 zcmb7E%Zl4D6qRJz@i>_>Oat@i7Z^$dT@NK?l5_$gQ<9WGC=?TEGOk}ia$o{36sC(V z`w8u`KhY1-qFr>AeOF!e%9Dr7%*Fy;A6@BQpL6v{_V*nG?bn|n`@DzHZ#^hB0|rg# z@;MlW7!FZ{Cm3r?LK2Y)0c^Y_6JtUd<@ky)lUbkW#AKBra_tqwAr~uin0hG(P`&00 z;mg#YUd1k1>Ab50Fp9`@O7-e>GvWIP`Z;v@5s1vt2b!VxKq4R_&gfU1k_>2o&wx&u zkztJ)Jw+!d_v9!wH!Cud8MpMd@(l!RiM{zxY8?a1lGza316@D2 zymq?E@!Hg7r>k6DKv!y|73LhHItoyMQ9<7kur0062*?*KdJiZpm+QX5vb8q!&i%(F z*QWO^KR0D7hM1LW`|*Iba@_7~D_0vh|Ha({jXO?jjYoGHOP(sY%+AlkV6=8h2Zxi#0yKeA6Gfrm*z%!Q7LQv)96hl&R09GG~bw-rRkb%8`rdluMZi z`5lQ92>E-~xK1M8h`c~9BR}-LxFI8-Pkm1WUZWeQJoIP4F81CP{5sFG`BG60^gAS< zf}z+UP29u|ZmrQFpjz&$ZBZQ%#o`GB^j%AtQonn--dv-23=^WRjb4(oESOX9Df=u+ T*dpXFbOogiriNFRL*eZ9)YP%anO~yN@ zT-JeTA|)!dDoUYM@GDBeM@q&sSKRBsLs&vhrABKbp+<9%_U#T@QFEJCoz>aisoK4z z*!%2s#deM(<@X(wLhClA;A4+>?o_GdwDx~m*&TdsZ)C{Gu#5g+YRJs+@$mIIdVPhD z38pc)9M4Y2JwqlN8}J+EeyT#5;Lixt#JIs~B@?0SpD^sxD%FOqt@Q@O{zY##>`l*e z|LpS3XzDu(zbTl#RiL#LgHT2Si}m$lA+=7G{t4<47W_Twen?~4jf16L$6*u(Nms|A zT!evI2Hjzj$tb+SVS3BwyMtX&22NG)as811(w7hyVZp diff --git a/astar.py b/astar.py new file mode 100644 index 0000000..834c2ee --- /dev/null +++ b/astar.py @@ -0,0 +1,61 @@ +from succ import succ as successors +from queue import PriorityQueue +from state import State + +def astar(istate, goalx, goaly, passedFields): + fringe = PriorityQueue() + fringe.put(istate) + explored = set() + steps = [] + while not fringe.empty(): + state = fringe.get() + if state.xpos == goalx and state.ypos == goaly: + steps.insert(0, state) + while (state.parent != None): + state = state.parent + steps.insert(0, state) + return steps + + element = successors(state, passedFields, goalx, goaly) + explored.add((state.xpos, state.ypos, state.orientation, state.priority)) + for value in element: + val = (value.xpos, value.ypos, value.orientation, value.priority) + if val in explored: + continue + cost = state.priority + value.priority + succesorState = State(state, value.action, + value.xpos, value.ypos, + value.orientation, cost, + value.heuristic) + if value not in fringe.queue: + fringe.put(succesorState) + else : + for element in fringe.queue: + if (element.xpos==value.xpos and element.ypos==value.ypos) and element > value: + element.parent = state + element.priority = value.priority + return False + + +# def bfs(istate, goalx, goaly, passedFields): +# fringe = [istate] +# explored = [] +# steps = [] +# while fringe: +# state = fringe.pop(0) +# if state.xpos == goalx and state.ypos == goaly: +# steps.insert(0, state) +# while (state.parent != None): +# state = state.parent +# steps.insert(0, state) +# return steps + +# element = successors(state, passedFields) +# explored.append((state.xpos, state.ypos, state.orientation)) +# for value in element: +# val = (value.xpos, value.ypos, value.orientation) +# if val not in explored and value not in fringe: +# fringe.append(value) +# return False + + diff --git a/bfs.py b/bfs.py deleted file mode 100644 index 1288543..0000000 --- a/bfs.py +++ /dev/null @@ -1,24 +0,0 @@ -from succ import succ as successors -from queue import PriorityQueue - - -def bfs(istate, goalx, goaly, passedFields): - fringe = [istate] - explored = [] - steps = [] - while fringe: - state = fringe.pop(0) - if state.xpos == goalx and state.ypos == goaly: - steps.insert(0, state) - while (state.parent != None): - state = state.parent - steps.insert(0, state) - return steps - - element = successors(state, passedFields) - explored.append((state.xpos, state.ypos, state.orientation)) - for value in element: - val = (value.xpos, value.ypos, value.orientation) - if val not in explored and value not in fringe: - fringe.append(value) - return False diff --git a/heuristicfn.py b/heuristicfn.py new file mode 100644 index 0000000..04d8486 --- /dev/null +++ b/heuristicfn.py @@ -0,0 +1,2 @@ +def heuristicfn(startx, starty, goalx, goaly): + return pow(((startx//50)-(starty//50)),2) + pow(((goalx//50)-(goaly//50)),2) \ No newline at end of file diff --git a/main.py b/main.py index a94873f..12c9020 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ import pygame import random -from bfs import bfs +from astar import astar from state import State import time from garbage_truck import GarbageTruck +from heuristicfn import heuristicfn pygame.init() WIDTH, HEIGHT = 800, 800 @@ -44,7 +45,7 @@ def randomize_map(): # tworzenie mapy z losowymi polami if 0 <= prob <= 10: field_array_2.append(COBBLE) temp_priority.append(3) - if 10 < prob <= 20: + elif 10 < prob <= 20: field_array_2.append(SAND) temp_priority.append(2) else: @@ -78,7 +79,7 @@ def main(): run = False # keys_pressed = pygame.key.get_pressed() draw_window(agent, fields) - steps = bfs(State(None, None, 0, 0, 'E'), 100, 50, fields) + steps = astar(State(None, None, 0, 0, 'E', priority_array[0][0], heuristicfn(0,0,300,100)), 300, 100, priority_array) for interm in steps: if interm.action == 'LEFT': agent.turn_left() diff --git a/state.py b/state.py index 56e67df..25a0c30 100644 --- a/state.py +++ b/state.py @@ -1,8 +1,11 @@ class State: - def __init__(self, parent, action, xpos, ypos, orientation): + def __init__(self, parent, action, xpos, ypos, orientation, priority, heuristic): self.parent = parent self.xpos = xpos self.ypos = ypos self.orientation = orientation self.action = action - # self.priority = priority \ No newline at end of file + self.priority = priority + self.heuristic = heuristic + def __gt__(self, other): + return self.priority > other.priority \ No newline at end of file diff --git a/succ.py b/succ.py index a01b49a..8773dcd 100644 --- a/succ.py +++ b/succ.py @@ -1,30 +1,31 @@ from state import State +from heuristicfn import heuristicfn FIELDWIDTH, FIELDCOUNT = 50, 16 -def succ(st: State, passedFields): +def succ(st: State, passedPriorities, goalx, goaly): successors = [] if st.orientation == 'N': - successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W')) - successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E')) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.ypos > 0: - successors.append(State(st, 'FORWARD', st.xpos, st.ypos - FIELDWIDTH , 'N')) + successors.append(State(st, 'FORWARD', st.xpos, st.ypos - FIELDWIDTH , 'N', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.orientation == 'S': - successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E')) - successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W')) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.ypos < FIELDWIDTH * (FIELDCOUNT - 1): - successors.append(State(st, 'FORWARD', st.xpos, st.ypos + FIELDWIDTH , 'S')) + successors.append(State(st, 'FORWARD', st.xpos, st.ypos + FIELDWIDTH , 'S', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.orientation == 'W': - successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S')) - successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N')) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.xpos > 0: - successors.append(State(st, 'FORWARD', st.xpos - FIELDWIDTH , st.ypos, 'W')) + successors.append(State(st, 'FORWARD', st.xpos - FIELDWIDTH , st.ypos, 'W', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.orientation == 'E': - successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N')) - successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S')) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) if st.xpos < FIELDWIDTH * (FIELDCOUNT - 1): - successors.append(State(st, 'FORWARD', st.xpos + FIELDWIDTH , st.ypos, 'E')) + successors.append(State(st, 'FORWARD', st.xpos + FIELDWIDTH , st.ypos, 'E', passedPriorities[st.xpos//50][st.ypos//50], heuristicfn(st.xpos, st.ypos, goalx, goaly))) return successors -- 2.20.1 From 9d9c1aaa4c2a09ffc4135d13143f87e9fbeb35d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Miko=C5=82ajczak?= Date: Fri, 5 May 2023 12:13:47 +0200 Subject: [PATCH 11/12] optimalized astar --- astar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astar.py b/astar.py index 834c2ee..2b30eaf 100644 --- a/astar.py +++ b/astar.py @@ -17,9 +17,9 @@ def astar(istate, goalx, goaly, passedFields): return steps element = successors(state, passedFields, goalx, goaly) - explored.add((state.xpos, state.ypos, state.orientation, state.priority)) + explored.add((state.xpos, state.ypos, state.orientation)) for value in element: - val = (value.xpos, value.ypos, value.orientation, value.priority) + val = (value.xpos, value.ypos, value.orientation) if val in explored: continue cost = state.priority + value.priority -- 2.20.1 From 21bcecd101dbe5234a69d61610103fcb2a915323 Mon Sep 17 00:00:00 2001 From: Tomasz Torchalski Date: Fri, 5 May 2023 12:20:29 +0200 Subject: [PATCH 12/12] Added visual rotation of agent, changed heuristic --- heuristicfn.py | 3 ++- main.py | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/heuristicfn.py b/heuristicfn.py index 04d8486..2b90daa 100644 --- a/heuristicfn.py +++ b/heuristicfn.py @@ -1,2 +1,3 @@ def heuristicfn(startx, starty, goalx, goaly): - return pow(((startx//50)-(starty//50)),2) + pow(((goalx//50)-(goaly//50)),2) \ No newline at end of file + return abs(startx - goalx) + abs(starty - goaly) + # return pow(((startx//50)-(starty//50)),2) + pow(((goalx//50)-(goaly//50)),2) \ No newline at end of file diff --git a/main.py b/main.py index 12c9020..2c9e7a5 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,7 @@ FPS = 10 FIELDCOUNT = 16 FIELDWIDTH = 50 + class Agent: def __init__(self, rect, direction): self.rect = rect @@ -42,10 +43,10 @@ def randomize_map(): # tworzenie mapy z losowymi polami temp_priority.append(1) else: prob = random.uniform(0, 100) - if 0 <= prob <= 10: + if 0 <= prob <= 12: field_array_2.append(COBBLE) temp_priority.append(3) - elif 10 < prob <= 20: + elif 12 < prob <= 24: field_array_2.append(SAND) temp_priority.append(2) else: @@ -57,37 +58,49 @@ def randomize_map(): # tworzenie mapy z losowymi polami return field_array_1, field_priority -def draw_window(agent, fields): +def draw_window(agent, fields, flip): + if flip: + direction = pygame.transform.flip(AGENT, True, False) + else: + direction = pygame.transform.flip(AGENT, False, False) for i in range(16): for j in range(16): window.blit(fields[i][j], (i * 50, j * 50)) - window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta + window.blit(direction, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta pygame.display.update() def main(): clock = pygame.time.Clock() run = True - agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta + x, y = [0, 0] + agent = GarbageTruck(0, 0, pygame.Rect(x, y, 50, 50), 0) # tworzenie pola dla agenta fields, priority_array = randomize_map() print(priority_array) - print(priority_array[4][8]) + final_x, final_y = [100, 300] while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # keys_pressed = pygame.key.get_pressed() - draw_window(agent, fields) - steps = astar(State(None, None, 0, 0, 'E', priority_array[0][0], heuristicfn(0,0,300,100)), 300, 100, priority_array) + draw_window(agent, fields, False) # false = kierunek east (domyslny), true = west + steps = astar(State(None, None, x, y, 'E', priority_array[0][0], heuristicfn(x, y, final_x, final_y)), final_x, final_y, priority_array) for interm in steps: if interm.action == 'LEFT': agent.turn_left() + draw_window(agent, fields, True) elif interm.action == 'RIGHT': agent.turn_right() + draw_window(agent, fields, False) elif interm.action == 'FORWARD': agent.forward() - draw_window(agent, fields) + if agent.orientation == 0: + draw_window(agent, fields, False) + elif agent.orientation == 2: + draw_window(agent, fields, True) + else: + draw_window(agent, fields, False) time.sleep(0.5) while True: -- 2.20.1