def shortest_path(source, goal):

    queue = QueueFrontier()
    source_movies = people[source]["movies"]

    for i in source_movies:
        temp_node = Node(source, None, i)
        queue.push(temp_node)

    while (queue.empty() != True):

        parent_node = queue.frontier[0]
        neighbours = get_neighbours(parent_node.state)
        answer = []
        push = answer.append

        for n in neighbours:
            temp_node = Node(n[1], parent_node, n[0])
            if temp_node.state == goal:
                while (temp_node.parent != None):
                    push((temp_node.action, temp_node.state))
                    temp_node = temp_node.parent
                return answer[::-1]
            else:
                response = any(x.state == temp_node.state
                               for x in queue.frontier)
                if (response == False):
                    queue.push(temp_node)
        queue.remove()
    return None
예제 #2
0
def number_to_linked_list(n):
    head = None
    for i in str(n):
        tmp = Node(int(i))
        tmp.next = head
        head = tmp

    return head
예제 #3
0
 def __init__(self, ast_node):
     Node.__init__(self)
     self.car = ast_node.car.cdr
     self.cdr = ast_node.cdr
     assert type(self.car) == AbstractSyntaxTree.IdentifierNode
     self.name = self.car.name
     self.lambda_expr =\
         AbstractSyntaxTree.LambdaNode(self.car.cdr)
     self.car.cdr = self.lambda_expr
예제 #4
0
def reverse_list(head):
  new_head = None
  cur = head
  while cur:
    node = Node(cur.data)
    node.next = new_head
    new_head = node
    cur = cur.next

  return new_head
예제 #5
0
def bfs(goal, pos, tail, bounds):
    visited, queue = set(), [Node(None, pos)]
    visited.add(pos)

    while queue:

        vertex = queue.pop(0)
        if vertex.pos == goal:
            return vertex

        for neighbour in get_neighbors(vertex.pos, tail, bounds):
            if neighbour not in visited:
                visited.add(neighbour)
                queue.append(Node(vertex, neighbour))
    return None
예제 #6
0
    def test_a1(self):
        head = Node(1)
        head.next = Node(1)
        head.next.next = Node(2)
        head.next.next.next = Node(2)
        head.next.next.next.next = Node(2)
        head.next.next.next.next.next = Node(3)

        result = remove_dupes_a(head)
        self.assertEqual(result.toList(), [1, 2, 3])
예제 #7
0
def main():
    with open('PuzzleGame\one_input') as input_file:
        i = 0
        for line in input_file.readlines():
            puzzle_dimension, max_depth, max_search_path, values = line.split()
            root_board = np.array(build_initial_board(int(puzzle_dimension), values), np.int32)
            root_node = Node(root_board, 0, 0, 0)
            build_boards(root_node)
            start_dfs(root_node, max_depth)
            save_to_file(list_of_solution_moves, f'{i}_dfs_solution.txt')
            save_to_file(list_of_search_moves, f'{i}_dfs_search.txt')
            i += 1
            clean_up()
예제 #8
0
def astar_search(goal, pos, tail, bounds):

    open = []
    closed = []

    start_node = Node(None, pos)
    goal_node = Node(None, goal)

    open.append(start_node)

    while len(open) > 0:

        open.sort()

        current_node = open.pop(0)

        closed.append(current_node)

        if current_node == goal_node:
            return current_node

        (x, y) = current_node.pos

        neighbors = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]

        for next_neighbor in neighbors:

            if not is_reachable(next_neighbor, tail, bounds):
                continue

            neighbor = Node(current_node, next_neighbor)

            if neighbor in closed:
                continue
            # Generate heuristics (Manhattan distance)
            neighbor.g = abs(neighbor.pos[0] -
                             start_node.pos[0]) + abs(neighbor.pos[1] -
                                                      start_node.pos[1])
            neighbor.h = abs(neighbor.pos[0] -
                             goal_node.pos[0]) + abs(neighbor.pos[1] -
                                                     goal_node.pos[1])
            neighbor.f = neighbor.g + neighbor.h
            # Check if neighbor is in open list and if it has a lower f value
            if add_to_open(open, neighbor):
                # Everything is green, add neighbor to open list
                open.append(neighbor)

    return []
예제 #9
0
 def __init__(self, ast_node):
     Node.__init__(self)
     self.name = ast_node.car.name
     self.car = ast_node.car.cdr
     self.cdr = ast_node.cdr
예제 #10
0
    def test_a2(self):
        head = Node(1)

        result = remove_dupes_a(head)
        self.assertEqual(result.toList(), [1])
예제 #11
0
    def test_b3(self):
        head = Node(1)
        head.next = Node(1)

        result = remove_dupes_b(head)
        self.assertEqual(result.toList(), [1])
예제 #12
0
#state_space = [1, 2, 3, 4, 5]
#initial_state = State([1, 3], [2, 4, 5])
#final_state = State([], [1, 2, 3, 4, 5])
#final_state = State([5], [1, 2, 3, 4])

#state_space = [1, 2, 3, 4]
#initial_state = State([2, 3], [1])
#initial_state = State([1, 4], [3, 2])
#final_state = State([], [1, 2, 3, 4])

#state_space = [1, 2, 3]
#initial_state = State([2, 3], [1])
#initial_state = State([1, 3], [2])
#final_state = State([], [1, 2, 3])

if __name__ == '__main__':

    root = Node(None, initial_state)

    #solution = bfs_search(root, [], final_state, state_space)

    solution = aStar_search(root, [], final_state, state_space)

    path = [solution]
    while solution.parent:
        solution = solution.parent
        path.append(solution)

    print(f'Solution Path\n{path[::-1]}')
    print(f'It took {algorithms.expansion_count} expansions (equal to depth)')
예제 #13
0
	if valid_response:
		update_tag_settings(new_tags)


############## BEGIN CONFIG #####################
print 'reading settings'
settings = Config()
############## BEGIN SPEAKER #####################
print 'reading settings'
speaker = Speaker(settings)
speaker.play_startup_sound()
############## BEGIN CLOUD #####################
print 'initing cloud'
cloud = Cloud(settings)
############## BEGIN DOOR #####################
print 'initing_door'
door = Door('back_door', settings)
door.DOOR_OPENED += door_opened
door.DOOR_CLOSED += door_closed
############## BEGIN NODE ###################
print 'setting up node'
node = Node(settings)
node.OPEN_DOOR_REQUEST += request_door_open
node.REFRESH_USERS_REQUEST += request_refresh_users
node.start()
############## BEGIN SENSOR ###################
print 'setting up sensor'
sensor = Sensor("outside_sensor")
sensor.FOUND_TAG += found_tag
sensor.start()
예제 #14
0
 def __init__(self, ast_node):
     Node.__init__(self)
     self.car = ast_node
     self.param_list = ast_node
     assert type(self.param_list) == AbstractSyntaxTree.ListNode
     self.body_expr = ast_node.cdr
예제 #15
0
 def __init__(self, parse_node):
     Node.__init__(self)
예제 #16
0
 def __init__(self, parse_node):
     Node.__init__(self)
     self.name = parse_node.value
예제 #17
0
 def __init__(self, ast_node):
     Node.__init__(self)
     self.car = ast_node.car.cdr
     self.predicate_expr = self.car
     self.left_expr = ast_node.car.cdr.cdr
     self.right_expr = ast_node.car.cdr.cdr.cdr
        right_status = self.lca_helper(root.right, p, q)

        if right_status.num_target_nodes == 2:
            return right_status

        return Status(num_target_nodes=left_status.num_target_nodes +
                      right_status.num_target_nodes +
                      (p.val, q.val).count(root.val),
                      ancestor=root)


sol = Solution()
tree: Node = TreeHelper.create_tree([
    3,
    5,
    1,
    6,
    2,
    0,
    8,
    None,
    None,
    7,
    4,
])
# ancestor = sol.lowestCommonAncestor(tree, Node(5, None, None), Node(1, None, None))
ancestor = sol.lowestCommonAncestor(tree, Node(5, None, None),
                                    Node(4, None, None))
print(ancestor.val)
예제 #19
0
 def __init__(self, parse_node):
     Node.__init__(self)
     operator_node = parse_node.car
     self.name = parse_node.value
예제 #20
0
 def __init__(self, parse_node):
     Node.__init__(this)
     this.value = value
예제 #21
0
 def __init__(self, ast_node=None):
     Node.__init__(self)
     if ast_node:
         self.car = ast_node.car
         self.cdr = ast_node.cdr
예제 #22
0
 def __init__(self, parse_node):
     Node.__init__(self)
     self.value = value
예제 #23
0
 def __init__(self, parse_node):
     Node.__init__(self)
     self.cdr = parse_node.cdr
예제 #24
0
def minimax(board):
    """
    Returns the optimal action for the current player on the board.
    """
    # create a node using the input board
    input_node = Node(parent=None, state=board, value=None)
    # if board is terminal, return None
    if terminal(board) == True:
        return None

    # check whose turn it is
    num_x = 0
    num_o = 0

    for row in board:
        for cell in row:
            if cell == X:
                num_x = num_x + 1
            if cell == O:
                num_o = num_o + 1

    if num_o == num_x:
        current_player = X
    else:
        current_player = O

    # create a stack queue
    stack_queue = StackQueue()

    # explore the current board
    possible_boards = explore_board(board)[0]
    moves = explore_board(board)[1]
    # keep track of all the possible moves
    possible_move_nodes = []
    # create a node for each possible board
    for board_state in possible_boards:
        new_node = Node(parent=input_node, state=board_state, value=None)

        # add the new node to the frontier
        stack_queue.frontier.append(new_node)
        possible_move_nodes.append(new_node)

    # continue exploring the frontier while it is not empty
    while stack_queue.frontier != []:
        # remove a node from the frontier
        current_node = stack_queue.remove()
        # check if current node is terminal
        if terminal(current_node.state) == True:
            # check who won the game
            utility_value = utility(current_node.state)

            # assign to each node and its parent nodes the value
            while current_node.parent != None:
                current_node.value = utility_value
                current_node = current_node.parent

        if terminal(current_node.state) == False:
            # if current node is not terminal, explore it
            possible_board_states = explore_board(current_node.state)[0]

            # create a node for each board state and add it to the frontier
            for board_state in possible_board_states:
                new_node = Node(parent=current_node,
                                value=None,
                                state=board_state)

                stack_queue.frontier.append(new_node)
    # once the frontier is empty, go over all the possible moves, and choose at random the ones that match the computers goal
    if current_player == X:
        optimal_moves = []
        for node in possible_move_nodes:
            if node.value == 1:
                # get the index of the node
                node_index = possible_move_nodes.index(node)
                move = moves(node_index)
                optimal_moves.append(move)

        # return a random optimal move
        random_index = randint(0, len(optimal_moves) - 1)
        return optimal_moves[random_index]

    if current_player == O:
        optimal_moves = []
        for node in possible_move_nodes:
            if node.value == -1:
                # get the index of the node
                node_index = possible_move_nodes.index(node)
                move = moves(node_index)
                optimal_moves.append(move)

        # return a random optimal move
        random_index = randint(0, len(optimal_moves) - 1)
        return optimal_moves[random_index]