2

Below is a solution for the N-queens problem using the Python-Constraint Resolver from Labix. Could someone explain to me, or refer me to any web page where it explains the meaning of the last 3 lines of this code?

Moreover, how could I use the AllDifferentConstraint constraint to make the below code shorter?

from constraint import *

problem = Problem()
size = 8
cols = range(size)
rows = range(size)
problem.addVariables(cols, rows)
for col1 in cols:
    for col2 in cols:
        if col1 < col2:
            problem.addConstraint(lambda row1, row2, col1=col1, col2=col2:
                                    abs(row1-row2) != abs(col1-col2) and
                                    row1 != row2, (col1, col2))
8
  • 1
    @Bhargav if the OP doesn't understand the code, there's really no point reviewing it for them.
    – jonrsharpe
    Apr 22, 2015 at 11:07
  • 1
    @Bhargav Explanations of code are off-topic for Code Review. Please read their help center before making recommendations. Furthermore, if you feel this question is not a good fit for Stack Overflow, consider explaining why and make recommendations that could make this question a better fit.
    – nhgrif
    Apr 22, 2015 at 11:07
  • 1
    Without knowing the definition of the Problem class or what module it's defined in, it's not possible to explain what that code snippet is doing. I'm guessing that it's using python-constraint. So you need to read their docs, and the relevant literature; IMHO, this question in its current form is not suitable for Stackoverflow.
    – PM 2Ring
    Apr 22, 2015 at 11:09
  • 1
    That lambda function describes the constraint that prevents queens from attacking each other. Do you see how it works?
    – PM 2Ring
    Apr 22, 2015 at 11:22
  • 2
    It looks like I guessed the correct module, since you've accepted Poke's answer. :) I've added the relevant info to your question, but in future please supply such info when you ask the question.
    – PM 2Ring
    Apr 23, 2015 at 11:24

1 Answer 1

3
problem.addConstraint(lambda row1, row2, col1=col1, col2=col2:
                        abs(row1-row2) != abs(col1-col2) and
                        row1 != row2, (col1, col2))

This is roughly equivalent to this:

def constraintFunction (col1, col2):
    def innerFunction (row1, row2):
        return abs(row1 - row2) != abs(col1 - col2) and row1 != row2
    return innerFunction

problem.addConstraint(constraintFunction(col1, col2), (col1, col2))

And that last line is equivalent to this:

func = constraintFunction(col1, col2)
problem.addConstraint(func, (col1, col2))
5
  • I'm not sure what is the value of row1 and row2 because it's not defined above. Notice, I'm new to Python.
    – user836026
    Apr 24, 2015 at 7:12
  • 1
    row1 and row2 are parameters of the inner function (which is ultimately returned). Apparently, addConstraint expects a function that has two parameters (which we call row1 and row2) and a 2-tuple of columns.
    – poke
    Apr 24, 2015 at 7:13
  • According to my understanding, row1 and row2 will have same value of col1 and col2, because it passed from col1 an dcol2 .. but apparently it's having different values ... why?
    – user836026
    Apr 24, 2015 at 14:34
  • No, constraintFunction is called with those two columns, but constraintFunction then returns a different function—innerFunction—which has row1 and row2 as parameters. And that function is passed to the addConstraint function, so when the the constraint is later evaluated, different values than col1/2 are likely passed.
    – poke
    Apr 24, 2015 at 17:31
  • @user836026 It seems you are struggeling with the concept of a closure here.
    – BlackJack
    Apr 28, 2015 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.