Cluedo SAT Problem
What does Cluedo have to do with digital circuit design? Boolean logic.
Cluedo can be solved as a boolean satisfiability (SAT) problem. A SAT problem is a special case of constraint problem, consisting of boolean variables rather than integer or real variables. A SAT problem consists of a collection of boolean equations in terms of a collection of boolean variables to solve for. Much like a system of linear equations, there can be zero, one, or multiple solutions depending on the number of variables and equations and how independent they are.
Here we formulate the SAT problem for Cluedo. python-cluedo uses the PySAT solver to find one or more solutions to the SAT problem. Python EDA, a library for electronic design automation, offers similar functionality.
First, the SAT problem is defined in terms of variables specifying the location where each rumor card exists – in a player’s hand, in the “case file”, or among the extra cards. The following notation is used:
is a boolean variable (also known as a literal in the SAT context) indicating whether a given rumor card is in a given location (a player’s hand, or the case file, or the extra cards). In the code, this is represented by
CardIsInLocation. In a non-boolean version of the problem, could be replaced by probability , which converges to 0 or 1.is the NOT operator. In the code, this is represented by
Not.is the OR operator, whose output is truthy if any of its inputs are truthy. In the code, this is represented by
Or.is the XOR operator, whose output is truthy if and only if exactly one of its inputs are truthy (they are mutually exclusive). In the code, this is represented by
Xor.
Conjunctive Normal Form¶
Conjunctive Normal Form (CNF) is a standard way of expressing one or more boolean statements. By expressing a SAT problem in CNF, it can be solved by many SAT solvers which accept problems formulated in CNF.
A boolean statement is in CNF if it consists of an -ary AND operation () of clauses. Each clause consists of an -ary OR operation () of boolean variables or NOT operations () thereon. This is analogous to a product of sums. CNF is easier to understand by seeing examples of valid CNF statements:
(valid even with no outer s)
(valid even with no inner s)
The same variable can be repeated multiple times within a given CNF statement.
Statements such as (which is logically impossible/untrue) or (which is always true, regardless of the value of ) are still valid CNF.
A collection of boolean statements in CNF can be combined into one boolean statement using the AND operator (), because all of the statements must be true. The result is also valid CNF:
When formulating the Cluedo SAT problem, we use the XOR operator (). In the two-operand case, an XOR statement can be converted to CNF as follows:
This generalizes to the -ary case:
General Knowledge of the Game¶
This section lists the SAT statements based on the fundamental setup of Cluedo rather than any specific game of Cluedo.
Firstly, the case file contains exactly one character, weapon, and room:
for rumor_cards in (CHARACTERS, WEAPONS, ROOMS): statements.append( Xor( [ CardIsInLocation(rumor_card, CASE_FILE) for rumor_card in rumor_cards ] ) )
Secondly, each rumor card is located in only one place – in one player’s hand, in the case file, or among the extra cards:
For each rumor card:
statements.append( Xor( [ CardIsInLocation(rumor_card, loc) for loc in [*player_indices, CASE_FILE, EXTRA_CARDS] ], ) )
Thirdly, each player has exactly the cards assigned to their hand at the beginning of the game, out of total rumor cards:
For each player:
In SAT problems, this is known as a cardinality constraint. In particular, among the possible cardinality constraints, the above is an equality constraint because it states that exactly must be true, rather than or . Consider the simplified case of out of total cards, as an example. This would be represented by the statement, “the player has only cards 1 and 2, or the player has only cards 1 and 3, or the player has only cards 2 and 3”. This is represented by the following SAT statement:
Note that this is in sum-of-products form, also known as disjunctive normal form (DNF). The outer sum consists of products, in variables. In a real Cluedo game, the highest possible value of is , which would occur in a two-player game in which each player receives cards.
In order to use a SAT solver, the SAT statement must be converted from DNF to CNF, or generated in CNF to begin with. DNF can be converted to CNF using boolean algebra or via brute force using a truth table of rows. In particular, the DNF statement above converts to a CNF statement with clauses. To avoid the number of clauses increasing exponentially with , more efficient approaches such as the Tseitin encoding AKA Tseitin transformation were developed. They introduce a linear rather than exponential number of additional clauses, but also add a linear number of auxiliary variables.
PySAT’s cardinality encoding,
CardEnc, uses these more efficient approaches to represent cardinality constraints in CNF without such large numbers of clauses. In particular, we useCardEnc.equalsto encode our equality constraint. Thanks to Axel Kemper for this tip.
Player-Specific Knowledge¶
This section lists the SAT equations specific to a given player, whether based on how the game was set up or – most importantly – based on observations during gameplay.
Player-Specific Knowledge Based on Game Setup¶
You have your own rumor cards and only those rumor cards:
For each rumor card:
If you have the rumor card:
statements.append( CardIsInLocation(rumor_card, player.agent_index) )Otherwise:
statements.append( Not(CardIsInLocation(rumor_card, player.agent_index)) )
Player-Specific Knowledge Based on Gameplay¶
In each gameplay turn , a player makes a guess – a (character, weapon, room) triplet – about what the crime is. Up to two other players reveal one rumor card each from their hand if matching one of the cards in the guess. This always gives you at least some information, regardless of whether any cards are revealed or you are the player to whom they are revealed:
For each turn :
For each other player who reveals a rumor card in turn ’s guess:
If you are the player making the guess, i.e., the rumor card was revealed to you, then you know the other player has that card:
statements.append( CardIsInLocation( card_reveal.rumor_card, card_reveal.other_player_index, ) )Otherwise, you only know that the other player has one of the cards in the guess:
statements.append( EventsUnion( [ CardIsInLocation( rumor_card, card_reveal.other_player_index, ) for rumor_card in game_log_entry.guess ] ) )For each opportunity in which a rumor card could have been revealed by a player but was not, you know that other player does not have any of the cards in the guess:
For rumor card in guess :
statements.append( Not( CardIsInLocation( rumor_card, card_reveal.other_player_index, ) ) )
PySAT¶
For input to PySAT, CNF statements such as are written as:
[[1, -2], [-1, 3]]