added BFSSearcher class with bfs algorithm, replaced two photo image assets and made small improvements to the constants.py
This commit is contained in:
parent
b94c1bf5ad
commit
8dd1511952
@ -5,7 +5,7 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (ai-project)" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
</project>
|
BIN
assets/images/cobblestone.jpg
Normal file
BIN
assets/images/cobblestone.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.4 KiB |
BIN
assets/images/tractor-transparent.png
Normal file
BIN
assets/images/tractor-transparent.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 147 KiB |
@ -1,11 +1,10 @@
|
||||
class Constants:
|
||||
""" Class to represent all constants for the app """
|
||||
|
||||
def __init__(self):
|
||||
self.move = "move"
|
||||
self.up = "up"
|
||||
self.down = "down"
|
||||
self.right = "right"
|
||||
self.left = "left"
|
||||
self.rotate_right = "rotate_right"
|
||||
self.rotate_left = "rotate_left"
|
||||
MOVE = "move"
|
||||
UP = "up"
|
||||
DOWN = "down"
|
||||
RIGHT = "right"
|
||||
LEFT = "left"
|
||||
ROTATE_RIGHT = "rotate_right"
|
||||
ROTATE_LEFT = "rotate_left"
|
||||
|
@ -7,7 +7,7 @@ class Tractor(Sprite):
|
||||
|
||||
def __init__(self, engine, fertilizer, settings):
|
||||
super().__init__()
|
||||
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor.png'),
|
||||
self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor-transparent.png'),
|
||||
(0.9*settings.tile_size, 0.9*settings.tile_size))
|
||||
self.engine = engine
|
||||
self.fertilizer = fertilizer
|
||||
|
121
src/utils/bfs.py
121
src/utils/bfs.py
@ -1,4 +1,5 @@
|
||||
from constants import Constants as c
|
||||
from constants import Constants as C
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, current_x, current_y, agent_direction, action=None, parent=None):
|
||||
@ -11,54 +12,100 @@ class Node:
|
||||
def successor(self):
|
||||
x = self.current_x
|
||||
y = self.current_y
|
||||
actions = None
|
||||
neighbours = []
|
||||
|
||||
if x < 9: # right neighbour
|
||||
if self.agent_direction == c.right:
|
||||
actions = c.move
|
||||
elif self.agent_direction == c.left:
|
||||
actions = [c.rotate_right, c.rotate_right, c.move]
|
||||
elif self.agent_direction == c.up:
|
||||
actions = [c.rotate_right, c.move]
|
||||
elif self.agent_direction == c.down:
|
||||
actions = [c.rotate_left, c.move]
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = C.MOVE
|
||||
elif self.agent_direction == C.LEFT:
|
||||
actions = [C.ROTATE_RIGHT, C.ROTATE_RIGHT, C.MOVE]
|
||||
elif self.agent_direction == C.UP:
|
||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||
|
||||
neighbours.append((actions, (x + 1, y), c.right))
|
||||
neighbours.append((actions, (x + 1, y), C.RIGHT))
|
||||
|
||||
if x > 0: # left neighbour
|
||||
if self.agent_direction == c.right:
|
||||
actions = [c.rotate_left, c.rotate_left, c.move]
|
||||
elif self.agent_direction == c.left:
|
||||
actions = c.move
|
||||
elif self.agent_direction == c.up:
|
||||
actions = [c.rotate_left, c.move]
|
||||
elif self.agent_direction == c.down:
|
||||
actions = [c.rotate_right, c.move]
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.LEFT:
|
||||
actions = C.MOVE
|
||||
elif self.agent_direction == C.UP:
|
||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||
|
||||
neighbours.append((actions, (x - 1, y), c.left))
|
||||
neighbours.append((actions, (x - 1, y), C.LEFT))
|
||||
|
||||
if y > 0: # upper neighbour
|
||||
if self.agent_direction == c.right:
|
||||
actions = [c.rotate_left, c.move]
|
||||
elif self.agent_direction == c.left:
|
||||
actions = [c.rotate_right, c.move]
|
||||
elif self.agent_direction == c.up:
|
||||
actions = c.move
|
||||
elif self.agent_direction == c.down:
|
||||
actions = [c.rotate_left, c.rotate_left, c.move]
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.LEFT:
|
||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||
elif self.agent_direction == C.UP:
|
||||
actions = C.MOVE
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
||||
|
||||
neighbours.append((actions, (x, y - 1), c.up))
|
||||
neighbours.append((actions, (x, y - 1), C.UP))
|
||||
|
||||
if y < 9: # down neighbour
|
||||
if self.agent_direction == c.right:
|
||||
actions = [c.rotate_right, c.move]
|
||||
elif self.agent_direction == c.left:
|
||||
actions = [c.rotate_left, c.move]
|
||||
elif self.agent_direction == c.up:
|
||||
actions = [c.rotate_left, c.rotate_left, c.move]
|
||||
elif self.agent_direction == c.down:
|
||||
actions = c.move
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||
elif self.agent_direction == C.LEFT:
|
||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.UP:
|
||||
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = C.MOVE
|
||||
|
||||
neighbours.append((actions, (x, y + 1), c.down))
|
||||
neighbours.append((actions, (x, y + 1), C.DOWN))
|
||||
|
||||
return neighbours
|
||||
|
||||
|
||||
class BFSSearcher:
|
||||
|
||||
def __init__(self):
|
||||
self.fringe = []
|
||||
self.explored_states = []
|
||||
self.path = []
|
||||
|
||||
def search(self, first_state: tuple, goal, agent_direction):
|
||||
self.fringe.append(Node(first_state[0], first_state[1], agent_direction))
|
||||
|
||||
while True:
|
||||
if not self.fringe:
|
||||
return False
|
||||
element = self.fringe.pop(0)
|
||||
|
||||
if element.state == goal:
|
||||
return self.__state_eq_goal_action(element)
|
||||
|
||||
self.explored_states.append(element)
|
||||
|
||||
for action, state, direction in element.succesor():
|
||||
fringe_states = []
|
||||
explored_states = []
|
||||
for node in self.fringe:
|
||||
fringe_states.append(node.state)
|
||||
for node in self.explored_states:
|
||||
explored_states.append(node)
|
||||
if state not in fringe_states and state not in explored_states:
|
||||
x = Node(state[0], state[1], direction)
|
||||
x.parent = element
|
||||
x.action = action
|
||||
self.fringe.append(x)
|
||||
|
||||
def __state_eq_goal_action(self, current_element: Node):
|
||||
while current_element.parent:
|
||||
if type(current_element.action) == list:
|
||||
current_element.action.reverse()
|
||||
for action in current_element.action:
|
||||
self.path.append(action)
|
||||
else:
|
||||
self.path.append(current_element.action)
|
||||
|
||||
return self.path.reverse()
|
||||
|
@ -17,7 +17,7 @@ class World:
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1]]
|
||||
self.dirt_image = pygame.image.load('assets/images/dirt.jpeg')
|
||||
self.gravel_image = pygame.image.load('assets/images/gravel.jpeg')
|
||||
self.gravel_image = pygame.image.load('assets/images/cobblestone.jpg')
|
||||
self.settings = settings
|
||||
self.tiles = pygame.sprite.Group() # mamy tiles jako Sprite Group, to sie przyda potem do kolizji itp.
|
||||
self.create_tiles()
|
||||
|
Loading…
Reference in New Issue
Block a user