WMICraft/algorithms/a_star.py
2022-04-11 20:04:53 +02:00

75 lines
1.4 KiB
Python

from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum, unique
from typing import Tuple, Optional, List
@unique
class Direction(Enum):
LEFT = (0, -1)
RIGHT = (0, 1)
UP = (-1, 0)
DOWN = (1, 0)
@unique
class Action(Enum):
TURN_LEFT = 'TURN_LEFT'
TURN_RIGHT = 'TURN_RIGHT'
FORWARD = 'FORWARD'
@dataclass
class State:
position: Tuple[int, int]
direction: Direction
@dataclass
class Node:
state: State
parent: Optional[Node]
action: Action
cost: int = field(init=False)
depth: int = field(init=False)
def __post_init__(self) -> None:
self.cost = 0 if not self.parent else self.parent.cost + 1
self.depth = self.cost
def __eq__(self, other: Node) -> bool:
return self.state == other.state
def __hash__(self) -> int:
return hash(self.state)
def expand(node: Node) -> List[Node]:
return [child_node(node=node, action=action) for action in actions(node.state)]
def child_node(node: Node, action: Action) -> Node:
next_state = result(state=node.state, action=action)
return Node(state=next_state, parent=node, action=action)
def actions(state: State) -> List[Action]:
pass
def result(state: State, action: Action) -> State:
pass
def goal_test(state: State, goal_list: List[Tuple[int, int]]) -> bool:
pass
def h(state: State) -> int:
pass
def f(state: State) -> int:
pass