-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparsing.jl
More file actions
100 lines (94 loc) · 3.47 KB
/
parsing.jl
File metadata and controls
100 lines (94 loc) · 3.47 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
"""
$TYPEDSIGNATURES
Create a `VSPInstance` from file `filepath` containing a VRPTW instance.
It uses time window values to compute task times as the middle of the interval.
Round all values to `Int` if `rounded=true`.
Normalize all time values by the `normalization` parameter.
"""
function read_vsp_instance(filepath::String; normalization=3600.0, digits=2)
type = Float64 #rounded ? Int : Float64
mode = ""
edge_weight_type = ""
edge_weight_format = ""
duration_matrix = Vector{type}[]
nb_locations = 0
demand = type[]
service_time = type[]
coordinates = Matrix{type}(undef, 0, 2)
start_time = type[]
file = open(filepath, "r")
for line in eachline(file)
line = strip(line, [' ', '\n', '\t'])
if line == ""
continue
elseif startswith(line, "DIMENSION")
nb_locations = parse(Int, split(line, " : ")[2])
demand = zeros(type, nb_locations)
service_time = zeros(type, nb_locations)
coordinates = zeros(type, (nb_locations, 2))
start_time = zeros(type, nb_locations)
elseif startswith(line, "EDGE_WEIGHT_TYPE")
edge_weight_type = split(line, " : ")[2]
elseif startswith(line, "EDGE_WEIGHT_FORMAT")
edge_weight_format = split(line, " : ")[2]
elseif startswith(line, "NODE_COORD_SECTION")
mode = "coord"
elseif line == "DEMAND_SECTION"
mode = "demand"
elseif line == "DEPOT_SECTION"
mode = "depot"
elseif line == "EDGE_WEIGHT_SECTION"
mode = "edge_weights"
@assert edge_weight_type == "EXPLICIT"
@assert edge_weight_format == "FULL_MATRIX"
elseif line == "TIME_WINDOW_SECTION"
mode = "time_windows"
elseif line == "SERVICE_TIME_SECTION"
mode = "service_t"
elseif line == "EOF"
break
elseif mode == "coord"
node, x, y = split(line) # Split by whitespace or \t, skip duplicate whitespace
node = parse(Int, node)
x, y = (parse(type, x), parse(type, y))
coordinates[node, :] = [x, y]
elseif mode == "demand"
node, d = split(line)
node, d = parse(Int, node), parse(type, d)
if node == 1 # depot
@assert d == 0
end
demand[node] = d
elseif mode == "edge_weights"
push!(duration_matrix, [parse(type, e) for e in split(line)])
elseif mode == "service_t"
node, t = split(line)
node = parse(Int, node)
t = parse(type, t)
if node == 1 # depot
@assert t == 0
end
service_time[node] = t
elseif mode == "time_windows"
node, l, u = split(line)
node = parse(Int, node)
l, u = parse(type, l), parse(type, u)
start_time[node] = (u + l) / 2
end
end
close(file)
duration = mapreduce(permutedims, vcat, duration_matrix)
coordinate = [
Point(round(x / normalization; digits), round(y / normalization; digits)) for
(x, y) in zip(coordinates[:, 1], coordinates[:, 2])
]
service_time ./= normalization
start_time ./= normalization
duration ./= normalization
return StaticInstance(;
coordinate,
service_time=round.(service_time; digits),
start_time=round.(start_time; digits),
duration=round.(duration; digits),
)
end