Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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:

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 nn-ary AND operation (∧\wedge) of clauses. Each clause consists of an nn-ary OR operation (∨\vee) of boolean variables or NOT operations (¬\neg) thereon. This is analogous to a product of sums. CNF is easier to understand by seeing examples of valid CNF statements:

The same variable can be repeated multiple times within a given CNF statement.

Statements such as A∧¬AA \wedge \neg A (which is logically impossible/untrue) or A∨¬AA \vee \neg A (which is always true, regardless of the value of AA) are still valid CNF.

A collection of boolean statements in CNF can be combined into one boolean statement using the AND operator (∧\wedge), because all of the statements must be true. The result is also valid CNF:

{A∨¬BA∧C∧¬F}⟺(A∨¬B)⏟1st statement∧A∧C∧¬F⏟2nd statement\left\{ \begin{array}{l} A \vee \neg B \\ A \wedge C \wedge \neg F \end{array} \right\} \quad \Longleftrightarrow \quad \underbrace{(A \vee \neg B)}_\text{1st statement} \wedge \underbrace{A \wedge C \wedge \neg F}_\text{2nd statement}

When formulating the Cluedo SAT problem, we use the XOR operator (⊕\oplus). In the two-operand case, an XOR statement can be converted to CNF as follows:

A⊕B=(A∨B)∧(¬A∨¬B)A \oplus B = (A \vee B) \wedge (\neg A \vee \neg B)

This generalizes to the nn-ary case:

⨁inAi=⋁i = 1nAi∧ ⁣ ⁣ ⁣ ⁣ ⁣⋀i,j∈2-combosof 1…n ⁣ ⁣ ⁣ ⁣ ⁣(¬Ai∨¬Aj)\bigoplus_i^n A_i \quad = \quad \bigvee_{i \, = \, 1}^n A_i \quad \wedge \quad \!\!\!\!\! \bigwedge_{\scriptsize \begin{array}{c} i, j \in \\ \text{2-combos} \\ \text{of} \ 1 \dots n \end{array}} \!\!\!\!\! ( \neg A_i \vee \neg A_j )

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:

⨁ character ∈ characters X charactercase file⨁ weapon ∈ weapons X weaponcase file⨁ room ∈ rooms X roomcase file\begin{aligned} & {\bigoplus\,}_{\text{character} \ \in \ \text{characters}} \, {X\,}_\text{character}^\text{case file} \\ & {\bigoplus\,}_{\text{weapon} \ \in \ \text{weapons}} \, {X\,}_\text{weapon}^\text{case file} \\ & {\bigoplus\,}_{\text{room} \ \in \ \text{rooms}} \, {X\,}_\text{room}^\text{case file} \end{aligned}
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:

(⨁ player ∈ players X rumor cardplayer)⊕X rumor cardrumor card⊕X rumor cardextra cards\left( {\bigoplus\,}_{\text{player} \ \in \ \text{players}} \, {X\,}_\text{rumor card}^\text{player} \right) \oplus {X\,}_\text{rumor card}^\text{rumor card} \oplus {X\,}_\text{rumor card}^\text{extra cards}
statements.append(
    Xor(
        [
            CardIsInLocation(rumor_card, loc)
            for loc in [*player_indices, CASE_FILE, EXTRA_CARDS]
        ],
    )
)

Thirdly, each player has exactly the kk cards assigned to their hand at the beginning of the game, out of n=24n = 24 total rumor cards:

For each player:

k of x iplayer must be true, the remaining n−k must be false\text{$k$ of ${x\,}_i^\text{player}$ must be true, the remaining $n - k$ must be false}

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 kk must be true, rather than ≤k\leq k or ≥k\geq k. Consider the simplified case of k=2k = 2 out of n=3n = 3 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:

(x 1player∧x 2player∧¬ x 3player) ∨(x 1player∧¬ x 2player∧x 3player) ∨(¬ x 1player∧x 2player∧x 3player)\begin{aligned} & ({x\,}_1^\text{player} \wedge {x\,}_2^\text{player} \wedge \neg \, {x\,}_3^\text{player}) \, \vee \\ & ({x\,}_1^\text{player} \wedge \neg \, {x\,}_2^\text{player} \wedge {x\,}_3^\text{player}) \, \vee \\ & (\neg \, {x\,}_1^\text{player} \wedge {x\,}_2^\text{player} \wedge {x\,}_3^\text{player}) \end{aligned}

Note that this is in sum-of-products form, also known as disjunctive normal form (DNF). The outer sum consists of n choose kn \ \text{choose} \ k products, in kk variables. In a real Cluedo game, the highest possible value of n choose kn \ \text{choose} \ k is 24 choose 10=1, ⁣961, ⁣25624 \ \text{choose} \ 10 = 1,\!961,\!256, which would occur in a two-player game in which each player receives k=floor((24−3)/2)=10k = \mathrm{floor}((24 - 3) / 2) = 10 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 2n=16, ⁣777, ⁣2162^n = 16,\!777,\!216 rows. In particular, the DNF statement above converts to a CNF statement with 2n−(n choose k)2^n - (n \ \text{choose} \ k) clauses. To avoid the number of clauses increasing exponentially with nn, 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 use CardEnc.equals to 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:

X rumor cardplayer{X\,}_\text{rumor card}^\text{player}
statements.append(
    CardIsInLocation(rumor_card, player.agent_index)
)

Otherwise:

¬X rumor cardplayer\neg {X\,}_\text{rumor card}^\text{player}
statements.append(
    Not(CardIsInLocation(rumor_card, player.agent_index))
)

Player-Specific Knowledge Based on Gameplay

In each gameplay turn ii, 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 ii:

For each other player who reveals a rumor card in turn ii’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:

X rumor cardother player who revealed card{X\,}_\text{rumor card}^\text{other player who revealed 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:

⋁rumor card ∈ guess[i]X rumor cardother player who revealed card\bigvee_{\text{rumor card} \ \in \ \text{guess}[i]} {X\,}_\text{rumor card}^\text{other player who revealed card}
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 ii:

¬X rumor cardplayer who did not reveal card\neg {X\,}_\text{rumor card}^\text{player who did not reveal card}
statements.append(
    Not(
        CardIsInLocation(
            rumor_card, card_reveal.other_player_index,
        )
    )
)

PySAT

For input to PySAT, CNF statements such as (A1∨¬A2)∧(¬A1∨A3)(A_1 \lor \neg A_2) \wedge (\neg A_1 \lor A_3) are written as:

[[1, -2], [-1, 3]]