| dodgr_flows_optalloc | R Documentation |
Solve the transportation problem of allocating flows from N
source ('from') points with associated densities to M target ('to')
points with finite capacities, such that total flow from each source is
fully allocated, no target receives more than its capacity, and total
allocation cost (source-to-target network distance) is minimized. The
resultant optimal allocation is then aggregated on to the network with
dodgr_flows_aggregate, so this function returns a graph with an
additional flow column, just like dodgr_flows_aggregate,
dodgr_flows_disperse, and dodgr_flows_si.
dodgr_flows_optalloc(
graph,
from,
to,
source_densities,
target_capacities,
control = list(algorithm = "sinkhorn"),
contract = TRUE,
heap = "BHeap",
norm_sums = TRUE,
quiet = TRUE
)
graph |
|
from |
Vector or matrix of points from which route distances are to be calculated, specified as one of the following:
|
to |
Vector or matrix of points to which route distances are to be
calculated. If |
source_densities |
Numeric vector of densities at each |
target_capacities |
Numeric vector of capacities at each |
control |
A named list controlling the allocation algorithm. Must
include an
|
contract |
If |
heap |
Type of heap to use in priority queue. Options include
Fibonacci Heap (default; |
norm_sums |
Standardise sums from all origin points, so sum of flows throughout entire network equals sum of densities from all origins (see Note). |
quiet |
If |
This function performs an initial call to dodgr_dists to
obtain the N x M matrix of shortest-path distances between all from and
to points. The optimal allocation is then obtained by numerical
optimization over that matrix alone (see control, below), with no further
path-finding required, before finally calling dodgr_flows_aggregate
with the resultant allocation matrix as its flows argument.
Because targets may collectively have more capacity than sources have
density, sum(source_densities) <= sum(target_capacities) must hold. This
is checked before any allocation is attempted.
The input graph, with an additional flow column added, similar to
behaviour of dodgr_flows_aggregate.
The "sinkhorn" algorithm is generally the faster choice, especially
for large numbers of source/target points, but only approximates the true
optimal allocation, with accuracy controlled by control$lambda. Use
control = list(algorithm = "lp") for the exact optimum, at the cost of
both requiring the lpSolve package and scaling less well to large
numbers of points.
Other flows:
dodgr_flows_aggregate(),
dodgr_flows_disperse(),
dodgr_flows_si()
graph <- weight_streetnet (hampi)
graphc <- dodgr_contract_graph (graph)
set.seed (1)
from <- sample (graphc$from_id, size = 10)
to <- sample (graphc$to_id, size = 5)
to <- to [!to %in% from]
source_densities <- runif (length (from))
target_capacities <- runif (length (to))
# scale target_capacities to ensure sum(source_densities) <=
# sum(target_capacities):
target_capacities <- target_capacities *
1.5 * sum (source_densities) / sum (target_capacities)
graph <- dodgr_flows_optalloc (
graph,
from = from,
to = to,
source_densities = source_densities,
target_capacities = target_capacities
)
# graph then has an additional 'flow' column, exactly as for
# 'dodgr_flows_aggregate'
# The exact optimum can be obtained instead with the 'lpSolve' package:
graph <- dodgr_flows_optalloc (
graph,
from = from,
to = to,
source_densities = source_densities,
target_capacities = target_capacities,
control = list (algorithm = "lp")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.