Skip to content

Commit 8da08c5

Browse files
committed
Added doctests for readinp
1 parent 6952343 commit 8da08c5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

genetic_algorithm/traveling_salesman_problem.py

+23
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ def readinp(filename: str) -> tsplib95.models.Problem:
3434
Returns:
3535
problem (tsplib95.models.Problem): The TSP problem object containing
3636
nodes and distances.
37+
38+
Example:
39+
>>> import tsplib95
40+
>>> tsp_data = '''
41+
... NAME: sample
42+
... TYPE: TSP
43+
... DIMENSION: 4
44+
... EDGE_WEIGHT_TYPE: EUC_2D
45+
... NODE_COORD_SECTION
46+
... 1 0 0
47+
... 2 1 0
48+
... 3 0 1
49+
... 4 1 1
50+
... EOF
51+
... '''
52+
>>> tsp_filename = "sample.tsp"
53+
>>> with open(tsp_filename, "w") as f:
54+
... f.write(tsp_data)
55+
>>> prob = readinp('sample.tsp')
56+
>>> len(list(prob.get_nodes())) > 0
57+
True
58+
>>> isinstance(prob, tsplib95.models.StandardProblem)
59+
True
3760
"""
3861
problem = tsplib95.load(filename)
3962
return problem

0 commit comments

Comments
 (0)