def find_solution_BFS(connections, initial_state, solution):
    solved = False
    visited_nodes = []
    frontier_nodes = []
    startNode = Node(initial_state)
    frontier_nodes.append(startNode)
    while(not solved) and len(frontier_nodes) != 0:
        node = frontier_nodes[0]
        # extract node and add it to visited_nodes
        visited_nodes.append(frontier_nodes.pop(0));
        if node.get_data() == solution:
            # solution found
            solved = True
            return node
        else:
            # expand child nodes (cities with connection)
            node_data = node.get_data();
            children_list = []
            for one_child in connections[node_data]:
                child = Node(one_child)
                children_list.append(child)
                if not child.on_list(visited_nodes) \
                   and not child.on_list(frontier_nodes):
                    frontier_nodes.append(child)

            node.set_children(children_list)
Ejemplo n.º 2
0
def find_solution_rec_DFS(node, solution, visited, limit):
    if limit > 0:
        visited.append(node)
        if node.get_data() == solution:
            return node
        else:
            # expand children nodes (cities with connection)
            node_data = node.get_data();
            children_list = []
            for a_child in connections[node_data]:
                child = Node(a_child)
                if not child.on_list(visited):
                    children_list.append(child)

            node.set_children(children_list)

            for child_node in node.get_children():
                if not child_node.get_data() in visited:
                    # recursive call
                    sol = find_solution_rec_DFS(child_node, solution, \
                                                visited, limit-1)
                    if sol != None:
                        return sol

        return None
Ejemplo n.º 3
0
def find_sol_UCS(conns, init_st, sol):
    solved = False
    visited_nodes = []
    frontier_nodes = []
    start_node = Node(init_st)
    start_node.set_cost(0)
    frontier_nodes.append(start_node)
    while (not solved) and len(frontier_nodes) != 0:
        # sort the frontier nodes
        frontier_nodes = sorted(frontier_nodes, cmp=compare)
        current_node = frontier_nodes[0]
        # extract first node and add it to visited
        visited_nodes.append(frontier_nodes.pop(0))
        if current_node.get_data() == sol:
            # solution found
            solved = True
            return current_node
        else:
            # expand child nodes (connected cities)
            cn_data = current_node.get_data()
            children_list = []
            for ch in conns[cn_data]:
                child = Node(ch)
                # find g(n)
                cost = conns[cn_data][ch]
                child.set_cost(current_node.get_cost()+cost)
                children_list.append(child)
                if not child.on_list(visited_nodes):
                    # if is on list we replace it the new
                    # cost value, if less.
                    if child.on_list(frontier_nodes):
                        for n in frontier_nodes:
                            if n.equals(child) and n.get_cost() > child.get_cost():
                                frontier_nodes.remove(n)
                                frontier_nodes.append(child)
                    else:
                        frontier_nodes.append(child)

            current_node.set_children(children_list)
def find_solution_DFS(initial_state, solution):
    solved = False
    visited_nodes = []
    frontier_nodes = []
    startNode = Node(initial_state)
    frontier_nodes.append(startNode)
    while (not solved) and len(frontier_nodes) != 0:
        for i in frontier_nodes:
            print i,
        print ""
        node = frontier_nodes.pop()
        # extract node and add it to visited_nodes
        visited_nodes.append(node)
        if node.get_data() == solution:
            # solution found
            solved = True
            return node
        else:
            # expand children nodes
            node_data = node.get_data()

            # right operator
            child = [node_data[0], node_data[1], node_data[3], node_data[2]]
            right_child = Node(child)
            if not right_child.on_list(visited_nodes) \
               and not right_child.on_list(frontier_nodes):
                frontier_nodes.append(right_child)

            # central operator
            child = [node_data[0], node_data[2], node_data[1], node_data[3]]
            central_child = Node(child)
            if not central_child.on_list(visited_nodes) \
               and not central_child.on_list(frontier_nodes):
                frontier_nodes.append(central_child)

            # left operator
            child = [node_data[1], node_data[0], node_data[2], node_data[3]]
            left_child = Node(child)
            if not left_child.on_list(visited_nodes) \
               and not left_child.on_list(frontier_nodes):
                frontier_nodes.append(left_child)
            

            node.set_children([left_child, central_child, right_child])