def test_addCheapestCycle(self): n = 10 graph = WeightedGraph() for i in range(n): graph.addVertex(i) addCheapestCycle(graph, 1) self.assertTrue(all(e[2] == 1 for e in graph))
def test_get_links_tiny(self): graph = WeightedGraph() graph.add_direct_link(0, 1, 50) graph.add_direct_link(1, 2, 34) graph.add_direct_link(1, 3, 42) graph.add_direct_link(4, 1, 24) links = sorted(graph.get_links(1)) self.assertEqual(links, [0, 2, 3, 4])
self.dist = dist def display(self): print "Shortest Distance :", self.dist[self.dest] print "Shortest Path:" print self.src, for i in self.path: if i != -1: print "-->", print i, if __name__ == '__main__': V = 6 wg = WeightedGraph(V) node = Node(0, 1, 2) wg.addEdge(node) node = Node(0, 2, 4) wg.addEdge(node) node = Node(1, 2, 1) wg.addEdge(node) node = Node(1, 3, 4) wg.addEdge(node) node = Node(1, 4, 2) wg.addEdge(node) node = Node(2, 4, 3) wg.addEdge(node) node = Node(3, 5, 2) wg.addEdge(node) node = Node(4, 3, 3)
if args['n'] is None and args['i'] is None: parser.print_help() exit(1) seed = args['s'] vertexCount = 0 if args['n']: vertexCount = args['n'] graph = genGraph(vertexCount, costMedian=10, costDeviation=5, seed=seed) if args['i']: graph = Graph.load(args['i']) vertexCount = len(graph.vertices()) setSeed(seed) generations = args['g'] eliteSize = args['e'] if eliteSize is None: eliteSize = int(vertexCount * 0.3) mutationRate = args['m'] if mutationRate is None: mutationRate = 10.0 / vertexCount startTime = time() bestPath, progress, lastGeneration = evolutionaryAlgorithm( graph, generations, eliteSize, mutationRate)
for edge in self.graph: if self.checkCycle(uf,edge): self.mst.append(edge) uf.union(edge.src,edge.dest) def display(self): print "Kruskal's Algorithm: " print " U | V | cost |" print "-----------+------------+--------------+" for node in self.mst: print " ",node.src," | ",node.dest," | ",node.cost," |" if __name__=='__main__': V = 6 g = WeightedGraph(V) node = Node(0,1,6) g.addEdge(node) node = Node(0,2,1) g.addEdge(node) node = Node(0,3,5) g.addEdge(node) node = Node(1,0,6) g.addEdge(node) node = Node(1,2,5) g.addEdge(node) node = Node(1,4,3) g.addEdge(node) node = Node(2,1,5) g.addEdge(node) node = Node(2,3,5)
def setUp(self): self.g = WeightedGraph()
class GraphTest(unittest.TestCase): def setUp(self): self.g = WeightedGraph() def test_addVertex(self): self.g.addVertex('a') self.g.addVertex('a') self.g.addVertex('b') self.assertEqual(2, len(self.g.vertices())) def test_addEdge(self): self.g.addEdge('a', 'b', 2) self.g.addEdge('a', 'c', 3) self.g.addEdge('a', 'b', 5) self.assertEqual(3, len(self.g.vertices())) def test_iter(self): self.g.addEdge('a', 'd', 2) self.g.addEdge('b', 'c', 3) self.g.addEdge('a', 'b', 5) self.g.addEdge('a', 'b', 3) self.assertTrue(('a', 'd', 2) in self.g) self.assertTrue(('b', 'c', 3) in self.g) self.assertFalse(('a', 'b', 3) in self.g) self.assertEqual(3, len(list(self.g)) // 2) def test_getItem(self): self.g.addEdge('a', 'b', 1000) self.g.addEdge('x', 'y', 2) self.assertEqual(1000, self.g[('a', 'b')]) self.assertEqual(2, self.g['x', 'y']) self.assertEqual(None, self.g['a', 'x'])
def test_MST_small(self): graph = WeightedGraph() graph.add_direct_link(0, 1, 2) graph.add_direct_link(2, 1, 3) graph.add_direct_link(2, 5, 4) graph.add_direct_link(2, 4, 1) graph.add_direct_link(2, 6, 5) graph.add_direct_link(3, 7, 13) graph.add_direct_link(4, 7, 6) graph.add_direct_link(5, 8, 7) graph.add_direct_link(8, 9, 8) graph.add_direct_link(8, 10, 9) graph.add_direct_link(10, 11, 10) graph.add_direct_link(12, 11, 11) graph.add_direct_link(13, 11, 12) graph.add_direct_link(0, 11, 54) graph.add_direct_link(0, 12, 64) graph.add_direct_link(0, 7, 42) graph.add_direct_link(0, 2, 6) graph.add_direct_link(1, 3, 63) graph.add_direct_link(1, 9, 99) graph.add_direct_link(5, 6, 42) graph.add_direct_link(5, 9, 87) graph.add_direct_link(8, 12, 46) mst = graph.min_tree() self.assertEqual(sorted(mst.get_links(0)), [1]) self.assertEqual(sorted(mst.get_links(1)), [0, 2]) self.assertEqual(sorted(mst.get_links(2)), [1, 4, 5, 6]) self.assertEqual(sorted(mst.get_links(3)), [7]) self.assertEqual(sorted(mst.get_links(4)), [2, 7]) self.assertEqual(sorted(mst.get_links(5)), [2, 8]) self.assertEqual(sorted(mst.get_links(6)), [2]) self.assertEqual(sorted(mst.get_links(7)), [3, 4]) self.assertEqual(sorted(mst.get_links(8)), [5, 9, 10]) self.assertEqual(sorted(mst.get_links(9)), [8]) self.assertEqual(sorted(mst.get_links(10)), [8, 11]) self.assertEqual(sorted(mst.get_links(11)), [10, 12, 13]) self.assertEqual(sorted(mst.get_links(12)), [11]) self.assertEqual(sorted(mst.get_links(13)), [11])
def test_MST_tiny(self): graph = WeightedGraph() graph.add_direct_link(1, 3, 15) graph.add_direct_link(1, 2, 1) graph.add_direct_link(1, 4, 10) graph.add_direct_link(2, 4, 15) graph.add_direct_link(2, 3, 16) graph.add_direct_link(2, 0, 4) graph.add_direct_link(3, 0, 2) graph.add_direct_link(4, 0, 3) mst = graph.min_tree() self.assertEqual(sorted(mst.get_links(0)), [2, 3, 4]) self.assertEqual(sorted(mst.get_links(1)), [2]) self.assertEqual(sorted(mst.get_links(2)), [0, 1]) self.assertEqual(sorted(mst.get_links(3)), [0]) self.assertEqual(sorted(mst.get_links(4)), [0])
def test_get_links_small(self): graph = WeightedGraph() graph.add_direct_link(0, 5, 1) graph.add_direct_link(0, 1, 1) graph.add_direct_link(0, 2, 1) graph.add_direct_link(1, 5, 1) graph.add_direct_link(2, 5, 1) graph.add_direct_link(2, 3, 1) graph.add_direct_link(3, 4, 1) graph.add_direct_link(3, 5, 1) graph.add_direct_link(5, 6, 1) graph.add_direct_link(5, 7, 1) graph.add_direct_link(6, 9, 1) graph.add_direct_link(7, 11, 1) graph.add_direct_link(8, 9, 1) graph.add_direct_link(9, 10, 1) graph.add_direct_link(9, 11, 1) links = sorted(graph.get_links(1)) self.assertEqual(links, [0, 5]) links = sorted(graph.get_links(3)) self.assertEqual(links, [2, 4, 5]) links = sorted(graph.get_links(5)) self.assertEqual(links, [0, 1, 2, 3, 6, 7]) links = sorted(graph.get_links(11)) self.assertEqual(links, [7, 9])
class AiUtilsTest(unittest.TestCase): def setUp(self): self.g = WeightedGraph() def test_calcDistance(self): self.g.addEdge('a', 'b', 12) self.g.addEdge('a', 'c', 3) self.g.addEdge('b', 'd', 5) self.g.addEdge('b', 'c', 6) self.g.addEdge('b', 'd', 4) path = random.sample(self.g.vertices(), len(self.g.vertices())) pathDist = AiUtils.calcDistance(self.g, path) self.assertEqual(-1, pathDist) self.g.addEdge('c', 'd', 7) self.g.addEdge('d', 'a', 8) pathDist = AiUtils.calcDistance(self.g, path) self.assertTrue(pathDist > 0) def test_calcFitness(self): self.g.addEdge('a', 'b', 10) self.g.addEdge('c', 'a', 3) path = AiUtils.genPath(self.g.vertices()) fitness = AiUtils.calcFitness(self.g, path) self.assertEqual(-1, fitness) self.g.addEdge('b', 'c', 5) path = AiUtils.genPath(self.g.vertices()) fitness = AiUtils.calcFitness(self.g, path) self.assertTrue(fitness == 1.0/18) def test_rankPaths(self): self.g = genGraph(100, 0.9) population = AiUtils.initPopulation(50, self.g.vertices()) ranked = AiUtils.rankPaths(self.g, population) self.assertEqual(50, len(ranked)) for _, fitness in ranked: self.assertTrue(fitness > 0 or fitness == -1) def test_selection(self): self.g = genGraph(100, 1) population = AiUtils.initPopulation(100, self.g.vertices()) ranked = AiUtils.rankPaths(self.g, population) with self.assertRaises(Exception) as context: AiUtils.selection(ranked, 30, selectionSize=1.1) self.assertTrue('Selection size must be in [0 .. 1]' in context.exception) def test_breed(self): parent1 = list(range(10)) parent2 = parent1[:] random.shuffle(parent2) self.assertTrue(len(parent1) == len(parent2)) child1, child2 = AiUtils.breed(parent1, parent2) self.assertTrue(len(child1) == len(child2)) self.assertTrue(len(child1) == len(parent1)) self.assertTrue(len(child1) == len(set(child1))) self.assertTrue(len(child2) == len(set(child2))) def test_mutate(self): self.g = genGraph(10, 1) population = AiUtils.initPopulation(100, self.g.vertices()) mutated = AiUtils.mutatePopulation(population, 1, 1) self.assertTrue(all(x not in population for x in mutated))