Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implementation: Tree CSP Solver #432

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

Closed
wants to merge 3 commits into from
Closed

Implementation: Tree CSP Solver #432

wants to merge 3 commits into from

Conversation

antmarakis
Copy link
Collaborator

Completed the implementation of tree_csp_solver and added tests. In detail:

  • Implemented make_arc_consistent. For a node n and its parent p, it removes the possible values of p which do not have a consistent assignment with any possible value of n.
  • Added new function, assign. It assigns a value to a node consistent to the assignment of the node's parent.
  • Added new function, node_domains which converts UniversalDict to a built-in dictionary. This way we can play around more with the dictionaries.
  • Added test for the algorithm. The test uses the example from the book (Australia with a node removed).

Note: I had to add two new lines to the algorithm for initialization purposes. If that's an issue, I can work them into the called functions, but it will be ugly.

@lucasmoura
Copy link
Contributor

lucasmoura commented Mar 29, 2017

@MrDupin Nice work, but just some suggestions:

  • Instead of creating the node_domains method, wouldn't it be better to just call the support_prunning method before running the tree_csp_solver ? That way, inside the
    make_arc_consistent method, you can just call the prune method with the invalid variables, you will not need to create a new domain for a given variable.

  • I don't think the break variable is usefull in the code, since in your current implementation you could just append the variables if the constraint returns True.

With that said, my ideia for the code was something like this:

def make_arc_consistent(Xj, Xk, csp):
    """Make arc between parent (Xj) and child (Xk) consistent under the csp's constraints,
    by removing the possible values of Xj that cause inconsistencies."""
    for val1 in csp.domains[Xj]:
        for val2 in csp.domains[Xk]:
            if not csp.constraints(Xj, val1, Xk, val2):
                csp.prune(Xj, val1)

    return csp.curr_domains[Xj]

But again, this will require calling the support_prunning method. But this is just an idea.

  • Maybe I am missing something, but is the assign function necessary ? The make_arc_consistent method should not guarantee that the variables which remain in the domain are valid for the problem ?

  • If the assign function is really necessary, I don't know if assign is a good name for the function you are creating. I understand your intention, but when I see a method called assign, I assume it will have a different behavior. Specially since CSP has an assign method too. Maybe something like get_first_valid_value or something like that will make it clearer what the function is doing. But even my suggestion can be improved.

  • Maybe it will be good to add a test on a example that does not have a solution, just to clarify what will happen in that case.

  • Since you have two commits with the same title, maybe it will be better to just squash them.

@antmarakis
Copy link
Collaborator Author

@lucasmoura: Thanks for the feedback.

  • I will take a look at support_pruning since it seems more general than node_domains. With node_domains I wanted to write something specific for UniversalDict, but I can see how that can be restricting.

  • The break is not necessary, but is useful. We only need to find one possible value that satisfies the constraint in order to keep val1, so I break when I find one.

  • I would have used prune, but unfortunately we cannot use it the way you mentioned. We need to check all the possible values val2 before making a decision on removing/keeping val1, so we cannot prune first time the constraint is not satisfied. I will look into using prune after the inner loop though, since then it will work fine.

  • assign is necessary, yes. make_arc_consistent makes sure that there is a possible assignment for any value of the parent, but since we don't know in advance what the value of the parent will be, we cannot assign a value to the child. With assign we pick the first value in a node that satisfies the constraints given its parent's value. I will change the name though, since I somehow missed that CSP had a same name function. Good catch.

  • I am looking into adding a failing example, but unfortunately constraints are now a bit limiting. Since input is a tree, we cannot use map coloring constraints since for any non-trivial problem (aka with more than 1 color) there will be a solution. I might in the end put such an example in just to have a test, but I want to work on something more substantial first.

  • What do you mean two commits with the same name? The two updates on the test_csp?

@antmarakis
Copy link
Collaborator Author

antmarakis commented Mar 29, 2017

Accidentally deleted branch, will now make new PR. Sorry about that.

New PR is #434.

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.

2 participants