diff --git a/sat.py b/sat.py index 416affd..efa77b7 100644 --- a/sat.py +++ b/sat.py @@ -1,17 +1,24 @@ from pysmt.shortcuts import Symbol, Bool, Not, Implies, Iff, And, Or, AtMostOne, ExactlyOne, get_model, get_atoms, get_formula_size, get_unsat_core from pysmt.rewritings import conjunctive_partition import json +MOVES = 60 +NUM_STRIKES = 3 +NUM_PLAYERS = 5 colors = 'rygbp' deck_str = 'p5 p3 b4 r5 y4 y4 y5 r4 b2 y2 y3 g5 g2 g3 g4 p4 r3 b2 b3 b3 p4 b1 p2 b1 b1 p2 p1 p1 g1 r4 g1 r1 r3 r1 g1 r1 p1 b4 p3 g2 g3 g4 b5 y1 y1 y1 r2 r2 y2 y3' deck = [(s[0], int(s[1])) for s in deck_str.split(' ')] -MOVES = 52 -NUM_STRIKES = 3 # clues[m][i] == "after move m we have at least i clues" clues = {-1: {i: Bool(i < 9) for i in range(0, 10)}, **{m: {0: Bool(True), 9: Bool(False), **{i: Symbol('m{}c{}'.format(m, i)) for i in range(1, 9)}} for m in range(MOVES)}} # strikes[m][i] == "after move m we have at least i strikes" strikes = {-1: {i: Bool(i == 0) for i in range(0,NUM_STRIKES+1)}, **{m: {0: Bool(True), NUM_STRIKES: Bool(False), **{s: Symbol('m{}s{}'.format(m,s)) for s in range(1,NUM_STRIKES)}} for m in range(MOVES)} } +# extraround[m][i] == "after move m, the extraround has started and at least i turns of it have taken place" +# extraround = {28: {i: Bool(False) for i in range(0, NUM_PLAYERS)}, **{m: {NUM_PLAYERS+1: Bool(False), **{e: Symbol('m{}e{}'.format(m,t) for e in range(0, NUM_PLAYERS+1)}} for m in range(MOVES)} } +# extraturn[m] = "turn m is a move part of the extra round or a dummy turn" +extraround = {-1: Bool(False), **{m: Symbol('m{}e'.format(m)) for m in range(0, MOVES)}} +# dummyturn[m] = "turn m is a dummy turn and not actually part of the game" +#dummyturn = {-1: Bool(False), **{m: Symbol('m{}e'.format(m)) for m in range(0, MOVES)}} # strike[m] = "at move m we get a strike" strike = {-1: Bool(False), **{m: Symbol('m{}s+'.format(m)) for m in range(MOVES)}} # draw[m][i] == "at move m we draw deck[i]" @@ -40,7 +47,7 @@ def print_model(model): print('discard: ' + ', '.join('{} [{}{}]'.format(i, deck[i][0], deck[i][1]) for i in range(50) if model.get_py_value(discard[m][i]))) for c in colors: print('progress {}: '.format(c) + ''.join(str(k) for k in range(1, 6) if model.get_py_value(progress[m][c, k]))) - flags = ['discard_any', 'draw_any', 'play', 'play5', 'incr_clues', 'strike'] + flags = ['discard_any', 'draw_any', 'play', 'play5', 'incr_clues', 'strike', 'extraround'] print(', '.join(f for f in flags if model.get_py_value(globals()[f][m]))) def toJSON(model): @@ -82,7 +89,8 @@ def toJSON(model): "actions": actions, "first_player": 0, "options": { - "variant": "No Variant" + "variant": "No Variant", + "allOrNothing": True } } print(json.dumps(game)) @@ -93,7 +101,8 @@ valid_move = lambda m: And( # definition of draw_any Iff(draw_any[m], Or(draw[m][i] for i in range(20, 50))), # draw implies discard (and converse true before last 5 moves) - (Iff if m < MOVES-5 else Implies)(draw_any[m], discard_any[m]), + Implies(draw_any[m], discard_any[m]), + Implies(discard_any[m], Or(extraround[m], draw_any[m])), # play requires discard Implies(play[m], discard_any[m]), # definition of play5 @@ -103,7 +112,7 @@ valid_move = lambda m: And( #Iff(incr_clues[m], And(discard_any[m], Implies(play[m], play5[m]))), # change of clues *[Iff(clues[m][i], Or(clues[m-1][i+1], And(clues[m-1][i], discard_any[m]), And(clues[m-1][i-1], incr_clues[m]))) for i in range(1, 9)], - ## more than 8 clues not allowed (TODO: not really. 2x we can deliberately play unplayable card to discard it) + ## more than 8 clues not allowed, discarding produces a strike Iff(strike[m], And(discard_any[m], Not(play[m]), clues[m-1][8])), # change of strikes *[Iff(strikes[m][i], Or(strikes[m-1][i], And(strikes[m-1][i-1], strike[m]))) for i in range(1, NUM_STRIKES+1)], @@ -128,7 +137,11 @@ valid_move = lambda m: And( # we can only play a card if it matches the progress *[Implies(And(discard[m][i], play[m]), And(Not(progress[m-1][deck[i]]), progress[m-1][deck[i][0], deck[i][1]-1])) for i in range(50)], # change of progress - *[Iff(progress[m][c, k], Or(progress[m-1][c, k], And(play[m], Or(discard[m][i] for i in range(50) if deck[i] == (c, k))))) for c in colors for k in range(1, 6)] + *[Iff(progress[m][c, k], Or(progress[m-1][c, k], And(play[m], Or(discard[m][i] for i in range(50) if deck[i] == (c, k))))) for c in colors for k in range(1, 6)], + # extra round bool + Iff(extraround[m], Or(extraround[m-1], draw[m-1][49])), + # dummy turn bool +# *[Iff(dummyturn[m], dummyturn[m-1]) for i in range(0,1) if m >= 5] ) win = And( @@ -138,7 +151,7 @@ win = And( *[Or(And(discard[m][i], play[m]) for m in range(MOVES) for i in range(50) if deck[i] == (c, k)) for c in colors for k in range(1, 6)] ) -constraints = And(*[valid_move(m) for m in range(MOVES)], draw[MOVES-6][49], win) +constraints = And(*[valid_move(m) for m in range(MOVES)], win) print('{} variables, {} nodes'.format(len(get_atoms(constraints)), get_formula_size(constraints))) model = get_model(constraints)