コード例 #1
0
def factor(n):
    print("factoring %d" % n)

    # size of inputs.
    # in other words, how many bits we have to allocate to store 'n'?
    input_bits = int(math.ceil(math.log(n, 2)))
    print("input_bits=%d" % input_bits)

    s = Xu.Xu(False)

    factor1, factor2 = s.alloc_BV(input_bits), s.alloc_BV(input_bits)
    product = s.multiplier(factor1, factor2)

    # at least one bit in each input must be set, except lowest.
    # hence we restrict inputs to be greater than 1
    s.fix(s.OR(factor1[:-1]), True)
    s.fix(s.OR(factor2[:-1]), True)

    # output has a size twice as bigger as each input:
    s.fix_BV(product, Xu.n_to_BV(n, input_bits * 2))

    if s.solve() == False:
        print("%d is prime (unsat)" % n)
        return [n]

    # get inputs of multiplier:
    factor1_n = Xu.BV_to_number(s.get_BV_from_solution(factor1))
    factor2_n = Xu.BV_to_number(s.get_BV_from_solution(factor2))

    print("factors of %d are %d and %d" % (n, factor1_n, factor2_n))
    # factor factors recursively:
    rt = sorted(factor(factor1_n) + factor(factor2_n))
    assert reduce(mul, rt, 1) == n
    return rt
コード例 #2
0
ファイル: XOR_SAT.py プロジェクト: wxdublin/SAT_SMT_article
def chk1():
    input_bits=8

    s=Xu.Xu(False)

    x,y=s.alloc_BV(input_bits),s.alloc_BV(input_bits)
    step1=s.BV_AND(x,y)
    minus_2=[s.const_true]*(input_bits-1)+[s.const_false]
    product=s.multiplier(step1,minus_2)[input_bits:]
    result1=s.adder(s.adder(product, x)[0], y)[0]

    result2=s.BV_XOR(x,y)

    s.fix(s.OR(s.BV_XOR(result1, result2)), True)

    if s.solve()==False:
        print ("unsat")
        return

    print ("sat")
    print ("x=%x" % Xu.BV_to_number(s.get_BV_from_solution(x)))
    print ("y=%x" % Xu.BV_to_number(s.get_BV_from_solution(y)))
    print ("step1=%x" % Xu.BV_to_number(s.get_BV_from_solution(step1)))
    print ("product=%x" % Xu.BV_to_number(s.get_BV_from_solution(product)))
    print ("result1=%x" % Xu.BV_to_number(s.get_BV_from_solution(result1)))
    print ("result2=%x" % Xu.BV_to_number(s.get_BV_from_solution(result2)))
    print ("minus_2=%x" % Xu.BV_to_number(s.get_BV_from_solution(minus_2)))
コード例 #3
0
def div_test():
    s=Xu.Xu(False)

    BITS=32
    divident=s.alloc_BV(BITS)
    divisor=s.alloc_BV(BITS)
    
    s.fix_BV(divident, Xu.n_to_BV(1234567890, BITS))
    s.fix_BV(divisor, Xu.n_to_BV(123, BITS))

    quotient, remainder=s.divider(divident, divisor)

    assert s.solve()==True

    assert Xu.BV_to_number(s.get_BV_from_solution(quotient))==10037137
    assert Xu.BV_to_number(s.get_BV_from_solution(remainder))==39
コード例 #4
0
def AND_list_test():
    s=Xu.Xu(False)

    _vars=s.alloc_BV(4)
    s.fix(s.AND_list(_vars),False)

    assert (s.count_solutions()==15)
コード例 #5
0
def SumIsNot1_test():
    s=Xu.Xu(False)

    _vars=s.alloc_BV(4)
    s.SumIsNot1(_vars)

    assert s.count_solutions()==12
コード例 #6
0
ファイル: XOR_SAT.py プロジェクト: wxdublin/SAT_SMT_article
def chk2():
    input_bits=64

    s=Xu.Xu(False)

    x,y=s.alloc_BV(input_bits),s.alloc_BV(input_bits)
    step1=s.BV_AND(x,y)
    step2=s.shift_left_1(s.NEG(step1))

    result1=s.adder(s.adder(step2, x)[0], y)[0]

    result2=s.BV_XOR(x,y)

    s.fix(s.OR(s.BV_XOR(result1, result2)), True)

    if s.solve()==False:
        print ("unsat")
        return

    print ("sat")
    print ("x=%x" % Xu.BV_to_number(s.get_BV_from_solution(x)))
    print ("y=%x" % Xu.BV_to_number(s.get_BV_from_solution(y)))
    print ("step1=%x" % Xu.BV_to_number(s.get_BV_from_solution(step1)))
    print ("step2=%x" % Xu.BV_to_number(s.get_BV_from_solution(step2)))
    print ("result1=%x" % Xu.BV_to_number(s.get_BV_from_solution(result1)))
    print ("result2=%x" % Xu.BV_to_number(s.get_BV_from_solution(result2)))
コード例 #7
0
def div(dividend, divisor):

    # size of inputs.
    # in other words, how many bits we have to allocate to store 'n'?
    input_bits = int(math.ceil(math.log(dividend, 2)))
    print("input_bits=%d" % input_bits)

    s = Xu.Xu(False)

    factor1, factor2 = s.alloc_BV(input_bits), s.alloc_BV(input_bits)
    product = s.multiplier(factor1, factor2)

    # connect divisor to one of multiplier's input:
    s.fix_BV(factor1, Xu.n_to_BV(divisor, input_bits))
    # output has a size twice as bigger as each input.
    # connect dividend to multiplier's output:
    s.fix_BV(product, Xu.n_to_BV(dividend, input_bits * 2))

    if s.solve() == False:
        print("remainder!=0 (unsat)")
        return None

    # get 2nd input of multiplier, which is quotient:
    return Xu.BV_to_number(s.get_BV_from_solution(factor2))
コード例 #8
0
def do_all():
    s = Xu.Xu(maxsat=True)

    code = [s.alloc_BV(BITS) for r in range(ROWS)]
    ch = [s.alloc_BV(BITS) for r in range(ROWS)]

    # each rows must be different from a previous one and a next one by 1 bit:
    for i in range(ROWS):
        # get bits of the current row:
        lst1 = [code[i][bit] for bit in range(BITS)]
        # get bits of the next row.
        # important: if the current row is the last one, (last+1)&MASK==0, so we overlap here:
        lst2 = [code[(i + 1) & MASK][bit] for bit in range(BITS)]
        s.hamming1(lst1, lst2)

    # no row must be equal to any another row:
    for i in range(ROWS):
        for j in range(ROWS):
            if i == j:
                continue
            lst1 = [code[i][bit] for bit in range(BITS)]
            lst2 = [code[j][bit] for bit in range(BITS)]
            s.fix_BV_NEQ(lst1, lst2)

    # 1 in ch[] table means that run of 1's has been changed to run of 0's, or back.
    # "run" change detected using simple XOR:
    for i in range(ROWS):
        for bit in range(BITS):
            # row overlapping works here as well.
            # we add here "soft" constraint with weight=1:
            s.fix_soft(s.EQ(ch[i][bit],
                            s.XOR(code[i][bit], code[(i + 1) & MASK][bit])),
                       False,
                       weight=1)

    if s.solve() == False:
        print("unsat")
        exit(0)

    print("code table:")

    for i in range(ROWS):
        tmp = ""
        for bit in range(BITS):
            t = s.get_var_from_solution(code[i][BITS - 1 - bit])
            if t:
                tmp = tmp + "*"
            else:
                tmp = tmp + " "
        print(tmp)

    # get statistics:
    stat = {}

    for i in range(ROWS):
        for bit in range(BITS):
            x = s.get_var_from_solution(ch[i][BITS - 1 - bit])
            if x == 0:
                # increment if bit is present in dict, set 1 if not present
                stat[bit] = stat.get(bit, 0) + 1

    print("stat (bit number: number of changes): ")
    print(stat)
コード例 #9
0
def do_all():
    #s=Xu.Xu(maxsat=False)
    s = Xu.Xu(maxsat=True)

    cells = [[s.alloc_BV(BITS_PER_CELL) for c in range(width)]
             for r in range(height)]

    L = [[s.create_var() for c in range(width)] for r in range(height)]
    R = [[s.create_var() for c in range(width)] for r in range(height)]
    U = [[s.create_var() for c in range(width)] for r in range(height)]
    D = [[s.create_var() for c in range(width)] for r in range(height)]

    cell_is_empty = [[s.create_var() for c in range(width)]
                     for r in range(height)]

    # U for a cell must be equal to D of the cell above, etc:
    for r in range(height):
        for c in range(width):
            if r != 0:
                s.fix_EQ(U[r][c], D[r - 1][c])
            if r != height - 1:
                s.fix_EQ(D[r][c], U[r + 1][c])
            if c != 0:
                s.fix_EQ(L[r][c], R[r][c - 1])
            if c != width - 1:
                s.fix_EQ(R[r][c], L[r][c + 1])

            # "maximize" number of empty cells:
            s.fix_soft_always_true(cell_is_empty[r][c], 1)

    for r in range(height):
        for c in range(width):
            t = puzzle[r][c]
            if t == ' ':
                # puzzle has space, so degree=2, IOW, this cell must have 2 connections, no more, no less.
                # enumerate all possible L/R/U/D booleans. two of them must be True, others are False.
                t = []
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]), L[r][c], R[r][c],
                        s.NOT(U[r][c]),
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]), L[r][c],
                        s.NOT(R[r][c]), U[r][c],
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]), L[r][c],
                        s.NOT(R[r][c]),
                        s.NOT(U[r][c]), D[r][c]
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]),
                        s.NOT(L[r][c]), R[r][c], U[r][c],
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]),
                        s.NOT(L[r][c]), R[r][c],
                        s.NOT(U[r][c]), D[r][c]
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(cell_is_empty[r][c]),
                        s.NOT(L[r][c]),
                        s.NOT(R[r][c]), U[r][c], D[r][c]
                    ]))
                # OR this cell has degree=0, i.e., no links:
                t.append(
                    s.AND_list([
                        cell_is_empty[r][c],
                        s.NOT(L[r][c]),
                        s.NOT(R[r][c]),
                        s.NOT(U[r][c]),
                        s.NOT(D[r][c])
                    ]))
                s.fix_always_true(s.OR_list(t))
            else:
                # cell has number, add it to cells[][] as a constraint:
                s.fix_BV(cells[r][c], Xu.n_to_BV(int(t), BITS_PER_CELL))
                # cell has degree=1, IOW, this cell must have 1 connection, no more, no less
                # enumerate all possible ways:
                t = []
                t.append(
                    s.AND_list([
                        L[r][c],
                        s.NOT(R[r][c]),
                        s.NOT(U[r][c]),
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(L[r][c]), R[r][c],
                        s.NOT(U[r][c]),
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(L[r][c]),
                        s.NOT(R[r][c]), U[r][c],
                        s.NOT(D[r][c])
                    ]))
                t.append(
                    s.AND_list([
                        s.NOT(L[r][c]),
                        s.NOT(R[r][c]),
                        s.NOT(U[r][c]), D[r][c]
                    ]))
                s.fix_always_true(s.OR_list(t))

            # if L[][]==True, cell's number must be equal to the number of cell at left, etc:
            if c != 0:
                s.fix_always_true(
                    s.ITE(L[r][c], s.const_true,
                          s.BV_EQ(cells[r][c], cells[r][c - 1])))
            if c != width - 1:
                s.fix_always_true(
                    s.ITE(R[r][c], s.const_true,
                          s.BV_EQ(cells[r][c], cells[r][c + 1])))
            if r != 0:
                s.fix_always_true(
                    s.ITE(U[r][c], s.const_true,
                          s.BV_EQ(cells[r][c], cells[r - 1][c])))
            if r != height - 1:
                s.fix_always_true(
                    s.ITE(D[r][c], s.const_true,
                          s.BV_EQ(cells[r][c], cells[r + 1][c])))

    # L/R/U/D's of borderline cells must always be False:
    for r in range(height):
        s.fix_always_false(L[r][0])
        s.fix_always_false(R[r][width - 1])

    for c in range(width):
        s.fix_always_false(U[0][c])
        s.fix_always_false(D[height - 1][c])

    if s.solve() == False:
        print("unsat")
        exit(0)
    else:
        print("sat")

    print("")

    for r in range(height):
        t = ""
        for c in range(width):
            t = t + ("%2d" % Xu.BV_to_number(
                s.get_BV_from_solution(cells[r][c]))) + " "
        print(t)

    print("")

    for r in range(height):
        t = ""
        for c in range(width):
            t = t + ("L" if s.get_var_from_solution(L[r][c]) else " ")
            t = t + ("R" if s.get_var_from_solution(R[r][c]) else " ")
            t = t + ("U" if s.get_var_from_solution(U[r][c]) else " ")
            t = t + ("D" if s.get_var_from_solution(D[r][c]) else " ")
            t = t + "|"
        print(t)

    print("")

    for r in range(height):
        row = ""
        for c in range(width):
            t = puzzle[r][c]
            if t == ' ':
                tl = (True if s.get_var_from_solution(L[r][c]) else False)
                tr = (True if s.get_var_from_solution(R[r][c]) else False)
                tu = (True if s.get_var_from_solution(U[r][c]) else False)
                td = (True if s.get_var_from_solution(D[r][c]) else False)

                if tl == False and tr == False and tu == False and td == False:
                    row = row + " "
                if tu and td:
                    row = row + "┃"
                if tr and td:
                    row = row + "┏"
                if tr and tu:
                    row = row + "┗"
                if tl and td:
                    row = row + "┓"
                if tl and tu:
                    row = row + "┛"
                if tl and tr:
                    row = row + "━"
            else:
                row = row + t
        print(row)
コード例 #10
0
def do_all(STEPS):

    # this is MaxSAT problem:
    s=Xu.Xu(True)

    STATES=STEPS+1

    # bool variables for all 64 cells for all states:
    states=[[[s.create_var() for r in range(SIZE)] for c in range(SIZE)] for st in range(STATES)]

    # for all states:
    # False - horizontal, True - vertical:
    H_or_V=[s.create_var() for i in range(STEPS)]
    # this is "shared" variable, can hold both and or column:
    row_col=[[s.create_var() for r in range(SIZE)] for i in range(STEPS)]

    # set variables on state #0:
    for r in range(SIZE):
        for c in range(SIZE):
            s.fix(states[0][r][c], initial[r][c])

    # each row_col bitvector can only hold one single bit:
    for st in range(STEPS):
        s.POPCNT1(row_col[st])

    # now this is the essence of the problem.
    # each bit is flipped if H_or_V[]==False AND row_col[] is equal to its row
    # ... OR H_or_V[]==True AND row_col[] is equal to its column
    for st in range(STEPS):
        for r in range(SIZE):
            for c in range(SIZE):
                cond1=s.AND(s.NOT(H_or_V[st]), row_col[st][r])
                cond2=s.AND(H_or_V[st], row_col[st][c])
                bit=s.OR([cond1,cond2])

                s.fix(s.EQ(states[st+1][r][c], s.XOR(states[st][r][c], bit)), True)

    # soft constraint: unlike hard constraints, they may be satisfied, or may be not, 
    # but MaxSAT solver's task is to find a solution, where maximum of soft clauses will be satisfied:
    for r in range(SIZE):
        for c in range(SIZE):
            s.fix_soft(states[STEPS][r][c], False, 1)
            # set to True if you like to find solution with the most cells set to 1:
            #s.fix_soft(states[STEPS][r][c], True, 1)

    assert s.solve()==True

    # get solution:
    total=0
    for r in range(SIZE):
        for c in range(SIZE):
            t=s.get_var_from_solution(states[STEPS][r][c])
            print t,
            total=total+t
        print ""
    print "total=", total

    for st in range(STEPS):
        if s.get_var_from_solution(H_or_V[st]):
            print "vertical, ",
        else:
            print "horizontal, ",
        print int(math.log(Xu.BV_to_number(s.get_BV_from_solution(row_col[st])), 2))
コード例 #11
0
ファイル: solver.py プロジェクト: wxdublin/SAT_SMT_article
def do_all(turns):
    global s, TURNS, STATES, facelets, selectors

    TURNS = turns
    STATES = TURNS + 1
    facelets = None
    selectors = None

    s = Xu.Xu(False)

    alloc_facelets_and_selectors(STATES)

    # solved state:
    set_state(
        STATES - 1, {
            "F": "WWWW",
            "U": "GGGG",
            "D": "BBBB",
            "R": "OOOO",
            "L": "RRRR",
            "B": "YYYY"
        })

    # 4: rdur, 2 solutions (picosat)
    set_state(
        0, {
            "F": "RYOG",
            "U": "YRGO",
            "D": "WRBO",
            "R": "GYWB",
            "L": "BYWG",
            "B": "BOWR"
        })

    # other tests:

    # 9 or less
    #set_state(0, {"F":"OROB", "U":"GYGG", "D":"BRWO", "R":"WGYY", "L":"RWRW", "B":"OYBB"})

    # 5
    #set_state(0, {"F":"BROW", "U":"YWOB", "D":"WROY", "R":"YGBG", "L":"BWYG", "B":"RORG"})

    # 5
    #set_state(0, {"F":"RYOG", "U":"YBGO", "D":"WRBW", "R":"GOWB", "L":"RYYG", "B":"WBRO"})

    # 1 (RCW)
    #set_state(0, {"F":"WGWG", "U":"GYGY", "D":"BWBW", "R":"OOOO", "L":"RRRR", "B":"BYBY"})

    # 9 or less
    #set_state(0, {"F":"ROWB", "U":"RYYB", "D":"RYWG", "R":"WGOR", "L":"WBOG", "B":"OBYG"})

    # 6, 2 solutions (picosat)...
    #set_state(0, {"F":"RBGW", "U":"YRGW", "D":"YGWB", "R":"OBRO", "L":"RYOO", "B":"WBYG"})

    # 4, 1 solution (picosat)!
    #set_state(0, {"F":"GGYB", "U":"GRRY", "D":"RWBO", "R":"OGRY", "L":"OWOB", "B":"YWBW"})

    # 6, 6 solutions (picosat)
    #set_state(0, {"F":"GRRB", "U":"YYWY", "D":"WOBW", "R":"GGWR", "L":"BORB", "B":"OOGY"})

    # 6, 3 solutions (picosat)
    #set_state(0, {"F":"RWBG", "U":"BWYR", "D":"WYYO", "R":"GORG", "L":"RBOO", "B":"GWYB"})

    # 3 or less
    #set_state(0, {"F":"RORW", "U":"BRBB", "D":"GOOR", "R":"WYGY", "L":"OWYW", "B":"BYGG"})

    # 4
    #set_state(0, {"F":"RBRO", "U":"WGWY", "D":"YWOB", "R":"RWBO", "L":"BGYG", "B":"ORYG"})

    # 8
    #set_state(0, {"F":"GBOO", "U":"BBRO", "D":"BYGY", "R":"YWGG", "L":"YWWW", "B":"RRRO"})

    # 8
    #set_state(0, {"F":"BGBO", "U":"GWYR", "D":"YGYO", "R":"YRWB", "L":"WOGR", "B":"BRWO"})

    # 3 or less
    #set_state(0, {"F":"GRBG", "U":"BYOG", "D":"WYOY", "R":"WORR", "L":"RYGO", "B":"BWBW"})

    # 7
    #set_state(0, {"F":"BBWY", "U":"OGOR", "D":"ROYY", "R":"WYBO", "L":"WWBG", "B":"RGGR"})

    # 8
    #set_state(0, {"F":"YRRO", "U":"WWBY", "D":"WYWB", "R":"GBGO", "L":"RROB", "B":"OGYG"})

    # 3 or less (stuck)
    # set_state(0, {"F":"OWBB", "U":"RYBG", "D":"RORG", "R":"ORWY", "L":"BYWW", "B":"GYOG"})

    # 9 or less
    # set_state(0, {"F":"GGRW", "U":"BBYO", "D":"GRBO", "R":"WYBG", "L":"WRRW", "B":"OOYY"})

    # 8 or less
    #set_state(0, {"F":"WBRG", "U":"RGBR", "D":"GORB", "R":"WWYO", "L":"YOYW", "B":"OGYB"})

    # 8
    #set_state(0, {"F":"GOWW", "U":"RYRG", "D":"GRBY", "R":"YOBG", "L":"BWOO", "B":"BYRW"})

    # FCW, RCW
    #set_state(0, {"F":"WOWB", "U":"GWRW", "D":"OYBY", "R":"GGOO", "L":"RBRB", "B":"RYGY"})

    # 9
    #set_state(0, {"F":"YOWY", "U":"OGGY", "D":"OBGR", "R":"BRRW", "L":"WOYB", "B":"WGBR"})

    # 1 F
    #set_state(0, {"F":"WWWW", "U":"GGRR", "D":"OOBB", "R":"GOGO", "L":"RBRB", "B":"YYYY"})

    # 2 BCW / FCCW
    #set_state(0, {"F":"WWWW", "U":"RRRR", "D":"OOOO", "R":"GGGG", "L":"BBBB", "B":"YYYY"})

    # 4 RDUR, but backwards: RCCW UCCW DCCW RCCW
    #set_state(0, {"F":"OBRY", "U":"GOWR", "D":"BOYR", "R":"WGBY", "L":"WBGY", "B":"WRGO"})

    # 2, UCCW / DCW
    #set_state(0, {"F":"OOOO", "U":"GGGG", "D":"BBBB", "R":"YYYY", "L":"WWWW", "B":"RRRR"})

    # 2, RCCW / LCW
    #set_state(0, {"F":"BBBB", "U":"WWWW", "D":"YYYY", "R":"OOOO", "L":"RRRR", "B":"GGGG"})

    # 1, UCCW
    #set_state(0, {"F":"OOWW", "U":"GGGG", "D":"BBBB", "R":"YYOO", "L":"WWRR", "B":"RRYY"})

    # 1, UCW
    #set_state(0, {"F":"RRWW", "U":"GGGG", "D":"BBBB", "R":"WWOO", "L":"YYRR", "B":"OOYY"})

    # 1, RCCW
    #set_state(0, {"F":"WBWB", "U":"GWGW", "D":"BYBY", "R":"OOOO", "L":"RRRR", "B":"GYGY"})

    # 1, RCW
    #set_state(0, {"F":"WGWG", "U":"GYGY", "D":"BWBW", "R":"OOOO", "L":"RRRR", "B":"BYBY"})

    # 1, DCCW
    #set_state(0, {"F":"WWRR", "U":"GGGG", "D":"BBBB", "R":"OOWW", "L":"RRYY", "B":"YYOO"})

    # 1. DCW
    #set_state(0, {"F":"WWOO", "U":"GGGG", "D":"BBBB", "R":"OOYY", "L":"RRWW", "B":"YYRR"})

    # 1 LCCW
    #set_state(0, {"F":"GWGW", "U":"YGYG", "D":"WBWB", "R":"OOOO", "L":"RRRR", "B":"YBYB"})

    # 1 LCW
    #set_state(0, {"F":"BWBW", "U":"WGWG", "D":"YBYB", "R":"OOOO", "L":"RRRR", "B":"YGYG"})

    # 1 FCCW
    #set_state(0, {"F":"WWWW", "U":"GGRR", "D":"OOBB", "R":"GOGO", "L":"RBRB", "B":"YYYY"})

    # 1. FCW
    #set_state(0, {"F":"WWWW", "U":"GGOO", "D":"RRBB", "R":"BOBO", "L":"RGRG", "B":"YYYY"})

    # 1 BCCW
    #set_state(0, {"F":"WWWW", "U":"OOGG", "D":"BBRR", "R":"OBOB", "L":"GRGR", "B":"YYYY"})

    # 1. BCW
    #set_state(0, {"F":"WWWW", "U":"RRGG", "D":"BBOO", "R":"OGOG", "L":"BRBR", "B":"YYYY"})

    # 3 or less. right top corner rotated CW, left top rotated CCW
    #set_state(0, {"F":"ROWW", "U":"GGWW", "D":"BBBB", "R":"GOOO", "L":"RGRR", "B":"YYYY"})

    # 8
    #set_state(0, {"F":"RWWY", "U":"YOWB", "D":"OOOR", "R":"RYBY", "L":"RGGB", "B":"GBGW"})

    #      dst,  FCW  FH   FCCW UCW  UH   UCCW DCW  DH   DCCW RCW  RH   RCCW LCW  LH   LCCW BCW  BH   BCCW
    add_r("F1", [
        "F3", "F4", "F2", "R1", "B1", "L1", "F1", "F1", "F1", "F1", "F1", "F1",
        "U1", "B4", "D1", "F1", "F1", "F1"
    ])
    add_r("F2", [
        "F1", "F3", "F4", "R2", "B2", "L2", "F2", "F2", "F2", "D2", "B3", "U2",
        "F2", "F2", "F2", "F2", "F2", "F2"
    ])
    add_r("F3", [
        "F4", "F2", "F1", "F3", "F3", "F3", "L3", "B3", "R3", "F3", "F3", "F3",
        "U3", "B2", "D3", "F3", "F3", "F3"
    ])
    add_r("F4", [
        "F2", "F1", "F3", "F4", "F4", "F4", "L4", "B4", "R4", "D4", "B1", "U4",
        "F4", "F4", "F4", "F4", "F4", "F4"
    ])
    add_r("U1", [
        "U1", "U1", "U1", "U3", "U4", "U2", "U1", "U1", "U1", "U1", "U1", "U1",
        "B4", "D1", "F1", "R2", "D4", "L3"
    ])
    add_r("U2", [
        "U2", "U2", "U2", "U1", "U3", "U4", "U2", "U2", "U2", "F2", "D2", "B3",
        "U2", "U2", "U2", "R4", "D3", "L1"
    ])
    add_r("U3", [
        "L4", "D2", "R1", "U4", "U2", "U1", "U3", "U3", "U3", "U3", "U3", "U3",
        "B2", "D3", "F3", "U3", "U3", "U3"
    ])
    add_r("U4", [
        "L2", "D1", "R3", "U2", "U1", "U3", "U4", "U4", "U4", "F4", "D4", "B1",
        "U4", "U4", "U4", "U4", "U4", "U4"
    ])
    add_r("D1", [
        "R3", "U4", "L2", "D1", "D1", "D1", "D3", "D4", "D2", "D1", "D1", "D1",
        "F1", "U1", "B4", "D1", "D1", "D1"
    ])
    add_r("D2", [
        "R1", "U3", "L4", "D2", "D2", "D2", "D1", "D3", "D4", "B3", "U2", "F2",
        "D2", "D2", "D2", "D2", "D2", "D2"
    ])
    add_r("D3", [
        "D3", "D3", "D3", "D3", "D3", "D3", "D4", "D2", "D1", "D3", "D3", "D3",
        "F3", "U3", "B2", "L1", "U2", "R4"
    ])
    add_r("D4", [
        "D4", "D4", "D4", "D4", "D4", "D4", "D2", "D1", "D3", "B1", "U4", "F4",
        "D4", "D4", "D4", "L3", "U1", "R2"
    ])
    add_r("R1", [
        "U3", "L4", "D2", "B1", "L1", "F1", "R1", "R1", "R1", "R3", "R4", "R2",
        "R1", "R1", "R1", "R1", "R1", "R1"
    ])
    add_r("R2", [
        "R2", "R2", "R2", "B2", "L2", "F2", "R2", "R2", "R2", "R1", "R3", "R4",
        "R2", "R2", "R2", "D4", "L3", "U1"
    ])
    add_r("R3", [
        "U4", "L2", "D1", "R3", "R3", "R3", "F3", "L3", "B3", "R4", "R2", "R1",
        "R3", "R3", "R3", "R3", "R3", "R3"
    ])
    add_r("R4", [
        "R4", "R4", "R4", "R4", "R4", "R4", "F4", "L4", "B4", "R2", "R1", "R3",
        "R4", "R4", "R4", "D3", "L1", "U2"
    ])
    add_r("L1", [
        "L1", "L1", "L1", "F1", "R1", "B1", "L1", "L1", "L1", "L1", "L1", "L1",
        "L3", "L4", "L2", "U2", "R4", "D3"
    ])
    add_r("L2", [
        "D1", "R3", "U4", "F2", "R2", "B2", "L2", "L2", "L2", "L2", "L2", "L2",
        "L1", "L3", "L4", "L2", "L2", "L2"
    ])
    add_r("L3", [
        "L3", "L3", "L3", "L3", "L3", "L3", "B3", "R3", "F3", "L3", "L3", "L3",
        "L4", "L2", "L1", "U1", "R2", "D4"
    ])
    add_r("L4", [
        "D2", "R1", "U3", "L4", "L4", "L4", "B4", "R4", "F4", "L4", "L4", "L4",
        "L2", "L1", "L3", "L4", "L4", "L4"
    ])
    add_r("B1", [
        "B1", "B1", "B1", "L1", "F1", "R1", "B1", "B1", "B1", "U4", "F4", "D4",
        "B1", "B1", "B1", "B3", "B4", "B2"
    ])
    add_r("B2", [
        "B2", "B2", "B2", "L2", "F2", "R2", "B2", "B2", "B2", "B2", "B2", "B2",
        "D3", "F3", "U3", "B1", "B3", "B4"
    ])
    add_r("B3", [
        "B3", "B3", "B3", "B3", "B3", "B3", "R3", "F3", "L3", "U2", "F2", "D2",
        "B3", "B3", "B3", "B4", "B2", "B1"
    ])
    add_r("B4", [
        "B4", "B4", "B4", "B4", "B4", "B4", "R4", "F4", "L4", "B4", "B4", "B4",
        "D1", "F1", "U1", "B2", "B1", "B3"
    ])

    if s.solve() == False:
        print "unsat!"
        exit(0)

    turn_name = [
        "FCW", "FH", "FCCW", "UCW", "UH", "UCCW", "DCW", "DH", "DCCW", "RCW",
        "RH", "RCCW", "LCW", "LH", "LCCW", "BCW", "BH", "BCCW"
    ]

    print "sat!"
    for turn in selectors:
        # 'turn' is array of 5 variables
        # convert each variable to "1" or "0" string and create 5-digit string:
        rt = "".join([str(s.get_var_from_solution(bit)) for bit in turn])
        # reverse string, convert it to integer and use it as index for array to get a move name:
        print turn_name[int(frolic.rvr(rt), 2)]

    print ""
コード例 #12
0
import Xu
import itertools, math

PERSONS, DAYS, GROUPS = 12, 11, 6

s = Xu.Xu(False)

# each element - group for each person and each day:
tbl = [[s.alloc_BV(GROUPS) for day in range(DAYS)]
       for person in range(PERSONS)]


def chr_to_n(c):
    return ord(c) - ord('A')


# A must play B on day 1, G on day 3, and H on day 6.
# A/B, day 1:
s.fix_BV_EQ(tbl[chr_to_n('A')][0], tbl[chr_to_n('B')][0])
# A/G, day 3:
s.fix_BV_EQ(tbl[chr_to_n('A')][2], tbl[chr_to_n('G')][2])
# A/H, day 5:
s.fix_BV_EQ(tbl[chr_to_n('A')][4], tbl[chr_to_n('H')][4])

# F must play I on day 2 and J on day 5.
s.fix_BV_EQ(tbl[chr_to_n('F')][1], tbl[chr_to_n('I')][1])
s.fix_BV_EQ(tbl[chr_to_n('F')][4], tbl[chr_to_n('J')][4])

# K must play H on day 9 and E on day 11.
s.fix_BV_EQ(tbl[chr_to_n('K')][8], tbl[chr_to_n('H')][8])
s.fix_BV_EQ(tbl[chr_to_n('K')][10], tbl[chr_to_n('E')][10])
コード例 #13
0
ファイル: 8queens.py プロジェクト: wxdublin/SAT_SMT_article
def main():
    global first_var

    s = Xu.Xu(False)

    _vars = s.alloc_BV(SIZE**2)
    first_var = int(_vars[0])

    # enumerate all rows:
    for row in range(SIZE):
        s.POPCNT1([row_col_to_var(row, col) for col in range(SIZE)])

    # enumerate all columns:
    # POPCNT1() could be used here as well:
    for col in range(SIZE):
        s.AtMost1([row_col_to_var(row, col) for row in range(SIZE)])

    # enumerate all diagonals:
    for row in range(SIZE):
        for col in range(SIZE):
            gen_diagonal(s, row, col, 1, SIZE)  # from L to R
            gen_diagonal(s, row, col, -1, -1)  # from R to L

    # find all solutions:
    sol_n = 1
    while True:
        if s.solve() == False:
            print "unsat!"
            print "solutions total=", sol_n - 1
            exit(0)

        # print solution:
        print "solution number", sol_n, ":"

        # get solution and make 2D array of bools:
        solution_as_2D_bool_array = []
        for row in range(SIZE):
            solution_as_2D_bool_array.append([
                s.get_var_from_solution(row_col_to_var(row, col))
                for col in range(SIZE)
            ])

        # print 2D array:
        for row in range(SIZE):
            tmp = [([" ", "*"][solution_as_2D_bool_array[row][col]] + "|")
                   for col in range(SIZE)]
            print "|" + "".join(tmp)

        # add 2D array as negated constraint:
        add_2D_array_as_negated_constraint(s, solution_as_2D_bool_array)

        # if we skip symmetries, rotate/reflect soluion and add them as negated constraints:
        if SKIP_SYMMETRIES:
            for a in range(4):
                tmp = frolic.rotate_rect_array(solution_as_2D_bool_array, a)
                add_2D_array_as_negated_constraint(s, tmp)

                tmp = frolic.reflect_horizontally(
                    frolic.rotate_rect_array(solution_as_2D_bool_array, a))
                add_2D_array_as_negated_constraint(s, tmp)

        sol_n = sol_n + 1