Tagged with puzzles

Pickleball revisited

Who would have thought the world of Pickleball is so interesting it deserves two blog posts? The last time we looked at pickleball it was to determine the optimal mixed double tournament structure to maximize fairness. This time, we're going to solve for the match pairings without the gender distinction, but still as a doubles tournament. Also, we want to solve the matches so that we maximize court usage throughout the tournament. (Okay, this doesn't relate specifically to Pickleball, but whatever.)

The Problem

Put another way, how to optimally choose matchups for a doubles tournament of n people, where each person plays n-1 matches, to optimize pairing so that each person is only paired with another once, and plays against another person at most twice. Also want to solve as a tournament so we can maximize court usage (e.g., if there are 8 players, at each round we should be using two courts.)

Again, we're trying to solve this in the ballpark of n=8.


This is still an exact cover problem, but with different constraints as last time. We could still use the grid style structure, but it becomes awkward when trying to implement the court usage constraint.

Let's first generalize our exact cover solver, then restructure our "board state" data structure to implement the above contraints.

Exact Cover Problems

Last time, the heart of our constraint-propogating depth-first over-hyphen-using exact cover solver was this function:

def chooser(g, pm, n):
    if unable_to_solve(g):
        return None
    if is_solved(g, pm):
        return g
    iplace = canplace(g, pm)
    if iplace is not None:
        return chooser(*place(iplace, g[iplace], g, pm, n))
    # Have to make a choice
    ichoice = findfirst(g, lambda x: len(x) > 2)
    for v in aschoices(g[ichoice]):
        gcopy = chooser(*place(ichoice, v, deepcopy(g), deepcopy(pm), n))
        if gcopy:
            return gcopy
    return None

The arguments are the "board state", representing what choices we've made so far and what choices remain. We first check to see if the current board state is in an end condition - either unsolvable or solved. If it's neither of those, we check if there are any automatic choices to be made - choices with only one option. This is the second half of constraint propogation. If there is only one choice to make, choose that choice in the board state (which should internally enforce some constraints on the other choices), then continue.

If there aren't any freebie choices to make, we need to continue with the depth first search. Here is where we:

  1. Save the board state
  2. Try the choice and recurse
  3. If that choice didn't result in a solution, undo the choice, and try the next one

This is a similar structure to most depth first solvers. The tricky part is the undo step - because internally there could be lots of changes resulting from the constraint propogation, it's often easier to just store a copy of the board state before making the choice, then revert back to that copy if that branch didn't work out. This can be slow, however, since you are always copying chunks of memory. Also, to make sure you take advantage of constraint propogation, you need to make sure it is not taking the same code path as that copy.

Other approaches remove the need for saving the board state by allowing an undo choice operation, or "backtracking". This can result in large speed increases by reducing the memory footprint. The best example of this is Knuth's Dancing Links (DLX) technique for solving exact cover problems. Maybe we'll do a pickleball solver with DLX some other time.

So, how do we generalize this thing? It's pretty close already - we just need to implement the interface functions

  • unable_to_solve - is the board state unsolvable?
  • is_solved - is the board state solved?
  • canplace - is there an automatic choice to make?
  • findfirst - find the first position that has multiple choices
  • place - make a choice in a certain position

That sounds like a class. So, rewriting assuming a BoardState interface:

def chooser(board):
    if board.unable_to_solve():
        return None
    if board.is_solved():
        return board
    constrained_pos = board.canplace()
    if constrained_pos is not None:
        return chooser(board.place(constrained_pos, board.choices(constrained_pos)[0]))
    # Have to make a choice
    pos = board.first_position_with_choices()
    for choice in board.choices(pos):
        boardcopy = chooser(deepcopy(board).place(pos, choice))
        if boardcopy is not None:
            return boardcopy
    return None

Again, this assumes we are using the "copy" method of backtracking, instead of an explicit undo operation. Also, the "place" method contains the clever constraint propogation parts. This seems okay, since if we had less clever constraint propogation, we'd just do more depth first searching, without any changes to this code.

Pickleball Specifics

Now let's write the pickle ball board state class. As I mentioned before, the grid-style representation last time doesn't work as well when you only have players in a single category, as opposed to split into two distinctly competing groups (gender), and you also want to solve for the actual court usage during the tournament.

After thinking this over for a while, let's try representing our board state as an actual tournament bracket, like so:

Court 1    Court 2
+----+     +----+
|    |     |    |
|----|     |----|     Match 1
|    |     |    |
+----+     +----+

+----+     +----+
|    |     |    |
|----|     |----|     Match 2
|    |     |    |
+----+     +----+

+----+     +----+
|    |     |    |
|----|     |----|     Match 3
|    |     |    |
+----+     +----+


With n=8 players, we'll need 8/4 = 2 courts and 8-1 = 7 matches for the tournament.

The goal will be to fill in each court in each match to meet all of our constraints. Namely:

  • Each player is paired on the same team with every other player exactly once
  • Each player will never play against another player more than twice

So, our finished solution will look something like:

Court 1    Court 2
+----+     +----+
| AB |     | EF |
|----|     |----|     Match 1
| CD |     | GH |
+----+     +----+

+----+     +----+
| AC |     | FG |
|----|     |----|     Match 2
| DE |     | BH |
+----+     +----+


meaning that in Match 1, player A and player B play against player C and player D, and player E and player F play against player G and player H, and so on.

Note that we are not restricting the court and the position on the court (for now). So, for this solution, we are just trying to get the pairing for each match - which court and position each pair ends up on doesn't matter (can easily be switched at game time). Put another way, we are assuming that each position on each court are exactly the same (which is a pretty big assumption if you know that Court 1 is nearer to the door, so it's really drafty, but we have to start somewhere.)

So, without loss of generality, we can assign one player to the same position in each court for each match on initialization, and cycle through all partners:

Court 1    Court 2
+----+     +----+
| AB |     |    |
|----|     |----|     Match 1
|    |     |    |
+----+     +----+

+----+     +----+
| AC |     |    |
|----|     |----|     Match 2
|    |     |    |
+----+     +----+

+----+     +----+
| AD |     |    |
|----|     |----|     Match 3
|    |     |    |
+----+     +----+


How are we going to represent this in code? Let's try representing the tournament as a list of matches, and each match as a list of pairs, where pairs[0] plays pairs[1] on Court 1 and pairs[2] plays pairs[3] on Court 2. Actually, each pairs entry will be a list of remaining pairs that can go in that position. Something like:

matches = [
    # Match 1
    [['AB'], ['BC', 'BD', ...],              # Choices for court 1
     ['EF', 'EH', ...], ['FG', 'FH', ...]],  # Choices for court 2
    # Match 2
    [['AC'], ['BD', 'BE', ...],              # Choices for court 1
     ['EF', 'EH', ...], ['FG', 'FH', ...]],  # Choices for court 2


Now we're getting somewhere. So, the board state class will look something like:


class PickleBallBoardState(object):
    def __init__(self, n):
        allpairs = []
        for i in xrange(n):
            for j in xrange(i+1, n):
                allpairs.append(PLAYER_IDS[i] + PLAYER_IDS[j])
        print allpairs

        n_courts = n / 4
        positions_per_court = 2
        n_matches = n - 1
        self.matches = []
        for i_match in xrange(n_matches):
            match = []
            for i_position in xrange(n_courts * positions_per_court):

I've found that a "is_placed" data structure is helpful when doing these problems, to easily distinguish the case where a position has a single choice available, but hasn't yet had the constraints propogated. So, let's add a placed structure of boolean values with a similar match/position structure:

        self.placed = []
        for i_match in xrange(n_matches):
            self.placed = [False] * n_courts * positions_per_court

Finally, let's fill in those initial choices that prevent certain types of duplicate solutions. Meaning, AB will always be in match 1, AC in match 2, AD in match 3, and so on. We'll use the "place" function which will start removing constrained choices.

        for i_match in xrange(n_matches):
            self.place((i_match, 0), PLAYER_IDS[0] + PLAYER_IDS[i_match + 1])

Now for some of the good stuff. Let's do some easy functions first, then come back to the constraint enforcing place function.

    def unable_to_solve(self):
        return any(not choices for choices in iflatten2d(self.matches))

    def is_solved(self):
        return all(len(choices) == 1 for choices in iflatten2d(self.matches)) \
               and all(iflatten2d(self.placed))

Okay, I can't resist a little premature pythoptimization. I've used a little flatten utility that returns an iterator so we don't have to double loop through matches then positions. For the following functions, we'll actually need to do the double loop, though:

    def canplace(g):
        for i_match, match in enumerate(self.matches):
            for i_pos, choices in enumerate(match):
                if len(choices) == 1 and not self.placed[i_match][i_pos]:
                    return (i_match, i_pos)
        return None

    def first_position_with_choices(self):
        for i_match, match in enumerate(self.matches):
            for i_pos, choices in enumerate(match):
                if len(choices) > 1:
                    assert not self.placed[i_match][i_pos]
                    return (i_match, i_pos)
        return None

   def choices(self, location):
       i_match, i_pos = location
       return self.matches[i_match][i_pos]

Now it's just the place function. It should consist of setting the fixed choice for that position, then removing all the other choices due to the constraints. It gets a little complicated, though, since one of the constraints depends on the number of times one player has played another. So, lets add that as a member variable and only update it when place is called

    def _filter_out_players(self, i_match, i_pos, players):
        if not self.placed[i_match][i_pos]:
            self.matches[i_match][i_pos] = filter(
                lambda c: (c[0] not in players) and (c[1] not in players),

    def _blocked_players(self, players):
        blocked_set = set()
        for player in players:
            blocked_set.update(p for p, count in self.play_counts[player].iteritems() if count >= 2)
        return blocked_set

    def place(self, location, choice):
        placed_match, placed_pos = location
        assert not self.placed[placed_match][placed_pos]

        # First check if we are completing a versus match
        opposing_pos = placed_pos + 1 if placed_pos % 2 == 0 else placed_pos - 1
        completing_versus = self.placed[placed_match][opposing_pos]

        # Place the choice
        self.matches[placed_match][placed_pos] = [choice]
        self.placed[placed_match][placed_pos] = True

        # Remove all other instances of this pairing
        for i_match, match in enumerate(self.matches):
            for i_pos, choices in enumerate(match):
                if not self.placed[i_match][i_pos]:
                    except ValueError:

        # Remove all choices containing these players from this match
        for i_pos in xrange(len(self.matches[placed_match])):
            self._filter_out_players(placed_match, i_pos, choice)

        # Make sure person only plays against another player maximum twice
        if completing_versus:
            self._update_play_counts(placed_match, placed_pos)
            for i_match, match in enumerate(self.matches):
                for i_pos, choices in enumerate(match):
                    opposing_pos = i_pos + 1 if i_pos % 2 == 0 else i_pos - 1
                    if self.placed[i_match][opposing_pos] and not self.placed[i_match][i_pos]:
                            i_match, i_pos,

        return self

That's pretty much it. A quick little pretty printer and solver function and we're done.

    def solnstr(self):
        lines = []
        for i_match, match in enumerate(self.matches):
            lines.append(('Match %d: ' % (i_match + 1)) + \
                         ', '.join(match[ipos][0] + ' vs ' + match[ipos+1][0]
                                   for ipos in xrange(0, len(match), 2)))
        return '\n'.join(lines)

def solve(n):
    board = PickleBallBoardState(n)
    return chooser(board)



print solve(8).solnstr()


Match 1: AB vs CD, EF vs GH
Match 2: AC vs EG, BD vs FH
Match 3: AD vs EH, BC vs FG
Match 4: AE vs BF, CG vs DH
Match 5: AF vs CH, BE vs DG
Match 6: AG vs DF, BH vs CE
Match 7: AH vs BG, CF vs DE

Great! It worked. All pairings are represented and no player plays another one more than twice.

Trying with n=12 looks to be too slow to finish quickly, and require some more cleverness. Maybe have to do that in a future post. Some quick tests imply that the deepcopy operation is pretty slow, especially with the overly-clever nested datastructures.

You can get the full source here.

Tagged , , ,

The Pickleball Problem

Here's an interesting puzzle that my Dad ran into when trying to organize a mixed doubles Pickleball tournament.

The Problem

Given a group of n women and n men playing in a mixed doubles tournament (each match is always a woman/man team versus another woman/man team), how do you structure the matches so that you find the "best" woman and the "best" man? In other words, if the men are only competing against the men for prizes, and the women only competing against the women, so how do you maximize fairness for each of the matches - not pairing up a man and woman more than once in the tournament, and not playing against another person more than once in the tournament?

We're trying to solve this in the ballpark of n = 8.

How to solve

The interesting observation is that this can be structured as an exact cover problem. If you write out the matches as a grid, where the women are the columns (1, 2, 3...) and the men are the rows (A, B, C...), each grid entry becomes a tournament match of who that team will play. For n=4:

    1  2  3  4
A | XX .. .. ..
B | .. XX .. ..
C | .. .. XX ..
D | .. .. .. XX

The problem is to fill in the empty values in the grid with opposing teams (e.g., C3) such that all of the matches are fair. The diagonal entries are X'd out because a team will not play itself. Further, the entries need to be reflexive - if the entry for row C, column 2 is B4, then the entry for row B column 4 should be C2.

So, for the n=4 problem, the goal is to fill in the values A2, A3, A4, B1, B3, B4, C1, C2, C4, D1, D2, D3 such that each row and column only has one 'A' value, one 'B' value, one 'C' value and one 'D' value, (excepting the value of the row that you are on - row A will not have any 'A' values) and only one '1' value, one '2' value, and so on. Once you do that, you can read off all of the tournament matches for each woman (by reading the columns) or by each man (by reading the rows).

This is similar to solving sudoku, since it is also an exact cover problem. Since I already had a sudoku solver (a brute force with constraint propogation solver, inspired by Peter Norvig's python sudoku solver), I adapted it to solve this problem.

You can see the code here.


Here are solutions up to n=9. Any more would require additional cleverness, or days of computation time.


    1  2  3  4
A | XX B3 C4 D2
B | D3 XX A2 C1
C | B4 D1 XX A3
D | C2 A4 B1 XX


    1  2  3  4  5 
  + --------------
A | XX B3 C4 D5 E2
B | C5 XX A2 E1 D3
C | D2 E4 XX A3 B1
D | E3 C1 B5 XX A4
E | B4 A5 D1 C2 XX


    1  2  3  4  5  6 
  + -----------------
A | XX B3 C4 D5 E6 F2
B | E4 XX A2 C6 F3 D1
C | F5 E1 XX A3 D2 B4
D | B6 C5 F1 XX A4 E3
E | C2 F4 D6 B1 XX A5
F | D3 A6 B5 E2 C1 XX


    1  2  3  4  5  6  7 
  + --------------------
A | XX B1 C2 D5 E6 G3 F4
B | A2 XX F5 G1 C7 E4 D3
C | G6 A3 XX E2 F1 D7 B5
D | E3 G5 B7 XX A4 F2 C6
E | F7 C4 D1 B6 XX A5 G2
F | C5 D6 G4 A7 B3 XX E1
G | B4 E7 A6 F3 D2 C1 XX


    1  2  3  4  5  6  7  8 
  + -----------------------
A | XX B1 C2 D3 E4 F5 G6 H7
B | A2 XX D6 E7 F8 H4 C5 G3
C | E6 A3 XX G1 B7 D8 H2 F4
D | H5 E8 A4 XX G2 B3 F1 C6
E | G8 H6 F7 A5 XX C1 B4 D2
F | D7 G4 H1 C8 A6 XX E3 B5
G | C4 D5 B8 F2 H3 A7 XX E1
H | F3 C7 G5 B6 D1 E2 A8 XX


    1  2  3  4  5  6  7  8  9
  + --------------------------
A | XX B1 C2 D3 E4 F5 G6 H7 I8
B | A2 XX D1 E8 F7 H9 I3 C6 G4
C | G9 A3 XX I6 H2 B8 F4 E1 D5
D | B3 F8 A4 XX C9 I2 H1 G5 E7
E | C8 I7 H6 A5 XX G1 D9 B4 F2
F | I4 E9 G8 C7 A6 XX B5 D2 H3
G | E6 H4 I5 B9 D8 A7 XX F3 C1
H | D7 C5 F9 G2 I1 E3 A8 XX B6
I | H5 D6 B7 F1 G3 C4 E2 A9 XX
Tagged , , ,