Beispiel #1
0
def testTspDirK():
    testvals = [
        (';1', 'no'),
        ('a,a,3;1', 3),
        ('a,a,3 ; 1 ', 3),
        ('a,a,3;2', 'no'),
        ('a,b,4;1', 'no'),
        ('a,b,4;2', 'no'),
        ('a,b,4;3', 'no'),
        ('a,b,4 b,a,9;1', 13),
        ('a,b,4 b,a,9;2', 13),
        ('a,b,4 b,a,9;3', 'no'),
        ('a,b,4 b,a,9 b,c,6 c,a,10;2', 13),
        ('a,b,4 b,a,9 b,c,6 c,a,10;3', 20),
        ('a,b,4 b,a,9 b,c,6 c,a,1;2', 11),
        ('a,b,4 b,a,9 b,c,6 c,a,10;4', 'no'),
        ('a,b,4 b,a,9 b,c,6 c,a,10 a,c,2 c,b,3;3', 14),
        ('a,b,4 b,a,9 b,c,6 c,a,10 a,c,2 c,b,3;4', 'no'),
    ]
    for (inString, solution) in testvals:
        val = tspDirK(inString)
        utils.tprint(inString.strip(), ':', val)
        if solution == 'no':
            assert val == solution
        else:
            (graphString, K) = inString.split(';')
            graph = Graph(graphString, directed=True)
            K = int(K)
            cycle = Path.fromString(val)
            dist = graph.cycleLength(cycle)
            assert graph.isCycle(cycle)
            assert len(cycle) >= K
            assert dist == solution
def testConvertDHCtoUHC():
    from uhc import uhc
    from dhc import dhc
    from graph import Path
    instances = [
        '',
        'a,a',
        'a,b',
        'a,b b,a',
        'a,b b,c c,a',
        'a,b b,c a,c',
        'a,b b,c c,d',
        'a,b b,c c,d d,a',
        'a,b b,c c,d a,d',
        'a,b b,c c,d a,d d,b c,a',
    ]
    for instance in instances:
        convertedInstance = convertDHCtoUHC(instance)
        instanceSolution = dhc(instance)
        convertedInstanceSolution = uhc(convertedInstance)
        revertedSolution = revertSolution(convertedInstanceSolution)

        utils.tprint(instance, 'maps to', convertedInstance,\
              ' solutions were: ', instanceSolution, ';', convertedInstanceSolution)
        utils.tprint('revertedSolution', revertedSolution)

        if revertedSolution == 'no':
            assert instanceSolution == 'no'
        else:
            g = Graph(instance, weighted=False)
            path = Path.fromString(revertedSolution)
            # print('g', g, 'path', path)
            assert g.isHamiltonCycle(path) or g.isHamiltonCycle(path.reverse())
Beispiel #3
0
def testTsp():
    testvals = [
        ('a,b,5', 'no', None),
        ('a,b,5 b,c,6 c,d,8 d,a,9 a,c,1 d,b,2', 'a,b,d,c', 16),
        ('a,b,5 b,c,6 c,d,8 d,a,9 a,c,1 d,b,200', 'a,b,c,d', 28),
        ('a,b,5 b,c,6 d,a,9 a,c,1', 'no', None),
    ]
    for (inString, solution, length) in testvals:
        val = tsp(inString)
        utils.tprint(inString, ':', val)
        if solution == 'no':
            assert val == solution
        else:
            g = Graph(inString, weighted=True, directed=False)
            p = Path.fromString(val)
            assert g.isHamiltonCycle(p)
            assert g.cycleLength(p) == length
Beispiel #4
0
def verifyTspD(I, S, H):
    if S == 'no': return 'unsure'
    # extract G,L from I, and convert to correct data types etc.
    (G, L) = I.split(';')
    G = Graph(G, directed=False)
    L = int(L)

    # split the hint string into a list of vertices, which will
    # form a Hamilton cycle of length at most L, if the hint is correct
    cycle = Path.fromString(H)

    # verify the hint is a Hamilton cycle, and has length at most L
    if G.isHamiltonCycle(cycle) and \
               G.cycleLength(cycle) <= L:
        return 'correct'
    else:
        return 'unsure'
Beispiel #5
0
def testUhc():
    testVals = [
        ('', True),
        ('a,b', False),
        ('a,b b,c', False),
        ('a,b b,c c,a', True),
        ('a,b b,c c,a a,d', False),
        ('a,b b,c c,a a,d b,d', True),
        ('aB,aA aB,aC aA,aC', True),
    ]
    for (inString, hasUhc) in testVals:
        result = uhc(inString)
        utils.tprint(inString, ':', result)
        if not hasUhc:
            assert result == 'no'
        else:
            g = Graph(inString, weighted=False, directed=False)
            path = Path.fromString(result)
            assert g.isHamiltonCycle(path)
def tspPath(inString):
    graphStr, source, dest = [x.strip() for x in inString.split(";")]
    graph = Graph(graphStr, weighted=True, directed=False)

    # If there are two vertices, our conversion won't work correctly,
    # so treat this as a special case.
    if len(graph) == 2:
        if graph.containsEdge(Edge([source, dest])):
            return str(Path([source, dest]))
        else:
            return 'no'

    # add an overwhelmingly negative edge from source to dest, thus
    # forcing that to be part of a shortest Ham cycle.
    sumOfWeights = graph.sumEdgeWeights()
    fakeEdge = Edge([source, dest])
    if graph.containsEdge(fakeEdge) == True:
        graph.removeEdge(fakeEdge)
    graph.addEdge(fakeEdge, -sumOfWeights)
    tspSoln = tsp(str(graph))
    # print('graph', graph)
    # print('tspSoln', tspSoln)
    if tspSoln == 'no':
        return 'no'

    # convert from string to Path object
    tspSoln = Path.fromString(tspSoln)

    rotatedSoln = tspSoln.rotateToFront(source)
    if rotatedSoln[-1] != dest:
        # Probably oriented the wrong way (or else fakeEdge wasn't
        # used -- see below). Reverse then rotate source to front
        # again
        rotatedSoln = rotatedSoln.reverse().rotateToFront(source)

    # If dest still isn't at the end of the cycle, then the orginal graph didn't
    # have a Ham path from source to dest (but it did have a Ham
    # cycle -- a cycle that did *not* include fakeEdge).
    if rotatedSoln[-1] != dest:
        return 'no'
    else:
        return str(rotatedSoln)
Beispiel #7
0
def testTspPath():
    testvals = [
        ('a,b,5;a;b', 5),
        ('a,b,5 b,c,6 c,d,8 d,a,9 a,c,1 d,b,2;a;b', 11),
        ('a,b,5 b,c,6 c,d,8 d,a,9 a,c,1 d,b,2 c,e,10 d,e,20;a;b', 33),
        ('a,b,5 b,c,6 d,a,9 a,c,1 d,b,2;a;b', 'no'),
        ('a,b,5 b,c,6 d,a,9 a,c,1;a;b', 'no'),
    ]
    for (inString, solution) in testvals:
        val = tspPath(inString)
        utils.tprint(inString.strip(), ':', val)
        if solution == 'no':
            assert val == solution
        else:
            graphStr, source, dest = [x.strip() for x in inString.split(";")]
            graph = Graph(graphStr, directed=False)
            path = Path.fromString(val)
            dist = graph.pathLength(path)
            assert graph.isHamiltonPath(path)
            assert dist == solution
def verifyTspDPolytime(I, S, H):
    # reject excessively long solutions and hints
    if len(S) > len(I) or len(H) > len(I):
        return 'unsure'
    # The remainder of the program is identical to verifyTspD.py
    # ...
    if S == 'no': return 'unsure'
    # extract G,L from I, and convert to correct data types etc.
    (G, L) = I.split(';')
    G = Graph(G, directed=False)
    L = int(L)

    # split the hint string into a list of vertices, which will
    # form a Hamilton cycle of length at most L, if the hint is correct
    cycle = Path.fromString(H)

    # verify the hint is a Hamilton cycle, and has length at most L
    if G.isHamiltonCycle(cycle) and \
               G.cycleLength(cycle) <= L:
        return 'correct'
    else:
        return 'unsure'
Beispiel #9
0
def testTspDir():
    testvals = [
        ('aC,aB,1 aA,aB,1 aC,aA,1 aB,aA,1 aB,aC,1 aA,aC,1', 3),
        ('', 0),
        ('a,a,3', 3),
        ('a,b,4', 'no'),
        ('a,b,4 b,a,9', 13),
        ('a,b,4 b,a,9 b,c,6 c,a,10', 20),
        ('a,b,4 b,a,9 b,c,6 c,a,10 a,c,2 c,b,3', 14),
        ('a,b,4 b,a,9 b,c,6 c,a,10 a,c,2 c,b,300', 20),
        ('a,b,4 b,a,9 b,c,6 c,a,10 a,c,2 c,b,3 c,d,1', 'no'),
    ]
    for (inString, solution) in testvals:
        val = tspDir(inString)
        utils.tprint(inString.strip(), ':', val)
        if solution == 'no':
            assert val == solution
        else:
            graph = Graph(inString, directed=True)
            cycle = Path.fromString(val)
            dist = graph.cycleLength(cycle)
            assert graph.isHamiltonCycle(cycle)
            assert dist == solution
Beispiel #10
0
def testHalfUhc():
    testvals = [
        ('', 'yes'),
        ('a,b', 'no'),
        ('a,a', 'yes'),
        ('a,b b,c', 'no'),
        ('a,b b,c c,a', 'yes'),
        ('a,b b,c c,a d,e d,f', 'yes'),
        ('a,b b,c c,a d,e f,g', 'no'),
        ('a,b b,c c,a a,d', 'yes'),
        ('a,b b,c c,a a,d b,d', 'yes'),
        ('a,b b,c c,d d,e e,f f,g g,h h,i i,j h,a', 'yes'),
        ('a,b b,c c,d d,e e,f f,g g,h h,i i,j d,a', 'no'),
    ]
    for (inString, solution) in testvals:
        val = halfUhc(inString)
        utils.tprint(inString, ':', val)
        if solution == 'no':
            assert val == solution
        else:
            g = Graph(inString, weighted=False, directed=False)
            path = Path.fromString(val)
            assert g.isCycle(path)
            assert g.cycleLength(path) >= len(g) / 2