示例#1
0
 def testBiDirectionalBothDynamic(self, _, seed):
     """
     Find shortest path of simple test digraph using the BiDirectional
     algorithm for a range of seeds.
     Note the first argument is required to work using parameterized and unittest.
     """
     bidirec = BiDirectional(self.G,
                             self.max_res,
                             self.min_res,
                             REF_forward=self.custom_REF_forward,
                             REF_backward=self.custom_REF_backward,
                             seed=seed)
     # Check classification
     with self.assertLogs('cspy.algorithms.bidirectional') as cm:
         bidirec.name_algorithm()
     # Log should contain the word 'dynamic'
     self.assertRegex(cm.output[0], 'dynamic')
     # Check exception for not running first
     with self.assertRaises(Exception) as context:
         bidirec.path
     self.assertTrue("run()" in str(context.exception))
     # Run and test results
     bidirec.run()
     path = bidirec.path
     cost = bidirec.total_cost
     total_res = bidirec.consumed_resources
     self.assertEqual(path, ['Source', 1, 2, 3, 4, 'Sink'])
     self.assertEqual(cost, -23)
     self.assertTrue(all(total_res == [5, 30, 1]))
示例#2
0
    def testBiDirectionalBackward(self):
        """
        Find shortest path of simple test digraph using the BiDirectional
        algorithm with only backward direction.
        """
        bidirec = BiDirectional(self.G,
                                self.max_res,
                                self.min_res,
                                direction='backward')
        # Check classification
        with self.assertLogs('cspy.algorithms.bidirectional') as cm:
            bidirec.name_algorithm()
        # Log should contain the word 'backward'
        self.assertRegex(cm.output[0], 'backward')

        bidirec.run()
        path = bidirec.path
        cost = bidirec.total_cost
        total_res = bidirec.consumed_resources
        # Check path and other attributes
        self.assertEqual(path, ['Source', 2, 5, 'Sink'])
        self.assertEqual(cost, 1)
        self.assertTrue(all(total_res == [3, 3]))
        self.assertTrue(all(e in self.G.edges() for e in zip(path, path[1:])))
        self.assertEqual(self.max_res, [len(self.G.edges()), 6])
        self.assertEqual(self.min_res, [0, 0])
示例#3
0
文件: tests.py 项目: aashish24/cspy
 def testBothDirections(self):
     # Find shortest path of simple test digraph
     alg_obj = BiDirectional(self.G, self.max_res, self.min_res)
     with self.assertLogs('cspy.algorithms.bidirectional') as cm:
         alg_obj.name_algorithm(U=self.max_res[0], L=self.min_res[0])
     self.assertRegex(cm.output[0], 'dynamic')
     path = alg_obj.run()
     self.assertEqual(path, ['Source', 'A', 'B', 'C', 'Sink'])
示例#4
0
文件: tests.py 项目: aashish24/cspy
 def testBackward(self):
     # Find shortest path of simple test digraph
     alg_obj = BiDirectional(self.G,
                             self.max_res, [-1, 0],
                             direction='backward')
     with self.assertLogs('cspy.algorithms.bidirectional') as cm:
         alg_obj.name_algorithm()
     self.assertRegex(cm.output[0], 'backward')
     path = alg_obj.run()
     self.assertEqual(path, ['Source', 'A', 'B', 'C', 'Sink'])
示例#5
0
    def testBiDirectionalBothDynamic(self, _, seed):
        """
        Find shortest path of simple test digraph using BiDirectional.
        """
        bidirec = BiDirectional(self.G, self.max_res, self.min_res, seed=seed)
        # Check classification
        with self.assertLogs('cspy.algorithms.bidirectional') as cm:
            bidirec.name_algorithm()
        # Log should contain the word 'dynamic'
        self.assertRegex(cm.output[0], 'dynamic')

        bidirec.run()
        path = bidirec.path
        cost = bidirec.total_cost
        total_res = bidirec.consumed_resources
        # Check path and other attributes
        self.assertEqual(path, ['Source', 2, 1, 'Sink'])
        self.assertEqual(cost, -10)
        self.assertTrue(all(total_res == [3, 2]))
 def testBiDirectionalForward(self):
     """
     Find shortest path of simple test digraph using the BiDirectional
     algorithm with only forward direction.
     """
     bidirec = BiDirectional(self.G,
                             self.max_res,
                             self.min_res,
                             direction='forward')
     # Check classification
     with self.assertLogs('cspy.algorithms.bidirectional') as cm:
         bidirec.name_algorithm()
     # Log should contain the word 'forward'
     self.assertRegex(cm.output[0], 'forward')
     bidirec.run()
     path = bidirec.path
     cost = bidirec.total_cost
     total_res = bidirec.consumed_resources
     self.assertEqual(path, ['Source', 'A', 'B', 'C', 'Sink'])
     self.assertEqual(cost, -13)
     self.assertTrue(all(total_res == [4, 15.3]))
 def testBiDirectionalBothDynamic(self):
     """
     Find shortest path of simple test digraph using the BiDirectional
     algorithm with dynamic halfway point.
     """
     bidirec = BiDirectional(self.G, self.max_res, self.min_res)
     # Check classification
     with self.assertLogs('cspy.algorithms.bidirectional') as cm:
         bidirec.name_algorithm()
     # Log should contain the word 'dynamic'
     self.assertRegex(cm.output[0], 'dynamic')
     # Check exception for not running first
     with self.assertRaises(Exception) as context:
         bidirec.path
     self.assertTrue("run()" in str(context.exception))
     # Run and test results
     bidirec.run()
     path = bidirec.path
     cost = bidirec.total_cost
     total_res = bidirec.consumed_resources
     self.assertEqual(path, ['Source', 'A', 'B', 'C', 'Sink'])
     self.assertEqual(cost, -13)
     self.assertTrue(all(total_res == [4, 15.3]))
示例#8
0
    def testBiDirectionalBothDynamic(self, _, seed):
        """
        Find shortest path of simple test digraph using the BiDirectional
        algorithm for a range of seeds.
        Note the first argument is required to work using parameterized and unittest.
        """
        bidirec = BiDirectional(self.G, self.max_res, self.min_res, seed=seed)
        # Check classification
        with self.assertLogs('cspy.algorithms.bidirectional') as cm:
            bidirec.name_algorithm()
        # Log should contain the word 'dynamic'
        self.assertRegex(cm.output[0], 'dynamic')

        bidirec.run()
        path = bidirec.path
        cost = bidirec.total_cost
        total_res = bidirec.consumed_resources
        # Check path and other attributes
        self.assertEqual(path, ['Source', 2, 5, 'Sink'])
        self.assertEqual(cost, 1)
        self.assertTrue(all(total_res == [3, 3]))
        self.assertTrue(all(e in self.G.edges() for e in zip(path, path[1:])))
        self.assertEqual(self.max_res, [len(self.G.edges()), 6])
        self.assertEqual(self.min_res, [0, 0])