コード例 #1
0
ファイル: dijkstra_base.py プロジェクト: dsajanice/sumo
    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
コード例 #2
0
    def test_dikstra_get_nodes_list(self):
        """test the getter of the nodes lists"""

        start = (10, 10)
        end = (20, 10)
        myFlaeche = main.Flaeche(xdim=30, ydim=30, scale=1)
        myD = Dijkstra(myFlaeche, start, end)
        self.assertEqual(myD.get_open_nodes(), [])
        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=5, tt=4, dd=5, lastNode=None)
        myDN_4 = StarNode(xx=5, yy=6, tt=4, dd=5, lastNode=None)

        """get the nodes tuples"""
        myD.open_nodes_list[0:0] = [myDN_1, myDN_2]
        self.assertEqual(
            sorted(myD.get_open_nodes('tuples')), sorted([(3, 3), (4, 3)]))

        """get the node them self """
        self.assertEqual(
            sorted(myD.get_open_nodes()), sorted([myDN_1, myDN_2]))

        """get the node tuples of closed list """
        myD.closed_nodes_list[0:0] = [myDN_3, myDN_4]
        self.assertEqual(
            sorted(myD.get_closed_nodes('tuples')), sorted([(5, 5), (5, 6)]))
コード例 #3
0
    def test_dijkstra_nodes_id_creation(self):
        """test to check the id string and get_coords of a DijkstraNode"""

        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)

        self.assertEqual('3_3', myDN_1.id)
        self.assertEqual((3, 3), myDN_1.get_coords())
コード例 #4
0
    def test_dijkstra_nodes_id_creation(self):
        """test to check the id string and get_coords of a DijkstraNode"""

        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)

        self.assertEqual('3_3', myDN_1.id)
        self.assertEqual((3, 3), myDN_1.get_coords())
コード例 #5
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())
コード例 #6
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