refactor file: use methods dependent on deck type
This commit is contained in:
parent
8abe9fd257
commit
aae9dec351
1 changed files with 91 additions and 81 deletions
158
sat.py
158
sat.py
|
@ -1,13 +1,15 @@
|
||||||
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.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
|
from pysmt.rewritings import conjunctive_partition
|
||||||
import json
|
import json
|
||||||
MAX_MOVES = 60
|
|
||||||
|
MAX_MOVES = 56
|
||||||
NUM_STRIKES = 3
|
NUM_STRIKES = 3
|
||||||
NUM_PLAYERS = 5
|
NUM_PLAYERS = 5
|
||||||
|
COLORS = 'rygbp'
|
||||||
|
|
||||||
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_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(' ')]
|
|
||||||
|
# instance independent names of variables
|
||||||
|
|
||||||
# clues[m][i] == "after move m we have at least i clues"
|
# 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(MAX_MOVES)}}
|
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(MAX_MOVES)}}
|
||||||
|
@ -26,7 +28,7 @@ draw = {-1: {i: Bool(i == 19) for i in range(19, 50)}, **{m: {19: Bool(False), *
|
||||||
# draw[m][i] == "at move m we play/discard deck[i]"
|
# draw[m][i] == "at move m we play/discard deck[i]"
|
||||||
discard = {m: {i: Symbol('m{}-{}'.format(m, i)) for i in range(50)} for m in range(MAX_MOVES)}
|
discard = {m: {i: Symbol('m{}-{}'.format(m, i)) for i in range(50)} for m in range(MAX_MOVES)}
|
||||||
# progress[m][c, k] == "after move m we have played in color c until k"
|
# progress[m][c, k] == "after move m we have played in color c until k"
|
||||||
progress = {-1: {(c, k): Bool(k == 0) for c in colors for k in range(6)}, **{m: {**{(c, 0): Bool(True) for c in colors}, **{(c, k): Symbol('m{}:{}{}'.format(m, c, k)) for c in colors for k in range(1, 6)}} for m in range(MAX_MOVES)}}
|
progress = {-1: {(c, k): Bool(k == 0) for c in COLORS for k in range(6)}, **{m: {**{(c, 0): Bool(True) for c in COLORS}, **{(c, k): Symbol('m{}:{}{}'.format(m, c, k)) for c in COLORS for k in range(1, 6)}} for m in range(MAX_MOVES)}}
|
||||||
# discard_any[m] == "at move m we play/discard a card"
|
# discard_any[m] == "at move m we play/discard a card"
|
||||||
discard_any = {m: Symbol('m{}d'.format(m)) for m in range(MAX_MOVES)}
|
discard_any = {m: Symbol('m{}d'.format(m)) for m in range(MAX_MOVES)}
|
||||||
# draw_any[m] == "at move m we draw a card"
|
# draw_any[m] == "at move m we draw a card"
|
||||||
|
@ -38,65 +40,11 @@ play5 = {m: Symbol('m{}p5'.format(m)) for m in range(MAX_MOVES)}
|
||||||
# incr_clues[m] == "at move m we obtain a clue"
|
# incr_clues[m] == "at move m we obtain a clue"
|
||||||
incr_clues = {m: Symbol('m{}c+'.format(m)) for m in range(MAX_MOVES)}
|
incr_clues = {m: Symbol('m{}c+'.format(m)) for m in range(MAX_MOVES)}
|
||||||
|
|
||||||
def print_model(model):
|
|
||||||
for m in range(MAX_MOVES):
|
|
||||||
print('=== move {} ==='.format(m))
|
|
||||||
print('clues: ' + ''.join(str(i) for i in range(1, 9) if model.get_py_value(clues[m][i])))
|
|
||||||
print('strikes: ' + ''.join(str(i) for i in range(1, NUM_STRIKES) if model.get_py_value(strikes[m][i])))
|
|
||||||
print('draw: ' + ', '.join('{} [{}{}]'.format(i, deck[i][0], deck[i][1]) for i in range(20, 50) if model.get_py_value(draw[m][i])))
|
|
||||||
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', 'extraround', 'dummyturn']
|
|
||||||
print(', '.join(f for f in flags if model.get_py_value(globals()[f][m])))
|
|
||||||
|
|
||||||
def toJSON(model):
|
def solve(deck_str):
|
||||||
deck_json = [{"suitIndex": colors.index(s), "rank": r} for (s,r) in deck]
|
deck = [(s[0], int(s[1])) for s in deck_str.split(' ')]
|
||||||
players = ["Alice", "Bob", "Cathy", "Donald", "Emily"]
|
|
||||||
hands = [deck[4*p:4*(p+1)] for p in range(0,5)]
|
|
||||||
actions = []
|
|
||||||
for m in range(MAX_MOVES):
|
|
||||||
if model.get_py_value(dummyturn[m]):
|
|
||||||
break
|
|
||||||
if model.get_py_value(discard_any[m]):
|
|
||||||
discarded = next(i for i in range(0,50) if model.get_py_value(discard[m][i]))
|
|
||||||
icard = hands[m % 5].index(deck[discarded])
|
|
||||||
for i in range(icard, 3):
|
|
||||||
hands[m % 5][i] = hands[m % 5][i + 1]
|
|
||||||
if model.get_py_value(draw_any[m]):
|
|
||||||
hands[m % 5][3] = next(deck[i] for i in range(20, 50) if model.get_py_value(draw[m][i]))
|
|
||||||
if model.get_py_value(play[m]) or model.get_py_value(strike[m]):
|
|
||||||
actions.append({
|
|
||||||
"type": 0,
|
|
||||||
"target": discarded
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
actions.append({
|
|
||||||
"type": 1,
|
|
||||||
"target": discarded
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
actions.append({
|
|
||||||
"type": 3,
|
|
||||||
"target": (m + 1) % 5,
|
|
||||||
"value": hands[(m+1) % 5][0][1]
|
|
||||||
})
|
|
||||||
actions.append({
|
|
||||||
"type": 4,
|
|
||||||
"value": 1
|
|
||||||
})
|
|
||||||
game = {
|
|
||||||
"deck": deck_json,
|
|
||||||
"players": players,
|
|
||||||
"actions": actions,
|
|
||||||
"first_player": 0,
|
|
||||||
"options": {
|
|
||||||
"variant": "No Variant",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
print(json.dumps(game))
|
|
||||||
|
|
||||||
valid_move = lambda m: And(
|
valid_move = lambda m: And(
|
||||||
Implies(dummyturn[m], Not(discard_any[m])),
|
Implies(dummyturn[m], Not(discard_any[m])),
|
||||||
# definition of discard_any
|
# definition of discard_any
|
||||||
Iff(discard_any[m], Or(discard[m][i] for i in range(50))),
|
Iff(discard_any[m], Or(discard[m][i] for i in range(50))),
|
||||||
|
@ -139,32 +87,94 @@ valid_move = lambda m: And(
|
||||||
# we can only play a card if it matches the progress
|
# 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)],
|
*[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
|
# 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
|
# extra round bool
|
||||||
Iff(extraround[m], Or(extraround[m-1], draw[m-1][49])),
|
Iff(extraround[m], Or(extraround[m-1], draw[m-1][49])),
|
||||||
# dummy turn bool
|
# dummy turn bool
|
||||||
*[Iff(dummyturn[m], Or(dummyturn[m-1], draw[m-6][49])) for i in range(0,1) if m >= 5]
|
*[Iff(dummyturn[m], Or(dummyturn[m-1], draw[m-6][49])) for i in range(0,1) if m >= 5]
|
||||||
)
|
)
|
||||||
|
|
||||||
win = And(
|
win = And(
|
||||||
# maximum progress at each color
|
# maximum progress at each color
|
||||||
*[progress[MAX_MOVES-1][c, 5] for c in colors],
|
*[progress[MAX_MOVES-1][c, 5] for c in COLORS],
|
||||||
# played every color/value combination (NOTE: redundant)
|
# played every color/value combination (NOTE: redundant)
|
||||||
*[Or(And(discard[m][i], play[m]) for m in range(MAX_MOVES) for i in range(50) if deck[i] == (c, k)) for c in colors for k in range(1, 6)]
|
*[Or(And(discard[m][i], play[m]) for m in range(MAX_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(MAX_MOVES)], win)
|
constraints = And(*[valid_move(m) for m in range(MAX_MOVES)], win)
|
||||||
print('{} variables, {} nodes'.format(len(get_atoms(constraints)), get_formula_size(constraints)))
|
print('{} variables, {} nodes'.format(len(get_atoms(constraints)), get_formula_size(constraints)))
|
||||||
|
|
||||||
model = get_model(constraints)
|
model = get_model(constraints)
|
||||||
if model:
|
if model:
|
||||||
print_model(model)
|
# print_model(model, deck)
|
||||||
toJSON(model)
|
print(toJSON(model, deck))
|
||||||
else:
|
return True
|
||||||
|
else:
|
||||||
print('unsatisfiable')
|
print('unsatisfiable')
|
||||||
|
return False
|
||||||
#conj = list(conjunctive_partition(constraints))
|
#conj = list(conjunctive_partition(constraints))
|
||||||
#print('statements: {}'.format(len(conj)))
|
#print('statements: {}'.format(len(conj)))
|
||||||
#ucore = get_unsat_core(conj)
|
#ucore = get_unsat_core(conj)
|
||||||
#print('unsat core size: {}'.format(len(ucore)))
|
#print('unsat core size: {}'.format(len(ucore)))
|
||||||
#for f in ucore:
|
#for f in ucore:
|
||||||
# print(f.serialize())
|
# print(f.serialize())
|
||||||
|
|
||||||
|
def print_model(model, deck):
|
||||||
|
for m in range(MAX_MOVES):
|
||||||
|
print('=== move {} ==='.format(m))
|
||||||
|
print('clues: ' + ''.join(str(i) for i in range(1, 9) if model.get_py_value(clues[m][i])))
|
||||||
|
print('strikes: ' + ''.join(str(i) for i in range(1, NUM_STRIKES) if model.get_py_value(strikes[m][i])))
|
||||||
|
print('draw: ' + ', '.join('{} [{}{}]'.format(i, deck[i][0], deck[i][1]) for i in range(20, 50) if model.get_py_value(draw[m][i])))
|
||||||
|
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', 'extraround', 'dummyturn']
|
||||||
|
print(', '.join(f for f in flags if model.get_py_value(globals()[f][m])))
|
||||||
|
|
||||||
|
def toJSON(model, deck):
|
||||||
|
deck_json = [{"suitIndex": COLORS.index(s), "rank": r} for (s,r) in deck]
|
||||||
|
players = ["Alice", "Bob", "Cathy", "Donald", "Emily"]
|
||||||
|
hands = [deck[4*p:4*(p+1)] for p in range(0,5)]
|
||||||
|
actions = []
|
||||||
|
for m in range(MAX_MOVES):
|
||||||
|
if model.get_py_value(dummyturn[m]):
|
||||||
|
break
|
||||||
|
if model.get_py_value(discard_any[m]):
|
||||||
|
discarded = next(i for i in range(0,50) if model.get_py_value(discard[m][i]))
|
||||||
|
icard = hands[m % 5].index(deck[discarded])
|
||||||
|
for i in range(icard, 3):
|
||||||
|
hands[m % 5][i] = hands[m % 5][i + 1]
|
||||||
|
if model.get_py_value(draw_any[m]):
|
||||||
|
hands[m % 5][3] = next(deck[i] for i in range(20, 50) if model.get_py_value(draw[m][i]))
|
||||||
|
if model.get_py_value(play[m]) or model.get_py_value(strike[m]):
|
||||||
|
actions.append({
|
||||||
|
"type": 0,
|
||||||
|
"target": discarded
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
actions.append({
|
||||||
|
"type": 1,
|
||||||
|
"target": discarded
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
actions.append({
|
||||||
|
"type": 3,
|
||||||
|
"target": (m + 1) % 5,
|
||||||
|
"value": hands[(m+1) % 5][0][1]
|
||||||
|
})
|
||||||
|
actions.append({
|
||||||
|
"type": 4,
|
||||||
|
"value": 1
|
||||||
|
})
|
||||||
|
game = {
|
||||||
|
"deck": deck_json,
|
||||||
|
"players": players,
|
||||||
|
"actions": actions,
|
||||||
|
"first_player": 0,
|
||||||
|
"options": {
|
||||||
|
"variant": "No Variant",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return json.dumps(game)
|
||||||
|
|
||||||
|
solve(deck_str)
|
||||||
|
|
Loading…
Reference in a new issue