Repository navigation
Conversation
|
This is a consistent issue when people use floating point edge/node attributes in algorithms that look for min/max/equality/etc. That is, any time we use floating point data for algorithms where floating point errors change the answers. Our advice is to convert the data to integer type, e.g. int(weight * 1e12), run with that and convert any final result back to floating point. The algorithms just don't work for floating point. In your example the approximation using those tolerance levels with those data values does work. But those tolerances won't work with other data values. I don't know of a way that we can make this work for any user input. So, our current policy is to make the user decide how to convert to and from floating point, rather than doing the conversion in a way that we choose. We also considered raising an error for floating point values on attributes, but we decided not to take that approach. It's also possible that its time to re-consider that discussion/decision. But I'm pretty sure we don't want to use approximate computation with tolerance levels that are chosen arbitrarily when they should be chosen based on the data being used. Perhaps it would be helpful if you explained what happened that led to this PR. ?? |
|
Thanks for the comments. Context: I feel this should be addressed since the documentation for If the user intends to use capacities with more than the fixed 1e-12 precision, then this fix indeed does not work, but this could be considered as a rare use case, and it may be sufficient to include the information about floating-point rounding in the documentation. If we want to allow arbitrary tolerance levels, we could also change the API for |
|
Many algorithms don't work with floating points. The moment you use them here your solution becomes an approximation. I'm +1 on Dan's comment on multiplying by a big N and converting to int. I see how this can be a common pain point for users. However the problem comes from using floating points and expecting exact results. Many algorithms will have the same issue, for example Dijkstra's, so I don't think we should update all numeric comparisons in NetworkX to allow for a tolerance. What we could do here is provide more flexibility for the capacity and allow users to specify a function (like we do for Dijkstra's and cost). This way users don't need to modify the whole input graph. Moreover, we could also update the tutorial with considerations on using floating point as algorithm inputs. |
|
You're right, sometimes the algorithms just don't work correctly with floating-point capacities. In the following example, edmonds_karp works correctly, but preflow_push results in an error: import networkx as nx
from networkx.algorithms.flow import preflow_push, edmonds_karp
G = nx.DiGraph()
G.add_edge("s", "a", capacity=1)
G.add_edge("a", "b", capacity=1)
G.add_edge("b", "c", capacity=1)
G.add_edge("c", "t", capacity=0.1)
G.add_edge("s", "d", capacity=0.1)
G.add_edge("b", "d", capacity=0.2)
print("edmonds_karp flow:", nx.maximum_flow(G, "s", "t", flow_func=edmonds_karp)[0]) # 0.1
# Raises: ValueError: min() iterable argument is empty (in preflow_push relabel)
print("preflow_push flow:", nx.maximum_flow(G, "s", "t", flow_func=preflow_push)[0])Allowing floating-point capacities could be done by adding small tolerances to all capacity comparisons in the flow algorithms, but converting the capacities to integers may be a cleaner approach. |
|
I will close this and create a new pull request that uses the discussed approach of converting edge capacities to integers instead of adding tolerances to floating-point comparisons. |
Fixes #4972.
Fixes #4592.
When using floating-point capacities, the partition returned by
minimum_cutmay be incorrect due to floating-point precision errors.Example:
On this graph,
nx.minimum_cut(G, "s", "t")returns(3.1, ({'s'}, {'a', 'b', 'c', 't'})), but the cost of this returned partition is 4 and the correct return value should be(3.1, ({'b', 's'}, {'a', 'c', 't'})).I have resolved this issue by replacing the strict equality check with one that can handle floating-point rounding errors.
I have also included this example graph as a test instance and added a small tolerance for the tests to avoid errors when using floating-point capacities.