示例#1
0
def make_transition_tuples(pattern):
    p_len = len(pattern)
    num_states = p_len + sum(pattern)

    tuples = pywrapcp.IntTupleSet(3)

    # this is for handling 0-clues. It generates
    # just the minimal state
    if num_states == 0:
        tuples.Insert3(1, 0, 1)
        return (tuples, 1)

    # convert pattern to a 0/1 pattern for easy handling of
    # the states
    tmp = [0]
    c = 0
    for pattern_index in range(p_len):
        tmp.extend([1] * pattern[pattern_index])
        tmp.append(0)

    for i in range(num_states):
        state = i + 1
        if tmp[i] == 0:
            tuples.Insert3(state, 0, state)
            tuples.Insert3(state, 1, state + 1)
        else:
            if i < num_states - 1:
                if tmp[i + 1] == 1:
                    tuples.Insert3(state, 1, state + 1)
                else:
                    tuples.Insert3(state, 0, state + 1)
    tuples.Insert3(num_states, 0, num_states)
    return (tuples, num_states)
示例#2
0
def main(base=10, start=1, len1=1, len2=4):

    # Create the solver.
    solver = pywrapcp.Solver('Traffic lights')

    #
    # data
    #
    n = 4
    r, ry, g, y = range(n)
    lights = ["r", "ry", "g", "y"]

    # The allowed combinations
    allowed = pywrapcp.IntTupleSet(4)
    allowed.InsertAll([(r,r,g,g),
                       (ry,r,y,r),
                       (g,g,r,r),
                       (y,r,ry,r)])


    #
    # declare variables
    #
    V = [solver.IntVar(0, n-1, 'V[%i]' % i) for i in range(n)]
    P = [solver.IntVar(0, n-1, 'P[%i]' % i) for i in range(n)]

    #
    # constraints
    #
    for i in range(n):
        for j in range(n):
            if j == (1+i) % n:
                solver.Add(solver.AllowedAssignments((V[i], P[i], V[j], P[j]), allowed))

    #
    # Search and result
    #
    db = solver.Phase(V + P,
                 solver.INT_VAR_SIMPLE,
                 solver.INT_VALUE_DEFAULT)

    solver.NewSearch(db)
    num_solutions = 0
    while solver.NextSolution():
        for i in range(n):
            print "%+2s %+2s" % (lights[V[i].Value()], lights[P[i].Value()]),
        print
        num_solutions += 1

    solver.EndSearch()

    print
    print "num_solutions:", num_solutions
    print "failures:", solver.Failures()
    print "branches:", solver.Branches()
    print "WallTime:", solver.WallTime()
    print
示例#3
0
def regular(x, Q, S, d, q0, F):

    solver = x[0].solver()

    assert Q > 0, 'regular: "Q" must be greater than zero'
    assert S > 0, 'regular: "S" must be greater than zero'

    # d2 is the same as d, except we add one extra transition for
    # each possible input;  each extra transition is from state zero
    # to state zero.  This allows us to continue even if we hit a
    # non-accepted input.

    d2 = pywrapcp.IntTupleSet(3)
    for i in range(Q + 1):
        for j in range(1, S + 1):
            if i == 0:
                d2.Insert3(0, j, 0)
            else:
                d2.Insert3(i, j, d[i - 1][j - 1])

    solver.Add(solver.TransitionConstraint(x, d2, q0, F))
示例#4
0
def regular(x, Q, S, d, q0, F):

    solver = x[0].solver()

    assert Q > 0, 'regular: "Q" must be greater than zero'
    assert S > 0, 'regular: "S" must be greater than zero'

    # d2 is the same as d, except we add one extra transition for
    # each possible input;  each extra transition is from state zero
    # to state zero.  This allows us to continue even if we hit a
    # non-accepted input.

    d2 = pywrapcp.IntTupleSet(3)
    for i in range(Q + 1):
        for j in range(S):
            if i == 0:
                d2.Insert3(0, j, 0)
            else:
                d2.Insert3(i, j, d[i - 1][j])

    # If x has index set m..n, then a[m-1] holds the initial state
    # (q0), and a[i+1] holds the state we're in after processing
    # x[i].  If a[n] is in F, then we succeed (ie. accept the
    # string).
    x_range = range(0, len(x))
    m = 0
    n = len(x)

    a = [solver.IntVar(0, Q + 1, 'a[%i]' % i) for i in range(m, n + 1)]

    # Check that the final state is in F
    solver.Add(solver.MemberCt(a[-1], F))
    # First state is q0
    solver.Add(a[m] == q0)
    for i in x_range:
        solver.Add(x[i] >= 1)
        solver.Add(x[i] <= S)
        # Determine a[i+1]: a[i+1] == d2[a[i], x[i]]
        solver.Add(solver.AllowedAssignments((a[i], x[i] - 1, a[i + 1]), d2))