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))
false
  • 10,264
  • 13
  • 101
  • 209
user836026
  • 10,608
  • 15
  • 73
  • 129
  • You should try the codereview stackexchange site, it is more up to these kind of questions. – Bhargav Apr 22 '15 at 11:04
  • 1
    @Bhargav if the OP doesn't understand the code, there's really no point reviewing it for them. – jonrsharpe Apr 22 '15 at 11:07
  • 1
    @Bhargav Explanations of code are off-topic for [codereview.se]. 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 '15 at 11:07
  • @Bhargav: I suspect that this is not the OP's code: they just want it explained, not reviewed. – PM 2Ring Apr 22 '15 at 11:08
  • 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](https://labix.org/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 '15 at 11:09
  • My bad, I need to go read their faq more properly now. – Bhargav Apr 22 '15 at 11:10
  • 1
    That lambda function describes the constraint that prevents queens from attacking each other. Do you see how it works? – PM 2Ring Apr 22 '15 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 '15 at 11:24

1 Answers1

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))
poke
  • 369,085
  • 72
  • 557
  • 602
  • 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 '15 at 07: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 '15 at 07: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 '15 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 '15 at 17:31
  • @user836026 It seems you are struggeling with the concept of a [closure](https://en.wikipedia.org/wiki/Closure_%28computer_science%29) here. – BlackJack Apr 28 '15 at 12:26