Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/alldifferent #115

Merged
merged 27 commits into from
May 4, 2021
Merged

Feature/alldifferent #115

merged 27 commits into from
May 4, 2021

Conversation

PierreTsr
Copy link
Contributor

@PierreTsr PierreTsr commented Apr 26, 2021

This is my proposition for the AllDifferent constraint, relating to the issue #49. It contains:

  • The AllDifferent constraint;
  • The associated testset;
  • The supporting algorithm for matching computation;

My implementation uses only LightGraphs.jl and no third-party solver.
The downside is that I manually implemented the matching algorithm and thus introduced many potential inefficiency sources. I tried to use LightGraphsMatching.jl but the BlossomV modules breaks the cross-platform portability as it requires some C++ files.
Moreover the matching algorithm code is placed in the same file as the AllDifferent constraint as no dedicated folder exists for backend algorithms currently. But it could be wiser and cleaner to move it to a dedicated part of the architecture if more complex constraints are to be added in the future.

Thus potential improvements are:

  • Adding a function to complete an existing matching instead of going back from scratch each time a new matching is needed;
  • Moving the matching algorithm to another part of the project;
  • Adding more complex test cases for intermediate functions;
  • Increasing code readability;
  • Replacing the algorithm with a more recent one;
  • Using benchmarking tools to track unefficient code;

One hesitation I had is in the last 2 test: I created a n-queens model to grant a full test of the constraint. But these tests thus rely on many other parts of the code. Is this anti-pattern? Because otherwise it is hard to guarantee the correct behavior of the constraint (for instance, I missed a few solutions in the n-queens at some point, but solving the problem integrally was the only way to detect this bug).

@PierreTsr PierreTsr added the enhancement New feature or request label Apr 26, 2021
@PierreTsr PierreTsr linked an issue Apr 26, 2021 that may be closed by this pull request
@PierreTsr PierreTsr self-assigned this Apr 26, 2021
@PierreTsr
Copy link
Contributor Author

has an inconsistant behavior been introduced recently in the tsptw.jl test? Because it ran smoothly until I merged the last version of master and now the test seems to fail only part of the time. I haven't modified any of the related files afaik

Copy link
Contributor

@kimriouxparadis kimriouxparadis left a comment

Choose a reason for hiding this comment

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

Overall great code!
I would like to discuss the variable and function names, but otherwise it seems good!

- `start::Int`: the free value node to start the search from.
- `free::Vector{Int}`: the list of all the free variables indexes.
"""
function augmentmatching!(digraph::DiGraph{Int}, start::Int, free::Set{Int})::Union{Nothing, Pair{Int, Int}}
Copy link
Contributor

Choose a reason for hiding this comment

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

I thing the function names and the variable names should be in camel case.
Example : augmentMatching

"""
function augmentmatching!(digraph::DiGraph{Int}, start::Int, free::Set{Int})::Union{Nothing, Pair{Int, Int}}
parents = bfs_parents(digraph, start; dir=:out)
reached = intersect(free, findall(v -> v > 0, parents))
Copy link
Contributor

Choose a reason for hiding this comment

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

nodes_reached ?


Compute a random matching in a bipartite graph, between 1:lastfirst and (lastfirst + 1):nv(graph).
"""
function randommatching(graph::Graph{Int}, lastfirst::Int)::Matching{Int}
Copy link
Contributor

Choose a reason for hiding this comment

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

randomMatching?

Compute a random matching in a bipartite graph, between 1:lastfirst and (lastfirst + 1):nv(graph).
"""
function randommatching(graph::Graph{Int}, lastfirst::Int)::Matching{Int}
seen = Set{Int}()
Copy link
Contributor

Choose a reason for hiding this comment

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

nodesSeen?

(lastfirst + 1):nv(digraph). A variable is assigned to a value if the directed
edge (Var => Val) exists.
"""
function matchingfromdigraph(digraph::DiGraph{Int}, lastfirst::Int)::Matching{Int}
Copy link
Contributor

Choose a reason for hiding this comment

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

matchingFromDigraph?

the first group.
"""
function maximizematching!(digraph::DiGraph{Int}, lastfirst::Int)::Matching{Int}
currentmatching = matchingfromdigraph(digraph, lastfirst)
Copy link
Contributor

Choose a reason for hiding this comment

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

currentMatching
maximizeMatching

function maximizematching!(digraph::DiGraph{Int}, lastfirst::Int)::Matching{Int}
currentmatching = matchingfromdigraph(digraph, lastfirst)
stop = currentmatching.size == lastfirst
freevar = Set(filter(v -> outdegree(digraph, v) == 0, 1:lastfirst))
Copy link
Contributor

Choose a reason for hiding this comment

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

Use complet name
freeVariables
freeValues
It is longer to write, but clearer to read.


Return the node index of a value.
"""
function val2node(con::AllDifferent, val::Int)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure about the names. i Would use To instead of 2.

Return all the edges in a strongly connected component vars ∪ vars.
"""
function getalledges(digraph::DiGraph{Int}, vars::Vector{Int}, vals::Vector{Int})::Set{Edge{Int}}
res = Set{Edge{Int}}()
Copy link
Contributor

Choose a reason for hiding this comment

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

res ?

end
end

allvar = 1:constraint.numberOfVars
Copy link
Contributor

Choose a reason for hiding this comment

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

complete name?

@PierreTsr PierreTsr merged commit a12e657 into master May 4, 2021
@PierreTsr PierreTsr deleted the feature/alldifferent branch May 6, 2021 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the all different constraint
2 participants