コード例 #1
0
ファイル: gui_lab.py プロジェクト: DavideCanton/pyIA
    def reconstruct_path(self, path):
        """
        Reconstructs the complete path inserting nodes which
        were "jumped" by the generating function. This is possible
        because JPS generates jump only in straight or diagonal lines.

        For example:
        [(0,0),(4,4)] -> [(0,0),(1,1),(2,2),(3,3),(4,4)].
        """

        def pairwise(iterable):
            a, b = it.tee(iterable)
            next(b, None)
            return zip(a, b)

        cost = 0
        expanded_path = [path[0]]
        sqrt_2 = 2 ** 0.5
        for cur_node, next_node in pairwise(path):
            cur_node = np.array(cur_node)
            next_node = np.array(next_node)
            direction = normalize(next_node - cur_node)
            cost_unit = sqrt_2 if np.all(direction) else 1
            while not np.array_equal(cur_node, next_node):
                cur_node = tuple(int(x) for x in cur_node + direction)
                expanded_path.append(cur_node)
                cost += cost_unit
        return expanded_path, cost
コード例 #2
0
ファイル: run_in_term.py プロジェクト: DavideCanton/pyIA
def reconstruct_path(path, labyrinth):
    """
    Reconstructs the complete path inserting nodes which
    were "jumped" by the generating function. This is possible
    because JPS generates jump only in straight or diagonal lines.

    For example:
    [(0,0),(4,4)] -> [(0,0),(1,1),(2,2),(3,3),(4,4)].
    """
    expanded_path = [path[0]]
    for cur_node, next_node in pairwise(path):
        cur_node = np.array(cur_node)
        next_node = np.array(next_node)
        direction = lab_module.normalize(next_node - cur_node)
        while not np.array_equal(cur_node, next_node):
            cur_node = tuple(int(x) for x in cur_node + direction)
            expanded_path.append(cur_node)
    return expanded_path