def test_addEdge(self): vertex1 = Vertex(1) vertex2 = Vertex(2) graph = Graph([vertex1, vertex2], []) # Case 1: Both of edge's vertexes are in graph edge1 = Edge(vertex1, vertex2, 3) graph.addEdge(edge1) self.assertIn(edge1, graph.edges) # Case 2: One edge's vertex is not in graph vertex3 = Vertex(3) edge2 = Edge(vertex2, vertex3, 4) graph.addEdge(edge2) self.assertIn(edge2, graph.edges) self.assertIn(vertex3, graph.vertexes) # Case 3: Both of edge's vertexes are not in graph vertex4 = Vertex(4) vertex5 = Vertex(5) edge3 = Edge(vertex4, vertex5, 3) graph.addEdge(edge3) self.assertIn(edge3, graph.edges) self.assertIn(vertex4, graph.vertexes) self.assertIn(vertex5, graph.vertexes)
def test_getTraversibleEdges(self): vertex1 = Vertex(1) vertex2 = Vertex(2) edge1 = Edge(vertex1, vertex2, 3) edge2 = Edge(vertex1, vertex2, 4) edge2.traversible = False self.assertNotIn(edge2, vertex1.getTraversibleEdges())
def test_pathAsString(self): vertex1 = Vertex(1) vertex2 = Vertex(2) vertex3 = Vertex(3) edge1 = Edge(vertex1, vertex2, 1) edge2 = Edge(vertex2, vertex3, 2) path = Path(vertex1, [edge1, edge2], 1 + 2) self.assertEqual(path.pathAsString(), "1 - 2 - 3 --- weight = 3")
def test_delete(self): vertex1 = Vertex(1) vertex2 = Vertex(2) edge1 = Edge(vertex1, vertex2, 3) edge2 = Edge(vertex1, vertex2, 4) vertex1.delete() self.assertEqual(vertex1.ind, None) self.assertNotIn(edge1, vertex2.edges) self.assertNotIn(edge2, vertex2.edges)
def test_constructor(self): vertex1 = Vertex(1) vertex2 = Vertex(2) vertex3 = Vertex(3) edge1 = Edge(vertex1, vertex2, 1) edge2 = Edge(vertex2, vertex3, 2) path = Path(vertex1, [edge1, edge2], 1 + 2) self.assertEqual(vertex1, path.beginning) self.assertEqual(path.cost, 3) self.assertIn(edge1, path.edges) self.assertIn(edge2, path.edges)
def test_constructor(self): vertex1 = Vertex(1) vertex2 = Vertex(2) edge = Edge(vertex1, vertex2, 3) self.assertIn(edge, vertex1.edges) self.assertIn(edge, vertex2.edges)
def test_delete(self): vertex1 = Vertex(1) vertex2 = Vertex(2) edge = Edge(vertex1, vertex2, 3) edge.delete() self.assertNotIn(edge, vertex1.edges) self.assertNotIn(edge, vertex2.edges)
def test_makeVertexNotAccesible(self): vertex1 = Vertex(1) vertex2 = Vertex(2) edge = Edge(vertex1, vertex2, 3) ant = Ant(1, 1) ant.makeVertexNotAccessible(vertex1) self.assertFalse(edge.traversible)
def test_getOtherEnd(self): vertex1 = Vertex(1) vertex2 = Vertex(2) vertex3 = Vertex(3) edge = Edge(vertex1, vertex2, 3) self.assertEqual(edge.getOtherEnd(vertex1), vertex2) self.assertEqual(edge.getOtherEnd(vertex2), vertex1) self.assertEqual(edge.getOtherEnd(vertex3), None)
def test_createPath2(self): """Case 2: Path between vertexes exists. """ vertex1 = Vertex(1) vertex2 = Vertex(2) edge = Edge(vertex1, vertex2, 3) edge.depositedPheromone = 1 ant = Ant(1, 1) actualPath, antReachedEnd = ant.createPath(vertex1, vertex2) self.assertIsNotNone(actualPath) self.assertTrue(antReachedEnd)
def test_getDataAsString(self): testRunNumber = 1 testInputFilename = "testInputFilename.txt" testVertexStartInd = 1 testVertexEndInd = 2 testPheromoneInfluence = 0.1 testDesirabilityInfluence = 0.1 testEvaporationCoefficent = 0.1 testItersNum = 10 testAlgorithmStartTime = datetime.datetime.strptime( "2020_01_29_12_59", "%Y_%m_%d_%H_%M") testAlgorithmEndTime = datetime.datetime.strptime( "2020_01_29_13_59", "%Y_%m_%d_%H_%M") testElapsedTime = testAlgorithmEndTime - testAlgorithmStartTime v1 = Vertex(1) v2 = Vertex(2) e = Edge(v1, v2, 9) testPath = Path(v1, [e], e.weight) testData = ACOShortestPathOutputData( testRunNumber, testInputFilename, testVertexStartInd, testVertexEndInd, testPheromoneInfluence, testDesirabilityInfluence, testEvaporationCoefficent, testItersNum, testAlgorithmStartTime, testAlgorithmEndTime, testElapsedTime, testPath, False, "") expectedDataAsString = """ ------------------------------------ Run: 1 Input: testInputFilename.txt Parameters: - vertex_start = 1 - vertex_end = 2 - pheromone_influence = 0.1 - desirability_influence = 0.1 - evaporation_coefficent = 0.1 - iters_num = 10 Start: 12:59:00 29/01/2020 End: 13:59:00 29/01/2020 Time running: 3600.0 sec Shortest path: weight = 9 1 - 2 --- weight = 9 ------------------------------------""" actualDataAsString = testData.getDataAsString() self.assertEqual(expectedDataAsString, actualDataAsString)
def readGraphFromInputFile(inputFilePath: str) -> Graph: file = open(inputFilePath) try: vertexesNum = int(file.readline().strip().split()[2]) edgesNum = int(file.readline().strip().split()[2]) vertexes = [Vertex(i + 1) for i in range(vertexesNum)] edges = [] line = file.readline().strip() while line: content = line.split() # Vertex start vertexStartInd = int(content[1]) vertexStart = None vertex: Vertex for vertex in vertexes: if vertex.ind == vertexStartInd: vertexStart = vertex # Vertex end vertexEndInd = int(content[2]) vertexEnd = None vertex: Vertex for vertex in vertexes: if vertex.ind == vertexEndInd: vertexEnd = vertex # Weight weight = int(content[3]) edges.append(Edge(vertexStart, vertexEnd, weight)) line = file.readline().strip() except Exception as e: file.close() raise e file.close() return Graph(vertexes, edges)
def test_writeOutputToFile_withAllPaths(self): # Initialize data testRunNumber = 1 testInputFilename = "testInputFilename.txt" testVertexStartInd = 1 testVertexEndInd = 2 testPheromoneInfluence = 0.1 testDesirabilityInfluence = 0.1 testEvaporationCoefficent = 0.1 testItersNum = 10 testAlgorithmStartTime = datetime.datetime.strptime( "2020_01_29_12_59", "%Y_%m_%d_%H_%M") testAlgorithmEndTime = datetime.datetime.strptime( "2020_01_29_13_59", "%Y_%m_%d_%H_%M") testElapsedTime = testAlgorithmEndTime - testAlgorithmStartTime v1 = Vertex(1) v2 = Vertex(2) e = Edge(v1, v2, 9) testPath = Path(v1, [e], e.weight) testPrintAllPaths = True testPathToAllPathsFile = "testAllPathsFile.txt" """ All paths file """ allPathsFile = open(testPathToAllPathsFile, "w") allPathsFileContent = """1. 1 - 2 --- weight = 9 2. 1 - 2 --- weight = 9 3. 1 - 2 --- weight = 9 4. 1 - 2 --- weight = 9 5. 1 - 2 --- weight = 9""" allPathsFile.write(allPathsFileContent) allPathsFile.close() """ All paths file end """ testData = ACOShortestPathOutputData( testRunNumber, testInputFilename, testVertexStartInd, testVertexEndInd, testPheromoneInfluence, testDesirabilityInfluence, testEvaporationCoefficent, testItersNum, testAlgorithmStartTime, testAlgorithmEndTime, testElapsedTime, testPath, testPrintAllPaths, testPathToAllPathsFile) testOutputFilename = "test_writeOutputToFile_withAllPaths" outputDataDirPath = os.path.abspath( os.path.join(__file__, os.pardir, os.pardir, os.pardir, os.pardir, "Data", "OutputResultFiles", "ResultsWithAllPathsPrinted")) testOutputFilePath = os.path.join(outputDataDirPath, testOutputFilename) # Testing ACOShortestPathOutputWriter.writeOutputToFile(testOutputFilePath, testData) os.remove(testPathToAllPathsFile) expectedFileContent = """ ------------------------------------ Run: 1 Input: testInputFilename.txt Parameters: - vertex_start = 1 - vertex_end = 2 - pheromone_influence = 0.1 - desirability_influence = 0.1 - evaporation_coefficent = 0.1 - iters_num = 10 Start: 12:59:00 29/01/2020 End: 13:59:00 29/01/2020 Time running: 3600.0 sec Shortest path: weight = 9 1 - 2 --- weight = 9 ------------------------------------ All paths: 1. 1 - 2 --- weight = 9 2. 1 - 2 --- weight = 9 3. 1 - 2 --- weight = 9 4. 1 - 2 --- weight = 9 5. 1 - 2 --- weight = 9""" actualFileContent = "" actualFile = open(testOutputFilePath, "r") line = actualFile.readline() while line: actualFileContent += line line = actualFile.readline() actualFile.close() os.remove(testOutputFilePath) self.assertEqual(expectedFileContent, actualFileContent)