Exemplo n.º 1
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    board_size = range(n)
    queens = ["queen%d" % i for i in range(1, n + 1)]
    for var in queens:
        csp.add_variable(var, board_size)
    for id1 in range(n):
        for id2 in range(id1 + 1, n):
            csp.add_binary_factor(queens[id1], queens[id2],
                                  lambda rcolv1, rcolv2: rcolv1 != rcolv2)
            csp.add_binary_factor(
                queens[id1], queens[id2],
                lambda rcolv1, rcolv2: abs(rcolv1 - rcolv2) != abs(id1 - id2))
    return csp
Exemplo n.º 2
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem b
    # TODO: BEGIN_YOUR_CODE
    for i in range(n):
        csp.add_variable(i, range(n))
    for var1 in range(n):
        for var2 in range(n):
            if var1 != var2:
                csp.add_binary_factor(
                    var1, var2,
                    lambda x, y: x != y and abs(x - y) != abs(var1 - var2))
    # raise NotImplementedError
    # TODO: END_YOUR_CODE
    return csp
Exemplo n.º 3
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem b
    # TODO: BEGIN_YOUR_CODE
    # Queens is a list of variables, which denote the position of the queen in each row
    # Queens is defined as 'Qi', where i is the row of a queen
    Queens = ['Q' + str(i+1) for i in range(n)]
    value_list = [i+1 for i in range(n)]
    for queen in Queens:
        csp.add_variable(queen, value_list)
    # binary_factor: two queens can't in the same column, and duijiaoxian
    for queen1 in Queens:
        for queen2 in Queens:
            if queen1 != queen2:
                # Using the define 'Qi', to get i
                row1 = int(queen1.split('Q')[1])
                row2 = int(queen2.split('Q')[1])
                delta = abs(row1 - row2)
                csp.add_binary_factor(queen1, queen2, lambda x, y: x != y and abs(x-y) != delta)
    # TODO: END_YOUR_CODE
    return csp
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem a
    # TODO: BEGIN_YOUR_CODE
    domin = []
    for i in range(1, n + 1):
        domin.append(i)
    for i in range(1, n + 1):
        csp.add_variable(i, domin)
    # print(csp.values)
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            if i != j:
                k = abs(i - j)
                csp.add_binary_factor(
                    i, j, lambda x, y: x + k != y and x - k != y and x != y)
    # print((csp.binary_factors[1][4][3]))
    # raise NotImplementedError
    # TODO: END_YOUR_CODE
    return csp
Exemplo n.º 5
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem b
    # TODO: BEGIN_YOUR_CODE
    variables = []
    for i in range(n):
        # name variables from y1 to yn
        var = 'y{}'.format(i + 1)
        # assign values to variables
        csp.add_variable(var, range(1, n + 1))
        variables.append(var)
    for i in range(len(variables)):
        for j in range(len(variables)):
            if variables[i] != variables[j]:
                distance = abs(i - j)
                # Generate binary constraints between queens
                csp.add_binary_factor(
                    variables[i], variables[j],
                    lambda y1, y2: y1 != y2 and distance != abs(y1 - y2))
    #raise NotImplementedError
    # TODO: END_YOUR_CODE
    return csp
Exemplo n.º 6
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem b
    # TODO: BEGIN_YOUR_CODE

    # refer the formulation 2
    # there n variables in TOTAL, each one represents the Position of queen in row Qi
    for i in range(n):  # add all the variables to CSP
        csp.add_variable(var=i, domain=list(range(n)))
        # 添加一元约束
        csp.add_unary_factor(
            var=i,
            factor_function=lambda x: 1)  # factor_function作用在变量var的每个值value上
    # def binaryConstraints(value1,value2):
    #     return abs(value1-value2)

    # 添加二元约束
    for i in range(n):
        for j in range(n):
            if i == j:
                continue
            # factor_function分别作用在变量var1和var2的每个值value1,value2上
            csp.add_binary_factor(var1=i,
                                  var2=j,
                                  factor_function=lambda x, y: abs(x - y) !=
                                  abs(i - j))  # 两个皇后不在对角线上
            csp.add_binary_factor(
                var1=i, var2=j,
                factor_function=lambda x, y: x != y)  # 两个皇后不在同一列上

    # raise NotImplementedError
    # TODO: END_YOUR_CODE
    return csp
Exemplo n.º 7
0
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem a
    # TODO: BEGIN_YOUR_CODE
    for i in range(n):
        csp.add_variable('Q' + str(i), list(range(n)))    # the position of a queen in each row (from 0 to n-1)
    for i in range(n-1):
        for j in range(i+1, n):
            csp.add_binary_factor('Q' + str(i), 'Q' + str(j), lambda x, y: x != y)
            csp.add_binary_factor('Q' + str(i), 'Q' + str(j), lambda x, y: abs(x-y) != abs(j-i))
    # TODO: END_YOUR_CODE
    return csp
def create_n_queens_csp(n=8):
    """Create an N-Queen problem on the board of size n * n.

    You should call csp.add_variable() and csp.add_binary_factor().

    Args:
        n: int, number of queens, or the size of one dimension of the board.

    Returns
        csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver
    """
    csp = CSP()
    # TODO: Problem a
    # TODO: BEGIN_YOUR_CODE
    vars = []
    for i in range(1, n + 1):
        # we suppose that on a board, we put in queens with order.
        # In other words, Xi is fixed for each queen,
        # we only have to consider Yi for each queen
        varName = 'Y' + str(i)
        csp.add_variable(varName, range(1, n + 1))
        vars.append(varName)

    rule1 = lambda x, y: x != y
    for var in vars:
        for anotherVar in vars:
            if var != anotherVar:
                # csp.add_binary_factor(var, anotherVar, lambda y1, y2: y1 != y2)
                dist = abs(vars.index(anotherVar) - vars.index(var))
                csp.add_binary_factor(
                    var, anotherVar,
                    lambda y1, y2: y1 != y2 and abs(y1 - y2) != dist)

    # raise NotImplementedError
    # TODO: END_YOUR_CODE
    return csp