From 1fdf02ae5e0dc903f8ca179b8290469d643c27fa Mon Sep 17 00:00:00 2001 From: etienne Date: Thu, 19 Sep 2019 00:04:49 +0200 Subject: [PATCH 1/4] handling floating point errors --- src/push_relabel.jl | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/push_relabel.jl b/src/push_relabel.jl index b4ed27a..060f49b 100644 --- a/src/push_relabel.jl +++ b/src/push_relabel.jl @@ -169,6 +169,21 @@ function relabel! end nothing end +""" + is_zero(value) + +Test if the value is equal to zero. It handles floating point errors. +""" +function is_zero end +function is_zero( + value::T + ) where {T} + if isa(value,AbstractFloat) + return isapprox(value, 0, atol = eps(T)) + else + return value == 0 + end +end """ discharge!(residual_graph, v, capacity_matrix, flow_matrix, excess, height, active, count, Q) @@ -189,11 +204,11 @@ function discharge! end Q::AbstractVector # FIFO queue ) for to in lg.outneighbors(residual_graph, v) - excess[v] == 0 && break + is_zero(excess[v]) && break push_flow!(residual_graph, v, to, capacity_matrix, flow_matrix, excess, height, active, Q) end - if excess[v] > 0 + if ! is_zero(excess[v]) if count[height[v] + 1] == 1 gap!(residual_graph, height[v], excess, height, active, count, Q) else From cc870147c74981a1a0ca9832954f5adf8db9d02b Mon Sep 17 00:00:00 2001 From: etienneINSA Date: Mon, 25 Nov 2019 14:24:01 +0100 Subject: [PATCH 2/4] handles floating point on dinic's algorithm --- src/dinic.jl | 4 ++-- src/maximum_flow.jl | 16 ++++++++++++++++ src/push_relabel.jl | 16 ---------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/dinic.jl b/src/dinic.jl index f10c1c8..681038d 100644 --- a/src/dinic.jl +++ b/src/dinic.jl @@ -21,7 +21,7 @@ function dinic_impl end while true augment = blocking_flow!(residual_graph, source, target, capacity_matrix, flow_matrix, P) - augment == 0 && break + is_zero(augment) && break flow += augment end return flow, flow_matrix @@ -80,7 +80,7 @@ function blocking_flow! end end end - flow == 0 && continue # Flow cannot be augmented along path + is_zero(flow) && continue # Flow cannot be augmented along path v = target u = bv diff --git a/src/maximum_flow.jl b/src/maximum_flow.jl index 7b0fd1a..be1e1c1 100644 --- a/src/maximum_flow.jl +++ b/src/maximum_flow.jl @@ -178,3 +178,19 @@ function maximum_flow( end return maximum_flow(flow_graph, source, target, capacity_matrix, algorithm) end + +""" + is_zero(value) + +Test if the value is equal to zero. It handles floating point errors. +""" +function is_zero end +function is_zero( + value::T + ) where {T} + if isa(value,AbstractFloat) + return isapprox(value, 0, atol = eps(T)) + else + return value == 0 + end +end diff --git a/src/push_relabel.jl b/src/push_relabel.jl index 060f49b..e22d81b 100644 --- a/src/push_relabel.jl +++ b/src/push_relabel.jl @@ -169,22 +169,6 @@ function relabel! end nothing end -""" - is_zero(value) - -Test if the value is equal to zero. It handles floating point errors. -""" -function is_zero end -function is_zero( - value::T - ) where {T} - if isa(value,AbstractFloat) - return isapprox(value, 0, atol = eps(T)) - else - return value == 0 - end -end - """ discharge!(residual_graph, v, capacity_matrix, flow_matrix, excess, height, active, count, Q) From fe2aeff16636b31c8fdcce4b32457eb32d59c99d Mon Sep 17 00:00:00 2001 From: etienneINSA Date: Tue, 26 Nov 2019 09:47:31 +0100 Subject: [PATCH 3/4] wider tolerance for floating point --- src/maximum_flow.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/maximum_flow.jl b/src/maximum_flow.jl index be1e1c1..093aaad 100644 --- a/src/maximum_flow.jl +++ b/src/maximum_flow.jl @@ -189,7 +189,7 @@ function is_zero( value::T ) where {T} if isa(value,AbstractFloat) - return isapprox(value, 0, atol = eps(T)) + return isapprox(value, 0, atol = sqrt(eps(T))) else return value == 0 end From 60f2f8a3cd0847b2fa4b617c01d2c85543e46e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Mon, 12 Oct 2020 17:43:08 +0200 Subject: [PATCH 4/4] Update src/push_relabel.jl --- src/push_relabel.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/push_relabel.jl b/src/push_relabel.jl index e22d81b..88313b1 100644 --- a/src/push_relabel.jl +++ b/src/push_relabel.jl @@ -192,7 +192,7 @@ function discharge! end push_flow!(residual_graph, v, to, capacity_matrix, flow_matrix, excess, height, active, Q) end - if ! is_zero(excess[v]) + if excess[v] ≉ zero(excess[v]) if count[height[v] + 1] == 1 gap!(residual_graph, height[v], excess, height, active, count, Q) else