pacai.search.common

Common search utilities.

 1"""
 2Common search utilities.
 3"""
 4
 5import typing
 6
 7import pacai.core.board
 8import pacai.core.search
 9
10def unit_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
11    """
12    One of the most simple search functions,
13    always return 1 (a single unit).
14    """
15
16    return 1.0
17
18def longitudinal_cost_function(node: pacai.core.search.SearchNode, base: float = 1.0, **kwargs: typing.Any) -> float:
19    """
20    If the search node has a "position" attribute,
21    use that to assign a score based on its longitudinal position (its column).
22    If there is no "position" attribute, just use unit cost.
23
24    The cost will be `base ^ node.position.col`.
25    """
26
27    if (hasattr(node, 'position') and isinstance(getattr(node, 'position'), pacai.core.board.Position)):
28        return float(base ** getattr(node, 'position').col)
29
30    return unit_cost_function(node, **kwargs)
31
32def stay_east_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
33    """
34    A longitudinal_cost_function that prioritizes staying east.
35    """
36
37    return longitudinal_cost_function(node, base = 0.5)
38
39def stay_west_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
40    """
41    A longitudinal_cost_function that prioritizes staying west.
42    """
43
44    return longitudinal_cost_function(node, base = 2.0)
45
46def null_heuristic(node: pacai.core.search.SearchNode, problem: pacai.core.search.SearchProblem, **kwargs: typing.Any) -> float:
47    """ A trivial heuristic that returns a constant. """
48
49    return 0.0
def unit_cost_function(node: pacai.core.search.SearchNode, **kwargs: Any) -> float:
11def unit_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
12    """
13    One of the most simple search functions,
14    always return 1 (a single unit).
15    """
16
17    return 1.0

One of the most simple search functions, always return 1 (a single unit).

def longitudinal_cost_function( node: pacai.core.search.SearchNode, base: float = 1.0, **kwargs: Any) -> float:
19def longitudinal_cost_function(node: pacai.core.search.SearchNode, base: float = 1.0, **kwargs: typing.Any) -> float:
20    """
21    If the search node has a "position" attribute,
22    use that to assign a score based on its longitudinal position (its column).
23    If there is no "position" attribute, just use unit cost.
24
25    The cost will be `base ^ node.position.col`.
26    """
27
28    if (hasattr(node, 'position') and isinstance(getattr(node, 'position'), pacai.core.board.Position)):
29        return float(base ** getattr(node, 'position').col)
30
31    return unit_cost_function(node, **kwargs)

If the search node has a "position" attribute, use that to assign a score based on its longitudinal position (its column). If there is no "position" attribute, just use unit cost.

The cost will be base ^ node.position.col.

def stay_east_cost_function(node: pacai.core.search.SearchNode, **kwargs: Any) -> float:
33def stay_east_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
34    """
35    A longitudinal_cost_function that prioritizes staying east.
36    """
37
38    return longitudinal_cost_function(node, base = 0.5)

A longitudinal_cost_function that prioritizes staying east.

def stay_west_cost_function(node: pacai.core.search.SearchNode, **kwargs: Any) -> float:
40def stay_west_cost_function(node: pacai.core.search.SearchNode, **kwargs: typing.Any) -> float:
41    """
42    A longitudinal_cost_function that prioritizes staying west.
43    """
44
45    return longitudinal_cost_function(node, base = 2.0)

A longitudinal_cost_function that prioritizes staying west.

def null_heuristic( node: pacai.core.search.SearchNode, problem: pacai.core.search.SearchProblem, **kwargs: Any) -> float:
47def null_heuristic(node: pacai.core.search.SearchNode, problem: pacai.core.search.SearchProblem, **kwargs: typing.Any) -> float:
48    """ A trivial heuristic that returns a constant. """
49
50    return 0.0

A trivial heuristic that returns a constant.