MCPcopy Create free account
hub / github.com/PyVRP/PyVRP / read

Function read

pyvrp/read.py:39–92  ·  view source on GitHub ↗

Reads the ``VRPLIB`` file at the given location, and returns a :class:`~pyvrp._pyvrp.ProblemData` instance. .. note:: See the :doc:`VRPLIB format explanation <../dev/supported_vrplib_fields>` page for more details. Parameters ---------- where

(
    where: str | pathlib.Path,
    round_func: str | _RoundingFunc = "none",
)

Source from the content-addressed store, hash-verified

37
38
39def read(
40 where: str | pathlib.Path,
41 round_func: str | _RoundingFunc = "none",
42) -> ProblemData:
43 """
44 Reads the ``VRPLIB`` file at the given location, and returns a
45 :class:`~pyvrp._pyvrp.ProblemData` instance.
46
47 .. note::
48
49 See the
50 :doc:`VRPLIB format explanation <../dev/supported_vrplib_fields>` page
51 for more details.
52
53 Parameters
54 ----------
55 where
56 File location to read. Assumes the data on the given location is in
57 ``VRPLIB`` format.
58 round_func
59 Optional rounding function that is applied to all data values in the
60 instance. This can either be a function or a string:
61
62 * ``'round'`` rounds the values to the nearest integer;
63 * ``'trunc'`` truncates the values to an integer;
64 * ``'dimacs'`` scales by 10 and truncates the values to an integer;
65 * ``'exact'`` scales by 1000 and rounds to the nearest integer.
66 * ``'none'`` does no rounding. This is the default.
67
68 Raises
69 ------
70 TypeError
71 When ``round_func`` does not name a rounding function, or is not
72 callable.
73 ValueError
74 When the data file does not provide information on the problem size.
75
76 Returns
77 -------
78 ProblemData
79 Data instance constructed from the read data.
80 """
81 if (key := str(round_func)) in ROUND_FUNCS:
82 round_func = ROUND_FUNCS[key]
83
84 if not callable(round_func):
85 raise TypeError(
86 f"round_func = {round_func} is not understood. Can be a function,"
87 f" or one of {ROUND_FUNCS.keys()}."
88 )
89
90 parser = _InstanceParser(vrplib.read_instance(where), round_func)
91 builder = _ProblemDataBuilder(parser)
92 return builder.data()
93
94
95def read_solution(where: str | pathlib.Path, data: ProblemData) -> Solution:

Callers 1

_solveFunction · 0.90

Calls 3

_InstanceParserClass · 0.85
_ProblemDataBuilderClass · 0.85
dataMethod · 0.45

Tested by

no test coverage detected