-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance.jl
More file actions
129 lines (110 loc) · 3.41 KB
/
instance.jl
File metadata and controls
129 lines (110 loc) · 3.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
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
"""
$TYPEDEF
Instance of the stochastic VSP problem.
# Fields
$TYPEDFIELDS
"""
struct Instance{CC,G<:AbstractGraph,M1<:AbstractMatrix,M2<:AbstractMatrix,F,C}
"graph computed from `city` with the `create_VSP_graph(city::City)` method"
graph::G
"features matrix computed from `city`"
features::Matrix{F}
"slack matrix"
slacks::M1
"intrinsic delays scenario matrix"
intrinsic_delays::M2
"cost of a vehicle"
vehicle_cost::C
"cost of one minute delay"
delay_cost::C
"associated city"
city::CC
end
"""
$TYPEDSIGNATURES
Display a compact summary of an [`Instance`](@ref): number of tasks, scenarios, and edges.
"""
function Base.show(io::IO, instance::Instance)
return print(
io,
"VSP Instance($(get_nb_tasks(instance)) tasks, $(get_nb_scenarios(instance)) scenarios, $(ne(instance.graph)) arcs)",
)
end
"""
$TYPEDSIGNATURES
Return the acyclic directed graph corresponding to `city`.
Each vertex represents a task. Vertices are ordered by start time of corresponding task.
There is an edge from task u to task v the (end time of u + tie distance between u and v <= start time of v).
"""
function create_VSP_graph(city::City)
# Initialize directed graph
nb_vertices = city.nb_tasks + 2
graph = SimpleDiGraph(nb_vertices)
starting_task = 1
end_task = nb_vertices
job_tasks = 2:(city.nb_tasks + 1)
travel_times = [
distance(task1.end_point, task2.start_point) for task1 in city.tasks,
task2 in city.tasks
]
# Create existing edges
for iorigin in job_tasks
# link every task to base
add_edge!(graph, starting_task, iorigin)
add_edge!(graph, iorigin, end_task)
for idestination in (iorigin + 1):(city.nb_tasks + 1)
travel_time = travel_times[iorigin, idestination]
origin_end_time = city.tasks[iorigin].end_time
destination_begin_time = city.tasks[idestination].start_time # get_prop(graph, idestination, :task).start_time
# there is an edge only if we can reach destination from origin before start of task
if origin_end_time + travel_time <= destination_begin_time
add_edge!(graph, iorigin, idestination)
end
end
end
return graph
end
"""
$TYPEDSIGNATURES
Constructor for [`Instance`](@ref).
Build an `Instance` for the stochatsic vehicle scheduling problem, with `nb_tasks` tasks and `nb_scenarios` scenarios.
"""
function Instance(;
nb_tasks::Int,
nb_scenarios::Int,
rng::AbstractRNG=Random.default_rng(),
store_city=true,
kwargs...,
)
city = create_random_city(; rng=rng, nb_tasks, nb_scenarios, kwargs...)
graph = create_VSP_graph(city)
features = compute_features(city)
slacks = compute_slacks(city, graph)
intrinsic_delays = compute_delays(city)
return Instance(
graph,
features,
slacks,
intrinsic_delays,
city.vehicle_cost,
city.delay_cost,
store_city ? city : nothing,
)
end
"""
$TYPEDSIGNATURES
Returns the number of scenarios in instance.
"""
function get_nb_scenarios(instance::Instance)
return size(instance.intrinsic_delays, 2)
end
"""
$TYPEDSIGNATURES
Returns the number of tasks in `instance`.
"""
get_nb_tasks(instance::Instance) = nv(instance.graph) - 2
"""
$TYPEDSIGNATURES
Returns the feature matrix associated to `instance`.
"""
get_features(instance::Instance) = instance.features