refactoring
This commit is contained in:
parent
afdcac8b56
commit
f4ad111371
@ -14,7 +14,7 @@ class State:
|
|||||||
return self.x == other.x and self.y == other.y
|
return self.x == other.x and self.y == other.y
|
||||||
|
|
||||||
|
|
||||||
class StateGraphSearchBFS:
|
class GoAnyDirectionBFS:
|
||||||
def __init__(self, world: World, start_state: State, goal_state: State):
|
def __init__(self, world: World, start_state: State, goal_state: State):
|
||||||
self.start_state = start_state
|
self.start_state = start_state
|
||||||
self.goal_state = goal_state
|
self.goal_state = goal_state
|
@ -1,6 +1,5 @@
|
|||||||
import queue
|
import queue
|
||||||
|
|
||||||
from domain.commands.vacuum_move_command import VacuumMoveCommand
|
|
||||||
from domain.world import World
|
from domain.world import World
|
||||||
|
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ def action_sequence(node: Node):
|
|||||||
return actions
|
return actions
|
||||||
|
|
||||||
|
|
||||||
class TemplateBFS:
|
class RotateAndGoBFS:
|
||||||
def __init__(self, world: World, start_state: State, goal_state: State):
|
def __init__(self, world: World, start_state: State, goal_state: State):
|
||||||
self.world = world
|
self.world = world
|
||||||
self.start_state = start_state
|
self.start_state = start_state
|
||||||
@ -49,7 +48,7 @@ class TemplateBFS:
|
|||||||
|
|
||||||
while self.fringe:
|
while self.fringe:
|
||||||
elem = self.fringe.get()
|
elem = self.fringe.get()
|
||||||
if elem.state == self.goal_state:
|
if self.is_goal(elem.state):
|
||||||
self.actions = action_sequence(elem)
|
self.actions = action_sequence(elem)
|
||||||
return True
|
return True
|
||||||
self.explored.add(elem.state)
|
self.explored.add(elem.state)
|
||||||
@ -67,12 +66,18 @@ class TemplateBFS:
|
|||||||
|
|
||||||
def successors(self, state: State):
|
def successors(self, state: State):
|
||||||
new_successors = [
|
new_successors = [
|
||||||
# ("GO", State(state.x + state.direction[0], state.y + state.direction[1], state.direction)),
|
|
||||||
# rotate right
|
# rotate right
|
||||||
("RR", State(state.x, state.y, (-state.direction[1], state.direction[0]))),
|
("RR", State(state.x, state.y, (-state.direction[1], state.direction[0]))),
|
||||||
# rotate left
|
# rotate left
|
||||||
("RL", State(state.x, state.y, (state.direction[1], -state.direction[0]))),
|
("RL", State(state.x, state.y, (state.direction[1], -state.direction[0]))),
|
||||||
]
|
]
|
||||||
if self.world.accepted_move(state.x + state.direction[0], state.y + state.direction[1]):
|
if self.world.accepted_move(state.x + state.direction[0], state.y + state.direction[1]):
|
||||||
new_successors.append(("GO", State(state.x + state.direction[0], state.y + state.direction[1], state.direction)))
|
new_successors.append(
|
||||||
|
("GO", State(state.x + state.direction[0], state.y + state.direction[1], state.direction)))
|
||||||
return new_successors
|
return new_successors
|
||||||
|
|
||||||
|
def is_goal(self, state: State) -> bool:
|
||||||
|
return (
|
||||||
|
state.x == self.goal_state.x
|
||||||
|
and state.y == self.goal_state.y
|
||||||
|
)
|
22
main.py
22
main.py
@ -12,7 +12,7 @@ from domain.entities.docking_station import Doc_Station
|
|||||||
from domain.world import World
|
from domain.world import World
|
||||||
from view.renderer import Renderer
|
from view.renderer import Renderer
|
||||||
# from AI_brain.movement import StateGraphSearchBFS, State
|
# from AI_brain.movement import StateGraphSearchBFS, State
|
||||||
from AI_brain.template_bfs import TemplateBFS, State
|
from AI_brain.rotate_and_go_bfs import RotateAndGoBFS, State
|
||||||
|
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@ -49,22 +49,26 @@ class Main:
|
|||||||
start_state = State(self.world.vacuum.x, self.world.vacuum.y)
|
start_state = State(self.world.vacuum.x, self.world.vacuum.y)
|
||||||
end_state = State(self.world.doc_station.x, self.world.doc_station.y)
|
end_state = State(self.world.doc_station.x, self.world.doc_station.y)
|
||||||
|
|
||||||
# SGS_BFS = StateGraphSearchBFS(self.world, start_state, end_state)
|
# path_searcher = GoAnyDirectionBFS(self.world, start_state, end_state)
|
||||||
SGS_BFS = TemplateBFS(self.world, start_state, end_state)
|
path_searcher = RotateAndGoBFS(self.world, start_state, end_state)
|
||||||
if not SGS_BFS.search():
|
if not path_searcher.search():
|
||||||
print("No solution")
|
print("No solution")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
SGS_BFS.actions.reverse()
|
path_searcher.actions.reverse()
|
||||||
while self.running:
|
while self.running:
|
||||||
self.renderer.render(self.world)
|
for event in pygame.event.get():
|
||||||
self.clock.tick(5)
|
if event.type == pygame.QUIT:
|
||||||
if len(SGS_BFS.actions) > 0:
|
self.running = False
|
||||||
action_direction = SGS_BFS.actions.pop()
|
|
||||||
|
if len(path_searcher.actions) > 0:
|
||||||
|
action_direction = path_searcher.actions.pop()
|
||||||
# self.handle_action1(action_direction)
|
# self.handle_action1(action_direction)
|
||||||
self.handle_action2(action_direction)
|
self.handle_action2(action_direction)
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
self.renderer.render(self.world)
|
||||||
|
self.clock.tick(5)
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user