Skip to content

Commit

Permalink
Enumerates all states properly
Browse files Browse the repository at this point in the history
  • Loading branch information
cslarsen committed Mar 11, 2016
1 parent 2991d6f commit ee11348
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ How to run

Just type `python busybeaver.py`. It supports Python 2 and 3.

*Protip:* Run with pypy.

Todo
----

* Run all machines at once, one step at a time (at least in batches).
* Multiplex batches onto processes with multiprocessing
* Find a cheap way to suspend and resume machines (e.g., make unique
numerical ID for each machine, save tape + ID, instead of dicts).
* With all above, have a queue, the ones that halt are removed from queue,
keep chugging on the ones that don't seem to finish.
* Finally, add some heuristics for detecting non-halting machines. Try to
cover all cases for Sigma(0..2) at least.

Author and license
------------------

Expand Down
38 changes: 20 additions & 18 deletions busybeaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import collections
import itertools
import sys

def binary_machines(n):
Expand Down Expand Up @@ -129,15 +130,15 @@ def ones(self):


def show(machine):
log("%d ones, %d shifts: %s\n" % (machine.ones(), machine.tape.shifts,
log(" %d ones, %d shifts: %s\n" % (machine.ones(), machine.tape.shifts,
machine.tape))

for (state, symbol), instr in sorted(machine.transition.items()):
state = chr(ord("A") + state)
write, move, next = instr
move = {-1: "L", 1: "R"}.get(move, move)
next = chr(ord("A") + next) if next != "H" else next
log(" %s %s: %s%s%s\n" % (state, symbol, write, move, next))
log(" %s %s: %s%s%s\n" % (state, symbol, write, move, next))

def sigma(states):
def instr():
Expand All @@ -149,38 +150,39 @@ def instr():

def all_transitions():
"""Generate all possible transition functions."""
# TODO: This one is hardwired for two-state busy beaver
for a in instr():
for b in instr():
for c in instr():
for d in instr():
trans = {
(0, 0): a,
(0, 1): b,
(1, 0): c,
(1, 1): d
}
yield trans
for i in itertools.product(instr(), repeat=states*2):
trans = {}
n=0
if len(i)>states:
for state in range(states):
for symbol in [0, 1]:
trans[(state, symbol)] = i[n]
n += 1
yield trans

champion = BusyBeaver({})

count = binary_machines(states)
for num, tran in enumerate(all_transitions(), 1):
candidate = BusyBeaver(transition=tran)
try:
if num % 101 == 0:
log("%d / %d\r" % (num, binary_machines(states)))
candidate.run(7) # cheating: stop after ops > S(n)
log("%.2f%% %d / %d\r" % (100.0*num/count, num, count))
candidate.run(107+1) # cheating: op>S(3) => op = 1+S(3)
# above S from http://www.drb.insel.de/~heiner/BB/
continue # Did not halt
except KeyError:
# By definition, halts
pass
log("%.2f%% %d / %d\r" % (100.0*num/count, num, count))
if candidate.ones() > champion.ones():
champion = candidate
show(champion)

log("\n%d-state machines enumerated: %d\n" % (states, num))
log(" %d-state machines enumerated: %d of %d\n" % (states, num, count))
return champion.ones()


if __name__ == "__main__":
log("Sigma(2) = %s\n" % str(sigma(2)))
for n in range(0,4):
log("Sigma(%d) = %s\n" % (n, str(sigma(n))))

0 comments on commit ee11348

Please sign in to comment.