Repository navigation
Conversation
dschult
left a comment
There was a problem hiding this comment.
OK Thanks!
Now I'd like to dive into the floating point troubles. With shortest_path, we could end up with incorrect shortest paths -- not just off by a "small" amount, but off by seemingly a lot more than the original round-off error.
Can that happen here? The new example here tells folks how to apply round-off to the final answer. But it doesn't apply round-off to each capacity in the network.
I think what we need is to convert all the capacity values to integers e.g. int(10*value) before calling nx.maximum_flow(G, "s", "t"). Another solution would involve changing maximum_flow to allow a capacity function (like we did with the shortest_path functions weight argument). I believe that is in #8406. So maybe this should wait for that to be merged??
method 1 code:
old_cap = nx.get_edge_attributes(G, "capacity")
nx.set_edge_attributes(G, {e: int(1e6 * c) for e, c in old_cap.items()}, "new_capacity")
flow_value, flow_dict = nx.maximum_flow(G, "s", "t")method 2 code:
def int_capacity(u, v, eattr):
return int(1e6 * eattr["capacity"])
flow_value, flow_dict = nx.maximum_flow(G, "s", "t", capacity=int_capacity)|
Hi @Jiya873 , Rossbar closed my PR (#8414) to keep the discussion consolidated here. I had already written and tested the examples for minimum_cut and minimum_cut_value using math.isclose as discussed. Since you are working on the flow functions, feel free to use this snippet to cover the cut functions if needed: If the capacities are floating point numbers, usage of ``math.isclose``
is recommended for value comparison to avoid machine-dependent errors:
>>> import math
>>> G = nx.DiGraph()
>>> G.add_edge("x", "y", capacity=0.1)
>>> G.add_edge("y", "z", capacity=0.1)
>>> G.add_edge("z", "x", capacity=0.1)
>>> cut_value, partition = nx.minimum_cut(G, "x", "z")
>>> math.isclose(cut_value, 0.1)
True |
|
@dschult, I agree with your concern about floating-point issues, similar to what can happen with shortest_path and both approaches you suggested make sense to me. |
|
Hi @Ashtam01, |
|
@Jiya873 IMO the problem here was not merging main with this PR, as I can see now the capacity function works? DetailsSince capacity is constructed by default as infinite capacity if that attribute is not present in edge. And the attribute being passed was a function, thus it was absent from the attribute dict, and hence, error was silent and all capacities get constructed infinite and hence NetworkXUnbounded. after the main merging in this PR, e1a7a64, the error is no longer there. Hence, we can remove the newly added test, and add it to the example section as suggested by @dschult .
Also, as it is correct now in latest commit, the capacity function must take three parameters, the first two are the edges u and v, the third is the edge attribute. |
amcandio
left a comment
There was a problem hiding this comment.
This PR looks good to me! Although I'm having doubts whether we should really clarify this in all methods. Sounds like too noisy/duplicated.
rossbar
left a comment
There was a problem hiding this comment.
I generally agree with @amcandio - this is a lot to add; the example is also incomplete as the resulting flow value is still "transformed", i.e. is not scaled back to the correct capacities.
My personal vote would be to significantly trim this down!
| floating point arithmetic. | ||
|
|
||
| Examples affecting comparisons | ||
| ------------------------------ |
There was a problem hiding this comment.
Adding non-numpydoc headings will break docstring rendering, so this should be removed.
fix: #8389