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])
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]))
def test_bidirectional(self): alg = BiDirectional(self.G, self.max_res, self.min_res, elementary=True) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertTrue(alg.consumed_resources == self.consumed_resources) self.assertTrue( all(e in self.G.edges() for e in zip(alg.path, alg.path[1:])))
def test_bidirectional_random(self, _, seed): alg = BiDirectional(self.G, self.max_res, self.min_res, seed=seed, elementary=True) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertTrue(all(alg.consumed_resources == self.consumed_resources))
def test_bidirectional(self): """ Test BiDirectional with randomly chosen sequence of directions for a range of seeds. """ alg = BiDirectional(self.G, self.max_res, self.min_res, elementary=False) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertEqual(alg.consumed_resources, self.consumed_resources)
def test_bidirectional_random(self, _, seed): """Test BiDirectional with randomly chosen sequence of directions for a range of seeds. """ alg = BiDirectional(self.G, self.max_res, self.min_res, REF_forward=self.custom_REF_forward, REF_backward=self.custom_REF_backward, seed=seed) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertTrue(all(alg.consumed_resources == self.consumed_resources))
def test_bidirectional_backward(self): """ Find shortest path of simple test digraph using BiDirectional algorithm with only backward direction. """ alg = BiDirectional(self.G, self.max_res, self.min_res, direction='backward', elementary=True) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertTrue(all(alg.consumed_resources == self.consumed_resources))
def testBiDirectional(self, _, seed): """ Find shortest path of simple test digraph using BiDirectional """ bidirec = BiDirectional(self.G, self.max_res, self.min_res, seed=seed) bidirec.run() path = bidirec.path cost = bidirec.total_cost total_res = bidirec.consumed_resources # Check path self.assertEqual(path, ['Source', 2, 1, 'Sink']) # Check attributes self.assertEqual(cost, -10) self.assertTrue(all(total_res == [3, 2])) self.assertTrue(all(e in self.G.edges() for e in zip(path, path[1:])))
def testBiDirectionalBothDynamic(self): """ 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) # Run and test results bidirec.run() path = bidirec.path cost = bidirec.total_cost total_res = bidirec.consumed_resources self.assertEqual(path, ['Source', "A", 'Sink']) self.assertEqual(cost, 0) self.assertTrue(all(total_res == [2, 12]))
def test_bidirectional(self): """ Test BiDirectional with randomly chosen sequence of directions for a range of seeds. """ alg = BiDirectional(self.G, self.max_res, self.min_res, elementary=True) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertEqual(alg.consumed_resources, self.consumed_resources) self.assertTrue( all(e in self.G.edges() for e in zip(alg.path, alg.path[1:])))
def test_bidirectional_generated(self): """ Test BiDirectional with the search direction chosen by the number of direction with lowest number of generated labels. """ alg = BiDirectional(self.G, self.max_res, self.min_res, method="generated", elementary=True) alg.run() self.assertEqual(alg.path, self.result_path) self.assertEqual(alg.total_cost, self.total_cost) self.assertTrue(all(alg.consumed_resources == self.consumed_resources)) self.assertTrue( all(e in self.G.edges() for e in zip(alg.path, alg.path[1:])))
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'])
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'])
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 _solve_cspy(self): # Solve subproblem with exact algorithm log.info(" Solving subproblem for aircraft {}".format(self.k)) G = deepcopy(self.G_pre) n_edges = len(G.edges()) crew_ub = AIRLINES_DATA[self.airline]['crew_budget'] max_res = [ n_edges, 0.0, self.max_FH, crew_ub, MAX_CREWD1, MAX_CREWD2, 0.0, 1.0 ] min_res = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] bidirec = BiDirectional(G, max_res, min_res, direction='both', preprocess=True, REF=self.REF) bidirec.run() path = bidirec.path self.shortest_path = [(edge[2]['data'], edge[2]['weight']) for edge in G.edges(data=True) if edge[0:2] in zip(path, path[1:])] return self.shortest_path
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]))
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])
def test_bidirectional(self): alg = BiDirectional(self.G, self.max_res, self.min_res) alg.run() self.assertEqual(alg.path, ['Source', "A", 'Sink']) self.assertEqual(alg.total_cost, 0) self.assertTrue(alg.consumed_resources == [2, 12])