Repository navigation
unified flow interfaces - #694
Conversation
Toptachamann
left a comment
There was a problem hiding this comment.
Looks good to me! It is a good decision to add methods for getting actual flow objects in the max. flow and min. cost flow interfaces.
|
There is one issue that needs some discussion, which is somehow related to this one. So far our interfaces force the "output types" of the algorithms, but have no particular requirement for the input types. We tend to provide input parameters from the constructor of each class and then call the interface methods to get the result back in a well defined format. As long as the input is straightforward, like in shortest paths or minimum spanning trees, this is ok. What happens however when the input becomes more complicated like in the minimum cost flow problem. Do we want to also have the input parameters in the interface? Which of the below two versions we prefer?
I actually like very much that Timofey has bundled all problem parameters in a well define object. |
|
@d-michail good point, @jsichi brought this up last weekend as well. Since here we have to specify a whole lot of parameters, it's quite nice to bundle them in a class which implements some problem interface, e.g. |
|
I agree with @jkinable: there is no need to create separate classes for ShortestPathProblem or MaxFlowProblem. The problem with this approach:
is that the class should become stateless, i.e. we no longer create an instance of the algorithm for a particular problem and then run it. In the maximum flow algorithms, we can specify the graph at construction time and then compute max. flow between different pairs of vertices. Since in many algorithms the input graph is specified at construction time, it'd be consistent to do the same thing with min. cost flow algorithm. |
|
Just to come to a consensus: |
|
In general it is good to have a unifying interface method for getting the result of the algorithm. In this case, we have to add a check to the methods |
|
I've added a @d-michail @Toptachamann have a look and let me know what you think. |
| } | ||
| dualSolution = new DualSolution<>(dualVariables); | ||
| if(minimumCostFLow == null) | ||
| throw new RuntimeException("Cannot return a dual solution before getMinimumCostFlow(MinimumCostFlowProblem minimumCostFlowProblem) is invoked!"); |
There was a problem hiding this comment.
This is too generic to throw. Either throw some standard exception that makes sense here, or maybe create some custom exception (which extends RuntimeException).
As a more general solution (which arguably would be used also in other places) we could have a generic JGraphTException which extends RuntimeException and all others custom exceptions extend this one. For example we have something like NegativeCycleDetectedException. @jsichi Opinions?
There was a problem hiding this comment.
IllegalStateException seems appropriate here.
I don't see much value in a JGraphTException for the kind of unchecked exceptions we currently throw. It might make sense in the future if we ever introduce checked exceptions.
There was a problem hiding this comment.
Actually, instead of throwing an exception, we might as well return null?
@return solution to the dual linear program formulated on the network, or null if no Minimum Cost Flow Problem has been solved.
Would you agree?
There was a problem hiding this comment.
I try to avoid returning null these days. A similar solution is to return Optional but we are not using such a pattern in the library. Not sure whether we want to. @jsichi @Toptachamann Preferences?
There was a problem hiding this comment.
I think it's fine if we start introducing Optional.
There was a problem hiding this comment.
I really dislike using Optional here. Normally the user would invoke:
Map<.> dualSol=getDual();
Only when the user forgets to invoke calculateMinimumCostFlow(.) this method goes wrong.
However, by returning an optional, the simple Map<.> dualSol=getDual(); now becomes:
Optional<Map<.>> optionalDualSol=getDual();
if(optionalDualSol.isPresent())
dualSol=optionalDualSol.get();
This is so incredibly verbose, especially since in 99.9% of the cases this is not needed at all.
So I would strongly prefer to simply return null, which seems appropriate here, and let the user do an old-fashioned null check if necessary, or throw an IllegalStateException. @d-michail @jsichi what's your preference? I'd like to wrap this one up such that we can proceed with the release.
There was a problem hiding this comment.
No strong preference. The documentation should contain the necessary information.
There was a problem hiding this comment.
Ok, let's keep it as-is for now. @d-michail if satisfied, could you go ahead and merge?
| */ | ||
| @Override | ||
| public Map<E, Double> getFlowMap() { | ||
| return this.minimumCostFLow.getFlowMap(); |
There was a problem hiding this comment.
We also need a null pointer check here.
Toptachamann
left a comment
There was a problem hiding this comment.
Left some small comments. Other than that looks good to me.
|
@jkinable is there still an unresolved issue that needs my input? I saw a notification email to that effect, but I can't find it anywhere in the github UI. |
|
I guess, it was triggered by Timofey's comments about the missing generic parameters. |
|
Alright, fixed the last issue. If everybody is happy, please go ahead and merge this one :) |
|
Great, thanks! |
* unified flow interfaces * fixed compile error * added return method for flow object * Fixed typos * Fixed javadoc of FlowAlgorithm * Added MinimumCostFlowProblem interface * Fixed PR comments * Fixed generic parameters
Added a
FlowAlgorithminterface to ensure that flow algorithms (min cost flow/max flow) have a unified interface. The purpose of this interface is not to allow the user to swap min cost flow and max flow algorithms (this doesn't seem to be very useful). Instead, the interface ensures that all flow algorithms have similar methods and names.