|
| 1 | +""" |
| 2 | + warcraft_grid_graph(costs::AbstractMatrix; acyclic::Bool=false) |
| 3 | +
|
| 4 | +Convert a grid of Warcraft cell costs into a weighted directed graph from [SimpleWeightedGraphs.jl](https://github.com/JuliaGraphs/SimpleWeightedGraphs.jl), where the vertices correspond to the cells and the edges are weighted by the cost of the arrival cell. |
| 5 | +
|
| 6 | +This represents the Warcraft shortest paths problem of |
| 7 | +
|
| 8 | +> [Differentiation of Blackbox Combinatorial Solvers](https://openreview.net/forum?id=BkevoJSYPB), Vlastelica et al. (2019) |
| 9 | +
|
| 10 | +- If `acyclic = false`, a cell has edges to each one of its 8 neighbors. |
| 11 | +- If `acyclic = true`, a cell has edges to its south, east and southeast neighbors only (ensures an acyclic graph where topological sort will work) |
| 12 | +""" |
| 13 | +function warcraft_grid_graph(costs::AbstractMatrix{R}; acyclic::Bool = false) where {R} |
| 14 | + h, w = size(costs) |
| 15 | + V = h * w |
| 16 | + E = count_edges(h, w; acyclic) |
| 17 | + |
| 18 | + sources = Int[] |
| 19 | + destinations = Int[] |
| 20 | + weights = R[] |
| 21 | + |
| 22 | + sizehint!(sources, E) |
| 23 | + sizehint!(destinations, E) |
| 24 | + sizehint!(weights, E) |
| 25 | + |
| 26 | + for v1 = 1:V |
| 27 | + i1, j1 = index_to_coord(v1, h, w) |
| 28 | + for Δi in (-1, 0, 1), Δj in (-1, 0, 1) |
| 29 | + i2, j2 = i1 + Δi, j1 + Δj |
| 30 | + valid_destination = 1 <= i2 <= h && 1 <= j2 <= w |
| 31 | + valid_step = if acyclic |
| 32 | + (Δi != 0 || Δj != 0) && Δi >= 0 && Δj >= 0 |
| 33 | + else |
| 34 | + (Δi != 0 || Δj != 0) |
| 35 | + end |
| 36 | + if valid_destination && valid_step |
| 37 | + v2 = coord_to_index(i2, j2, h, w) |
| 38 | + push!(sources, v1) |
| 39 | + push!(destinations, v2) |
| 40 | + push!(weights, costs[v2]) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + return SimpleWeightedDiGraph(sources, destinations, weights) |
| 46 | +end |
| 47 | + |
| 48 | +function count_edges(h::Integer, w::Integer; acyclic::Bool) |
| 49 | + @assert h >= 2 && w >= 2 |
| 50 | + if acyclic |
| 51 | + return (h - 1) * (w - 1) * 3 + ((h - 1) + (w - 1)) * 1 + 0 |
| 52 | + else |
| 53 | + return (h - 2) * (w - 2) * 8 + (2(h - 2) + 2(w - 2)) * 5 + 4 * 3 |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +function possible_neighbors(i::Integer, j::Integer) |
| 58 | + return ( |
| 59 | + # col - 1 |
| 60 | + (i - 1, j - 1), |
| 61 | + (i + 0, j - 1), |
| 62 | + (i + 1, j - 1), |
| 63 | + # col 0 |
| 64 | + (i - 1, j + 0), |
| 65 | + (i + 1, j + 0), |
| 66 | + # col + 1 |
| 67 | + (i - 1, j + 1), |
| 68 | + (i + 0, j + 1), |
| 69 | + (i + 1, j + 1), |
| 70 | + ) |
| 71 | +end |
| 72 | + |
| 73 | +""" |
| 74 | + coord_to_index(i, j, h, w) |
| 75 | +
|
| 76 | +Given a pair of row-column coordinates `(i, j)` on a grid of size `(h, w)`, compute the corresponding vertex index in the graph generated by [`warcraft_grid_graph`](@ref). |
| 77 | +""" |
| 78 | +function coord_to_index(i::Integer, j::Integer, h::Integer, w::Integer) |
| 79 | + if (1 <= i <= h) && (1 <= j <= w) |
| 80 | + v = (j - 1) * h + (i - 1) + 1 # enumerate column by column |
| 81 | + return v |
| 82 | + else |
| 83 | + return 0 |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +""" |
| 88 | + index_to_coord(v, h, w) |
| 89 | +
|
| 90 | +Given a vertex index in the graph generated by [`warcraft_grid_graph`](@ref), computethe corresponding row-column coordinates `(i, j)` on a grid of size `(h, w)`. |
| 91 | +""" |
| 92 | +function index_to_coord(v::Integer, h::Integer, w::Integer) |
| 93 | + if 1 <= v <= h * w |
| 94 | + j = (v - 1) ÷ h + 1 |
| 95 | + i = (v - 1) - h * (j - 1) + 1 |
| 96 | + return (i, j) |
| 97 | + else |
| 98 | + return (0, 0) |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +function get_path(parents::AbstractVector{<:Integer}, s::Integer, d::Integer) |
| 103 | + path = [d] |
| 104 | + v = d |
| 105 | + while v != s |
| 106 | + v = parents[v] |
| 107 | + pushfirst!(path, v) |
| 108 | + end |
| 109 | + return path |
| 110 | +end |
0 commit comments