From 5698a13cedbf301cc74b6f01fac95473af6f7036 Mon Sep 17 00:00:00 2001 From: mathieu17g <72861595+mathieu17g@users.noreply.github.com> Date: Fri, 25 Jun 2021 13:18:16 +0200 Subject: [PATCH 1/4] Add optimizer kwargs handing --- src/mincost.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mincost.jl b/src/mincost.jl index 45f7b71..37149fd 100644 --- a/src/mincost.jl +++ b/src/mincost.jl @@ -28,6 +28,7 @@ positive for sink nodes, and zero for all other nodes. - `edge_demand::Union{Nothing,AbstractMatrix}`: require a minimum flow for edges, or nothing. - `source_nodes` Collection of sources at which the nodal netflow is allowed to be greater than nodal demand, defaults to an empty tuple. - `sink_nodes` Collection of sinks at which the nodal netflow is allowed to be less than nodal demand, defaults to an empty tuple. +- `optimizer_kwargs...` optimzer kwargs... passed to JuMP.Model via JuMP.set_optimizer_attribute function `source_nodes` & `sink_nodes` are only needed when nodal flow are not explictly set in node_demand @@ -69,10 +70,12 @@ function mincost_flow end optimizer; edge_demand::Union{Nothing,AbstractMatrix} = nothing, source_nodes = (), # Source nodes at which to allow a netflow greater than nodal demand - sink_nodes = () # Sink nodes at which to allow a netflow less than nodal demand - ) where {AG <: lg.AbstractGraph} + sink_nodes = (), # Sink nodes at which to allow a netflow less than nodal demand + optimizer_kwargs...) where {AG <: lg.AbstractGraph} m = JuMP.Model(optimizer) + for (k, v) in optimizer_kwargs + set_optimizer_attribute(m, string(k), v) vtxs = vertices(g) source_nodes = [v for v in vtxs if v in source_nodes || node_demand[v] < 0] From 8e161be333caa6f5f8d596513d9aa21496ebe6ea Mon Sep 17 00:00:00 2001 From: mathieu17g <72861595+mathieu17g@users.noreply.github.com> Date: Fri, 25 Jun 2021 13:23:15 +0200 Subject: [PATCH 2/4] Add optimizer kwargs handling --- src/mincost.jl | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mincost.jl b/src/mincost.jl index 37149fd..fd02614 100644 --- a/src/mincost.jl +++ b/src/mincost.jl @@ -64,36 +64,36 @@ julia> flow = mincost_flow(g, demand, capacity, cost, Clp.Optimizer) function mincost_flow end @traitfn function mincost_flow(g::AG::lg.IsDirected, - node_demand::AbstractVector, - edge_capacity::AbstractMatrix, - edge_cost::AbstractMatrix, - optimizer; - edge_demand::Union{Nothing,AbstractMatrix} = nothing, - source_nodes = (), # Source nodes at which to allow a netflow greater than nodal demand - sink_nodes = (), # Sink nodes at which to allow a netflow less than nodal demand - optimizer_kwargs...) where {AG <: lg.AbstractGraph} + node_demand::AbstractVector, + edge_capacity::AbstractMatrix, + edge_cost::AbstractMatrix, + optimizer; + edge_demand::Union{Nothing,AbstractMatrix} = nothing, + source_nodes = (), # Source nodes at which to allow a netflow greater than nodal demand + sink_nodes = (), # Sink nodes at which to allow a netflow less than nodal demand + optimizer_kwargs...) where {AG <: lg.AbstractGraph} - m = JuMP.Model(optimizer) + m = JuMP.Model(optimizer) for (k, v) in optimizer_kwargs set_optimizer_attribute(m, string(k), v) - vtxs = vertices(g) + vtxs = vertices(g) - source_nodes = [v for v in vtxs if v in source_nodes || node_demand[v] < 0] - sink_nodes = [v for v in vtxs if v in sink_nodes || node_demand[v] > 0] + source_nodes = [v for v in vtxs if v in source_nodes || node_demand[v] < 0] + sink_nodes = [v for v in vtxs if v in sink_nodes || node_demand[v] > 0] - @variable(m, 0 <= f[i=vtxs,j=vtxs; (i,j) in lg.edges(g)] <= edge_capacity[i, j]) - @objective(m, Min, sum(f[src(e),dst(e)] * edge_cost[src(e), dst(e)] for e in lg.edges(g))) + @variable(m, 0 <= f[i=vtxs,j=vtxs; (i,j) in lg.edges(g)] <= edge_capacity[i, j]) + @objective(m, Min, sum(f[src(e),dst(e)] * edge_cost[src(e), dst(e)] for e in lg.edges(g))) - for v in lg.vertices(g) - if v in source_nodes + for v in lg.vertices(g) + if v in source_nodes @constraint(m, sum(f[v, vout] for vout in outneighbors(g, v)) - sum(f[vin, v] for vin in lg.inneighbors(g, v)) >= -node_demand[v] ) - elseif v in sink_nodes + elseif v in sink_nodes @constraint(m, sum(f[vin, v] for vin in lg.inneighbors(g, v)) - sum(f[v, vout] for vout in outneighbors(g, v)) >= node_demand[v] ) - else + else @constraint(m, sum(f[vin, v] for vin in lg.inneighbors(g, v)) == sum(f[v, vout] for vout in outneighbors(g, v)) ) From 85b225d57471efd45b283a1628c096b5e317b81b Mon Sep 17 00:00:00 2001 From: mathieu17g <72861595+mathieu17g@users.noreply.github.com> Date: Fri, 25 Jun 2021 13:45:19 +0200 Subject: [PATCH 3/4] Add optimizer kwargs handling in mincost_flow --- src/mincost.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mincost.jl b/src/mincost.jl index fd02614..cae772e 100644 --- a/src/mincost.jl +++ b/src/mincost.jl @@ -28,7 +28,7 @@ positive for sink nodes, and zero for all other nodes. - `edge_demand::Union{Nothing,AbstractMatrix}`: require a minimum flow for edges, or nothing. - `source_nodes` Collection of sources at which the nodal netflow is allowed to be greater than nodal demand, defaults to an empty tuple. - `sink_nodes` Collection of sinks at which the nodal netflow is allowed to be less than nodal demand, defaults to an empty tuple. -- `optimizer_kwargs...` optimzer kwargs... passed to JuMP.Model via JuMP.set_optimizer_attribute function +- `optimizer_kwargs...` optimizer kwargs passed to JuMP.Model via JuMP.set_optimizer_attribute function `source_nodes` & `sink_nodes` are only needed when nodal flow are not explictly set in node_demand From 0ada2085163430fc4cba68444fd28d9d200371b3 Mon Sep 17 00:00:00 2001 From: mathieu17g <72861595+mathieu17g@users.noreply.github.com> Date: Fri, 25 Jun 2021 17:50:02 +0200 Subject: [PATCH 4/4] Corrected a missing `end` in JuMP.Model attributes setting loop --- src/mincost.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mincost.jl b/src/mincost.jl index cae772e..c86198e 100644 --- a/src/mincost.jl +++ b/src/mincost.jl @@ -76,6 +76,7 @@ function mincost_flow end m = JuMP.Model(optimizer) for (k, v) in optimizer_kwargs set_optimizer_attribute(m, string(k), v) + end vtxs = vertices(g) source_nodes = [v for v in vtxs if v in source_nodes || node_demand[v] < 0]