diff --git a/ipynb/Coin Flip.ipynb b/ipynb/Coin Flip.ipynb
index 7843eae..5f4ec79 100644
--- a/ipynb/Coin Flip.ipynb
+++ b/ipynb/Coin Flip.ipynb
@@ -6,69 +6,105 @@
"source": [
"# The Devil and the Coin Flip Game\n",
"\n",
- ">You're playing a game with the devil, with your soul at stake. You're sitting at a circular table which has 4 coins, arranged in a diamond, at the 12, 3, 6, and 9 o'clock positions. You are blindfolded, and can never see the coins or the table.\n",
+ "If the Devil ever challenges me to a [fiddle contest](https://en.wikipedia.org/wiki/The_Devil_Went_Down_to_Georgia), I'm going to lose. I'd have a better chance at this contest:\n",
"\n",
- ">Your goal is to get all 4 coins showing heads, by telling the devil the position(s) of some coins to flip. We call this a \"move\" on your part. The devil must faithfully perform the requested flips, but may first sneakily rotate the table any number of quarter-turns, so that the coins are in different positions. You keep making moves, and the devil keeps rotating and flipping, until all 4 coins show heads.\n",
+ "> *You're playing a game with the devil, with your soul at stake. You're sitting at a circular table which has 4 coins, arranged in a diamond, at the 12, 3, 6, and 9 o'clock positions. You are blindfolded, and can never see the coins or the table.*\n",
"\n",
- "> Example: You tell the devil the 12 o'clock and 6 o'clock positions. The devil could leave the table unrotated (or could rotate it a half-turn), and then flip the two coins that you specified. Or the devil could rotate the table a quarter turn in either direction, and then flip the coins that are now in the 12 o'clock and 6 o'clock positions (which were formerly at 3 o'clock and 9 o'clock). You won't know which of these actions the devil took.\n",
+ "> *Your goal is to get all 4 coins showing heads, by telling the devil the position(s) of some coins to flip. We call this a \"move\" on your part. The devil must faithfully perform the requested flips, but may first sneakily rotate the table any number of quarter-turns, so that the coins are in different positions. You keep making moves, and the devil keeps rotating and flipping, until all 4 coins show heads.*\n",
"\n",
- "> What is a shortest sequence of moves that is *guaranteed* to win, no matter what the initial state of the coins, and no matter what rotations the devil applies?\n",
+ "> *Example: You tell the devil the 12 o'clock and 6 o'clock positions. The devil could leave the table unrotated (or could rotate it a half-turn), and then flip the two coins that you specified. Or the devil could rotate the table a quarter turn in either direction, and then flip the coins that are now in the 12 o'clock and 6 o'clock positions (which were formerly at 3 o'clock and 9 o'clock). You won't know which of these actions the devil took.*\n",
+ "\n",
+ "> *What is a shortest sequence of moves that is *guaranteed* to win, no matter what the initial state of the coins, and no matter what rotations the devil applies?*\n",
"\n",
"# Analysis\n",
"\n",
- "The player, being blindfolded, does not know the true state of the coins. So the player should represent what is known: the *set of possible states* of the coins. We call this a *belief state*. At the start of the game, each of the four coins could be either heads or tails, so that's 24 = 16 possibilities in the belief state. \n",
+ "The player, being blindfolded, does not know the true state of the coins. So the player should represent what is known: the *set of possible states* of the coins. We call this a *belief state*. At the start of the game, each of the four coins could be either heads or tails, so that's 24 = 16 possibilities in the belief state:\n",
"\n",
- "A move updates the belief state as follows: for every coin sequence in the current belief state, rotate it in every possible way, and then flip the coins specified by the position(s) in the move. Collect all these results together to form the new belief state. Solving the game means coming up with a sequence of moves that ends in a belief state consisting of just `{'HHHH'}`. It must be a shortest possible sequence, so a breadth-first search seems right. The search space will be tiny, so compute time will be trivial; the only issue is specifying the domain correctly. To increase the chance of getting it correct, I won't try to do anything fancy, such as noticing that some coin sequences are rotational variants of other sequences.\n",
+ " {HHHH, HHHT, HHTH, HHTT, HTHH, HTHT, HTTH, HTTT, THHH, THHT, THTH, THTT, TTHH, TTHT, TTTH, TTTT}\n",
+ "\n",
+ "The idea is that even though the player doesn't know for sure what the actual state of the coins is, nor what rotations the devil performs, the player can still manipulate the belief state towards the goal belief state:\n",
+ "\n",
+ " {HHHH}\n",
+ "\n",
+ "A move updates the belief state as follows: for every coin sequence in the current belief state, rotate it in every possible way, and then flip the coins specified by the position(s) in the move. Collect all these results together to form the new belief state. Solving the game means coming up with a list of moves that ends in the belief state `{'HHHH'}`. It must be a shortest possible sequence of moves, so a [breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search) is appropriate. The search space is small (just 216 possible belief states), so run time will be fast; the only issue is specifying the domain correctly. To increase the chance of getting it right, I won't try to do anything fancy, such as noticing that some coin sequences are rotational variants of other sequences.\n",
"\n",
"\n",
"# Implementation Choices\n",
"\n",
"Here are the main concepts, and my implementation choices:\n",
"\n",
- "- `Coins`: A *coin sequence* is represented as a `str` of four characters, such as `'HTTT'`. \n",
- "- `Belief`: A *belief state* is represented as a `frozenset` of `Coins` (frozen so that it can be hashed in a `set`).\n",
- "- `Move`: A *move* is a set of positions to flip. A position will be an integer index into the coin sequence, so a move is a set of these such as `{0, 1}`, which we can interpret as \"flip the 12 o'clock (0) and 3 o'clock (1) positions.\" \n",
- "- `all_coins`: Set of all possible coin sequences: `{'HHHH', 'HHHT', ...}`.\n",
- "- `rotations`: The function `rotations(coins)` returns a list of all 4 rotations of the coin sequence.\n",
- "- `update`: The function `update(belief, move)` returns an updated belief state, representing all the possible coin sequences that could result from any devil rotation followed by the specified flip(s). (But don't flip `'HHHH'`, because the game would have already ended.)\n",
- "- `flip`: The function `flip(coins, move)` flips the specified positions within the coin sequence."
+ "- `Coins`: A *coin sequence* (on the table) is represented as a `str` of four characters, such as `'HTTT'`. \n",
+ "- `Belief`: A *belief state* is represented as a `frozenset` of `Coins` (frozen so that it can be hashed).\n",
+ "- `Position`: A position is an integer index into the coin sequence; position `0` selects the `H` in `'HTTT'`\n",
+ "and corresponds to the 12 o'clock position; position 1 corresponds to 3 o'clock, and so on.\n",
+ "- `Move`: A *move* is a set of positions to flip, such as `{0, 1}`. \n",
+ "- `Strategy`: A strategy for playing the game is just a list of moves. Since there is no feedback while playing\n",
+ "(the player is blindfolded) there is no need for decision points in the strategy.\n",
+ "- `all_coins()`: A belief state consisting of the set of all possible coin sequences: `{'HHHH', 'HHHT', ...}`.\n",
+ "- `rotations(coins)`: returns a set of all 4 rotations of the coin sequence.\n",
+ "- `update(belief, move)`: an updated belief state: all the coin sequences that could result from any rotation followed by the specified flip(s).\n",
+ "- `flip(coins, move)`: flips the specified positions within the coin sequence.\n",
+ " (But don't flip `'HHHH'`, because the game would have already ended if this were the coin sequence.)"
]
},
{
"cell_type": "code",
"execution_count": 1,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from collections import deque, Counter\n",
"from itertools import product, combinations\n",
"import random\n",
"\n",
- "Coins = ''.join # A coin sequence, such as 'HHHT'.\n",
- "Belief = frozenset # A belief state is a set of possible coin sequences.\n",
- "Move = set # A Move is a set of positions to flip.\n",
- "all_coins = Belief(map(Coins, product('HT', repeat=4)))\n",
+ "Coins = ''.join # A coin sequence; a str: 'HHHT'.\n",
+ "Belief = frozenset # A set of possible coin sequences: {'HHHT', 'TTTH'}\n",
+ "Move = set # A set of positions to flip: {0, 1}\n",
+ "Strategy = list # A list of Moves: [{0, 1}, {0, 1, 2, 3}, ...]\n",
+ "\n",
+ "def all_coins() -> Belief:\n",
+ " \"Return the belief set consisting of all possible coin sequences.\"\n",
+ " return Belief(map(Coins, product('HT', repeat=4)))\n",
"\n",
"def rotations(coins) -> [Coins]: \n",
" \"A list of all possible rotations of a coin sequence.\"\n",
- " return [coins[r:] + coins[:r] for r in range(4)]\n",
+ " return {coins[r:] + coins[:r] for r in range(4)}\n",
"\n",
"def update(belief, move) -> Belief:\n",
" \"Update belief: consider all possible rotations, then flip.\"\n",
- " return Belief((c if c == 'HHHH' else flip(c, move))\n",
+ " return Belief(flip(c, move)\n",
" for coins in belief\n",
" for c in rotations(coins))\n",
"\n",
"def flip(coins, move) -> Coins:\n",
- " \"Flip the coins in the positions specified by the move.\"\n",
+ " \"Flip the coins in the positions specified by the move (but leave 'HHHH' alone).\"\n",
+ " if coins == 'HHHH': return coins\n",
" coins = list(coins) # Need a mutable sequence\n",
" for i in move:\n",
" coins[i] = ('H' if coins[i] == 'T' else 'T')\n",
" return Coins(coins)"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'HHHH, HHHT, HHTH, HHTT, HTHH, HTHT, HTTH, HTTT, THHH, THHT, THTH, THTT, TTHH, TTHT, TTTH, TTTT'"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "', '.join(sorted(all_coins()))"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -78,10 +114,8 @@
},
{
"cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 3,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -89,7 +123,7 @@
"'THTT'"
]
},
- "execution_count": 2,
+ "execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -100,18 +134,16 @@
},
{
"cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 4,
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "['HHHT', 'HHTH', 'HTHH', 'THHH']"
+ "{'HHHT', 'HHTH', 'HTHH', 'THHH'}"
]
},
- "execution_count": 3,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -122,10 +154,8 @@
},
{
"cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 5,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -148,13 +178,13 @@
" 'TTTT'})"
]
},
- "execution_count": 4,
+ "execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "all_coins"
+ "all_coins()"
]
},
{
@@ -166,10 +196,8 @@
},
{
"cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 6,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -191,13 +219,13 @@
" 'TTTH'})"
]
},
- "execution_count": 5,
+ "execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "update(all_coins, {0, 1, 2, 3})"
+ "update(all_coins(), {0, 1, 2, 3})"
]
},
{
@@ -209,10 +237,8 @@
},
{
"cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 7,
+ "metadata": {},
"outputs": [],
"source": [
"def powerset(sequence): \n",
@@ -224,10 +250,8 @@
},
{
"cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 8,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -250,7 +274,7 @@
" {0, 1, 2, 3}]"
]
},
- "execution_count": 7,
+ "execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -266,41 +290,53 @@
"# Search for a Solution\n",
"\n",
"The generic function `search` does a breadth-first search starting\n",
- "from a `start` state, looking for a `goal` belief state, considering possible `moves` at each turn,\n",
- "and updating the belief state according to the `update` function. As is typical for search algorithms, we build a search tree, keeping a queue of tree `nodes` to consider, where each \n",
- "node consists of a path (a sequence of moves) and a resulting belief state. We also keep track, in `explored`, of\n",
- "the states we have already explored, so that we don't have to revisit them. Note an interesting fact: the `search` function works on belief states, but would be exactly the same if we were searching on individual states, not belief states (the only difference would be to the parameters, not the code: the `start`, `goal`, and `update` values would be different).\n",
+ "from a `start` state, looking for a `goal` state, considering possible `actions` (a collection of moves) at each turn,\n",
+ "and computing the `result` of each action (`result` is a function such that `result(state, action)` returns the new state that results from executing the action in the current state). It works by keeping a queue of unexplored possibilities, where each entry in the queue is a pair consisting of a *strategy* (sequence of moves) and a *state* that that strategy leads to. We also keep a set of `explored` states, so that we don't repeat ourselves.\n",
"\n",
- "The `coin_search` function calls the generic `search` function to solve our specific problem:\n"
+ "Amazingly, \n",
+ "even though we want to search in the space of *belief states*, we can still use the generic search function that is designed for regular-old-states. The search for belief states just works, as long as we properly specify the start state, the goal state, and the means of moving between states."
]
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def search(start, goal, actions, result) -> Strategy:\n",
+ " \"Breadth-first search from start state to goal; return strategy to get to goal.\"\n",
+ " explored = set()\n",
+ " queue = deque([([], start)])\n",
+ " while queue:\n",
+ " (strategy, state) = queue.popleft()\n",
+ " if state == goal:\n",
+ " return strategy\n",
+ " for action in actions:\n",
+ " state2 = result(state, action)\n",
+ " if state2 not in explored:\n",
+ " explored.add(state2)\n",
+ " queue.append((strategy + [action], state2))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The `coin_search` function calls the generic `search` function to solve our specific problem:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
"metadata": {
- "collapsed": false
+ "collapsed": true
},
"outputs": [],
"source": [
- "def search(start, goal, update, moves) -> [Move]:\n",
- " \"Breadth-first search from start belief state to goal.\"\n",
- " explored = set()\n",
- " queue = deque([Node([], start)])\n",
- " while queue:\n",
- " (path, belief) = queue.popleft()\n",
- " if belief == goal:\n",
- " return path\n",
- " for move in moves:\n",
- " belief2 = update(belief, move)\n",
- " if belief2 not in explored:\n",
- " explored.add(belief2)\n",
- " queue.append(Node(path + [move], belief2))\n",
- " \n",
- "def coin_search() -> [Move]: \n",
- " \"Use the generic `search` function to solve the Coin Flip problem.\"\n",
- " return search(all_coins, {'HHHH'}, update, powerset(range(4)))\n",
- " \n",
- "def Node(path, belief) -> tuple: return (path, belief)"
+ "def coin_search() -> Strategy: \n",
+ " \"Use `search` to solve the Coin Flip problem.\"\n",
+ " return search(start=all_coins(), goal={'HHHH'}, \n",
+ " actions=powerset(range(4)), result=update)"
]
},
{
@@ -312,10 +348,8 @@
},
{
"cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 11,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -337,7 +371,7 @@
" {0, 1, 2, 3}]"
]
},
- "execution_count": 9,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -350,27 +384,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "That's a 15-move sequence that is guaranteed to lead to a win. You can stop reading here if all you want is the answer to the puzzle. Or you can continue on ...\n",
+ "That's a 15-move strategy that is guaranteed to lead to a win. Stop here if all you want is the answer to the puzzle. Or you can continue on ...\n",
+ "\n",
+ "----\n",
"\n",
"# Verifying the Solution\n",
"\n",
- "Can I verify that the solution is correct? Exploring with paper and pencil, it does appear to work. A colleague did the puzzle and got the same answer, so that's a good sign. And here's further validation: The function `random_devil` takes an initial coin sequence and a sequence of moves, and plays those moves with a devil that chooses rotations randomly, returning the number of moves it takes until the player wins. Note this is dealing ith concrete, individual states of the world, like `HTHH`, not belief states."
+ "I don't have a proof, but I have some evidence that the solution is correct:\n",
+ "- Exploring with paper and pencil, it does appear to work. \n",
+ "- A colleague did the puzzle and got the same answer. \n",
+ "- Running the function `random_devil` below is consistent with it working.\n",
+ "\n",
+ "The function `random_devil` takes an initial coin sequence and a sequence of moves, and plays those moves with a devil that chooses rotations randomly, returning the number of moves it takes until the player wins. Note this is dealing ith concrete, individual states of the world, like `HTHH`, not belief states."
]
},
{
"cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 12,
+ "metadata": {},
"outputs": [],
"source": [
- "def random_devil(coins, moves) -> int or None:\n",
+ "def random_devil(coins, strategy) -> int or None:\n",
" \"\"\"A random devil responds to moves starting from coins; \n",
" return the number of moves until win, or None.\"\"\"\n",
" if coins == 'HHHH': return 0\n",
- " for (i, move) in enumerate(moves, 1):\n",
- " coins = flip(random.choice(rotations(coins)), move)\n",
+ " for (i, move) in enumerate(strategy, 1):\n",
+ " coins = flip(random.choice(list(rotations(coins))), move)\n",
" if coins == 'HHHH': \n",
" return i\n",
" return None"
@@ -380,94 +419,33 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "I will let the `random_devil` play 1000 times from each possible starting coin sequence:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "Counter({0: 1000,\n",
- " 1: 1000,\n",
- " 2: 1026,\n",
- " 3: 974,\n",
- " 4: 938,\n",
- " 5: 1016,\n",
- " 6: 1009,\n",
- " 7: 1037,\n",
- " 8: 961,\n",
- " 9: 1035,\n",
- " 10: 983,\n",
- " 11: 1013,\n",
- " 12: 1009,\n",
- " 13: 966,\n",
- " 14: 1037,\n",
- " 15: 996})"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "moves = coin_search()\n",
- "\n",
- "Counter(random_devil(coins, moves) \n",
- " for coins in all_coins\n",
- " for _ in range(1000))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "This says that the player won all 16,000 times. (If the player had ever lost, there would have been an entry for `None` in the Counter.)\n",
- "The remarkable thing, which I can't explain, is that there are very nearly exactly 1,000 results for each of the move counts from 0 to 15. Can you explain that?\n",
- "\n",
- "# Canonical Coin Sequences and Moves\n",
- "\n",
- "Consider the four coin sequences `{'HHHT', 'HHTH', 'HTHH', 'THHH'}`. In a sense, these are all the same: they all denote the same sequence of coins with the table rotated to different degrees. Since the devil is free to rotate the table any amount at any time, we could be justified in treating all four of these as equivalent, and collapsing them into one representative member (we could arbitrarily choose the one that comes first in alphabetical order, `'HHHT'`). I will write a definition for `Belief` that returns a `frozenset`, just like before, but makes it a set of canonical coin sequences.\n",
- "\n",
- "Similarly, the four moves `{3}, {2}, {1},` and `{0}` are equivalent, in that they all say \"flip exactly one of the coins, but since you're going to rotate the table first anyway, it doesn't matter which one I specify.\" I will write a function `canonical_moves` that lists all the canonical rotationally invariant moves corresponding to a belief state."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "def Belief(coin_collection): \n",
- " \"A set of all the coin sequences in this collection, canonicalized.\"\n",
- " return frozenset(min(rotations(c)) for c in coin_collection)\n",
- "\n",
- "def canonical_moves(coin_collection):\n",
- " \"All rotationally invariant moves for a sequence of N coins.\"\n",
- " return [set(i for (i, coin) in enumerate(coins) if coin == 'H')\n",
- " for coins in sorted(coin_collection) \n",
- " if 'H' in coins]"
+ "I will let the `random_devil` play 10,000 times from each possible starting coin sequence:"
]
},
{
"cell_type": "code",
"execution_count": 13,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "frozenset({'HHHH', 'HHHT', 'HHTT', 'HTHT', 'HTTT', 'TTTT'})"
+ "Counter({0: 10000,\n",
+ " 1: 10000,\n",
+ " 2: 10067,\n",
+ " 3: 9933,\n",
+ " 4: 9862,\n",
+ " 5: 10014,\n",
+ " 6: 10162,\n",
+ " 7: 9962,\n",
+ " 8: 9951,\n",
+ " 9: 10108,\n",
+ " 10: 9947,\n",
+ " 11: 10044,\n",
+ " 12: 9841,\n",
+ " 13: 9986,\n",
+ " 14: 10127,\n",
+ " 15: 9996})"
]
},
"execution_count": 13,
@@ -476,45 +454,70 @@
}
],
"source": [
- "Belief(all_coins)"
+ "strategy = coin_search()\n",
+ "\n",
+ "Counter(random_devil(coins, strategy) \n",
+ " for coins in all_coins()\n",
+ " for _ in range(10000))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "The starting belief set is down from 16 sequences to 6. Call these 4 heads, 3 heads, 2 adjacent heads, 2 opposite heads, 1 head, and no heads, respectively. "
+ "This says that the player won all 16,000 times. (If the player had ever lost, there would have been an entry for `None` in the Counter.) This suggests the strategy is likely to win, but doesn't prove it will always win, and doesn't say anything about the possibility of a shorter solution.\n",
+ "The remarkable thing, which I can't explain, is that there are very nearly exactly 1,000 results for each of the move counts from 0 to 15. Can you explain that?\n",
+ "\n",
+ "# Canonical Coin Sequences\n",
+ "\n",
+ "Consider the four coin sequences `{'HHHT', 'HHTH', 'HTHH', 'THHH'}`. In a sense, these are all the same: they all denote the same sequence of coins with the table rotated to different degrees. Since the devil is free to rotate the table any amount at any time, we could be justified in treating all four of these as equivalent, and collapsing them into one representative member (we could arbitrarily choose the one that comes first in alphabetical order, `'HHHT'`). I will redefine `Belief` as a function that returns a `frozenset`, just like before, but makes it a set of `canonical` coin sequences."
]
},
{
"cell_type": "code",
"execution_count": 14,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[{0, 1, 2, 3}, {0, 1, 2}, {0, 1}, {0, 2}, {0}]"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
+ "metadata": {},
+ "outputs": [],
"source": [
- "canonical_moves(_)"
+ "def canonical(coins): return min(rotations(coins))\n",
+ "\n",
+ "def Belief(coin_collection): \n",
+ " \"A set of all the coin sequences in this collection, canonicalized.\"\n",
+ " return frozenset(canonical(coins) for coins in coin_collection)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Similarly, there are only 5 distinct moves, which we can call flip 4, flip 3, flip 2 adjacent, flip 2 opposite, and flip 1.\n",
- "\n",
- "I have to confess to a form of cheating here: I threw in the `sorted` in `canonical_moves` even though it was not necessary. By including it, I made sure that `{0, 1, 2, 3}` is the first of the possible moves, which makes `coin_search` faster, because half the time `{0, 1, 2, 3}` is the right move to make."
+ "With `Belief` redefined, the result of calling `all_coins` will be different:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "frozenset({'HHHH', 'HHHT', 'HHTT', 'HTHT', 'HTTT', 'TTTT'})"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "all_coins()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The starting belief set is down from 16 to 6, namely 4 heads, 3 heads, 2 adjacent heads, 2 opposite heads, 1 head, and no heads, respectively. "
]
},
{
@@ -523,118 +526,74 @@
"source": [
"# Solutions for *N* Coins\n",
"\n",
- "What if there are 3 coins on the table arranged in a triangle? Or 6 coins in a hexagon? To answer that, I'll generalize the two functions that have a \"4\" in them, `coin_search`, and `rotations`. I'll also generalize `update`, which had `'HHHH'` in it. And I'll introduce the function `canonical_coins` to return a belief state of all canonical coin sequences of length `N`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "def coin_search(N=4):\n",
- " start = canonical_coins(N)\n",
- " return search(start, {'H' * N}, update, canonical_moves(start))\n",
- "\n",
- "def rotations(coins) -> [Coins]: \n",
- " \"A list of all possible rotations of a coin sequence.\"\n",
- " return [coins[r:] + coins[:r] for r in range(len(coins))]\n",
- " \n",
- "def update(belief, move) -> Belief:\n",
- " \"Update belief: consider all possible rotations, then flip.\"\n",
- " return Belief((flip(c, move) if 'T' in c else c)\n",
- " for coins in belief\n",
- " for c in rotations(coins))\n",
- "\n",
- "def canonical_coins(N) -> Belief: \n",
- " return Belief(map(Coins, product('HT', repeat=N)))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "First verify that we didn't break `search`:"
+ "What if there are 3 coins on the table arranged in a triangle? Or 6 coins in a hexagon? To answer that, I'll generalize the three functions that have a \"4\" in them, `all_coins`, `rotations` and `coin_search`. I'll also generalize `flip`, which had `'HHHH'` in it. "
]
},
{
"cell_type": "code",
"execution_count": 16,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[{0, 1, 2, 3},\n",
- " {0, 2},\n",
- " {0, 1, 2, 3},\n",
- " {0, 1},\n",
- " {0, 1, 2, 3},\n",
- " {0, 2},\n",
- " {0, 1, 2, 3},\n",
- " {0, 1, 2},\n",
- " {0, 1, 2, 3},\n",
- " {0, 2},\n",
- " {0, 1, 2, 3},\n",
- " {0, 1},\n",
- " {0, 1, 2, 3},\n",
- " {0, 2},\n",
- " {0, 1, 2, 3}]"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
+ "metadata": {},
+ "outputs": [],
"source": [
- "coin_search(4)"
+ "def all_coins(N=4) -> Belief:\n",
+ " \"Return the belief set consisting of all possible coin sequences.\"\n",
+ " return Belief(map(Coins, product('HT', repeat=N)))\n",
+ "\n",
+ "def rotations(coins) -> [Coins]: \n",
+ " \"A list of all possible rotations of a coin sequence.\"\n",
+ " return {coins[r:] + coins[:r] for r in range(len(coins))}\n",
+ "\n",
+ "def coin_search(N=4) -> Strategy: \n",
+ " \"Use the generic `search` function to solve the Coin Flip problem.\"\n",
+ " return search(start=all_coins(N), goal={'H' * N}, \n",
+ " actions=all_moves(N), result=update)\n",
+ "\n",
+ "def flip(coins, move) -> Coins:\n",
+ " \"Flip the coins in the positions specified by the move (but leave all 'H' alone).\"\n",
+ " if 'T' not in coins: return coins\n",
+ " coins = list(coins) # Need a mutable sequence\n",
+ " for i in move:\n",
+ " coins[i] = ('H' if coins[i] == 'T' else 'T')\n",
+ " return Coins(coins)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Now test the new definitions:"
+ "I also need to generalize `all_moves`. To compute the set of possible moves for 4 coins, I used `powerset`, and got 16 possible moves. Now I want to know the set od canonicalized moves for any *N*. To get that, I'll look at the canonicalized set of `all_coins(N)`, and for each one pull out the positions that have an `H` in them, and flip those (the positions with a `T` should be symmetric, so we don't need them as well)."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
- "collapsed": false
+ "collapsed": true
},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['HHHHHT', 'HHHHTH', 'HHHTHH', 'HHTHHH', 'HTHHHH', 'THHHHH']"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
+ "outputs": [],
"source": [
- "rotations('HHHHHT')"
+ "def all_moves(N) -> [Move]:\n",
+ " \"All rotationally invariant moves for a sequence of N coins.\"\n",
+ " return [set(i for (i, coin) in enumerate(coins) if coin == 'H')\n",
+ " for coins in sorted(all_coins(N))]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Let's test the new definitions:"
]
},
{
"cell_type": "code",
"execution_count": 18,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "frozenset({'HHHHHH', 'HHHHTT', 'HHHTHT', 'HHTHHT'})"
+ "'ok'"
]
},
"execution_count": 18,
@@ -643,20 +602,25 @@
}
],
"source": [
- "update(_, {0})"
+ "assert all_moves(4) == [{0, 1, 2, 3}, {0, 1, 2}, {0, 1}, {0, 2}, {0}, set()]\n",
+ "assert all_coins(4) == {'HHHH', 'HHHT', 'HHTT', 'HTHT', 'HTTT', 'TTTT'}\n",
+ "assert all_coins(5) == {'HHHHH','HHHHT', 'HHHTT','HHTHT','HHTTT', 'HTHTT', 'HTTTT', 'TTTTT'}\n",
+ "assert rotations('HHHHHT') == {'HHHHHT', 'HHHHTH', 'HHHTHH', 'HHTHHH', 'HTHHHH', 'THHHHH'}\n",
+ "assert update({'TTTTTTT'}, {3}) == {'HTTTTTT'}\n",
+ "assert (update(rotations('HHHHHT'), {0}) == update({'HHHHHT'}, {1 })== update({'HHHHHT'}, {2})\n",
+ " == {'HHHHHH', 'HHHHTT', 'HHHTHT', 'HHTHHT'})\n",
+ "'ok'"
]
},
{
"cell_type": "code",
"execution_count": 19,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "frozenset({'HHHH', 'HHHT', 'HHTT', 'HTHT', 'HTTT', 'TTTT'})"
+ "[{0, 1, 2, 3}, {0, 1, 2}, {0, 1}, {0, 2}, {0}, set()]"
]
},
"execution_count": 19,
@@ -665,27 +629,18 @@
}
],
"source": [
- "canonical_coins(4)"
+ "all_moves(4)"
]
},
{
"cell_type": "code",
"execution_count": 20,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "frozenset({'HHHHH',\n",
- " 'HHHHT',\n",
- " 'HHHTT',\n",
- " 'HHTHT',\n",
- " 'HHTTT',\n",
- " 'HTHTT',\n",
- " 'HTTTT',\n",
- " 'TTTTT'})"
+ "{'HHHHHT', 'HHHHTH', 'HHHTHH', 'HHTHHH', 'HTHHHH', 'THHHHH'}"
]
},
"execution_count": 20,
@@ -694,20 +649,29 @@
}
],
"source": [
- "canonical_coins(5)"
+ " rotations('HHHHHT')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "With 4 coins we can flip 4, flip 3, flip 2 adjacent, flip 2 opposite, flip 1, or flip nothing.\n",
+ "Similarly, with 4 coins there can be 4 heads 3 heads, 2 adjacent heads, 2 opposite heads, 1 head, or no heads.\n",
+ "If we start with 6 coins of which one is `'T'`, and do a single flip (and it doesn't matter what position that flip is), then either the Devil flips the `'T'`, in which case we get all heads, or it can flip an `'H'` which is either adjacent to, 2 away from, or 3 away from the existing `'T'`.\n",
+ "\n",
+ "How many distinct canonical coin sequences are there for *N* coins from 1 to 10?"
]
},
{
"cell_type": "code",
"execution_count": 21,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[{0, 1, 2, 3, 4}, {0, 1, 2, 3}, {0, 1, 2}, {0, 1, 3}, {0, 1}, {0, 2}, {0}]"
+ "{1: 2, 2: 3, 3: 4, 4: 6, 5: 8, 6: 14, 7: 20, 8: 36, 9: 60, 10: 108}"
]
},
"execution_count": 21,
@@ -716,64 +680,7 @@
}
],
"source": [
- "canonical_moves(_)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "frozenset({'HHHHHH',\n",
- " 'HHHHHT',\n",
- " 'HHHHTT',\n",
- " 'HHHTHT',\n",
- " 'HHHTTT',\n",
- " 'HHTHHT',\n",
- " 'HHTHTT',\n",
- " 'HHTTHT',\n",
- " 'HHTTTT',\n",
- " 'HTHTHT',\n",
- " 'HTHTTT',\n",
- " 'HTTHTT',\n",
- " 'HTTTTT',\n",
- " 'TTTTTT'})"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "canonical_coins(6)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{1: 2, 2: 3, 3: 4, 4: 6, 5: 8, 6: 14, 7: 20, 8: 36, 9: 60, 10: 108}"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "{N: len(canonical_coins(N))\n",
+ "{N: len(all_coins(N))\n",
" for N in range(1, 11)}"
]
},
@@ -788,10 +695,8 @@
},
{
"cell_type": "code",
- "execution_count": 24,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 22,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -819,7 +724,7 @@
" 7: None}"
]
},
- "execution_count": 24,
+ "execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -834,7 +739,7 @@
"source": [
"Too bad; there are no solutions for N = 3, 5, 6, or 7. \n",
"\n",
- "There are solutions for N = 1, 2, 4; of lengths 1, 3, 15. That suggests the conjecture: \n",
+ "There are solutions for N = 1, 2, 4; they have lengths 1, 3, 15, respectively. That suggests the conjecture: \n",
"\n",
"> For every *N* that is a power of 2, there will be a shortest solution of length 2*N* - 1.\n",
"\n",
@@ -842,37 +747,44 @@
"\n",
"# Solution for 8 Coins\n",
"\n",
- "Even though there are 236 = 68 billion belief states fo N = 8, and the desired solution has 255 steps, I hope it won't take too long. Let's try:"
+ "For N = 8, there are 236 = 69 billion belief states and the desired solution has 255 steps. All the computations up to now have been instantaneous, but this one should take a few minutes. Let's see:"
]
},
{
"cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 23,
+ "metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "CPU times: user 1min 58s, sys: 1.36 s, total: 2min\n",
- "Wall time: 2min 18s\n"
+ "CPU times: user 1min 20s, sys: 295 ms, total: 1min 20s\n",
+ "Wall time: 1min 20s\n"
]
- },
+ }
+ ],
+ "source": [
+ "%time solution8 = coin_search(8)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {},
+ "outputs": [
{
"data": {
"text/plain": [
"255"
]
},
- "execution_count": 25,
+ "execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "%time solution8 = coin_search(8)\n",
"len(solution8)"
]
},
@@ -880,15 +792,97 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Eureka! That's evidence in favor of my conjecture, and it took less time than I thought it would. Here is the solution:"
+ "Eureka! That's evidence in favor of the conjecture. But not proof. And it leaves many questions unanswered:\n",
+ "- Can you show there are no solutions for *N* = 9, 10, 11, ...?\n",
+ "- Can you prove there are no solutions for any *N* that is not a power of 2?\n",
+ "- Can you find a solution of length 65,535 for *N* = 16 and verify that it works?\n",
+ "- Can you generate a solution for any power of 2 (without proving it is shortest)?\n",
+ "- Can you prove there are no shorter solutions for *N* = 16?\n",
+ "- Can you prove the conjecture in general?\n",
+ "- Can you *understand* and *explain* how the solution works, rather than just listing the moves?"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Visualizing the Solution\n",
+ "\n",
+ "To aid understanding, I'll print a table showing the belief state after each move, using the canonicalized `Belief` form, lined up neatly in columns."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def show(moves, N=4):\n",
+ " \"For each move, print the move number, move, and belief state.\"\n",
+ " belief = all_coins(N)\n",
+ " order = sorted(belief)\n",
+ " show_line(0, {}, belief, order, N)\n",
+ " for (i, move) in enumerate(moves, 1):\n",
+ " belief = update(belief, move)\n",
+ " show_line(i, move, belief, order, N)\n",
+ "\n",
+ "def show_line(i, move, belief, order, N):\n",
+ " \"Print the move number, move, and belief state.\"\n",
+ " ordered_belief = [(coins if coins in belief else ' ' * len(coins))\n",
+ " for coins in order]\n",
+ " movestr = join((i if i in move else ' ') for i in range(N))\n",
+ " print('{:3} | {:8} | {} | {}'\n",
+ " .format(i, movestr, join(ordered_belief, ' '), i))\n",
+ " \n",
+ "def join(items, sep='') -> str: return sep.join(map(str, items))"
]
},
{
"cell_type": "code",
"execution_count": 26,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " 0 | | HHHH HHHT HHTT HTHT HTTT TTTT | 0\n",
+ " 1 | 0123 | HHHH HHHT HHTT HTHT HTTT | 1\n",
+ " 2 | 0 2 | HHHH HHHT HHTT HTTT TTTT | 2\n",
+ " 3 | 0123 | HHHH HHHT HHTT HTTT | 3\n",
+ " 4 | 01 | HHHH HHHT HTHT HTTT TTTT | 4\n",
+ " 5 | 0123 | HHHH HHHT HTHT HTTT | 5\n",
+ " 6 | 0 2 | HHHH HHHT HTTT TTTT | 6\n",
+ " 7 | 0123 | HHHH HHHT HTTT | 7\n",
+ " 8 | 012 | HHHH HHTT HTHT TTTT | 8\n",
+ " 9 | 0123 | HHHH HHTT HTHT | 9\n",
+ " 10 | 0 2 | HHHH HHTT TTTT | 10\n",
+ " 11 | 0123 | HHHH HHTT | 11\n",
+ " 12 | 01 | HHHH HTHT TTTT | 12\n",
+ " 13 | 0123 | HHHH HTHT | 13\n",
+ " 14 | 0 2 | HHHH TTTT | 14\n",
+ " 15 | 0123 | HHHH | 15\n"
+ ]
+ }
+ ],
+ "source": [
+ "show(coin_search(4))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can see that every odd-numbered move flips all four coins to eliminate the possibility of `TTTT`, flipping it to `HHHH`. We can also see that moves 2, 4, and 6 flip two coins and have the effect of eventually eliminating the two \"two heads\" sequences from the belief state, and then move 8 eliminates the \"three heads\" and \"one heads\" sequences, while bringing back the \"two heads\" possibilities. Repeating moves 2, 4, and 6 in moves 10, 12, and 14 then re-eliminates the \"two heads\", and move 15 gets the belief state down to `{'HHHH'}`.\n",
+ "\n",
+ "You could call `show(solution8)`, but the results look bad unless you have a very wide (340 characters) screen to view it on. So I'll just show `solution8` itself:\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
"outputs": [
{
"data": {
@@ -1150,7 +1144,7 @@
" {0, 1, 2, 3, 4, 5, 6, 7}]"
]
},
- "execution_count": 26,
+ "execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -1158,95 +1152,6 @@
"source": [
"solution8"
]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Of course, seeing that the pattern works up to *N* = 8 is not a proof, and this leaves many questions for you, the reader, to consider:\n",
- "- Can you show there are no solutions for *N* = 9, 10, 11 (and maybe further)?\n",
- "- Can you prove there are no solutions for *N* that is not a power of 2?\n",
- "- Can you find a solution of length 65,535 for *N* = 16 and verify that it works?\n",
- "- Can you prove there are no shorter solutions for *N* = 16?\n",
- "- Can you prove the conjecture in general?\n",
- "- Can you *understand* and *explain* how the solution works, rather than just listing the moves?"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Visualizing the Solution\n",
- "\n",
- "To help you understand solutions, I'll print a table showing the belief state after each move, using the canonicalized `Belief` form, and lining them up neatly in columns."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "def show(moves, belief=all_coins):\n",
- " \"For each move, print the move number, move, and belief state.\"\n",
- " show_line(0, 'start', belief)\n",
- " for (i, move) in enumerate(moves, 1):\n",
- " belief = update(belief, move)\n",
- " show_line(i, move, belief)\n",
- "\n",
- "def show_line(i, move, belief, order=sorted(Belief(all_coins))):\n",
- " \"Print the move number, move, and belief state.\"\n",
- " ordered_belief = [(coins if coins in belief else ' ' * len(coins))\n",
- " for coins in order]\n",
- " print('{:3} | {:6} | {} | {}'\n",
- " .format(i, join(move), join(ordered_belief, ' '), i))\n",
- " \n",
- "def join(items, sep='') -> str: return sep.join(map(str, items))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 0 | start | HHHH HHHT HHTT HTHT HTTT TTTT | 0\n",
- " 1 | 0123 | HHHH HHHT HHTT HTHT HTTT | 1\n",
- " 2 | 02 | HHHH HHHT HHTT HTTT TTTT | 2\n",
- " 3 | 0123 | HHHH HHHT HHTT HTTT | 3\n",
- " 4 | 01 | HHHH HHHT HTHT HTTT TTTT | 4\n",
- " 5 | 0123 | HHHH HHHT HTHT HTTT | 5\n",
- " 6 | 02 | HHHH HHHT HTTT TTTT | 6\n",
- " 7 | 0123 | HHHH HHHT HTTT | 7\n",
- " 8 | 012 | HHHH HHTT HTHT TTTT | 8\n",
- " 9 | 0123 | HHHH HHTT HTHT | 9\n",
- " 10 | 02 | HHHH HHTT TTTT | 10\n",
- " 11 | 0123 | HHHH HHTT | 11\n",
- " 12 | 01 | HHHH HTHT TTTT | 12\n",
- " 13 | 0123 | HHHH HTHT | 13\n",
- " 14 | 02 | HHHH TTTT | 14\n",
- " 15 | 0123 | HHHH | 15\n"
- ]
- }
- ],
- "source": [
- "show(coin_search(4))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We can see that every odd-numbered move flips all four coins to eliminate the possibility of `TTTT`, flipping it to `HHHH`. We can also see that moves 2, 4, and 6 flip two coins and have the effect of eventually eliminating the two \"two heads\" sequences from the belief state, and then move 8 eliminates the \"three heads\" and \"one heads\" sequences, while bringing back the \"two heads\" possibilities. Repeating moves 2, 4, and 6 in moves 10, 12, and 14 then re-eliminates the \"two heads\", and move 15 gets the belief state down to `{'HHHH'}`.\n",
- "\n"
- ]
}
],
"metadata": {
@@ -1265,7 +1170,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.0"
+ "version": "3.5.3"
}
},
"nbformat": 4,