def create_nqueens_csp(n = 8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    ## idea is that if we limit each queen to to its column, then we just need to choose
    # a row for each queen! This reduces domain from n^2 to n.
    def  onDifferentRowAndDiagonals(v1,v2):
        # they are are on different rows
        if v1[1] != v2[1]:
            if abs(v1[0] - v2[0]) != abs(v1[1] - v2[1]):
                return True
        return False

    def dummyFunction(var):
        return 1
    csp = util.CSP()
    variables = ['q%d'%i for i in range(1, n+1)]
    for x in range(len(variables)):
        domain = [(x+1, j) for j in range(1,n+1)] 
        csp.add_variable(variables[x], domain)
        csp.add_unary_factor(variables[x], dummyFunction)
    for i in range(len(variables)):
        for j in range(i+1, len(variables)):
            csp.add_binary_factor(variables[i],variables[j], onDifferentRowAndDiagonals)
    return csp
Exemple #2
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    # Problem 1a

    domain = range(1, n + 1)
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()

    for var in variables:
        csp.add_variable(var, domain)

    for i in range(0, len(variables)):
        for j in range(i + 1, len(variables)):
            csp.add_binary_factor(
                variables[i], variables[j], lambda x, y:
                ((x != y) and (abs(x - y) != (j - i))))

    return csp
def create_nqueens_csp(n = 8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    domains=[]
    for i in range(n):
        domains.append(i)
    for i in range(n):
        csp.add_variable(i,domains)
    for i in range(n):
        for j in range(n):
            if i!=j:
                csp.add_binary_factor(i,j,lambda x,y: x!=y and abs(y-x)!=abs(j-i) )
    #raise Exception("Not implemented yet")
    # END_YOUR_CODE
    return csp
Exemple #4
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()

    # Problem 0c
    # ### START CODE HERE ###
    def xor(v1, v2):
        return v1 != v2

    print(f"n: {n}")
    for vi, v in enumerate(variables):
        try:
            csp.add_variable(v, [0, 1])
            if vi > 0:
                csp.add_binary_factor(variables[vi - 1], variables[vi], xor)
        except Exception as e:
            import sys
            print(sys.exc_info())
            raise RuntimeError(e)
    Bsearch = BacktrackingSearch()
    Bsearch.solve(csp, ac3=False)
    Bsearch.print_stats()

    # ### END CODE HERE ###
    return csp
Exemple #5
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()

    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    '''
    We use string of number 0,1,2......7 to name 8 variable, and each variable have 
    8 integers to take on, shows that the queen on this row stands on which column.
    '''
    for r in range(n):
        csp.add_variable(str(r), range(n))
    for i in range(n):
        for j in range(i):
            csp.add_binary_factor(
                str(i), str(j),
                lambda x, y: x != y and abs(i - j) != abs(x - y))
    # END_YOUR_CODE
    return csp
Exemple #6
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()

    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)

    def rowCol(x, y):
        if x != y:
            return 1
        return 0

    vars = ['var%d' % i for i in range(1, n + 1)]

    for i in vars:
        csp.add_variable(i, range(n))
    for one in range(n):
        for two in range(n):
            if one != two:
                csp.add_binary_factor(
                    vars[one], vars[two], lambda rowcol1, rowcol2: abs(
                        rowcol1 - rowcol2) != abs(one - two))
                csp.add_binary_factor(vars[one], vars[two], rowCol)
            # END_YOUR_CODE
    return csp
Exemple #7
0
def create_nqueens_csp(n = 8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    # raise Exception("Not implemented yet")

    # define the possible columns and rows for each queen
    boardSize = range(n)
    # define queens/variables
    queens = ['queen%d'%i for i in range(1, n+1)]
    # add variables and assiociate possible positions to csp
    for var in queens:
        csp.add_variable(var,boardSize)
    # add binary factors
    for firstIndex in range(n):
        for secondIndex in range(firstIndex+1, n):
            # check if on the same row and column
            csp.add_binary_factor(queens[firstIndex], queens[secondIndex], lambda rowcolumnVal1, rowcolumnVal2: rowcolumnVal1 != rowcolumnVal2)
            # check if on the diagonal
            csp.add_binary_factor(queens[firstIndex], queens[secondIndex], lambda rowcolumnVal1, rowcolumnVal2: abs(rowcolumnVal1 - rowcolumnVal2) != abs(firstIndex - secondIndex)) 
    # END_YOUR_CODE
    return csp
Exemple #8
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    # Domain specifies which column the queen will go on.
    domain = [i for i in range(n)]
    # Each variable corresponds to a queen in a given row. ie q0 is the variable for the queen
    # on row 0. If q0 == 1, this means this queen is at position (0, 1) on the board.
    variables = ['q%d' % i for i in range(n)]
    [csp.add_variable(variable, domain) for variable in variables]
    [
        csp.add_binary_factor(
            variables[row1], variables[row2], lambda col1, col2: 1
            if col1 != col2 and abs(row1 - row2) != abs(col1 - col2) else 0)
        for row2 in range(n) for row1 in range(row2 + 1, n)
    ]
    # END_YOUR_CODE
    return csp
Exemple #9
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    domain = [i in range(1, n + 1)]
    variables = ['x%d' % i for i in range(1, n + 1)]

    prevVar = None
    for var in variables:
        csp.add_variable(var, domain)
        if prevVar != None:
            csp.add_binary_factor(
                prevVar, var,
                lambda x, y: x != y - 1 and x != y and x != y + 1)
        prevVar = var
    # END_YOUR_CODE
    return csp
Exemple #10
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    variables = ['q%d' % (i + 1) for i in range(n)]
    domain = list(range(n))
    for i, v in enumerate(variables):
        csp.add_variable(v, zip([i] * n, domain))
    for v1 in variables:
        for v2 in variables:
            if v1 == v2:
                continue
            csp.add_binary_factor(v1, v2, lambda v1, v2: v1[1] != v2[1])
            csp.add_binary_factor(
                v1, v2, lambda v1, v2: v1[0] + v1[1] != v2[0] + v2[1])
            csp.add_binary_factor(
                v1, v2, lambda v1, v2: v1[0] - v1[1] != v2[0] - v2[1])

    # END_YOUR_CODE
    return csp
Exemple #11
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    variables = ['x%d' % i for i in range(1, n + 1)]
    for index, variable in enumerate(variables):
        csp.add_variable(variable, [(index, i) for i in range(0, n)])
    for index, variable in enumerate(variables):
        for index2, variable2 in enumerate(variables):
            if index != index2:
                csp.add_binary_factor(variable, variable2,
                                      lambda x, y: x[1] != y[1])
                csp.add_binary_factor(
                    variable, variable2, lambda x, y: (x[0] - x[1]) !=
                    (y[0] - y[1]))
                csp.add_binary_factor(
                    variable, variable2, lambda x, y: (x[0] + x[1]) !=
                    (y[0] + y[1]))
    # END_YOUR_CODE
    return csp
Exemple #12
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()

    # Problem 1a
    # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this)
    def f(a, b):  # a and b are values where value is row + ' ' + column
        values = a.split(' ')
        ar = int(values[0])
        ac = int(values[1])
        values = b.split(' ')
        br = int(values[0])
        bc = int(values[1])
        if (ar == br or ac == bc or (ar == br and ac == bc)
                or (abs(ar - br) == abs(ac - bc))):
            return 0
        return 1

    for i in range(n):
        csp.add_variable(
            i, [str(i) + ' ' + str(j) for j in range(n)
                ])  # example: first queen is in first(zero) row and 0-1 column
    for i in range(n - 1):
        for j in range(i + 1, n):
            csp.add_binary_factor(i, j, f)
    # END_YOUR_CODE
    return csp
Exemple #13
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_potential().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured potential tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (around 10 lines of code expected)

    # add variable:
    variables = ['Queen_%d' % i for i in range(n)]
    for i in range(n):
        var = variables[i]
        domain = [(i, j) for j in range(n)]  # queen i must on row i
        csp.add_variable(var, domain)
    # add potential:
    for i in range(n):
        for j in range(n):
            if i == j: continue
            csp.add_binary_potential(
                variables[i], variables[j], lambda a, b:
                (a[0] != b[0]) and (a[1] != b[1]) and
                (a[0] - b[0] != a[1] - b[1]) and (a[0] - b[0] != -a[1] + b[1]))
    # END_YOUR_CODE
    return csp
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # BEGIN_YOUR_CODE (around 10 lines of code expected)
    domain = range(n)
    variables = ['q%d' % i for i in range(0, n)]
    for v in variables:
        csp.add_variable(v, domain)
    for i in range(n):
        for j in range(i + 1, n):
            # the same row
            csp.add_binary_factor(variables[i], variables[j],
                                  lambda x, y: x != y)
            csp.add_binary_factor(variables[i], variables[j],
                                  lambda x, y: x != y + (j - i))
            csp.add_binary_factor(variables[i], variables[j],
                                  lambda x, y: x != y - (j - i))
    # END_YOUR_CODE
    return csp
Exemple #15
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_potential().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured potentials
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # BEGIN_YOUR_CODE (around 7 lines of code expected)
    queen_pos = list(range(
        n))  # each element is the row position of its queen for column `idx`
    domain = list(range(n))
    for pos in queen_pos:
        csp.add_variable(pos, domain)

    for i in range(n):
        for j in range(n):
            if i != j:
                # row constraint
                csp.add_binary_potential(queen_pos[i], queen_pos[j],
                                         lambda x, y: x != y)
                # diagonal constraint
            csp.add_binary_potential(queen_pos[i], queen_pos[j],
                                     lambda x, y: abs(i - j) != abs(x - y))
    # END_YOUR_CODE
    return csp
Exemple #16
0
def create_zebra_problem():
    csp = util.CSP()
    domain = list(range(1, 6))
    variables = [
        'Englishmen', 'Spaniard', 'Norwegian', 'Japanese', 'Ukrainian',
        'coffee', 'milk', 'orange-juice', 'tea', 'water', 'dog', 'horse',
        'fox', 'zebra', 'snail', 'red', 'yellow', 'blue', 'green', 'ivory',
        'kitkats', 'snickers', 'smarties', 'hershey', 'milkyways'
    ]

    for var in variables:
        csp.add_variable(var, domain)

    csp.add_binary_potential('Englishmen', 'red', lambda x, y: x == y)
    csp.add_binary_potential('Spaniard', 'dog', lambda x, y: x == y)
    csp.add_unary_potential('Norwegian', lambda x: x == 1)
    csp.add_binary_potential('green', 'ivory', lambda x, y: x == y + 1)
    csp.add_binary_potential('hershey', 'fox', lambda x, y: abs(x - y) == 1)
    csp.add_binary_potential('kitkats', 'yellow', lambda x, y: x == y)
    csp.add_binary_potential('Norwegian', 'blue', lambda x, y: abs(x - y) == 1)
    csp.add_binary_potential('smarties', 'snail', lambda x, y: x == y)
    csp.add_binary_potential('snickers', 'orange-juice', lambda x, y: x == y)
    csp.add_binary_potential('Ukrainian', 'tea', lambda x, y: x == y)
    csp.add_binary_potential('Japanese', 'milkyways', lambda x, y: x == y)
    csp.add_binary_potential('kitkats', 'horse', lambda x, y: abs(x - y) == 1)
    csp.add_binary_potential('coffee', 'green', lambda x, y: x == y)
    csp.add_unary_potential('milk', lambda x: x == 3)

    for idx1 in range(5):
        for idx2 in range(5):
            if idx1 != idx2:
                csp.add_binary_potential(variables[idx1], variables[idx2],
                                         lambda x, y: x != y)

    for idx1 in range(5, 10):
        for idx2 in range(5, 10):
            if idx1 != idx2:
                csp.add_binary_potential(variables[idx1], variables[idx2],
                                         lambda x, y: x != y)

    for idx1 in range(10, 15):
        for idx2 in range(10, 15):
            if idx1 != idx2:
                csp.add_binary_potential(variables[idx1], variables[idx2],
                                         lambda x, y: x != y)

    for idx1 in range(15, 20):
        for idx2 in range(15, 20):
            if idx1 != idx2:
                csp.add_binary_potential(variables[idx1], variables[idx2],
                                         lambda x, y: x != y)

    for idx1 in range(20, 25):
        for idx2 in range(20, 25):
            if idx1 != idx2:
                csp.add_binary_potential(variables[idx1], variables[idx2],
                                         lambda x, y: x != y)

    return csp
Exemple #17
0
def test2b_2():
    csp = util.CSP()
    sumVar = submission.get_sum_variable(csp, 'zero', [], 15)
    sumSolver = submission.BacktrackingSearch()
    sumSolver.solve(csp)

    csp.add_unary_factor(sumVar, lambda n: n > 0)
    sumSolver = submission.BacktrackingSearch()
    sumSolver.solve(csp)
Exemple #18
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (our solution is 4 lines of code, but don't worry if you deviate from this)
    raise Exception("Not implemented yet")
    # END_YOUR_CODE
    return csp
Exemple #19
0
    def get_basic_csp(self):
        """
        Return a CSP that only enforces the basic constraints that a course can
        only be taken when it's offered and that a request can only be satisfied
        in at most one semester.

        @return csp: A CSP where basic variables and constraints are added.
        """
        csp = util.CSP()
        self.add_variables(csp)
        self.add_bulletin_constraints(csp)
        return csp
Exemple #20
0
    def test_1(self):
        """2b-1-hidden:  Test get_sum_variable with empty list of variables."""
        csp = util.CSP()
        sumVar = submission.get_sum_variable(csp, 'zero', [], 15)
        sumSolver = submission.BacktrackingSearch()
        sumSolver.solve(csp)
        # BEGIN_HIDE
        # END_HIDE

        csp.add_unary_factor(sumVar, lambda n: n > 0)
        sumSolver = submission.BacktrackingSearch()
        sumSolver.solve(csp)
Exemple #21
0
def test2b_3():
    csp = util.CSP()
    csp.add_variable('A', [0, 1, 2])
    csp.add_variable('B', [0, 1, 2])
    csp.add_variable('C', [0, 1, 2])

    sumVar = submission.get_sum_variable(csp, 'sum-up-to-7', ['A', 'B', 'C'], 7)
    sumSolver = submission.BacktrackingSearch()
    sumSolver.solve(csp)

    csp.add_unary_factor(sumVar, lambda n: n == 6)
    sumSolver = submission.BacktrackingSearch()
    sumSolver.solve(csp)
Exemple #22
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    for i, var in enumerate(variables):
        csp.add_variable(var, domain)
        if i > 0:
            csp.add_binary_factor(variables[i - 1], variables[i],
                                  lambda x, y: x != y)
    return csp
Exemple #23
0
def create_nqueens_csp(n=8):
    """
    Return an N-Queen problem on the board of size |n| * |n|.
    You should call csp.add_variable() and csp.add_binary_factor().

    @param n: number of queens, or the size of one dimension of the board.

    @return csp: A CSP problem with correctly configured factor tables
        such that it can be solved by a weighted CSP solver.
    """
    csp = util.CSP()
    # Problem 1a
    # ### START CODE HERE ###
    print(f"n: {n}")

    try:

        def check_row_diag(r1=None, r2=None, k=None):
            def diff_row_diff_diag(r1, r2):
                if r1 == r2:
                    return 0
                if abs(r1 - r2) == k:
                    return 0
                return 1

            return diff_row_diff_diag

    except Exception as e:
        import sys
        print(sys.exc_info())
        raise RuntimeError(str(e))

    domain = list(range(n))
    for vi in range(n):
        try:
            csp.add_variable(f"v_{vi}", domain)
            for vj in range(vi):
                k = vi - vj
                f_k = check_row_diag(k=k)
                csp.add_binary_factor(f"v_{vi}", f"v_{vj}", f_k)
        except Exception as e:
            import sys
            print(sys.exc_info())
            raise RuntimeError(e)

    Bsearch = BacktrackingSearch()
    Bsearch.solve(csp)
    # Bsearch.print_stats()

    # ### END CODE HERE ###
    return csp
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d'%i for i in range(1, n+1)]
    csp = util.CSP()
    # Problem 0c
    def xor(v1, v2):
        return v1!= v2
    for  i in range(len(variables)):
        csp.add_variable(variables[i], domain)
        if i >0:
            csp.add_binary_factor(variables[i-1],variables[i],xor)
    return csp
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (around 5 lines of code expected)
    for i, v in enumerate(variables):
        csp.add_variable(v, domain)
        if (i > 0):
            csp.add_binary_factor(variables[i - 1], v, lambda x, y: x != y)
    # END_YOUR_CODE
    return csp
Exemple #26
0
def make_teams(player_filename = "./PredictionsData/12.8.2015.csv"):
    csp = util.CSP()
    #Read in players
    players = make_player_dict(player_filename)
    #Make a variable for each position
    add_position_variables(csp, players)
    #Ensure no position chooses the same player as another
    add_same_player_constraints(csp, players)
    #Ensure that total salary is under the limit
    add_salary_constraints(csp, players)
    #Add assignment weights
    add_weights(csp, players)
    #ipdb.set_trace()
    return get_beamsearch_results(csp, players)
Exemple #27
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (our solution is 4 lines of code, but don't worry if you deviate from this)
    for index in range(len(variables)):
        csp.add_variable(variables[index], domain)
        if index > 0:
            csp.add_binary_factor(variables[index - 1], variables[index],
                                  lambda a, b: a != b)
    # END_YOUR_CODE
    return csp
Exemple #28
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (around 5 lines of code expected)
    for var in variables:
        csp.add_variable(var, domain)
    for i in range(n - 1):
        csp.add_binary_potential(variables[i], variables[i + 1],
                                 lambda a, b: a != b)
    # END_YOUR_CODE
    return csp
Exemple #29
0
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d' % i for i in range(1, n + 1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (our solution is 5 lines of code, but don't worry if you deviate from this)
    for v in variables:
        csp.add_variable(v, domain)
    for i in range(len(variables) - 1):
        csp.add_binary_factor(variables[i], variables[i + 1],
                              lambda d1, d2: d1 ^ d2)
    # END_YOUR_CODE
    return csp
def create_chain_csp(n):
    # same domain for each variable
    domain = [0, 1]
    # name variables as x_1, x_2, ..., x_n
    variables = ['x%d'%i for i in range(1, n+1)]
    csp = util.CSP()
    # Problem 0c
    # BEGIN_YOUR_CODE (our solution is 4 lines of code, but don't worry if you deviate from this)
    variables = [f'X{i}' for i in range(1, n + 1)]
    for i in range(n):
        csp.add_variable(variables[i], [0, 1])
    for i in range(n-1):
        csp.add_binary_factor(variables[i], variables[i+1], lambda x, y: x ^ y)  # ^ is XOR
    # END_YOUR_CODE
    return csp