Tuesday, June 18, 2013

How to Play Sprouts with Playing Cards

It's not much fun to play sprouts with playing cards, but it is possible. Why would anyone want to do such a thing? Maybe it will be easier to analyze the game or more straightforward to implement it on a computer this way. Maybe you're fresh out of pens and paper.

To play a game with N initial spots, you'll need three identical decks of playing cards with at least 3N+1 distinct cards in each deck. This way each card will have exactly two identical "twins". (Actually, the cards are incidental; integers will suffice. This document is really about how to generate legal moves and update a position represented in Dan Hoey's boundary-list notation.) In the rules below, we'll assume you've combined the three decks into one large deck. You will also need a flat space for at least 2N+1 rows of cards. A large wooden table will do nicely. Each row will contain zero or more stacks of cards, and each stack will contain one or more cards.

To "cut" a stack of cards means the usual thing: remove one or more cards from the top of the stack (preserving order) and put them under the remaining cards in the stack. Here, the player may examine the stack and cut it in any place they choose.

To "play" a card, remove it from the deck and place it in the specified location.

Initial position and play

The initial position consists of N different cards, each in an isolated stack, all in the same row. Players take turns moving. There are two kinds of moves, joining moves and dividing moves. On his turn, a player must execute either a legal joining move or a legal dividing move, but not both in the same turn. Under the normal play convention, the last player wins. Under the misere play convention, the last player loses.

Joining moves

A joining move combines two stacks in the same row. If any step of the move is not possible, the move is illegal.
  1. Choose two different nonempty stacks A and B in the same row. Optionally, cut stack A. Optionally, cut stack B.
  2. If A has more than one card, then play a twin of the top card to the bottom of the stack.
  3. If B has more than one card, then play a twin of the top card to the bottom of the stack.
  4. From the deck, remove two identical cards that are not already on the table. Place one on top of A, and one on top of B.
  5. Place A on top of B.

Dividing moves

A dividing move divides a stack into two stacks, one of which is placed in a new row. One or more stacks from the original row may be moved to the new row. If any step of the move is not possible, the move is illegal.
  1. Choose a nonempty stack. Optionally, cut it.
  2. Play a twin of the top card to the bottom of the stack.
  3. Remove one or more cards -- but not all of them -- from the top of the stack and place them in a new row.
  4. If the original stack or the new stack has more than one card, then play a twin of the the bottom card of the new stack to the top of the original stack.
  5. From the deck, remove two identical cards that are not already on the table. Place one on top of the new stack, and one on top of the original stack.
  6. Optionally, move one or more stacks -- other than the original stack — from the original row to the new row.

Example game

Consider the following game of 5 moves (in WGOSA notation): 2+ 1(3)2 1(4)2 1(5)4 2(6)3 (see images below)

Here's how that game would be played with cards. To represent the position in text, we will use the following conventions: A stack of cards is written in order from top card to bottom, with a comma between each card. A semicolon separates each stack from adjacent stacks in the same row. A slash separates each row from its neighbors. (This is the notation used by Dan Hoey in his paper on sprouts notation.)

(2)initial position:
  • 1;2
(3)joining move:
  1. 1;2
  2. (skipped)
  3. (skipped)
  4. 1,3;2,3
  5. 1,3,2,3
(4)dividing move:
  1. 1,3,2,3
  2. 1,3,2,3,1
  3. 1,3,2/3,1
  4. 1,3,2/2,3,1
  5. 4,1,3,2/4,2,3,1
  6. (skipped)
(5)dividing move:
  1. 1,3,2,4/4,2,3,1
  2. 1,3,2,4,1/4,2,3,1
  3. 1,3,2,4/1/4,2,3,1
  4. 1,3,2,4/4,1/4,2,3,1
  5. 5,1,3,2,4/5,4,1/4,2,3,1
  6. (skipped)
(6)dividing move:
  1. 5,1,3,2,4/5,4,1/2,3,1,4
  2. 5,1,3,2,4/5,4,1/2,3,1,4,2
  3. 5,1,3,2,4/5,4,1/2,3/1,4,2
  4. 5,1,3,2,4/5,4,1/2,3/3,1,4,2
  5. 5,1,3,2,4/5,4,1/6,2,3/6,3,1,4,2
  6. (skipped)

Analogy with graph

We have represented a plane graph using rows of stacks of cards.
Playing card termGraph theory term
cardoccurrence of vertex in boundary
stackleft-hand walk of vertices on boundary
rowface, i.e. set of boundaries
tableplane graph, i.e. set of faces

Credits

Author: Josh Jordan
Initially published at http://www.wgosa.org/playingcardssprouts.htm

This document builds upon the sprouts notation system devised by Dan Hoey.

Tuesday, April 23, 2013

John Graham-Cunning's Minimum Coin Problems

In The Minimum Coin Problem, John Graham-Cunning poses the following problems:
  1. Given a pile of coins and a target amount, find a way to pay exactly that amount using the maximum number of coins from the pile.
  2. Given a pile of coins and a target amount, find a way to pay at least that amount using the maximum number of coins from the pile, such that removing any coin from the payment yields an amount that is less than the target amount.
  3. Given a pile of coins — each type of coin having a given weight — and a target amount, find a way to pay at least that amount using the maximum weight of coins from the pile, such that removing any coin from the payment yields an amount that is less than the target amount.
These can each be posed as integer programming problems and solved with a solver such as GLPK. Here is a solution to problem 1, a solution to problem 2, and a solution to problem 3.

Tuesday, September 20, 2011

Open-source answer set solver vs Fico Xpress Optimization Suite

Fico's Xpress Optimization Suite is used by American Airlines, Avis, Honeywell, and the NFL to solve industrial-sized problems in operations research. I was reading one of Fico's white papers, Hybrid MIP/CP solving with Xpress-Optimizer and Xpress-Kalis (May, 2009), when I came across the following statement about a problem in machine assignment and sequencing.
"It is possible to implement this problem entirely either with Xpress-Optimizer or wit Xpress-Kalis. However, already for this three machines – 12 jobs instance the problem is extremely hard for either technique on its own." [Section 3.4 ("Results"), page 19, italics mine]
Fico approached this dilemma by creating a hybrid of the solutions for both solvers. The code looked complicated, though, and the problem itself didn't seem that hard. I wanted to see if I was missing something, so I wrote it up in the language of the open-source answer-set solver Clingo.
According to the paper, "Optimality is proven within a few seconds on a Pentium IV PC." It takes clingo 3.5 seconds on an Athlon 64 (a similar CPU) to find the same solution. The Xpress-Optimizer+Xpress-Kalis hybrid solution is about 220 lines of code (LOC), while the Clingo solution is about 20 LOC. (*)

According to Section 4 (Summary), "Hybrid solution algorithms need to be developed, implemented, and tested on a case-by-case basis, meaning a considerable investment in terms of development effort and requiring a good understanding of the solution methods and solvers involved."

Indeed. Or you could just use Clingo.


(*)  When counting LOC, I ignored lines that consisted only of
  • comments,
  • code for displaying output,
  • code for specifying the problem parameters (there were none of these in the Xpress solution), or
  • code for configuring the solver (there were none of these in the clingo solution).




Wednesday, March 2, 2011

How I Beat the World Champion at Sprouts

With the help of my program Aunt Beast, I won the World Game of Sprouts Association 2010-2011 Tournament in January, 2011. The human world champion requested a follow-up match, billed by WGOSA as the “2011 Clash of the Titans” . As of March, 2011, the match is still ongoing, but in mid-February, I won the first game. Here’s how I did it.

Update I: I also won game 2. Here's my analysis.
Update II: I lost game 3, but won game 4. Analysis forthcoming.
Update III: I won game 5 and lost game 6, so I won the match. WGOSA has a write-up.

Tuesday, May 4, 2010

FlowerFinder by William Waite

FlowerFinder is a puzzle with only 6 pieces but a rather large state space. One objective is to place them into the frame in two layers so that in each of the 9 circles, exactly 3 petals are visible.

Each circle can be one of four types, which we will call "top-left", "top-right", "bottom-left", and "bottom-right". We will assign each piece a number from 1 to 6. Each piece is defined as a sequence of three circle types. For example, in the photo, the circles in piece 1, from top to bottom, are of type down-left, up-right, and up-left. Before a piece is placed in the frame, it can (optionally) be flipped horizontally and (optionally) be flipped vertically. We define locations 1,2,3 as the bottom of the frame from left to right, and locations 4,5,6 as the locations in the top of the frame, perpendicular to 1,2,3. Without loss of generality, we can assume that piece 1 is in location 1 or 2, and is not flipped horizontally or vertically. Finally, we require that the petals in each circle in the top row align with the petals in the corresponding circle in the bottom row.

$ clingo 0 flowerfinder.lp

Answer: 1
fliph(3) fliph(5) fliph(6) flipv(2) flipv(3) pieceloc(1,1) pieceloc(2,2) pieceloc(3,3) pieceloc(4,6) pieceloc(5,4) pieceloc(6,5)
SATISFIABLE

Models      : 1
Time        : 0.020
  Prepare   : 0.010
  Prepro.   : 0.000
  Solving   : 0.010

So the unique solution is to horizontally flip pieces 3, 5, and 6, vertically flip 2 and 3, put the pieces in the specified locations.

How large is the state space? The first piece is in one of two locations, and there are 5! ways to place the remaining pieces, 25 ways to represent the "horizontally flipped" state of those pieces, and 25 ways to represent the "vertically flipped" state of each of those pieces. Finally, the top layer can either be perpendicular to the bottom layer or not. By this reasoning, there are 5! · 25 · 25 · 2 = 257760 different ways to place the 6 pieces in the frame.

Wednesday, April 21, 2010

Pasquale's Problem

Pasquale is a self-employed machinist who sells two products: P and Q. The market will accept up to 100 Ps and 100 Qs per week. P sells for $200 and Q sells for $195. The raw materials for each part cost $50. It takes him 18.5 minutes to build a P and 16.5 minutes to build a Q. Pasquale's supplier also sells partially-assembled raw materials for P and Q. The partially-assembled raw materials save him 3 minutes per P and 3.5 minutes per Q, but they also cost more: an additional $25 per P and $29 per Q. Given that Pasquale works at most 40 hours per week, what is Pasquale's maximum weekly profit? For simplicity, assume that the quantity of Ps and Qs he produces each week can be specified by a rational number.

Calculators are allowed, but solving this without a computer is quite a challenge.

Here's a hint (rot13): Ubj zhpu jbhyq Cnfdhnyr or noyr gb znxr vs ur qvqa'g unir gur bcgvba bs hfvat cnegvnyyl-nffrzoyrq enj zngrevnyf?

Click here to view the solution

This puzzle was inspired by a paper by Balakrishnan [1].

[1] Balakrishnan, Jaydeep. The theory of constraints and the make-or-buy decision. Journal of Supply Chain Management, January 1, 2005 .

Wednesday, April 14, 2010

Nonograms / Paint by Numbers / Griddlers

Nonograms / Paint by Numbers / Griddlers are "picture logic puzzles in which cells in a grid have to be colored or left blank according to numbers given at the side of the grid to reveal a hidden picture. In this puzzle type, the numbers measure how many unbroken lines of filled-in squares there are in any given row or column. For example, a clue of "4 8 3" would mean there are sets of four, eight, and three filled squares, in that order, with at least one blank square between successive groups." [1]

Simon Tatham's Portable Puzzle Collection has an excellent implementation of these puzzles, under the name "Pattern". They run under Windows, OS X, Linux, and as a Java applet in a browser.

As usual, the powerful answer set solver clingo makes short work of these puzzles. The source files are:
  • nonogram.lp (clingo input file describing the nonogram puzzle type)
  • 10x10.lp (lists the constraints in the above 10x10 puzzle)
  • display (Perl program to display the solution in human-readable form and check its validity against the original constraints)
In this model of the puzzle, the objective is to find the cell in which each unbroken line of filled-in squares ("run") starts, subject to the following constraints:
  • Two runs in the same row or column must be separated by at least one cell of whitespace
  • From the starting cells and lengths of the runs in a row or column, we can infer which cells in that row or column must be filled in. The set of cells filled in by the row runs must be the same as the set of cells filled in by the column runs.
Here's the output of solving the above 10x10 puzzle:
$ clingo nonogram.lp 10x10.lp | perl display
Answer: 1
X X X . .   . . . . . 
. . X . .   . . . . . 
. . . . .   . . X X X 
. . . . .   . . X . X 
X . X . X   X . X . . 

X . X X X   X X X . X 
. . X X X   X . X . X 
X . X X X   X X X . X 
X X X X X   X X X . . 
X X . . X   X X X . . 
OK
SATISFIABLE

Models      : 1+    
Time        : 0.020
  Prepare   : 0.010
  Prepro.   : 0.010
  Solving   : 0.000
Here's the output from solving a 30x30 puzzle, along with an image of the initial puzzle state and the corresponding Simon Tatham's Portable Puzzle Collection save file.

[1] Wikipedia, Nonogram, (as of Apr. 14, 2010, 08:56 GMT).

Followers

Contributors