-
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathtest_read.py
More file actions
737 lines (587 loc) · 26 KB
/
Copy pathtest_read.py
File metadata and controls
737 lines (587 loc) · 26 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
from math import sqrt
import numpy as np
import pytest
from numpy.testing import (
assert_,
assert_allclose,
assert_equal,
assert_raises,
)
from pyvrp import Activity, CostEvaluator
from pyvrp.constants import MAX_VALUE
from pyvrp.exceptions import ScalingWarning
from tests.helpers import read, read_solution
@pytest.mark.parametrize(
("where", "exception"),
[
("data/UnknownEdgeWeightFmt.txt", ValueError),
("data/UnknownEdgeWeightType.txt", ValueError),
("somewhere that does not exist", FileNotFoundError),
("data/DepotNotOne.txt", ValueError),
("data/DepotsNotLowerIndices.txt", ValueError),
("data/TimeWindowOpenLargerThanClose.txt", ValueError),
("data/EdgeWeightsNoExplicit.txt", ValueError),
("data/EdgeWeightsNotFullMatrix.txt", ValueError),
("data/MissingVehicleData.txt", ValueError),
],
)
def test_raises_invalid_file(where: str, exception: Exception):
"""
Tests that ``read()`` raises when there are issues with the given file.
"""
with assert_raises(exception):
read(where)
def test_raises_unknown_round_func():
"""
Tests that ``read()`` raises when the rounding function is not known.
"""
with assert_raises(TypeError):
# Unknown round_func, so should raise.
read("data/OkSmall.txt", round_func="asdbsadfas")
# Is the default round_func, so should not raise.
read("data/OkSmall.txt", round_func="none")
def test_reading_OkSmall_instance():
"""
Tests that the parsed data from the "OkSmall" instance is correct.
"""
data = read("data/OkSmall.txt")
# From the DIMENSION, VEHICLES, and CAPACITY fields in the file.
assert_equal(data.num_clients, 4)
assert_equal(data.num_vehicles, 3)
assert_equal(data.num_vehicle_types, 1)
assert_equal(data.vehicle_type(0).capacity, [10])
# From the NODE_COORD_SECTION in the file
expected = [
(2334, 726),
(226, 1297),
(590, 530),
(435, 718),
(1191, 639),
]
for idx, loc in enumerate(data.locations()):
assert_equal(loc.x, expected[idx][0])
assert_equal(loc.y, expected[idx][1])
# From the EDGE_WEIGHT_SECTION in the file
expected = [
[0, 1544, 1944, 1931, 1476],
[1726, 0, 1992, 1427, 1593],
[1965, 1975, 0, 621, 1090],
[2063, 1433, 647, 0, 818],
[1475, 1594, 1090, 828, 0],
]
# For instances read through VRPLIB/read(), distance is duration. So the
# dist/durs should be the same as the expected edge weights above.
assert_equal(data.num_profiles, 1)
assert_equal(data.distance_matrix(profile=0), expected)
assert_equal(data.duration_matrix(profile=0), expected)
# From the DEMAND_SECTION in the file
expected = [5, 5, 3, 5]
for client in range(data.num_clients):
assert_equal(data.client(client).delivery, [expected[client]])
# From the TIME_WINDOW_SECTION in the file
expected = [
(15600, 22500),
(12000, 19500),
(8400, 15300),
(12000, 19500),
]
for client in range(data.num_clients):
assert_equal(data.client(client).tw_early, expected[client][0])
assert_equal(data.client(client).tw_late, expected[client][1])
# Vehicle time window is derived from the depot's time window in the
# TIME_WINDOW_SECTION of the file.
vehicle_type = data.vehicle_type(0)
assert_equal(vehicle_type.tw_early, 0)
assert_equal(vehicle_type.tw_late, 45000)
# From the SERVICE_TIME_SECTION in the file
expected = [360, 360, 420, 360]
for client in range(data.num_clients):
assert_equal(data.client(client).service_duration, expected[client])
def test_reading_vrplib_instance():
"""
Tests that a small VRPLIB-style instance is correctly parsed.
"""
data = read("data/E-n22-k4.txt", round_func="dimacs")
assert_equal(data.num_clients, 21)
assert_equal(data.num_depots, 1)
assert_equal(data.num_locations, 22)
assert_equal(data.vehicle_type(0).capacity, [60_000])
assert_equal(len(data.depots()), data.num_depots)
assert_equal(len(data.clients()), data.num_clients)
assert_equal(data.num_locations, data.num_depots + data.num_clients)
# Coordinates are scaled by 10 to align with 1 decimal distance precision
assert_equal(data.location(0).x, 1450) # depot [x, y] location
assert_equal(data.location(0).y, 2150)
assert_equal(data.location(1).x, 1510) # first customer [x, y] location
assert_equal(data.location(1).y, 2640)
# The data file specifies distances as 2D Euclidean. We take that and
# should compute integer equivalents with up to one decimal precision.
# For depot -> first customer:
# For depot -> first customer:
# dX = 151 - 145 = 6
# dY = 264 - 215 = 49
# dist = sqrt(dX^2 + dY^2) = 49.37
# int(10 * dist) = 493
assert_equal(data.num_profiles, 1)
distances = data.distance_matrix(profile=0)
assert_equal(distances[0, 1], 493)
assert_equal(distances[1, 0], 493)
# This is a CVRP instance, so all other fields should have default values.
for idx in range(data.num_clients):
client = data.client(idx)
assert_equal(client.service_duration, 0)
assert_equal(client.tw_early, 0)
assert_equal(client.tw_late, np.iinfo(np.int64).max)
assert_equal(client.release_time, 0)
assert_equal(client.prize, 0)
assert_equal(client.required, True)
def test_warns_about_scaling_issues():
"""
Tests that ``read()`` warns about scaling issues when a distance value is
very large.
"""
with pytest.warns(ScalingWarning):
# The arc from the depot to client 4 is really large (one billion), so
# that should trigger a warning.
read("data/ReallyLargeDistance.txt")
def test_round_func_round_nearest():
"""
Tests rounding to the nearest integer works well for the RC208 instance,
which has Euclidean distances computed from integer coordinates. Since the
instance is large, we'll test one particular distance.
"""
data = read("data/RC208.vrp", "round")
# We're going to test dist(0, 1) and dist(1, 0), which should be the same
# since the distances are symmetric/Euclidean.
assert_equal(data.location(0).x, 40)
assert_equal(data.location(0).y, 50)
assert_equal(data.location(1).x, 25)
assert_equal(data.location(1).y, 85)
# Compute the distance, and assert that it is indeed correctly rounded.
distances = data.distance_matrix(profile=0)
expected_dist = round(sqrt((40 - 25) ** 2 + (85 - 50) ** 2))
assert_equal(distances[0, 1], expected_dist)
assert_equal(distances[1, 0], expected_dist)
def test_round_func_exact():
"""
Tests rounding with the ``exact`` round function also works well for the
RC208 instance. This test is similar to the one for ``round``, but all
values are now multiplied by 1_000 before rounding.
"""
data = read("data/RC208.vrp", "exact")
# We're going to test dist(0, 1) and dist(1, 0), which should be the same
# since the distances are symmetric/Euclidean.
assert_equal(data.location(0).x, 40_000)
assert_equal(data.location(0).y, 50_000)
assert_equal(data.location(1).x, 25_000)
assert_equal(data.location(1).y, 85_000)
# Compute the distance, and assert that it is indeed correctly rounded.
distances = data.distance_matrix(profile=0)
expected_dist = round(sqrt((40 - 25) ** 2 + (85 - 50) ** 2) * 1_000)
assert_equal(distances[0, 1], expected_dist)
assert_equal(distances[1, 0], expected_dist)
def test_service_time_specification():
"""
Tests that specifying the service time as a specification (key-value pair)
results in a uniform service time for all clients.
"""
data = read("data/ServiceTimeSpecification.txt")
# Clients should all have the same service time.
services = [client.service_duration for client in data.clients()]
assert_allclose(services, 360)
def test_multiple_depots():
"""
Tests parsing a slightly modified version of the OkSmall instance, which
now has two depots rather than one.
"""
data = read("data/OkSmallMultipleDepots.txt")
# Still five locations, but now with two depots and three clients.
assert_equal(data.num_locations, 5)
assert_equal(data.num_depots, 2)
assert_equal(data.num_clients, 3)
assert_equal(data.num_vehicle_types, 2) # two, each at a different depot
assert_equal(data.num_vehicles, 3)
# First vehicle type should have two vehicles at the first depot.
veh_type1 = data.vehicle_type(0)
assert_equal(veh_type1.profile, 0)
assert_equal(veh_type1.start_depot, 0)
assert_equal(veh_type1.end_depot, 0)
assert_equal(veh_type1.num_available, 2)
assert_equal(veh_type1.tw_early, 0)
assert_equal(veh_type1.tw_late, 45_000)
# Second vehicle type should have one vehicle at the second depot. The
# vehicle should have a tighter time window than that associated with the
# first vehicle type.
veh_type2 = data.vehicle_type(1)
assert_equal(veh_type2.profile, 0)
assert_equal(veh_type2.start_depot, 1)
assert_equal(veh_type2.end_depot, 1)
assert_equal(veh_type2.num_available, 1)
assert_equal(veh_type2.tw_early, 5_000)
assert_equal(veh_type2.tw_late, 20_000)
# Test that the depot coordinates have been parsed correctly.
assert_equal(data.depot(0).location, 0)
assert_equal(data.location(0).x, 2_334)
assert_equal(data.location(0).y, 726)
assert_equal(data.depot(1).location, 1)
assert_equal(data.location(1).x, 226)
assert_equal(data.location(1).y, 1_297)
def test_mdvrptw_instance():
"""
Tests that reading an MDVRPTW instance happens correctly, particularly the
maximum route duration and multiple depot aspects.
"""
data = read("data/PR11A.vrp", round_func="trunc")
assert_equal(data.num_locations, 364)
assert_equal(data.num_depots, 4)
assert_equal(data.num_clients, 360)
assert_equal(data.num_vehicles, 40)
assert_equal(data.num_vehicle_types, 4) # one vehicle type per depot
for idx, vehicle_type in enumerate(data.vehicle_types()):
# There should be ten vehicles for each depot, with the following
# capacities and maximum route durations.
assert_equal(vehicle_type.num_available, 10)
assert_equal(vehicle_type.start_depot, idx)
assert_equal(vehicle_type.end_depot, idx)
assert_equal(vehicle_type.capacity, [200])
assert_equal(vehicle_type.max_duration, 450)
# Essentially all vehicle indices for each depot, separated by a comma.
# Each depot has ten vehicles, and they are nicely grouped (so the
# first ten are assigned to the first depot, the second ten to the
# second depot, etc.).
expected_name = ",".join(str(10 * idx + veh) for veh in range(10))
assert_equal(vehicle_type.name, expected_name)
# We haven't seen many instances with negative coordinates, but this
# MDVRPTW instance has those. That should be allowed.
assert_(any(depot.x < 0) for depot in data.depots())
assert_(any(depot.y < 0) for depot in data.depots())
assert_(any(client.x < 0) for client in data.clients())
assert_(any(client.y < 0) for client in data.clients())
def test_vrpspd_instance():
"""
Tests that reading an VRPSPD instance happens correctly, particularly the
linehaul and backhaul data.
"""
data = read("data/SmallVRPSPD.vrp", round_func="round")
assert_equal(data.num_load_dimensions, 1)
assert_equal(data.num_locations, 5)
assert_equal(data.num_depots, 1)
assert_equal(data.num_clients, 4)
assert_equal(data.num_vehicles, 4)
assert_equal(data.num_vehicle_types, 1)
vehicle_type = data.vehicle_type(0)
assert_equal(vehicle_type.num_available, 4)
assert_equal(vehicle_type.capacity, [200])
# The first client is a linehaul client (only delivery, no pickup), and
# the second client is a backhaul client (only pickup, no delivery). All
# other clients have both delivery and pickup.
deliveries = [1, 0, 16, 18]
pickups = [0, 3, 10, 40]
for idx, client in enumerate(data.clients()):
assert_equal(client.delivery[0], deliveries[idx])
assert_equal(client.pickup[0], pickups[idx])
# Test that distance/duration are not set to a large value, as in VRPB.
assert_equal(np.max(data.distance_matrix(profile=0)), 39)
assert_equal(np.max(data.duration_matrix(profile=0)), 39)
def test_vrpb_instance():
"""
Tests that reading an VRPB instance happens correctly, particularly the
backhaul data and modified distances to ensure linehaul is served before
backhaul.
"""
data = read("data/X-n101-50-k13.vrp", round_func="round")
assert_equal(data.num_load_dimensions, 1)
assert_equal(data.num_locations, 101)
assert_equal(data.num_depots, 1)
assert_equal(data.num_clients, 100)
assert_equal(data.num_vehicles, 100)
assert_equal(data.num_vehicle_types, 1)
vehicle_type = data.vehicle_type(0)
assert_equal(vehicle_type.num_available, 100)
assert_equal(vehicle_type.capacity, [206])
# The first 50 clients are linehaul, the rest are backhaul.
clients = data.clients()
for client in clients[:50]:
assert_equal(client.pickup, [0])
assert_(client.delivery[0] > 0)
for client in clients[50:]:
assert_(client.pickup[0] > 0)
assert_equal(client.delivery, [0])
# Tests that distance/duration from depot to backhaul clients is set to
# ``MAX_VALUE``, as well as for backhaul to linehaul clients.
linehauls = set(range(1, 51))
backhauls = set(range(51, 101))
distances = data.distance_matrix(profile=0)
durations = data.duration_matrix(profile=0)
for frm in range(data.num_locations):
for to in range(data.num_locations):
depot2back = frm == 0 and to in backhauls
back2line = frm in backhauls and to in linehauls
if depot2back or back2line:
assert_equal(distances[frm, to], MAX_VALUE)
assert_equal(durations[frm, to], MAX_VALUE)
else:
assert_(distances[frm, to] < MAX_VALUE)
assert_(durations[frm, to] < MAX_VALUE)
def test_max_distance_constraint():
"""
Tests a small instance with a maximum distance constraint, to confirm those
constraints are properly recognised and parsed.
"""
data = read("data/OkSmallMaxDistance.txt")
for vehicle_type in data.vehicle_types():
assert_equal(vehicle_type.max_distance, 5_000)
def test_reading_mutually_exclusive_group():
"""
Tests that read() correctly parses a small instance with mutually exclusive
client groups.
"""
data = read("data/OkSmallMutuallyExclusiveGroups.txt")
assert_equal(data.num_groups, 1)
group = data.group(0)
assert_equal(len(group), 3)
assert_equal(group.clients, [0, 1, 2])
for client in data.group(0):
client_data = data.client(client)
assert_equal(client_data.required, False)
assert_equal(client_data.group, 0)
def test_reading_allowed_clients():
"""
Tests that read() correctly parses a small instance with allowed clients
for each vehicle.
"""
data = read("data/OkSmallAllowedClients.txt")
assert_equal(data.num_vehicle_types, 2)
veh_type1 = data.vehicle_type(0)
assert_equal(veh_type1.capacity, [10])
assert_equal(veh_type1.num_available, 2)
assert_equal(veh_type1.profile, 0)
distance_matrix = data.distance_matrix(veh_type1.profile)
duration_matrix = data.duration_matrix(veh_type1.profile)
# First vehicle type has no vehicle-client restrictions.
assert_(np.all(distance_matrix != MAX_VALUE))
assert_(np.all(duration_matrix != MAX_VALUE))
veh_type2 = data.vehicle_type(1)
assert_equal(veh_type2.capacity, [10])
assert_equal(veh_type2.num_available, 1)
assert_equal(veh_type2.profile, 1)
distance_matrix = data.distance_matrix(veh_type2.profile)
duration_matrix = data.duration_matrix(veh_type2.profile)
# Second vehicle type is not allowed to serve client idx 4.
assert_equal(distance_matrix[:3, 4], MAX_VALUE)
assert_equal(distance_matrix[4, :3], MAX_VALUE)
assert_equal(duration_matrix[:3, 4], MAX_VALUE)
assert_equal(duration_matrix[4, :3], MAX_VALUE)
def test_sdvrptw_instance():
"""
Tests that reading an SDVRPTW instance happens correctly, particularly the
heterogeneous vehicles data sections.
"""
data = read("data/PR01.vrp")
# One routing profile per unique client group.
assert_equal(data.num_profiles, 4)
assert_equal(len(data.distance_matrices()), 4)
assert_equal(len(data.duration_matrices()), 4)
# Each vehicle type has a different capacity. We only check the first two.
veh_type1 = data.vehicle_type(0)
assert_equal(veh_type1.num_available, 2)
assert_equal(veh_type1.capacity, [100])
assert_equal(veh_type1.max_duration, 500)
assert_equal(veh_type1.profile, 0)
veh_type2 = data.vehicle_type(1)
assert_equal(veh_type2.num_available, 2)
assert_equal(veh_type2.capacity, [150])
assert_equal(veh_type2.max_duration, 500)
assert_equal(veh_type2.profile, 1)
# The first vehicle type cannot serve clients 38-48. Let's check that the
# distance and duration matrices reflect this.
distance_matrix = data.distance_matrix(veh_type1.profile)
duration_matrix = data.duration_matrix(veh_type1.profile)
for client in range(38, 48):
# This avoids checking diagonals.
idcs = [idx for idx in range(data.num_locations) if idx != client]
assert_equal(distance_matrix[idcs, client], MAX_VALUE)
assert_equal(distance_matrix[client, idcs], MAX_VALUE)
assert_equal(duration_matrix[idcs, client], MAX_VALUE)
assert_equal(duration_matrix[client, idcs], MAX_VALUE)
def test_read_solution_single_vehicle_type(ok_small):
"""
Tests that reading a solution with a single vehicle type works correctly.
"""
solution = read_solution("data/OkSmall.sol", ok_small)
routes = solution.routes()
assert_equal(str(routes[0]), "C0 C1")
assert_equal(str(routes[1]), "C2 C3")
assert_equal(routes[0].vehicle_type(), 0)
assert_equal(routes[1].vehicle_type(), 0)
def test_read_solution_multiple_vehicle_types(ok_small_multi_depot):
"""
Tests that reading a solution with multiple vehicle types works correctly.
"""
solution = read_solution(
"data/OkSmallMultipleDepots.sol", ok_small_multi_depot
)
routes = solution.routes()
# The solution file shows three routes, but empty routes are ignored.
assert_equal(solution.num_routes(), 2)
assert_equal(str(routes[0]), "C0")
assert_equal(str(routes[1]), "C1 C2")
# The instance has two vehicle types: two of the first type and one of the
# second type. Because the second route was empty, the second vehicle of
# the first type is not used.
assert_equal(routes[0].vehicle_type(), 0)
assert_equal(routes[1].vehicle_type(), 1)
def test_multi_trip_instance():
"""
Tests that a small multi-trip instance with vehicle reload options is
parsed correctly.
"""
data = read("data/OkSmallMultipleTrips.txt")
assert_equal(data.num_depots, 1)
assert_equal(data.num_vehicles, 3)
assert_equal(data.num_vehicle_types, 2)
veh_type1 = data.vehicle_type(0)
assert_equal(veh_type1.num_available, 2)
assert_equal(veh_type1.reload_depots, [0])
assert_equal(veh_type1.max_reloads, 1)
veh_type2 = data.vehicle_type(1)
assert_equal(veh_type2.num_available, 1)
assert_equal(veh_type2.reload_depots, [])
assert_equal(veh_type1.max_reloads, 1)
def test_read_solution_multiple_reload_depots():
"""
Tests that reading a solution to a problem with multiple reload depots is
parsed correctly.
"""
data = read("data/OkSmallMultipleReloadDepots.txt")
solution = read_solution("data/OkSmallMultipleReloadDepots.sol", data)
assert_equal(solution.num_routes(), 1)
assert_equal(solution.num_trips(), 2)
route = solution.routes()[0]
assert_equal(str(route), "C0 | C1 C2")
# Start and end depots are D0, but the reload happens at D1.
assert_equal(route.start_depot(), 0)
assert_equal(route.end_depot(), 0)
assert_equal(route[2], Activity("D1"))
def test_2d_data_sections_are_correctly_casted_from_1d():
"""
Tests that data sections that are expected to be 2D arrays (reload depots,
allowed clients, mutually exclusive groups) are correctly cast from 1D
arrays. This happens when the data section only has one element per row.
"""
data = read("data/CastDataSection2D.txt")
for idx, veh_type in enumerate(data.vehicle_types()):
assert_equal(veh_type.reload_depots, [idx])
dist = data.distance_matrix(veh_type.profile)
dur = data.duration_matrix(veh_type.profile)
# Client at location 3 is allowed.
assert_(np.all(dist[: data.num_depots, 3] != MAX_VALUE))
assert_(np.all(dur[: data.num_depots, 3] != MAX_VALUE))
# Client at location 4 is not allowed.
assert_equal(dist[: data.num_depots, 4], MAX_VALUE)
assert_equal(dur[: data.num_depots, 4], MAX_VALUE)
# No groups because groups with 1 client are ignored.
assert_equal(data.num_groups, 0)
def test_reading_unit_distance_cost():
"""
Tests that reading an instance with unit distance cost works correctly,
particularly that the unit distance costs are correctly added to the
vehicle types and are not affected by the round func.
"""
data = read("data/OkSmallUnitDistanceCost.txt", "exact")
assert_equal(data.num_vehicle_types, 3)
for idx, veh_type in enumerate(data.vehicle_types(), 1):
assert_equal(veh_type.unit_distance_cost, idx)
def test_read_hfvrp_instance():
"""
Tests that reading a HFVRP instance happens correctly, particularly the
heterogeneous vehicles data sections.
"""
data = read("data/X115-HVRP.vrp", "exact")
# One routing profile per unique unit distance cost.
assert_equal(data.num_vehicles, 19)
assert_equal(data.num_vehicle_types, 3)
# Each vehicle type has different attributes. We only check the first two.
veh_type1 = data.vehicle_type(0)
assert_equal(veh_type1.num_available, 11)
assert_equal(veh_type1.capacity, [54_000])
assert_equal(veh_type1.fixed_cost, 14_600_000)
assert_equal(veh_type1.unit_distance_cost, 58)
veh_type2 = data.vehicle_type(1)
assert_equal(veh_type2.num_available, 7)
assert_equal(veh_type2.capacity, [131_000])
assert_equal(veh_type2.fixed_cost, 43_600_000)
assert_equal(veh_type2.unit_distance_cost, 100)
def test_read_hfvrp_solution():
"""
Tests that reading a HFVRP solution results in the correct routes and
objective value.
"""
data = read("data/X115-HVRP.vrp", "exact")
sol = read_solution("data/X115-HVRP.sol", data)
routes = sol.routes()
def route2clients(route):
return [activity.idx for activity in route if activity.is_client()]
assert_equal(route2clients(routes[1]), [58, 34, 98, 48, 78, 46, 108, 17])
assert_equal(route2clients(routes[-1]), [4, 5, 2, 92, 41, 8])
assert_equal(routes[1].vehicle_type(), 0)
assert_equal(routes[-1].vehicle_type(), 2)
cost_eval = CostEvaluator([0], 0, 0)
assert_equal(cost_eval.cost(sol), 1941256006)
def test_reading_small_shipments():
"""
Tests that reading the SmallShipments instance correctly parses all the
pickup and delivery pairs.
"""
data = read("data/SmallShipments.txt")
assert_equal(data.num_shipments, 4)
assert_equal(data.num_clients, 0)
shipments = data.shipments()
# This data is all parsed from the PICKUP_AND_DELIVERY_SECTION of the
# instance.
assert_equal(shipments[0].pickup.location, 2)
assert_equal(shipments[0].pickup.tw_early, 12_800)
assert_equal(shipments[0].pickup.tw_late, 17_900)
assert_equal(shipments[0].pickup.service_duration, 900)
assert_equal(shipments[0].delivery.location, 1)
assert_equal(shipments[0].delivery.tw_early, 7_500)
assert_equal(shipments[0].delivery.tw_late, 80_900)
assert_equal(shipments[0].delivery.service_duration, 900)
assert_equal(shipments[1].pickup.location, 3)
assert_equal(shipments[1].pickup.tw_early, 14_700)
assert_equal(shipments[1].pickup.tw_late, 21_900)
assert_equal(shipments[1].pickup.service_duration, 900)
assert_equal(shipments[1].delivery.location, 4)
assert_equal(shipments[1].delivery.tw_early, 61_600)
assert_equal(shipments[1].delivery.tw_late, 66_100)
assert_equal(shipments[1].delivery.service_duration, 900)
assert_equal(shipments[2].pickup.location, 5)
assert_equal(shipments[2].pickup.tw_early, 47_800)
assert_equal(shipments[2].pickup.tw_late, 53_100)
assert_equal(shipments[2].pickup.service_duration, 900)
assert_equal(shipments[2].delivery.location, 6)
assert_equal(shipments[2].delivery.tw_early, 55_300)
assert_equal(shipments[2].delivery.tw_late, 60_200)
assert_equal(shipments[2].delivery.service_duration, 900)
assert_equal(shipments[3].pickup.location, 8)
assert_equal(shipments[3].pickup.tw_early, 35_100)
assert_equal(shipments[3].pickup.tw_late, 38_600)
assert_equal(shipments[3].pickup.service_duration, 900)
assert_equal(shipments[3].delivery.location, 7)
assert_equal(shipments[3].delivery.tw_early, 61_600)
assert_equal(shipments[3].delivery.tw_late, 68_000)
assert_equal(shipments[3].delivery.service_duration, 900)
def test_reading_small_shipments_solution():
"""
Tests that reading a solution for the SmallShipments instance maps the
route visits to the correct pickup and delivery activities.
"""
data = read("data/SmallShipments.txt")
sol = read_solution("data/SmallShipments.sol", data)
assert_(sol.is_feasible())
assert_equal(sol.num_routes(), 2)
assert_equal(sol.distance(), 65_128)
routes = sol.routes()
# The first route serves shipments 1 and 2, the second shipments 0 and 3.
assert_equal(str(routes[0]), "L1 L2 U2 U1")
assert_equal(str(routes[1]), "L0 U0 L3 U3")