Skip to content

unified flow interfaces - #694

Merged
d-michail merged 8 commits into
jgrapht:masterfrom
jkinable:flow
Nov 9, 2018
Merged

d-michail merged 8 commits into
jgrapht:masterfrom
jkinable:flow

Conversation

@jkinable

@jkinable jkinable commented Oct 14, 2018 •

Copy link
Copy Markdown
Collaborator

Added a FlowAlgorithm interface 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.

@jkinable
jkinable requested a review from Toptachamann October 14, 2018 13:59

@Toptachamann Toptachamann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@d-michail

Copy link
Copy Markdown
Member

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?

  • MinimumCostFlow<E> getMinimumCostFlow()
  • MinimumCostFlow<E> getMinimumCostFlow(MinimumCostFlowProblem input)

I actually like very much that Timofey has bundled all problem parameters in a well define object.

@jkinable

jkinable commented Oct 16, 2018 •

Copy link
Copy Markdown
Collaborator Author

@d-michail good point, @jsichi brought this up last weekend as well.
I would personally prefer MinimumCostFlow<E> getMinimumCostFlow(MinimumCostFlowProblem input) because this increases the consistency with the other classes. For instance, we have MaximumFlow<E> getMaximumFlow(V source, V sink); which also takes the problem params as input.

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. MinimumCostFlowProblem. In general, if the number of parameters is low, then I would not provide a class object. E.g. I would not create a MaxFlowProblem interface.

@Toptachamann

Toptachamann commented Oct 16, 2018 •

Copy link
Copy Markdown
Collaborator

I agree with @jkinable: there is no need to create separate classes for ShortestPathProblem or MaxFlowProblem.

The problem with this approach:

  • MinimumCostFlow<E> getMinimumCostFlow(MinimumCostFlowProblem input)

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.

@jkinable

Copy link
Copy Markdown
Collaborator Author

Just to come to a consensus:
We could have a MinimumCostFlowProblem interface, similar to the current MinimumCostFlowProblem class, including the getGraph method, with a MinimumCostFlowProblemImpl implementation class.
In addition we would have the interface method getMinimumCostFlow(MinimumCostFlowProblem input). The graph can still be provided at construction time. Whenever the graph provided at construction time is different from the graph provided in the MinimumCostFlowProblem input when getMinimumCostFlow is called, an IllegalArgumentException is thrown. That way, the implementation doesn't have to be stateless.

@Toptachamann

Copy link
Copy Markdown
Collaborator

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 getDualSolution() and testOptimaltiy whether the problem has been specified already.

@jkinable

Copy link
Copy Markdown
Collaborator Author

I've added a MinimumCostFlowProblem interface, and adjusted MinimumCostFlowAlgorithm accordingly. The MinimumCostFlow is now semi-stateless: each time a getMinimumCostFlow(MinimumCostFlowProblem problem) is invoked, the algorithm recomputes the flow. Methods like getDual and getCost only work when getMinimumCostFlow(MinimumCostFlowProblem problem) has been invoked before.

@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!");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine if we start introducing Optional.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong preference. The documentation should contain the necessary information.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a null pointer check here.

@Toptachamann Toptachamann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some small comments. Other than that looks good to me.

@jsichi

jsichi commented Nov 9, 2018

Copy link
Copy Markdown
Member

@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.

@d-michail

Copy link
Copy Markdown
Member

I guess, it was triggered by Timofey's comments about the missing generic parameters.

@jkinable

jkinable commented Nov 9, 2018

Copy link
Copy Markdown
Collaborator Author

Alright, fixed the last issue. If everybody is happy, please go ahead and merge this one :)

@d-michail
d-michail merged commit 586729e into jgrapht:master Nov 9, 2018
@d-michail

Copy link
Copy Markdown
Member

Great, thanks!

SChudakov pushed a commit to SChudakov/jgrapht that referenced this pull request Jan 9, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants