Пример #1
0
def main():
    args = get_config()
    url = Node(args.url)
    deep = args.deep
    try:
        LinkScrapper(HtmlParser()).get_urls(url=url, deep=deep, indentation=0)
    except (ConnectionError, MissingSchema) as e:
        print(f'Invalid host: {e}')
Пример #2
0
 def get_urls(self, url, deep, indentation):
     print("- " * indentation, url.value)
     if deep == 0:
         return
     links = self.htmlparser.get_links(url.value)
     for e in links:
         node = Node(e)
         self.get_urls(node, deep - 1, indentation + 1)
         url.add_children(node)
def getBranches(node, T, w1, w2, obs):
    actions = [[w1, w1], [w2, w2], [w1, 0], [0, w1], [w1, w2], [w2, w1],
               [0, w2], [w2, 0]]
    state = node.getState()
    branches = []

    for action in actions:
        new_state, path_array, cost = move(state, action, T, obs)
        if new_state is not None:
            branch_node = Node(new_state, node, action,
                               node.getCost() + cost, path_array)
            branches.append(branch_node)

    return branches
Пример #4
0
def getBranches(node, step_size, goal_state):
    moves = ["1", "2", "3", "4", "5"]
    state = node.getState()
    branches = []
    branches.append(
        Node(move30Clock(state, step_size), node, moves[0],
             node.getCost() + step_size))
    branches.append(
        Node(move30AntiClock(state, step_size), node, moves[1],
             node.getCost() + step_size))
    branches.append(
        Node(move60Clock(state, step_size), node, moves[2],
             node.getCost() + step_size))
    branches.append(
        Node(move60AntiClock(state, step_size), node, moves[3],
             node.getCost() + step_size))
    branches.append(
        Node(moveStraight(state, step_size), node, moves[4],
             node.getCost() + step_size))

    #remove None nodes
    b = [branch for branch in branches if branch.getState() is not None]

    return b
Пример #5
0
def main():

    Parser = argparse.ArgumentParser()
    Parser.add_argument("--InitState", nargs='+', type=int, default= [0, 0], help = 'init state')
    Parser.add_argument("--GoalState", nargs='+', type=int, default= [100, 200], help = 'goal state')
    Parser.add_argument('--SaveFolderName', default='./', help='Default path to store the video')
    Args = Parser.parse_args()

    start_point = Args.InitState
    goal_point = Args.GoalState
    save_folder_name = Args.SaveFolderName

    if (not save_folder_name[-1] == '/'):
        save_folder_name = save_folder_name + '/'
    SaveFileName = save_folder_name + "Dijkstra_path.avi"

    if (isInObstacleSpace(start_point[0], start_point[1])):
        print('Initial state is in obstacle space, please provide new valid state')
        exit()

    if (isInObstacleSpace(goal_point[0], goal_point[1])):
        print('Goal state is in obstacle space, please provide new valid state')
        exit()

    nodes = queue.PriorityQueue()
    init_node = Node(start_point, None, None, 0)
    nodes.put((init_node.getCost(), init_node))
    root2 = np.sqrt(2)

    goal_reached = False

    moves_cost = {'N':1, 'NE':root2, 'E':1, 'SE':root2, 'S':1, 'SW':root2, 'W':1, 'NW':root2}
    node_array = np.array([[Node([i,j], None, None, math.inf) for j in range(300)] for i in range(400)])

    space_size = [300, 400]
    sizey, sizex = space_size
    result = cv2.VideoWriter(SaveFileName,  
                         cv2.VideoWriter_fourcc(*'MJPG'), 
                         300, (sizex, sizey))

    space_map = np.zeros([space_size[0], space_size[1], 3], dtype=np.uint8)
    space_map = updateMapViz(space_map, start_point, [0,0,255])
    space_map = updateMapViz(space_map, goal_point, [0,0,255])
    space_map = addObstacles2Map(space_map)

    full_path = None
    print("Searching................")
    while (not nodes.empty()):

        current_node = nodes.get()[1]
        i, j = current_node.getState()

        space_map = updateMapViz(space_map, current_node.getState(), [0, 255, 0])
        # cv2.imshow('frame',space_map)
        result.write(space_map)
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #     break

        #define moves list
        moves_i = {'N':i, 'NE':i+1, 'E':i+1, 'SE':i+1, 'S':i, 'SW':i-1, 'W':i-1, 'NW':i-1}
        moves_j = {'N':j+1, 'NE':j+1, 'E':j, 'SE':j-1, 'S':j-1, 'SW':j-1, 'W':j, 'NW':j+1}

        if (current_node.getState() == goal_point):
            print('Goal reached')
            print("The cost of path: ", current_node.getCost())
            full_path, node_path = current_node.getFullPath()
            goal_reached = True

            for node in node_path:
                pos = node.getState()
                space_map = updateMapViz(space_map, pos, [0, 0, 255])
                # cv2.imshow('frame',space_map)
                result.write(space_map)
                # if cv2.waitKey(1) & 0xFF == ord('q'):
                #     break
            # keep final frame for 3s
            for i in range(900):
                result.write(space_map)

            # cv2.waitKey() 
            # cv2.destroyAllWindows()
            break
        else:
            # find the moves from the current position
            moves = find_moves(current_node)
            parent_cost = current_node.getCost()
            # iterate through each move and find corresponding child
            for move in moves:
                child_state = [moves_i.get(move), moves_j.get(move)]
                new_cost = parent_cost + moves_cost.get(move)
                # if not visited
                if (node_array[child_state[0], child_state[1]].getCost() == math.inf):
                    child_node = Node(child_state, current_node, move, new_cost)
                    node_array[child_state[0], child_state[1]] = child_node
                    nodes.put((child_node.getCost(), child_node))
                else :
                    if (new_cost < node_array[child_state[0], child_state[1]].getCost()):
                        child_node = Node(child_state, current_node, move, new_cost)
                        node_array[child_state[0], child_state[1]] = child_node
                        nodes.put((child_node.getCost(), child_node))
        
        if (goal_reached): break
def astar():

    h, w = 10, 10
    threshold = 0.5
    start_point = [5, 3, 0]
    goal_state = [9, 9]
    w1, w2 = 5, 10
    nodes = queue.PriorityQueue()
    init_node = Node(start_point, None, None, 0, None)
    nodes.put((init_node.getCost(), init_node))
    traversed_nodes = []

    obs = Obstacle(0.0)
    viz = Visualization(obs)

    fig, ax = plt.subplots(figsize=(10, 10))
    ax.set(xlim=(0, 10), ylim=(0, 10))
    ax = viz.addObstacles2Map(ax)
    ax.set_aspect("equal")

    goal_reached = False
    node_array = np.array([[[math.inf for k in range(360)]
                            for j in range(int(h / threshold))]
                           for i in range(int(w / threshold))])

    full_path = None
    goal_reached = False
    print('Finding path.........')

    while (not nodes.empty()):
        current_node = nodes.get()[1]
        traversed_nodes.append(current_node)

        if checkGoalReached(current_node, goal_state, 1):
            print('Goal reached')
            print("The cost of path: ", current_node.getCost())
            moves, node_path = current_node.getFullPath()

            visualize(viz, traversed_nodes, node_path)

            goal_reached = True

            fp = open('path_points.csv', 'w')
            fn = open('path_nodes.csv', 'w')
            fv = open('vel_points.csv', 'w')
            writer_p = csv.writer(fp)
            writer_n = csv.writer(fn)
            writer_v = csv.writer(fv)

            for move in moves:
                writer_v.writerow(move)

            for node in node_path:
                xi, yi, _ = node.getState()
                writer_n.writerow([xi, yi])

                points = node.getPathArray()
                if points is not None:
                    for point in points:
                        xn, yn = point
                        row = [xn, yn]
                        writer_p.writerow(row)
                        xi, yi = xn, yn
            fp.close()
            fv.close()
            fn.close()

        else:
            branches = getBranches(current_node, 1, w1, w2, obs)
            for branch_node in branches:
                branch_state = branch_node.getState()
                if checkVisited(branch_node,
                                node_array,
                                goal_state,
                                threshold=0.5):
                    node_array[int(halfRound(branch_state[0]) / threshold),
                               int(halfRound(branch_state[1]) / threshold),
                               branch_state[2]] = branch_node.getCost(
                               ) + computeHeuristicCost(
                                   branch_state, goal_state)
                    nodes.put((branch_node.getCost() +
                               computeHeuristicCost(branch_state, goal_state),
                               branch_node))
        if (goal_reached): break
Пример #7
0
def main():

    Parser = argparse.ArgumentParser()
    Parser.add_argument("--InitState",
                        nargs='+',
                        type=int,
                        default=[0, 0, 0],
                        help='init state')
    Parser.add_argument("--GoalState",
                        nargs='+',
                        type=int,
                        default=[350, 250],
                        help='goal state')
    Parser.add_argument("--StepSize",
                        type=int,
                        default=10,
                        help='Step size: 1-10')
    Parser.add_argument('--SaveFolderName',
                        default='./',
                        help='Default path to store the video')
    Args = Parser.parse_args()

    init_state = Args.InitState
    goal_state = Args.GoalState
    step_size = Args.StepSize
    save_folder_name = Args.SaveFolderName
    if (not save_folder_name[-1] == '/'):
        save_folder_name = save_folder_name + '/'
    SaveFileName = save_folder_name + "AStar_path.avi"

    if (isInvalidInput(init_state[0], init_state[1])):
        print(
            'Initial state is in obstacle space, please provide new valid state'
        )
        exit()

    if (isInvalidInput(goal_state[0], goal_state[1])):
        print(
            'Goal state is in obstacle space, please provide new valid state')
        exit()

    h = 300
    w = 400
    space_size = [h, w]
    threshold = 0.5
    step_angle = 30

    #visited = np.zeros((int(400/threshold),int(300/threshold),int(360/step_angle)), dtype='int')

    start_point = init_state
    goal_state = goal_state

    result = cv2.VideoWriter(SaveFileName, cv2.VideoWriter_fourcc(*'MJPG'),
                             300, (sizex, sizey))

    nodes = queue.PriorityQueue()
    init_node = Node(start_point, None, None, 0)

    nodes.put((init_node.getCost(), init_node))

    root2 = np.sqrt(2)

    goal_reached = False
    node_array = np.array([[[math.inf for k in range(12)]
                            for j in range(int(300 / threshold))]
                           for i in range(int(400 / threshold))])

    space_size = [300, 400]
    space_map = np.zeros([space_size[0], space_size[1], 3], dtype=np.uint8)
    space_map = updateMap(space_map, init_node, [0, 0, 255])
    space_map = addObstacles2Map(space_map)

    full_path = None
    goal_reached = False

    print('Finding path.........')

    while (not nodes.empty()):
        current_node = nodes.get()[1]
        space_map = updateMap(space_map, current_node, [0, 255, 0])
        result.write(space_map)
        # cv2.imshow('frame',space_map)
        # # cv2.waitKey()
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #     break

        if checkGoalReached(current_node, goal_state, 5):
            print('Goal reached')
            print("The cost of path: ", current_node.getCost())
            full_path, node_path = current_node.getFullPath()
            goal_reached = True

            for node in node_path:
                space_map = updateMap(space_map, node, [0, 0, 255])
                result.write(space_map)

            # keep final frame for 3s
            for i in range(900):
                result.write(space_map)
            #     cv2.imshow('frame',space_map)
            #     if cv2.waitKey(1) & 0xFF == ord('q'):
            #         break
            # cv2.waitKey()
        else:
            branches = getBranches(current_node, step_size, goal_state)
            for branch_node in branches:

                branch_state = branch_node.getState()
                if checkVisited(branch_node,
                                node_array,
                                goal_state,
                                threshold=0.5,
                                thetaStep=30):
                    node_array[int(halfRound(branch_state[0]) / threshold),
                               int(halfRound(branch_state[1]) / threshold),
                               int(halfRound(branch_state[2]) /
                                   30)] = branch_node.getCost(
                                   ) + computeHeuristicCost(
                                       branch_state, goal_state)
                    nodes.put((branch_node.getCost() +
                               computeHeuristicCost(branch_state, goal_state),
                               branch_node))

        if (goal_reached): break

    cv2.destroyAllWindows()
    print(
        'Goal Reached. Please check the video in the current directory or the path provided as input'
    )
Пример #8
0
 def test_get_urls_invalid_host(self):
     url = Node(value='https://www.google.')
     with self.assertRaises(ConnectionError):
         LinkScrapper(HtmlParser()).get_urls(url=url, deep=3, indentation=0)
Пример #9
0
 def test_get_urls_ok(self):
     thing = HtmlParser()
     thing.get_links = Mock(side_effect=st.url_mock)
     url = Node(value='google.es')
     LinkScrapper(thing).get_urls(url=url, deep=3, indentation=0)