Example #1
0
    def test_dijsktra_run_and_rebuild(self):
        """run the algorithm on a simple example"""
        visual = VISUAL
        visual = True
        myFlaeche = main.Flaeche(
            xdim=300, ydim=300, scale=10, output='result_hindrance_punctual')
        myD = Dijkstra(myFlaeche, (0, 0), (10, 10))
        myD.run()
        myD.rebuild_path()
        path = DNList(myD.path, 'tuples').get_tuples()
        self.assertEqual(path, [(0, 0), (1, 1), (2, 2), (3, 3),
                                (4, 4), (5, 5), (6, 6), (7, 7),
                                (8, 8), (9, 9), (10, 10)])
        if visual:
            myD.draw_path()
        del(myD)

        myD = Dijkstra(myFlaeche, (0, 0), (10, 10))
        self.assertRaisesRegexp(
            StandardError, "algorithm must be run first successfully", myD.rebuild_path)

        del(myD)
        blocked_nodes = [(xx, 15) for xx in range(5, 25)]
        myFlaeche.load_node_data(blocked_nodes)
        myD = Dijkstra(myFlaeche, (3, 11), (16, 19))
        myD.run()
        myD.rebuild_path()
        self.assertEqual(DNList(myD.path, 'tuples').get_tuples(),
                         [(3, 11), (4, 12), (4, 13), (4, 14), (4, 15),
                          (5, 16), (6, 16), (7, 16), (8, 16), (9, 16),
                          (10, 16), (11, 17), (12, 17), (13, 18),
                          (14, 18), (15, 18), (16, 19)])

        if visual:
            myD.draw_path()
Example #2
0
    def test_dijsktra_step_internals(self):
        """ Test for dijkstra's results after the first step        """
        """test if the start point is added to the open node list"""
        myFlaeche = main.Flaeche(xdim=300, ydim=300, scale=10)
        myD_step_zero = Dijkstra(myFlaeche, (0, 0), (20, 10))
        myD_step_zero.step()
        self.assertEqual(myD_step_zero.get_open_nodes('tuples'), [(0, 0)])
        self.assertEqual(myD_step_zero.get_closed_nodes('tuples'), [])
        """ test if nodes are added to the closed list """
        myFlaeche = main.Flaeche(xdim=300, ydim=300, scale=10)
        myD = Dijkstra(myFlaeche, (0, 0), (20, 10))
        myD.step()
        myD.step(visual=False)
        self.assertEqual(myD.get_open_nodes('tuples'), [(0, 1), (1, 0),
                                                        (1, 1)])
        self.assertEqual(myD.get_closed_nodes('tuples'), [(0, 0)])
        """test that nodes in the closed nodes list are omitted"""
        myD_fake_closed = Dijkstra(myFlaeche, (0, 0), (20, 10))

        myDN_1 = StarNode(xx=0, yy=0, tt=4, dd=5, lastNode=None)
        myDN_2 = StarNode(xx=1, yy=1, tt=4, dd=5, lastNode=None)

        myD_fake_closed.open_nodes_list.append(myDN_1)
        myD_fake_closed.closed_nodes_list.append(myDN_2)
        # should find myDN_2 as suspicious node
        # and reject as it alrerady is in closed node list
        myD_fake_closed.step()
        self.assertEqual(myD_fake_closed.get_open_nodes('tuples'), [(0, 1),
                                                                    (1, 0)])
        """test that nodes in the open lists are updated"""
        myD_fake_open = Dijkstra(myFlaeche, (0, 0), (20, 10))

        myDN_0 = StarNode(xx=1, yy=1, tt=400, dd=500, lastNode=None)
        myDN_1 = StarNode(xx=0, yy=0, tt=4, dd=5, lastNode=None)

        myD_fake_open.open_nodes_list.append(myDN_0)
        myD_fake_open.open_nodes_list.append(myDN_1)
        # should now take myDN_1 as next node
        # and finding a shorter path to myDN_0
        # and should update the open list after the step
        self.assertEqual(sorted(myD_fake_open.get_open_nodes('tuples')),
                         [(0, 0), (1, 1)])
        long_way = DNList(myD_fake_open.open_nodes_list).get_by_tuple(
            (1, 1)).full_costs
        """test the unfished return value"""
        self.assertFalse(myD_fake_open.step())
        short_way = DNList(myD_fake_open.open_nodes_list).get_by_tuple(
            (1, 1)).full_costs
        self.assertGreater(long_way, short_way)
        """test that the algo stops if the end is reached - manipulated lists"""
        myD_fake_end = Dijkstra(myFlaeche, (0, 0), (20, 10))

        myDN_1 = StarNode(xx=20, yy=10, tt=4, dd=0, lastNode=None)
        myDN_2 = StarNode(xx=12, yy=10, tt=4, dd=7, lastNode=None)

        # dest point in open and the shortest
        myD_fake_end.open_nodes_list.append(myDN_1)
        myD_fake_end.open_nodes_list.append(myDN_2)  # some point the longest
        self.assertTrue(myD_fake_end.step())
Example #3
0
    def step(self, visual=False):
        self.iteration_step += 1

        # first step ever
        if len(self.open_nodes_list) == 0 and len(self.closed_nodes_list) == 0:
            self.open_nodes_list.append(
                StarNode(
                    self.start[0], self.start[1], 0,
                    self.get_distance_between_points((self.start), (self.end)),
                    None))
            return False  # Algorithm is not jet finished

        current_node = DNList(self.open_nodes_list).get_min_node(pop=True)

        # even if the dest. node has been seen before,
        # the path is not prooven to be the shortest

        # until it has been teared from the open list
        if current_node.get_coords() == self.end:
            self.reached_dest_node = current_node
            return True  # finished / found

        # only coords are returned
        suspicious_nodes = self.flaeche.get_neighbours(
            current_node.get_coords())
        for nn in suspicious_nodes:
            # is in closed_list -> ignore
            closedDNL = DNList(self.closed_nodes_list, 'tuple')
            if not nn in closedDNL:

                # skip if blocked
                if self.flaeche.is_blocked(nn):
                    continue

                sus_node = StarNode(
                    nn[0],
                    nn[1],
                    tt=current_node.tt + self.get_distance_between_points(
                        current_node.get_coords(), nn),
                    dd=self.get_distance_between_points(nn, (self.end)),
                    lastNode=current_node)

                # is in open_list -> event. update open list
                openDNL = DNList(self.open_nodes_list, 'tuple')
                # returns None if not in list
                some_open_node = openDNL.get_by_tuple(nn)
                if some_open_node != None:
                    if sus_node.full_costs < some_open_node.full_costs:
                        self.open_nodes_list.remove(some_open_node)
                        self.open_nodes_list.append(sus_node)
                # neigther nor -> append to open list
                else:
                    self.open_nodes_list.append(sus_node)
                del (some_open_node)
        self.closed_nodes_list.append(current_node)

        if visual:
            self.vis_debug(self.iteration_step)

        return False  # not jet finshed
Example #4
0
 def draw_open_closed(self, step=None):
     self.flaeche.vis_reset()
     self.flaeche.vis_add_start(self.start)
     self.flaeche.vis_add_end(self.end)
     for nn in DNList(self.open_nodes_list).get_tuples():
         self.flaeche.vis_add_open(nn)
     del(nn)
     for nn in DNList(self.closed_nodes_list).get_tuples():
         self.flaeche.vis_add_closed(nn)
     for xx in range(self.flaeche.cluster_length_x):
         for yy in range(self.flaeche.cluster_length_y):
             if self.flaeche.cluster[xx][yy] is not None:
                 if self.flaeche.cluster[xx][yy][NodeDataHandler.is_blocked]:
                     self.flaeche.vis_add_blocked((xx, yy))
     self.flaeche.vis_show(step_num=step)
Example #5
0
    def test_wiki(self):
        visual = VISUAL
        #        visual = True
        """run the aglo like in the wikipedia example"""
        myFlaeche = main.Flaeche(xdim=300,
                                 ydim=300,
                                 scale=10,
                                 output='result_mediawiki_example')
        myD = Dijkstra(myFlaeche, (17, 3), (3, 17))

        blocked_nodes = [(xx, 7) for xx in range(4, 16)]
        blocked_nodes[0:0] = [(xx, 8) for xx in range(4, 16)]
        blocked_nodes[0:0] = [(xx, 9) for xx in range(4, 16)]
        blocked_nodes[0:0] = [(xx, 10) for xx in range(13, 16)]
        blocked_nodes[0:0] = [(xx, 11) for xx in range(13, 16)]
        blocked_nodes[0:0] = [(xx, 12) for xx in range(13, 16)]
        blocked_nodes[0:0] = [(xx, 13) for xx in range(13, 16)]
        blocked_nodes[0:0] = [(xx, 14) for xx in range(13, 16)]

        myFlaeche.load_node_data(blocked_nodes)
        myD = Dijkstra(myFlaeche, (3, 19), (18, 3))
        myD.run(visual=visual)
        myD.rebuild_path()
        self.assertEqual(
            DNList(myD.path, 'tuples').get_tuples(),
            [(3, 19), (4, 18), (5, 17), (6, 16), (7, 15), (8, 15), (9, 15),
             (10, 15), (11, 15), (12, 15), (13, 15), (14, 15), (15, 15),
             (16, 14), (16, 13), (16, 12), (16, 11), (16, 10), (16, 9),
             (16, 8), (16, 7), (17, 6), (17, 5), (17, 4), (18, 3)])

        if visual:
            myD.draw_path(final=True)
            main.make_movie(myFlaeche.output)
Example #6
0
 def draw_path(self, final=False):
     self.flaeche.vis_reset()
     self.flaeche.vis_add_start(self.start)
     self.flaeche.vis_add_end(self.end)
     for nn in DNList(self.open_nodes_list).get_tuples():
         self.flaeche.vis_add_open(nn)
     del (nn)
     for nn in DNList(self.closed_nodes_list).get_tuples():
         self.flaeche.vis_add_closed(nn)
     del (nn)
     for xx in range(self.flaeche.cluster_length_x):
         for yy in range(self.flaeche.cluster_length_y):
             if self.flaeche.cluster[xx][yy] != None:
                 if self.flaeche.cluster[xx][yy][
                         NodeDataHandler.is_blocked]:
                     self.flaeche.vis_add_blocked((xx, yy))
     for nn in DNList(self.path).get_tuples():
         self.flaeche.vis_add_path(nn)
     if final:
         self.flaeche.vis_show(step_num=self.iteration_step)
     else:
         self.flaeche.vis_show()
    def step(self, visual=False):
        self.iteration_step += 1

        # first step ever
        if len(self.open_nodes_list) == 0 and len(self.closed_nodes_list) == 0:
            self.open_nodes_list.append(StarNode(self.start[0], self.start[1],
                                                 0, self.get_distance_between_points((self.start),
                                                                                     (self.end)),
                                                 None))
            return False    # Algorithm is not jet finished

        current_node = DNList(self.open_nodes_list).get_min_node(pop=True)

        # even if the dest. node has been seen before,
        # the path is not prooven to be the shortest

        # until it has been teared from the open list
        if current_node.get_coords() == self.end:
            self.reached_dest_node = current_node
            return True  # finished / found

        # only coords are returned
        suspicious_nodes = self.flaeche.get_neighbours(
            current_node.get_coords())
        for nn in suspicious_nodes:
            # is in closed_list -> ignore
            closedDNL = DNList(self.closed_nodes_list, 'tuple')
            if not nn in closedDNL:

                # skip if blocked
                if self.flaeche.is_blocked(nn):
                    continue

                sus_node = StarNode(nn[0], nn[1],
                                    tt=current_node.tt +
                                    self.get_distance_between_points(
                                        current_node.get_coords(), nn),
                                    dd=self.get_distance_between_points(
                                        nn, (self.end)),
                                    lastNode=current_node)

                # is in open_list -> event. update open list
                openDNL = DNList(self.open_nodes_list, 'tuple')
                # returns None if not in list
                some_open_node = openDNL.get_by_tuple(nn)
                if some_open_node != None:
                    if sus_node.full_costs < some_open_node.full_costs:
                        self.open_nodes_list.remove(some_open_node)
                        self.open_nodes_list.append(sus_node)
                # neigther nor -> append to open list
                else:
                    self.open_nodes_list.append(sus_node)
                del(some_open_node)
        self.closed_nodes_list.append(current_node)

        if visual:
            self.vis_debug(self.iteration_step)

        return False  # not jet finshed
    def test_dijkstra_nodes_basic_funtionality(self):
        """testing init, get_min,  the home brewn node list """

        """test the ways of initialiation of DNLists"""
        myFlaeche = main.Flaeche(xdim=30, ydim=30, scale=1)
        myD = Dijkstra(myFlaeche, start_node=(0, 0), end_node=(20, 10))

        myDN_1 = StarNode(xx=3, yy=3, tt=4, dd=5, lastNode=None)
        myDN_2 = StarNode(xx=4, yy=3, tt=4, dd=5, lastNode=None)
        myDN_3 = StarNode(xx=5, yy=3, tt=4, dd=5, lastNode=None)
        myDN_4 = StarNode(xx=20, yy=9, tt=2, dd=1, lastNode=None)

        myDD_1 = DNList([myDN_1, myDN_2, myDN_3])
        myDD_2 = DNList([myDN_1, myDN_2, myDN_3])

        myDNL = DNList([myDN_1, myDN_2, myDN_3])
        self.assertEqual([ii for ii in myDNL], [myDN_1, myDN_2, myDN_3])

        """insert a few nodes into the list, and iter over them by id"""
        myDNL_id_1 = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual([ii for ii in myDNL_id_1], ['3_3', '4_3', '5_3'])

        """alter the order of the few nodes , and iter over them by id"""
        myDNL_id_2 = DNList([myDN_3, myDN_2, myDN_1], 'id')
        self.assertEqual([ii for ii in myDNL_id_2], ['5_3', '4_3', '3_3'])

        """insert a few nodes into the list, and iter over them by tuple"""
        myDNL_tuple = DNList([myDN_1, myDN_2, myDN_3], 'tuple')
        self.assertEqual([ii for ii in myDNL_tuple], [(3, 3), (4, 3), (5, 3)])

        """return a node by its id, if not exists return none """
        myDNL_ret_id = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual(myDNL_ret_id.get_by_id('3_3'), myDN_1)
        self.assertIsNone(myDNL_ret_id.get_by_id('5_5'))

        """return a node by its tuple, if not exists return none """
        myDNL_ret_tup = DNList([myDN_1, myDN_2, myDN_3], 'tuple')
        self.assertEqual(myDNL_ret_id.get_by_tuple((3, 3)), myDN_1)
        self.assertIsNone(myDNL_ret_id.get_by_tuple((5, 5)))

        """return a node by its tuple, even if the DNodeList iters on ids """
        myDNL_ret_tup = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual(myDNL_ret_id.get_by_tuple((3, 3)), myDN_1)

        """test to retrive the minimal node from List"""
        myDNL_get_min = DNList([myDN_1, myDN_2, myDN_3, myDN_4])
        self.assertEqual(myDNL_get_min.get_min_node(), myDN_4)

        """test to retrive the minimal node and remove it from the list"""
        myDNL_pop_min = DNList([myDN_1, myDN_2, myDN_3, myDN_4])
        self.assertEqual(myDNL_pop_min.get_min_node(pop=True), myDN_4)
        self.assertNotEqual(myDNL_pop_min.get_min_node(), myDN_4)

        """test to get back the tuples of all nodes in the list"""
        myDNL_tup_list = DNList([myDN_1, myDN_2, myDN_3])
        self.assertEqual(myDNL_tup_list.get_tuples(), [(3, 3), (4, 3), (5, 3)])

        """test to see what happens if the node list is empty"""

        """make sure that only each node id is only once in the list
           should be already managed in the initalization
           keep a 'global'/class list with all nodes
           
           * handling alternation after creation - maybe function_closurures
           * overwriting the append function
           
           """

        """make sure that only nodes from the same Dijkstra are added"""
        # currently there is only one Dijkstra at a time

        """test to make sure, the list only takes dijkstra nodes"""
        # this does not work - not too urgent now
        # self.assertRaises(AssertionError, Dijkstra.DNList, [myDN_1, myDN_2, 1] )

        """insert a few nodes into the list, and iter over them returning 
           the distance traveled"""  # to be done when needed

        """insert a few nodes into the list, and iter over them returning 
Example #9
0
    def test_dijkstra_nodes_basic_funtionality(self):
        """testing init, get_min,  the home brewn node list """
        """test the ways of initialiation of DNLists"""
        myFlaeche = main.Flaeche(xdim=30, ydim=30, scale=1)
        myD = Dijkstra(myFlaeche, start_node=(0, 0), end_node=(20, 10))

        myDN_1 = StarNode(xx=3, yy=3, tt=4, dd=5, lastNode=None)
        myDN_2 = StarNode(xx=4, yy=3, tt=4, dd=5, lastNode=None)
        myDN_3 = StarNode(xx=5, yy=3, tt=4, dd=5, lastNode=None)
        myDN_4 = StarNode(xx=20, yy=9, tt=2, dd=1, lastNode=None)

        myDD_1 = DNList([myDN_1, myDN_2, myDN_3])
        myDD_2 = DNList([myDN_1, myDN_2, myDN_3])

        myDNL = DNList([myDN_1, myDN_2, myDN_3])
        self.assertEqual([ii for ii in myDNL], [myDN_1, myDN_2, myDN_3])
        """insert a few nodes into the list, and iter over them by id"""
        myDNL_id_1 = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual([ii for ii in myDNL_id_1], ['3_3', '4_3', '5_3'])
        """alter the order of the few nodes , and iter over them by id"""
        myDNL_id_2 = DNList([myDN_3, myDN_2, myDN_1], 'id')
        self.assertEqual([ii for ii in myDNL_id_2], ['5_3', '4_3', '3_3'])
        """insert a few nodes into the list, and iter over them by tuple"""
        myDNL_tuple = DNList([myDN_1, myDN_2, myDN_3], 'tuple')
        self.assertEqual([ii for ii in myDNL_tuple], [(3, 3), (4, 3), (5, 3)])
        """return a node by its id, if not exists return none """
        myDNL_ret_id = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual(myDNL_ret_id.get_by_id('3_3'), myDN_1)
        self.assertIsNone(myDNL_ret_id.get_by_id('5_5'))
        """return a node by its tuple, if not exists return none """
        myDNL_ret_tup = DNList([myDN_1, myDN_2, myDN_3], 'tuple')
        self.assertEqual(myDNL_ret_id.get_by_tuple((3, 3)), myDN_1)
        self.assertIsNone(myDNL_ret_id.get_by_tuple((5, 5)))
        """return a node by its tuple, even if the DNodeList iters on ids """
        myDNL_ret_tup = DNList([myDN_1, myDN_2, myDN_3], 'id')
        self.assertEqual(myDNL_ret_id.get_by_tuple((3, 3)), myDN_1)
        """test to retrive the minimal node from List"""
        myDNL_get_min = DNList([myDN_1, myDN_2, myDN_3, myDN_4])
        self.assertEqual(myDNL_get_min.get_min_node(), myDN_4)
        """test to retrive the minimal node and remove it from the list"""
        myDNL_pop_min = DNList([myDN_1, myDN_2, myDN_3, myDN_4])
        self.assertEqual(myDNL_pop_min.get_min_node(pop=True), myDN_4)
        self.assertNotEqual(myDNL_pop_min.get_min_node(), myDN_4)
        """test to get back the tuples of all nodes in the list"""
        myDNL_tup_list = DNList([myDN_1, myDN_2, myDN_3])
        self.assertEqual(myDNL_tup_list.get_tuples(), [(3, 3), (4, 3), (5, 3)])
        """test to see what happens if the node list is empty"""
        """make sure that only each node id is only once in the list
           should be already managed in the initalization
           keep a 'global'/class list with all nodes

           * handling alternation after creation - maybe function_closurures
           * overwriting the append function

           """
        """make sure that only nodes from the same Dijkstra are added"""
        # currently there is only one Dijkstra at a time
        """test to make sure, the list only takes dijkstra nodes"""
        # this does not work - not too urgent now
        # self.assertRaises(AssertionError, Dijkstra.DNList, [myDN_1, myDN_2, 1] )

        """insert a few nodes into the list, and iter over them returning
           the distance traveled"""  # to be done when needed

        """insert a few nodes into the list, and iter over them returning
Example #10
0
 def draw_closed(self):
     self.flaeche.vis_reset()
     for nn in DNList(self.closed_nodes_list).get_tuples():
         self.flaeche.vis_add_closed(nn)
     self.flaeche.vis_show()
Example #11
0
 def draw_open(self):
     self.flaeche.vis_reset()
     for nn in DNList(self.open_nodes_list).get_tuples():
         self.flaeche.vis_add_open(nn)
     self.flaeche.vis_show()