-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparse_solution.py
More file actions
42 lines (34 loc) · 1.41 KB
/
parse_solution.py
File metadata and controls
42 lines (34 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Union
from .parse_utils import infer_type, text2lines
Solution = dict[str, Union[float, str, list]]
def parse_solution(text: str) -> Solution:
"""
Parses a VRPLIB-formatted solution text into a dictionary.
- Routes appear as "Route #n: node1 node2 node3..." where nodes can be:
* Integer indices (0-indexed, unlike 1-indexed instance data)
* String markers (e.g., "|" for reload depots).
- Additional metadata as key-value pairs (e.g., "Cost: 123.45")
Parameters
----------
text
The solution text.
Returns
-------
dict
The soluion data dictionary, containing:
- "routes": list of routes, each route being a list of nodes
- Additional metadata as key-value pairs from the solution
"""
solution: Solution = {"routes": []}
for line in text2lines(text):
if "Route" in line:
raw_visits = line.split(":")[1].split()
visits = [int(val) if val.isdigit() else val for val in raw_visits]
solution["routes"].append(visits) # type: ignore
elif ":" in line or " " in line: # Split at first colon or whitespace
split_at = ":" if ":" in line else " "
k, v = [word.strip() for word in line.split(split_at, 1)]
solution[k.lower()] = infer_type(v)
else: # Ignore lines without keyword-value pairs
continue
return solution