|
| 1 | +""" |
| 2 | +$TYPEDSIGNATURES |
| 3 | +
|
| 4 | +Create the acyclic digraph associated with the given VSP `instance`. |
| 5 | +""" |
| 6 | +function create_graph(instance::VSPInstance) |
| 7 | + (; duration, start_time, service_time) = instance |
| 8 | + # Initialize directed graph |
| 9 | + nb_vertices = nb_locations(instance) |
| 10 | + graph = SimpleDiGraph(nb_vertices) |
| 11 | + |
| 12 | + depot = 1 # depot is always index 1 |
| 13 | + customers = 2:nb_vertices # other vertices are customers |
| 14 | + |
| 15 | + # Create existing edges |
| 16 | + for i₁ in customers |
| 17 | + # link every task to depot |
| 18 | + add_edge!(graph, depot, i₁) |
| 19 | + add_edge!(graph, i₁, depot) |
| 20 | + |
| 21 | + t₁ = start_time[i₁] |
| 22 | + for i₂ in (i₁ + 1):nb_vertices |
| 23 | + t₂ = start_time[i₂] |
| 24 | + |
| 25 | + if t₁ <= t₂ |
| 26 | + if t₁ + service_time[i₁] + duration[i₁, i₂] <= t₂ |
| 27 | + add_edge!(graph, i₁, i₂) |
| 28 | + end |
| 29 | + else |
| 30 | + if t₂ + service_time[i₂] + duration[i₂, i₁] <= t₁ |
| 31 | + add_edge!(graph, i₂, i₁) |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + return graph |
| 38 | +end |
| 39 | + |
| 40 | +""" |
| 41 | +$TYPEDSIGNATURES |
| 42 | +
|
| 43 | +Create the acyclic digraph associated with the given VSP `state`. |
| 44 | +""" |
| 45 | +function create_graph(state::VSPState) |
| 46 | + return create_graph(state.instance) |
| 47 | +end |
| 48 | + |
| 49 | +""" |
| 50 | +$TYPEDSIGNATURES |
| 51 | +
|
| 52 | +Retrieve routes solution from the given MIP solution `y` matrix and `graph`. |
| 53 | +""" |
| 54 | +function retrieve_routes(y::AbstractArray, graph::AbstractGraph) |
| 55 | + nb_tasks = nv(graph) |
| 56 | + job_indices = 2:(nb_tasks) |
| 57 | + routes = Vector{Int}[] |
| 58 | + |
| 59 | + start = [i for i in job_indices if y[1, i] ≈ 1] |
| 60 | + for task in start |
| 61 | + route = Int[] |
| 62 | + current_task = task |
| 63 | + while current_task != 1 # < nb_tasks |
| 64 | + push!(route, current_task) |
| 65 | + local next_task |
| 66 | + for i in outneighbors(graph, current_task) |
| 67 | + if isapprox(y[current_task, i], 1; atol=0.1) |
| 68 | + next_task = i |
| 69 | + break |
| 70 | + end |
| 71 | + end |
| 72 | + current_task = next_task |
| 73 | + end |
| 74 | + push!(routes, route) |
| 75 | + end |
| 76 | + return routes |
| 77 | +end |
| 78 | + |
| 79 | +""" |
| 80 | +$TYPEDSIGNATURES |
| 81 | +
|
| 82 | +Solve the Prize Collecting Vehicle Scheduling Problem defined by `instance` and prize vector `θ`. |
| 83 | +""" |
| 84 | +function prize_collecting_vsp( |
| 85 | + θ::AbstractVector; instance::VSPState, model_builder=highs_model, kwargs... |
| 86 | +) |
| 87 | + (; duration) = instance.instance |
| 88 | + graph = create_graph(instance) |
| 89 | + |
| 90 | + model = model_builder() |
| 91 | + set_silent(model) |
| 92 | + |
| 93 | + nb_nodes = nv(graph) |
| 94 | + job_indices = 2:(nb_nodes) |
| 95 | + |
| 96 | + @variable(model, y[i=1:nb_nodes, j=1:nb_nodes; has_edge(graph, i, j)] >= 0) |
| 97 | + |
| 98 | + θ_ext = fill(0.0, nb_locations(instance)) # no prize for must dispatch requests, only hard constraints |
| 99 | + θ_ext[instance.is_postponable] .= θ |
| 100 | + |
| 101 | + @objective( |
| 102 | + model, |
| 103 | + Max, |
| 104 | + sum( |
| 105 | + (θ_ext[dst(edge)] - duration[src(edge), dst(edge)]) * y[src(edge), dst(edge)] |
| 106 | + for edge in edges(graph) |
| 107 | + ) |
| 108 | + ) |
| 109 | + @constraint( |
| 110 | + model, |
| 111 | + flow[i in 2:nb_nodes], |
| 112 | + sum(y[j, i] for j in inneighbors(graph, i)) == |
| 113 | + sum(y[i, j] for j in outneighbors(graph, i)) |
| 114 | + ) |
| 115 | + @constraint( |
| 116 | + model, demand[i in job_indices], sum(y[j, i] for j in inneighbors(graph, i)) <= 1 |
| 117 | + ) |
| 118 | + # must dispatch constraints |
| 119 | + @constraint( |
| 120 | + model, |
| 121 | + demand_must_dispatch[i in job_indices; instance.is_must_dispatch[i]], |
| 122 | + sum(y[j, i] for j in inneighbors(graph, i)) == 1 |
| 123 | + ) |
| 124 | + |
| 125 | + optimize!(model) |
| 126 | + |
| 127 | + return retrieve_routes(value.(y), graph) |
| 128 | +end |
| 129 | + |
| 130 | +# ? |
| 131 | +function prize_collecting_vsp_Q( |
| 132 | + θ::AbstractVector, |
| 133 | + vals::AbstractVector; |
| 134 | + instance::VSPState, |
| 135 | + model_builder=highs_model, |
| 136 | + kwargs..., |
| 137 | +) |
| 138 | + (; duration) = instance.instance |
| 139 | + graph = create_graph(instance) |
| 140 | + model = model_builder() |
| 141 | + set_silent(model) |
| 142 | + nb_nodes = nv(graph) |
| 143 | + job_indices = 2:(nb_nodes) |
| 144 | + @variable(model, y[i=1:nb_nodes, j=1:nb_nodes; has_edge(graph, i, j)] >= 0) |
| 145 | + θ_ext = fill(0.0, nb_locations(instance.instance)) # no prize for must dispatch requests, only hard constraints |
| 146 | + θ_ext[instance.is_postponable] .= θ |
| 147 | + # v_ext = fill(0.0, nb_locations(instance.instance)) # no prize for must dispatch requests, only hard constraints |
| 148 | + # v_ext[instance.is_postponable] .= vals |
| 149 | + @objective( |
| 150 | + model, |
| 151 | + Max, |
| 152 | + sum( |
| 153 | + (θ_ext[dst(edge)] + vals[dst(edge)] - duration[src(edge), dst(edge)]) * |
| 154 | + y[src(edge), dst(edge)] for edge in edges(graph) |
| 155 | + ) |
| 156 | + ) |
| 157 | + @constraint( |
| 158 | + model, |
| 159 | + flow[i in 2:nb_nodes], |
| 160 | + sum(y[j, i] for j in inneighbors(graph, i)) == |
| 161 | + sum(y[i, j] for j in outneighbors(graph, i)) |
| 162 | + ) |
| 163 | + @constraint( |
| 164 | + model, demand[i in job_indices], sum(y[j, i] for j in inneighbors(graph, i)) <= 1 |
| 165 | + ) |
| 166 | + # must dispatch constraints |
| 167 | + @constraint( |
| 168 | + model, |
| 169 | + demand_must_dispatch[i in job_indices; instance.is_must_dispatch[i]], |
| 170 | + sum(y[j, i] for j in inneighbors(graph, i)) == 1 |
| 171 | + ) |
| 172 | + optimize!(model) |
| 173 | + return retrieve_routes(value.(y), graph) |
| 174 | +end |
| 175 | + |
| 176 | +function my_objective_value(θ, routes; instance) |
| 177 | + (; duration) = instance.instance |
| 178 | + total = 0.0 |
| 179 | + θ_ext = fill(0.0, nb_locations(instance)) |
| 180 | + θ_ext[instance.is_postponable] .= θ |
| 181 | + for route in routes |
| 182 | + for (u, v) in partition(vcat(1, route), 2, 1) |
| 183 | + total += θ_ext[v] - duration[u, v] |
| 184 | + end |
| 185 | + end |
| 186 | + return -total |
| 187 | +end |
| 188 | + |
| 189 | +function _objective_value(θ, routes; instance) |
| 190 | + (; duration) = instance.instance |
| 191 | + total = 0.0 |
| 192 | + θ_ext = fill(0.0, nb_locations(instance)) |
| 193 | + θ_ext[instance.is_postponable] .= θ |
| 194 | + mapping = cumsum(instance.is_postponable) |
| 195 | + g = falses(length(θ)) |
| 196 | + for route in routes |
| 197 | + for (u, v) in partition(vcat(1, route), 2, 1) |
| 198 | + total -= duration[u, v] |
| 199 | + if instance.is_postponable[v] |
| 200 | + total += θ_ext[v] |
| 201 | + g[mapping[v]] = 1 |
| 202 | + end |
| 203 | + end |
| 204 | + end |
| 205 | + return -total, g |
| 206 | +end |
| 207 | + |
| 208 | +function ChainRulesCore.rrule(::typeof(my_objective_value), θ, routes; instance) |
| 209 | + total, g = _objective_value(θ, routes; instance) |
| 210 | + function pullback(dy) |
| 211 | + g = g .* dy |
| 212 | + return NoTangent(), g, NoTangent() |
| 213 | + end |
| 214 | + return total, pullback |
| 215 | +end |
0 commit comments