def test_NoCrossingAndWeakCrossing(self): # Testing that the function evaluates the interaction between the paths correctly when they don't cross G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(0, 1), (1, 2), (0, 3)]) # Basically a path graph [3, 0, 1, 2] G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(3, 2)]) # Only interaction is (3, 2) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() dictator = 0 soldier = 1 pin = (3, 1, "s", [0, 3]) pen = (2, 1, "t", [1, 2]) # When both paths don't cross or at least the end nodes don't cross, then the dictator moves first, and nobody # needs to take any extra steps expectedOutput = (True, 0, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput)
def test_rootMeetsCriteriaAlready(self): # Test that a list with just the tuple of root is returned if the root already is a desired node # G_swaps is the graph through which the function will traverse G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4]) G_swaps.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (3, 4)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4]) G_interactions.add_edges_from([(1, 0)]) # Root will be passive # Test when lookForPassive=True inputNode = 0 expectedOutput = [(0, 0, 't', [0])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=True) self.assertEqual(expectedOutput, obtainedOutput) # Test when lookForPassive=False inputNode = 1 # Output is tuple: (node, depth of node, assignedPlaceholder, path root-node) expectedOutput = [(1, 0, 's', [1])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False) self.assertEqual(expectedOutput, obtainedOutput)
def test_returnsAllDesiredNodesAtTheSameLevel(self): # Test that the function returns ALL of the nodes that meet the criteria of being active/passive and that are # on the same level # G_swaps is the graph through which the function will traverse G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4]) G_swaps.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (3, 4)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4]) # 4 and 3 are active and on the same level in G_swaps, 1 and 2 are passive and on the same level in G_swaps G_interactions.add_edges_from([(4, 1), (4, 2), (3, 1)]) # With lookForPassive=True (looking for passive nodes on the same level) inputNode = 0 expectedOutput = [(1, 1, "t", [0, 1]), (2, 1, "t", [0, 2])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=True) self.assertEqual(expectedOutput, obtainedOutput) # With lookForPassive=False (looking for active nodes on the same level) inputNode = 0 expectedOutput = [(3, 2, "s", [0, 1, 3]), (4, 2, "s", [0, 1, 4])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False) self.assertEqual(expectedOutput, obtainedOutput)
def test_NoNeighboursMeetCriteria(self): # Test that an exception is thrown when the function searches and there are no neighbours that meet the criteria # If this function runs and it does not find any nodes as either active or passive, it means that there is no # edge, and the input critic checkingG_interactions() should have caught this # G_swaps is the graph through which the function will traverse G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4]) G_swaps.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (3, 4)]) # No edge will be made in G_interactions G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4]) # Test when lookForPassive=True inputNode = 0 with self.assertRaises( Exception, msg=CONST_gInteractionsHasNoActiveOrPassiveNodesMSG): bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=True) # Test when lookForPassive=False (looking for active nodes inputNode = 0 with self.assertRaises( Exception, msg=CONST_gInteractionsHasNoActiveOrPassiveNodesMSG): bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False)
def test_IncludesPinsAtGreaterDepth(self): # Test that the function includes pins at a greater depth when specified G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 1), (0, 2), (2, 3), (3, 4), (4, 5)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(2, 1), (3, 1), (4, 1), (5, 1)]) # lookForPassive=False, since 2, 3, 4 and 5 are active nodes inputNode = 0 # Test for extraLevelsToConsider = -1. Should just default to 0 expectedOutput = [(2, 1, "s", [0, 2])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False, extraLevelsToConsider=-1) self.assertEqual(expectedOutput, obtainedOutput) # Test for extraLevelsToConsider = 1 expectedOutput = [(2, 1, "s", [0, 2]), (3, 2, "s", [0, 2, 3])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False, extraLevelsToConsider=1) self.assertEqual(expectedOutput, obtainedOutput) # Test for extraLevelsToConsider = 2 expectedOutput = [(2, 1, "s", [0, 2]), (3, 2, "s", [0, 2, 3]), (4, 3, 's', [0, 2, 3, 4])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False, extraLevelsToConsider=2) self.assertEqual(expectedOutput, obtainedOutput) # Test for extraLevelsToConsider = 3 expectedOutput = [(2, 1, "s", [0, 2]), (3, 2, "s", [0, 2, 3]), (4, 3, 's', [0, 2, 3, 4]), (5, 4, "s", [0, 2, 3, 4, 5])] obtainedOutput = bfsCheckingNeighbours(G_swaps, G_interactions, inputNode, lookForPassive=False, extraLevelsToConsider=3) self.assertEqual(expectedOutput, obtainedOutput)
def test_BothITargetAndISourceMove(self): # Testing that the correct output is given when both iTarget and iSource have to move G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 1), (1, 3), (3, 4), (0, 5), (5, 2)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(5, 3), (2, 4)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Finally, we test the output to be correct when both iSource and iTarget have to move once expectedOutput = ((5, 3), (1, 1, 0, 0), ([0, 5], [1, 3], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput) # Now let's make both of them have to move twice G_interactions.remove_edge(5, 3) # Get all the pins again iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # The only interaction left is (2, 4), which is 2 swaps away for iTarget and 2 swaps away for iSource expectedOutput = ((2, 4), (2, 2, 0, 0), ([0, 5, 2], [1, 3, 4], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput)
def test_OnlyITargetMoves(self): # Testing the cases when only iTarget needs to move G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 1), (1, 3), (5, 0), (2, 0), (4, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(0, 3), (0, 4)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Finally, we test the output to be correct when iTarget has to move once expectedOutput = ((0, 3), (0, 1, 0, 0), ([0], [1, 3], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput) # Now let's set it up so iTarget has to move twice G_interactions.remove_edge(0, 3) # Get all the pins again iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # The only interaction left is (0, 4), which is 2 swaps away for iTarget and 0 swaps away for iSource expectedOutput = ((0, 4), (0, 2, 0, 0), ([0], [1, 3, 4], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput)
def test_ITargetAndISourceCrossPaths(self): # Test that the output is correct despite the path of iTarget crossing with iSource and viceversa G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 1), (1, 3), (3, 4), (0, 5), (5, 2)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(3, 5)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget expectedOutput = ((3, 5), (2, 2, 0, -1), ([0, 1, 3], [1, 0, 5], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput)
def test_NoNeedToMoveAtAll(self): # Test whether the function detects properly that neither iSource nor iTarget have to move G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 1), (1, 3), (5, 0), (2, 0), (4, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(0, 1), (5, 1), (0, 3)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Now for the real test expectedOutput = ((0, 1), (0, 0, 0, 0), ([0], [1], True)) obtainedOutput = findBestPinPenCombo(G_swaps, G_interactions, allPinNodes, iSource, iTarget) self.assertEqual(expectedOutput, obtainedOutput)
def test_StrongCrossing(self): # Testing that the function evaluates the interaction between the paths correctly when they strong-cross G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 2), (2, 3), (1, 4), (4, 5), (5, 2)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(3, 2)]) # Only interaction is (3, 2) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() dictator = 0 soldier = 1 pin = (3, 2, 's', [0, 2, 3]) pen = (2, 3, 't', [1, 4, 5, 2]) # When there is a strong crossing, whichever path contains the goal node of the other must go first # For this case here, the dictator must go first expectedOutput = (True, 0, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput) ################################################################################################################ # Now let's set it up so that the soldier must go first G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3, 4, 5]) G_swaps.add_edges_from([(0, 4), (4, 5), (5, 2), (2, 3), (1, 2)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3, 4, 5]) G_interactions.add_edges_from([(3, 2)]) # Only interaction is (3, 2) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() dictator = 1 soldier = 0 pin = (2, 1, 't', [1, 2]) pen = (3, 4, 's', [0, 4, 5, 2, 3]) # When there is a strong crossing, whichever path contains the goal node of the other must go first # For this case here, the dictator must go first expectedOutput = (False, 0, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput)
def test_ContainedTypeII(self): # Test that the function can detect Contained Type II cases # SAME SIDE+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(0, 1), (1, 2), (2, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3]) G_interactions.add_edges_from([(2, 3)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() #DS dictator = 0 soldier = 1 pin = (2, 2, 's', [0, 1, 2]) pen = (3, 2, 't', [1, 2, 3]) # Expecting the situation of Contained Type I, same side expectedOutput = (False, 0, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput) ################################################################################################################ # Alternate to making the dictator go first G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(1, 0), (0, 2), (2, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3]) G_interactions.add_edges_from([(3, 2)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() # SD dictator = 0 soldier = 1 pin = (3, 2, 's', [0, 2, 3]) pen = (2, 2, 't', [1, 0, 2]) # Expecting the situation of Contained Type I, same side expectedOutput = (True, 0, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput) # OPPOSITE SIDE+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(0, 2), (2, 3), (3, 1)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3]) G_interactions.add_edges_from([(3, 2)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() # DS == SD, but we will always make dictator go first dictator = 0 soldier = 1 pin = (3, 2, 's', [0, 2, 3]) pen = (2, 2, 't', [1, 3, 2]) # Expecting the situation of Contained Type I, same side expectedOutput = (True, -1, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput)
def test_ContainedTypeI(self): # Test that the function can detect Contained Type I cases G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(0, 1), (1, 2), (2, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3]) G_interactions.add_edges_from([(3, 2)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() dictator = 1 soldier = 0 pin = (2, 1, 't', [1, 2]) pen = (3, 3, 's', [0, 1, 2, 3]) # Expecting the situation of Contained Type I, same side expectedOutput = (True, 1, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput) ################################################################################################################ # Now let's set up the situation so that the function detects a Contained Type I, opposite side G_swaps = nx.Graph() G_swaps.add_nodes_from([0, 1, 2, 3]) G_swaps.add_edges_from([(0, 2), (2, 1), (1, 3)]) G_interactions = nx.DiGraph() G_interactions.add_nodes_from([0, 1, 2, 3]) G_interactions.add_edges_from([(3, 2)]) iSource = 0 iTarget = 1 closestActiveNodesToSource = bfsCheckingNeighbours( G_swaps, G_interactions, iSource, lookForPassive=False) closestPassiveNodesToTarget = bfsCheckingNeighbours( G_swaps, G_interactions, iTarget, lookForPassive=True) allPinNodes = closestActiveNodesToSource + closestPassiveNodesToTarget # Simulate first section of findBestPinPenCombo(), until getting into the for-loop and findBestPendulumForPin() dictator = 1 soldier = 0 pin = (2, 1, 't', [1, 2]) pen = (3, 3, 's', [0, 2, 1, 3]) # Expecting the situation of Contained Type I, same side expectedOutput = (True, -1, 0) obtainedOutput = evaluatePathsInteraction(G_swaps, pin, pen, dictator, soldier) self.assertEqual(expectedOutput, obtainedOutput)