Skip to content

Exploring edges differs in QueryGraph #163

Closed

Description

@kschrab

I think there's a bug in QueryGraph. Until now it is not critical, since it would be only a problem for edge-based algorithms (see #158).

Consider one directional edge A->B. If I want to get all outgoing edges of A, I would use an EdgeExplorer with an EdgeFilter and would get (edgeId: 0, baseNode: A, adjNode: B). If I would like to obtain all incoming edges of B, I would retrieve the same edge with inverted nodes: (edgeId: 0, baseNode: B, adjNode: A). Note, the edge ID remains the same which is from crucial importance for edge-based routing algorithms!

However, using a QueryGraph and its VirtualEdgeIterator results in a different behavior. Consider two virtual nodes C and D which are directly on edge 0 (A--C-D-->B). When I request all outgoing edges from node C, I get (edgeId: x, baseNode: C, adjNode: D), however, when I request all incoming edges from node D I get (edgeId: y, baseNode: D, adjNode C), which is in my eyes wrong since the edgeId differs.

I think the problem is, that QueryGraph stores to much additional edges. In this specific examples, eight additional edges have been added for both virtual nodes:
four edges for C: (A-C, C-A, C-D, D-C) and for edges for D: (C-D,D-C,D-B,B-D), which not only have irrelevant edges for backward direction (A-C vs. C-A), but also duplicate edges (C-D and D-C).

IMHO, the following (new) test should pass: (to be added to QueryGraphTest)

    @Test
    public void testIterationBug()
    {
        //setup
        EncodingManager encodingManager = new EncodingManager("CAR");
        Graph g = new GraphHopperStorage(new RAMDirectory(), encodingManager).create(100);

        EdgeFilter outEdgeFilter = new DefaultEdgeFilter(encodingManager.getEncoder("CAR"), false, true);
        EdgeFilter inEdgeFilter = new DefaultEdgeFilter(encodingManager.getEncoder("CAR"), true, false);
        EdgeExplorer inExplorer = g.createEdgeExplorer(inEdgeFilter);
        EdgeExplorer outExplorer = g.createEdgeExplorer(outEdgeFilter);

        int nodeA = 0;
        int nodeB = 1;

        /* 
         * init test graph: one directional edge going from A to B, via virtual nodes C and D
         * 
         *   (C)-(D)
         *  /       \
         * A         B
         * 
         */
        g.setNode(nodeA, 1, 0);
        g.setNode(nodeB, 1, 10);
        g.edge(nodeA, nodeB, 10, false).setWayGeometry(Helper.createPointList(1.5, 3, 1.5, 7));

        //assert the behavior for classic edgeIterator        
        assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeA, nodeB);

        //setup query results
        EdgeIterator it = outExplorer.setBaseNode(0);
        it.next();
        QueryResult res1 = createLocationResult(1.5, 3, it, 0, Position.EDGE);
        QueryResult res2 = createLocationResult(1.5, 7, it, 0, Position.EDGE);

        QueryGraph q = new QueryGraph(g);
        q.lookup(Arrays.asList(res1, res2));
        int nodeC = res1.getClosestNode();
        int nodeD = res2.getClosestNode();

        inExplorer = q.createEdgeExplorer(inEdgeFilter);
        outExplorer = q.createEdgeExplorer(outEdgeFilter);

        //assert the same behavior for queryGraph
        assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeA, nodeC);
        assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeC, nodeD);
        assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeC, nodeB);
    }

    private void assertEdgeIdsStayingEqual( EdgeExplorer inExplorer, EdgeExplorer outExplorer, int startNode, int endNode )
    {
        EdgeIterator it = outExplorer.setBaseNode(startNode);
        it.next();
        assertEquals(startNode, it.getBaseNode());
        assertEquals(endNode, it.getAdjNode());
        int expectedEdgeId = it.getEdge(); //we expect the edge id to be the same when exploring in backward direction
        assertFalse(it.next());

        //backward iteration, edge id should remain the same!!
        it = inExplorer.setBaseNode(endNode);
        it.next();
        assertEquals(endNode, it.getBaseNode());
        assertEquals(startNode, it.getAdjNode());
        assertEquals("The edge id is not the same,", expectedEdgeId, it.getEdge());
        assertFalse(it.next());
    }

Want to back this issue? Place a bounty on it! We accept bounties via Bountysource.

Activity

  1. karussell commented on Mar 17, 2014

    @karussell
    Member

    Thanks! Indeed this behaviour is different for QueryGraph. Why do we need this behaviour for edge based algorithm? To avoid exploration of the same edge twice? This happens for current node based algos too.

    Or did you encounter a bug due to this?

    BTW: a really nice bug report with the unit tests :) !!

  2. kschrab commented on Mar 17, 2014

    @kschrab
    ContributorAuthor

    The bi-directional edge-based algorithms would not work properly with the behavior in QueryGraph due to different edge IDs for same edges when using different edge filters. Node-based algorithms do not have a problem with this behavior since they use IDs of nodes to store costs which are still the same. Edge-based algorithms would use the edge ID to store costs and therefore the IDs should be the same as long as it is functionally the same edge.
    In my pull request #158 there are tests for bi-directional Dijkstra and A* which do not pass due to this behavior (I turned off the tests temporary, e.g. see DijkstraBidirectionRefTest#testViaEdges_FromEqualsTo()).

  3. karussell commented on Mar 18, 2014

    @karussell
    Member

    Ah, now I understand what you mean. So this happens only if two virtual nodes are on the same edge. And yes there are too many virtual edges created, but this way it was relative easy to implement (associating id to virtual edge). I'll see how to fix it.

  4. added this to the 0.4 milestone on Jul 18, 2014
  5. self-assigned this
    on Jul 18, 2014
  6. added a commit that references this issue on Jul 18, 2014
    6fc81c9
  7. karussell commented on Jul 18, 2014

    @karussell
    Member

    Just assigning the same edgeIds to the virtual edges made the test pass (including minor fix to test). See 6fc81c9 and indeed this fixes the tests for dijkstrabi and astarbi (except the one where you were unsure :))

  8. karussell commented on Jul 18, 2014

    @karussell
    Member

    BTW: the list of virtual edge is as follows (assuming your graph): A->C, C->A, C->D, D->C, C->D, D->C, D->B, B->D. We duplicate the references for C->D and C->D to get the same behaviour as with entries on different edges. So in easy words: for every virtual node we create 4 new virtual edges. But now those 4 entries will get only 2 different ids: maxEdgeId+0 and maxEdgeId+2.

  9. added a commit that references this issue on Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions