read() helper that parses VRPLIB data into meaningful parts for further processing.
| 155 | |
| 156 | |
| 157 | class _InstanceParser: |
| 158 | """ |
| 159 | read() helper that parses VRPLIB data into meaningful parts for further |
| 160 | processing. |
| 161 | """ |
| 162 | |
| 163 | def __init__(self, instance: dict, round_func: _RoundingFunc): |
| 164 | self.instance = instance |
| 165 | self.round_func = round_func |
| 166 | |
| 167 | @property |
| 168 | def num_locations(self) -> int: |
| 169 | return self.instance["dimension"] |
| 170 | |
| 171 | @property |
| 172 | def num_depots(self) -> int: |
| 173 | return self.instance.get("depot", np.array([0])).size |
| 174 | |
| 175 | @property |
| 176 | def num_clients(self) -> int: |
| 177 | return self.num_locations - self.num_depots |
| 178 | |
| 179 | @property |
| 180 | def num_vehicles(self) -> int: |
| 181 | return self.instance.get("vehicles", self.num_locations - 1) |
| 182 | |
| 183 | def type(self) -> str: |
| 184 | return self.instance.get("type", "") |
| 185 | |
| 186 | def edge_weight(self) -> np.ndarray: |
| 187 | return self.round_func(self.instance["edge_weight"]) |
| 188 | |
| 189 | def depot_idcs(self) -> np.ndarray: |
| 190 | return self.instance.get("depot", np.array([0])) |
| 191 | |
| 192 | def backhauls(self) -> np.ndarray: |
| 193 | if "backhaul" not in self.instance: |
| 194 | return np.zeros((self.num_locations, 1), dtype=np.int64) |
| 195 | |
| 196 | return self.round_func(self.instance["backhaul"]) |
| 197 | |
| 198 | def demands(self) -> np.ndarray: |
| 199 | if "demand" not in self.instance and "linehaul" not in self.instance: |
| 200 | return np.zeros((self.num_locations, 1), dtype=np.int64) |
| 201 | |
| 202 | return self.round_func( |
| 203 | self.instance.get("demand", self.instance.get("linehaul")) |
| 204 | ) |
| 205 | |
| 206 | def coords(self) -> np.ndarray: |
| 207 | if "node_coord" not in self.instance: |
| 208 | return np.zeros((self.num_locations, 2), dtype=np.int64) |
| 209 | |
| 210 | return self.round_func(self.instance["node_coord"]) |
| 211 | |
| 212 | def service_times(self) -> np.ndarray: |
| 213 | service_times = self.instance.get("service_time", 0) |
| 214 |