Improved solver performance
This commit is contained in:
@@ -2,52 +2,55 @@
|
||||
import typing
|
||||
from typing import Iterator, List, Optional
|
||||
import time
|
||||
|
||||
from dataclasses import dataclass
|
||||
from ..board import Board
|
||||
from . import board_actions
|
||||
from .board_actions import DragonKillAction, GoalAction, HuaKillAction, MoveAction
|
||||
from .board_possibilities import possible_actions
|
||||
|
||||
|
||||
@dataclass
|
||||
class ActionStackFrame:
|
||||
iterator: Iterator[board_actions.Action]
|
||||
action: Optional[board_actions.Action]
|
||||
state: int
|
||||
|
||||
def next(self) -> Optional[board_actions.Action]:
|
||||
"""Get next iteration of top action iterator"""
|
||||
try:
|
||||
self.action = next(self.iterator)
|
||||
except StopIteration:
|
||||
return None
|
||||
return self.action
|
||||
|
||||
|
||||
class ActionStack:
|
||||
"""Stack of chosen actions on the board"""
|
||||
|
||||
iterator_stack: List[Iterator[board_actions.Action]]
|
||||
action_stack: List[Optional[board_actions.Action]]
|
||||
index_stack: List[int]
|
||||
state_stack: List[int]
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.iterator_stack = []
|
||||
self.index_stack = []
|
||||
self.action_stack = []
|
||||
self.state_stack = []
|
||||
self.frames: List[ActionStackFrame] = []
|
||||
|
||||
def push(self, board: Board) -> None:
|
||||
"""Append another board state to stack"""
|
||||
self.iterator_stack.append(possible_actions(board))
|
||||
self.action_stack.append(None)
|
||||
self.index_stack.append(0)
|
||||
self.state_stack.append(board.state_identifier)
|
||||
self.frames.append(
|
||||
ActionStackFrame(
|
||||
iterator=iter(possible_actions(board)),
|
||||
action=None,
|
||||
state=board.state_identifier,
|
||||
)
|
||||
)
|
||||
|
||||
def get(self) -> Optional[board_actions.Action]:
|
||||
@property
|
||||
def top(self) -> ActionStackFrame:
|
||||
"""Get next iteration of top action iterator"""
|
||||
try:
|
||||
self.action_stack[-1] = next(self.iterator_stack[-1])
|
||||
except StopIteration:
|
||||
return None
|
||||
self.index_stack[-1] += 1
|
||||
return self.action_stack[-1]
|
||||
return self.frames[-1]
|
||||
|
||||
def pop(self) -> None:
|
||||
"""Pop one action from stack"""
|
||||
self.action_stack.pop()
|
||||
self.iterator_stack.pop()
|
||||
self.index_stack.pop()
|
||||
self.state_stack.pop()
|
||||
self.frames.pop()
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.index_stack)
|
||||
return len(self.frames)
|
||||
|
||||
|
||||
def solve(
|
||||
@@ -61,27 +64,30 @@ def solve(
|
||||
def _limit_stack_size(stack_size: int) -> None:
|
||||
if len(stack) == stack_size:
|
||||
stack.pop()
|
||||
assert stack.action_stack[-1] is not None
|
||||
stack.action_stack[-1].undo(board)
|
||||
assert stack.top.action is not None
|
||||
stack.top.action.undo(board)
|
||||
assert board.state_identifier in state_set
|
||||
|
||||
def _backtrack_action() -> None:
|
||||
stack.pop()
|
||||
assert stack.action_stack[-1] is not None
|
||||
stack.action_stack[-1].undo(board)
|
||||
assert stack.top.action is not None
|
||||
stack.top.action.undo(board)
|
||||
assert board.state_identifier in state_set
|
||||
|
||||
def _skip_loop_move(action: board_actions.Action) -> bool:
|
||||
if isinstance(action, MoveAction):
|
||||
for prev_action in stack.action_stack[-2::-1]:
|
||||
if isinstance(prev_action, MoveAction):
|
||||
if prev_action.cards == action.cards:
|
||||
return True
|
||||
if not isinstance(action, MoveAction):
|
||||
return False
|
||||
for frame in stack.frames[-2::-1]:
|
||||
if not isinstance(frame.action, MoveAction):
|
||||
continue
|
||||
if frame.action.cards == action.cards:
|
||||
return True
|
||||
return False
|
||||
|
||||
iter_start = time.time()
|
||||
count = 0
|
||||
while stack:
|
||||
|
||||
count += 1
|
||||
if count > 5000:
|
||||
count = 0
|
||||
@@ -92,10 +98,10 @@ def solve(
|
||||
|
||||
# _limit_stack_size(80)
|
||||
|
||||
assert board.state_identifier == stack.state_stack[-1]
|
||||
action = stack.get()
|
||||
assert board.state_identifier == stack.top.state
|
||||
action = stack.top.next()
|
||||
|
||||
if not action:
|
||||
if action is None:
|
||||
_backtrack_action()
|
||||
continue
|
||||
|
||||
@@ -105,14 +111,11 @@ def solve(
|
||||
action.apply(board)
|
||||
|
||||
if board.solved():
|
||||
yield stack.action_stack
|
||||
assert all(x.action is not None for x in stack.frames)
|
||||
yield [typing.cast(board_actions.Action, x.action) for x in stack.frames]
|
||||
iter_start = time.time()
|
||||
stack.action_stack[-1].undo(board)
|
||||
while isinstance(
|
||||
stack.action_stack[-1], (GoalAction, HuaKillAction, DragonKillAction)
|
||||
):
|
||||
stack.pop()
|
||||
stack.action_stack[-1].undo(board)
|
||||
action.undo(board)
|
||||
assert board.state_identifier in state_set
|
||||
continue
|
||||
|
||||
if board.state_identifier in state_set:
|
||||
|
||||
Reference in New Issue
Block a user