Repository navigation
[ENH] Flow algorithms: Add support for callable capacity parameter - #8406
Conversation
dschult
left a comment
There was a problem hiding this comment.
The doc_strings of the function need to change the description of the capacity argument. Take a look at how the doc_strings for e.g. shortest_path word it for the weight argument. Do something similar here.
Also where the function processor is defined can be more clear (see below).
Finally, I think there are a couple of typos like eedge and capcity
Thanks for this!
|
Thank you for the review. I have now updated the docstrings related to doctest u = 0, v = 1
e_attr = {<function TestMaxflowMinCutCommon.test_capacity_func.<locals>.my_cap at 0x7fef0810f240>: inf}
def my_cap(u, v, e_attr):
> return 1 / e_attr["capacity"]
^^^^^^^^^^^^^^^^^^
E KeyError: 'capacity'Here, the function @nx._dispatchable(edge_attrs={"capacity": float("inf")}, returns_graph=True)used in I was able to reproduce this locally using the same command as in CI: NETWORKX_TEST_BACKEND=nx_loopback pytest networkx/algorithms/flow/tests/test_maxflow.pyI then did this inside the core algorithm: With this change, a different error occurs: Detailsnetworkx/utils/decorators.py:784: in func
return argmap._lazy_compile(__wrapper)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<class 'networkx.utils.decorators.argmap'> compilation 8:3: in argmap_boykov_kolmogorov_5
???
networkx/utils/backends.py:633: in _call_if_any_backends_installed
return self._convert_and_call_for_tests(
networkx/utils/backends.py:1878: in _convert_and_call_for_tests
G = self.orig_func(*args_nx, **kwargs_nx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
networkx/algorithms/flow/boykovkolmogorov.py:166: in boykov_kolmogorov
R = boykov_kolmogorov_impl(G, s, t, capacity, residual, cutoff)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
networkx/algorithms/flow/boykovkolmogorov.py:181: in boykov_kolmogorov_impl
R = build_residual_network(G, capacity)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<class 'networkx.utils.decorators.argmap'> compilation 12:3: in argmap_build_residual_network_9
???
networkx/utils/backends.py:633: in _call_if_any_backends_installed
return self._convert_and_call_for_tests(
networkx/utils/backends.py:1879: in _convert_and_call_for_tests
assert_graphs_equal(G, converted_result)
def assert_graphs_equal(G1, G2, strict=True):
assert G1.number_of_nodes() == G2.number_of_nodes()
assert G1.number_of_edges() == G2.number_of_edges()
assert G1.is_directed() is G2.is_directed()
assert G1.is_multigraph() is G2.is_multigraph()
print("G1:",G1._adj)
print("G2:",G2._adj)
if strict:
> assert G1.graph == G2.graph
^^^^^^^^^^^^^^^^^^^^
E AssertionErrorG1 = <networkx.classes.digraph.DiGraph object at 0x7560505b4510>
G2 = <networkx.classes.digraph.DiGraph object at 0x75604ff34050>
----------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------
G1: {1: {2: {'capacity': 1.0}}, 2: {1: {'capacity': 1.0}}}
G2: {1: {2: {'capacity': 1}}, 2: {1: {'capacity': 1}}}
G1.graph: {'inf': 3.0}
G2.graph: {'inf': 1}The graph attributes are not being copied and thus default value is being constructed for 'inf'. This originates from the default behavior described in backends.rst: I don't know if I am in right direction to solve the test failure.. But I did enjoy debugging.. |
dschult
left a comment
There was a problem hiding this comment.
The dispatch CI test shows that dispatching is not treating this well.
It looks like your first step in fixing it ended up changing the original graph's data attributes. That is the second error you found -- the backend testing machinery found that the graph itself had been changed.
I haven't chased that down completely. Changing the attr dict will update the original graph. But I don't see where that might be happening.
But I have a suggestion below for one section of the code to keep it in a list comprehension. Not sure if that will help with these errors. I'll come back to this later.
:/
|
I have looked more into this, and I could find the fix! It looks like u = 0, v = 1
e_attr = {<function TestMaxflowMinCutCommon.test_capacity_func.<locals>.my_cap at 0x7fef0810f240>: inf}The fix is to preserve all the edge attribute by adding But in shortest path algorithms, @dschult, is the above fix reasonable and correct? If yes, I will push the new changes. EDIT: I have pushed the changes. The circleci test looks unrelated to this PR. |
|
I have given some more time on why See line 1202-1236 in it: networkx/networkx/utils/backends.py Lines 1202 to 1236 in de90b03 In shortest path algos, we dispatch like: networkx/networkx/algorithms/shortest_paths/weighted.py Lines 81 to 83 in de90b03 Here But in our flow algorithms, we are dispatching like: networkx/networkx/algorithms/flow/maxflow.py Lines 20 to 22 in de90b03 Here edge_attr is a dictionary, with key as parameter name and mapping as default.Thus, it will go inside else condition of L-1229. But here we are not taking care of whether parameter will be callable or string, etc. I have verified that if I set Thus, there are two directions:
|
|
Thanks for tracking this down!! I'll ask @eriknw to comment if he has time. My guess is to keep the default specification and use |
Co-authored-by: Dan Schult <[email protected]>
dschult
left a comment
There was a problem hiding this comment.
This looks good to me!
Thanks @gulshan-123 !!
:)
|
It looks like we will need a more thorough pass through the flow folder to ensure that all uses of an edge attribute "capacity" will actually use the function.. For example, in "utils.py" the bounded calculations look up capacity values by assuming they are attributes in the edge datadict. We need to update these -- and look for more occurences of edge capacity lookup broadly. |
|
I’ve done a pass of the flow directory. It appears that most capacity lookups are performed on the residual graph R, rather than G. As in _build_residual_graph, we explicitly construct R and assign the "capacity" attribute ourselves: R = nx.DiGraph()
# ...
R[u][v]["capacity"] = rin the detect_unboundedness function in utils.py: def detect_unboundedness(R, s, t):
...
for v, attr in R[u].items():
if attr["capacity"] == inf and v not in seen:
...Since detect_unboundedness operates on the residual graph R, I believe it doesn't need to use the capacity function. I will perform one more scan of the flow directory to ensure no direct lookups are happening on the original input graph G. |
|
I should have made a link to the follow-up PR and discussion that seems to be causing troubles. Maybe the Residual Graph is being built somehow such that it doesn't use the capacity function. The symptom reported is that an UnBoundedError is occuring when it shouldn't. I jumped to the conclusion that the capacities were not being built into the bound correctly. But maybe something more is going on. |
|
Not sure I follow. The ticket you mention as a regression is a documentation PR. Did you mean another issue? |
|
I'm not sure it is a regression. After #8406 we can use a function in place of an attribute name in the Certainly it was unfortunate that #8402 was closed by the author. I think it will be straight-forward to accomplish the same goal. I put that together in #8761 We can move this discussion there I guess. |
|
My mistake - too many open tabs 🙃 . So no issues here: +1 for reviving the docs over in #8761 ! |
Fix: #8390
Similar to shortest path algorithms, flow algorithms can now accept a callable capacity function.
_capacity_functionis similar to_weight_functionof shortest path algorithms, except: Sincebuild_residual_networkis not supported for Multigraphs, I have removed that portion.will update docs and add test after the initial reviews.