예제 #1
0
 def test_tabu(self):
     alg = Tabu(self.G, self.max_res, self.min_res)
     alg.run()
     self.assertEqual(alg.total_cost, 1)
     self.assertEqual(alg.path, ['Source', 2, 5, 4, 'Sink'])
     self.assertTrue(all(alg.consumed_resources == [4, 4]))
     self.assertTrue(
         all(e in self.G.edges() for e in zip(alg.path, alg.path[1:])))
예제 #2
0
 def test_time_limit(self):
     alg = Tabu(self.G, self.max_res, self.min_res, time_limit=0.001)
     start = time()
     alg.run()
     self.assertTrue(time() - start <= 0.001)
     self.assertEqual(alg.path, self.result_path)
     self.assertEqual(alg.total_cost, self.total_cost)
     self.assertTrue(all(alg.consumed_resources == self.consumed_resources))
예제 #3
0
 def test_astar_threshold(self):
     'test threshold parameter'
     alg = Tabu(self.G,
                self.max_res,
                self.min_res,
                algorithm="astar",
                threshold=100)
     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))
예제 #4
0
 def test_tabu(self):
     """
     Find shortest path of using Tabu
     """
     alg = Tabu(self.G, self.max_res, self.min_res)
     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:])))
예제 #5
0
 def test_tabu(self):
     """Find shortest path of simple test digraph using Tabu.
     """
     alg = Tabu(self.G, self.max_res, self.min_res)
     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:])))
     # Check networkx's astar_path
     path_star = astar_path(self.G, "Source", "Sink")
     self.assertEqual(path_star, ['Source', 1, 'Sink'])
예제 #6
0
 def test_astar_time_limit(self):
     'time limit parameter'
     alg = Tabu(self.G,
                self.max_res,
                self.min_res,
                algorithm="astar",
                time_limit=0.001)
     start = time()
     alg.run()
     self.assertTrue(time() - start <= 0.001)
     self.assertEqual(alg.path, self.result_path)
     self.assertEqual(alg.total_cost, self.total_cost)
     self.assertTrue(all(alg.consumed_resources == self.consumed_resources))
예제 #7
0
 def testTabu(self):
     tabu = Tabu(self.G, self.max_res, self.min_res)
     # Check exception for not running first
     with self.assertRaises(Exception) as context:
         tabu.path
     self.assertTrue("run()" in str(context.exception))
     # Run and test results
     tabu.run()
     path = tabu.path
     cost = tabu.total_cost
     total_res = tabu.consumed_resources
     self.assertEqual(path, ['Source', 'A', 'C', 'D', 'E', 'Sink'])
     self.assertEqual(cost, 3)
     self.assertTrue(all(total_res == [5, 5]))
예제 #8
0
 def testTabu(self):
     """
     Find shortest path of simple test digraph using Tabu
     """
     tabu = Tabu(self.G, self.max_res, self.min_res)
     tabu.run()
     path = tabu.path
     cost = tabu.total_cost
     total_res = tabu.consumed_resources
     # Check attributes
     self.assertEqual(cost, -5)
     self.assertTrue(all(total_res == [3, 2]))
     self.assertEqual(path, ['Source', 3, 2, 'Sink'])
     # Check path
     self.assertTrue(all(e in self.G.edges() for e in zip(path, path[1:])))
예제 #9
0
 def testTabu(self):
     tabu = Tabu(self.G, self.max_res, self.min_res)
     tabu.run()
     path = tabu.path
     cost_tabu = tabu.total_cost
     total_res = tabu.consumed_resources
     cost = sum([
         edge[2]['weight']
         for edge in self.G.edges(data=True)
         if edge[0:2] in zip(path, path[1:])
     ])
     # Check new cost attribute
     self.assertEqual(cost, cost_tabu)
     self.assertTrue(all(total_res == [3, 3]))
     self.assertEqual(path, ['Source', 2, 5, 'Sink'])
     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])
예제 #10
0
 def testTabu(self):
     """
     Find shortest path of simple test digraph using Tabu.
     """
     tabu = Tabu(self.G, self.max_res, self.min_res)
     tabu.run()
     path = tabu.path
     cost = tabu.total_cost
     total_res = tabu.consumed_resources
     # Check attributes
     self.assertEqual(cost, -5)
     self.assertTrue(all(total_res == [3, 2]))
     # Check path
     self.assertEqual(path, ['Source', 3, 2, 'Sink'])
     self.assertTrue(all(e in self.G.edges() for e in zip(path, path[1:])))
     # Check if networkx's astar_path gives the same path
     path_star = astar_path(self.G, "Source", "Sink")
     self.assertEqual(path_star, ['Source', 1, 'Sink'])
예제 #11
0
 def test_threshold(self):
     alg = Tabu(self.G, self.max_res, self.min_res, threshold=100)
     alg.run()
     self.assertEqual(alg.path, ["Source", "A", "B", "Sink"])
     self.assertEqual(alg.total_cost, 8)
     self.assertTrue(all(alg.consumed_resources == [3, 4.3]))
예제 #12
0
 def test_astar(self):
     alg = Tabu(self.G, self.max_res, self.min_res, algorithm="astar")
     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))