コード例 #1
0
def add_option_group(parser):
    """Add Macro_Simulator options group to an OptParser parser object."""
    assert isinstance(parser, OptionParser)

    group = OptionGroup(parser, "Macro Simulator options")

    group.add_option("--steps",
                     type=int,
                     default=INF,
                     help="Max steps to run each simulation (0 for infinite). "
                     "[Default: infinite]")
    group.add_option("--time",
                     type=float,
                     default=15.0,
                     help="Max seconds to run each simulation. "
                     "[Default: %default]")
    group.add_option("--tape-limit",
                     type=int,
                     default=50,
                     help="Max tape size to allow.")
    group.add_option("--no-ctl",
                     dest="ctl",
                     action="store_false",
                     default=True,
                     help="Don't try CTL optimization.")

    parser.add_option_group(group)

    Simulator.add_option_group(parser)
    Block_Finder.add_option_group(parser)
コード例 #2
0
ファイル: Quick_Sim.py プロジェクト: nickdrozd/busy-beaver
def run(machine, block_size, back, prover, recursive, options):
  # Construct Machine (Backsymbol-k-Block-Macro-Machine)

  # If no explicit block-size given, use inteligent software to find one
  if not block_size:
    block_size = Block_Finder.block_finder(machine, options)

  # Do not create a 1-Block Macro-Machine (just use base machine)
  if block_size != 1:
    machine = Turing_Machine.Block_Macro_Machine(machine, block_size)
  if back:
    machine = Turing_Machine.Backsymbol_Macro_Machine(machine)

  global sim  # For debugging, especially with --manual
  sim = Simulator.Simulator(machine, options)

  if options.manual:
    return  # Let's us run the machine manually. Must be run as python -i Quick_Sim.py
  try:
    if options.quiet or options.verbose:  # Note verbose prints inside sim.step()
      if options.verbose:
        sim.verbose_print()

      total_loops = 0;

      while (sim.op_state == Turing_Machine.RUNNING and
             (options.loops == 0 or total_loops < options.loops)):
        sim.step()
        total_loops += 1;
    else:
      # TODO: maybe print based on time
      total_loops = 0;

      while (sim.op_state == Turing_Machine.RUNNING and
             (options.loops == 0 or total_loops < options.loops)):
        sim.print_self()
        sim.loop_run(options.print_loops)
        total_loops += options.print_loops;
  finally:
    sim.print_self()

  if sim.op_state == Turing_Machine.HALT:
    print
    print "Turing Machine Halted!"
    print
    if options.compute_steps:
      print "Steps:   ", sim.step_num
    print "Nonzeros:", sim.get_nonzeros()
    print
  elif sim.op_state == Turing_Machine.INF_REPEAT:
    print
    print "Turing Machine proven Infinite!"
    print "Reason:", sim.inf_reason
  elif sim.op_state == Turing_Machine.UNDEFINED:
    print
    print "Turing Machine reached Undefined transition!"
    print "State: ", sim.op_details[0][1]
    print "Symbol:", sim.op_details[0][0]
    print
    if options.compute_steps:
      print "Steps:   ", sim.step_num
    print "Nonzeros:", sim.get_nonzeros()
    print
コード例 #3
0
ファイル: Quick_Sim.py プロジェクト: nickdrozd/busy-beaver
                    help="Print step-by-step informaion from simulator "
                    "and prover (Overrides other --verbose-* flags).")
  parser.add_option("-l", "--loops", type=int, default=0,
                    help="Specify a maximum number of loops.")
  parser.add_option("--print-loops", type=int, default=10000, metavar="LOOPS",
                    help="Print every LOOPS loops [Default %default].")
  parser.add_option("--csv", action="store_true",
                    help="Read input file as CSV not standard format.")

  parser.add_option("--manual", action="store_true",
                    help="Don't run any simulation, just set up simulator "
                    "and quit. (Run as python -i Quick_Sim.py to interactively "
                    "run simulation.)")

  Simulator.add_option_group(parser)
  Block_Finder.add_option_group(parser)

  (options, args) = parser.parse_args()

  if options.quiet:
    options.verbose_simulator = False
    options.verbose_prover = False
    options.verbose_block_finder = False
  elif options.verbose:
    options.verbose_simulator = True
    options.verbose_prover = True
    options.verbose_block_finder = True

  if options.loops and options.print_loops > options.loops:
    options.print_loops = options.loops
コード例 #4
0
def recur_TM(TTable, block_size, back, prover, recursive, options):
    # Construct Machine (Backsymbol-k-Block-Macro-Machine)
    m = Turing_Machine.make_machine(TTable)

    # If no explicit block-size given, use inteligent software to find one
    if not block_size:
        block_size = Block_Finder.block_finder(m, options)

    #if not options.quiet:
    #  Turing_Machine.print_machine(m)

    # Do not create a 1-Block Macro-Machine (just use base machine)
    if block_size != 1:
        m = Turing_Machine.Block_Macro_Machine(m, block_size)
    if back:
        m = Turing_Machine.Backsymbol_Macro_Machine(m)

    global sim  # For debugging, especially with --manual
    sim = Simulator.Simulator(m, options)

    if options.manual:
        return  # Let's us run the machine manually. Must be run as python -i Quick_Sim.py

    groups = {}

    total_loops = 0
    # max_term_size = 100000000

    while (sim.op_state == Turing_Machine.RUNNING
           and (options.loops == 0 or total_loops < options.loops)):
        # print "%10d" % sim.step_num,"  ",sim.tape.print_with_state(sim.state)

        sim.step()

        # if sim.step_num > max_term_size:
        #   break

        if len(sim.tape.tape[0]) == 1 or len(sim.tape.tape[1]) == 1:
            min_config = strip_config(sim.state, sim.dir, sim.tape.tape)

            if len(min_config[0]) + len(min_config[-1]) <= 100:
                if min_config in groups:
                    # if len(groups[min_config]) >= 100:
                    #   groups[min_config].pop(0)

                    groups[min_config].append([
                        copy.deepcopy(sim.tape.tape[0][1:]),
                        copy.deepcopy(sim.tape.tape[1][1:]), sim.step_num
                    ])
                else:
                    groups[min_config] = [
                        [
                            copy.deepcopy(sim.tape.tape[0][1:]),
                            copy.deepcopy(sim.tape.tape[1][1:]), sim.step_num
                        ],
                    ]

        total_loops += 1

    #print "%10d" % sim.step_num,"  ",sim.tape.print_with_state(sim.state)
    #print

    sorted_keys = sorted(groups, key=lambda item: len(item[0]) + len(item[3]))

    print Output_Machine.display_ttable(TTable), "|",
    print "%5d" % len(groups)

    for min_config in sorted_keys:
        group = groups[min_config]

        recur_all = False

        if len(group) >= 4:
            print "  ", len(group), min_config

            growth = [[]
                      for i in xrange(len(group[0][0]) + len(group[0][1]) + 1)]
            for config in group:
                index = 0
                for symbol in config[0]:
                    growth[index].append(symbol.num)
                    index += 1
                for symbol in config[1]:
                    growth[index].append(symbol.num)
                    index += 1
                growth[index].append(config[2])

            recur_all = True

            for series in growth:
                print "     ", series

                (recur_found, coefs, constant) = recur_fit(series)

                if recur_found:
                    print "         success",
                    recur_print(coefs, constant)
                else:
                    print "         failure"

                recur_all = recur_all and recur_found
                print

            if recur_all:
                break

    print "  ", recur_all
    print
コード例 #5
0
def run(TTable, block_size, steps, runtime, recursive, progress, options,
        stats):
  # Get and initialize a new simulator object.
  m1 = Turing_Machine.Simple_Machine(TTable)
  if not block_size:
    # Find optimal macro block size automatically.
    block_size = Block_Finder.block_finder(m1, options)
  m2 = Turing_Machine.Block_Macro_Machine(m1, block_size)
  m3 = Turing_Machine.Backsymbol_Macro_Machine(m2)

  sim = Simulator.Simulator(m3, options)

  # Run with an optional timer.
  try:
    if runtime:
      ALARM.set_alarm(runtime)

    sim.loop_run(steps)

    ALARM.cancel_alarm()

  except AlarmException:
    ALARM.cancel_alarm()
    sim.op_state = Turing_Machine.TIME_OUT

  stats.num_rules += len(sim.prover.rules)
  stats.num_recursive_rules += sim.prover.num_recursive_rules
  stats.num_collatz_rules += sim.prover.num_collatz_rules
  stats.num_failed_proofs += sim.prover.num_failed_proofs

  if progress:
    pprint(stats.__dict__)

  if sim.op_state == Turing_Machine.RUNNING:
    if progress:
      print "\tMax_Steps", block_size, sim.step_num, sim.num_loops
    # TODO(shawn): Return time taken.
    return Exit_Condition.UNKNOWN, "Max_Steps", sim.get_nonzeros(), sim.step_num

  elif sim.op_state == Turing_Machine.TIME_OUT:
    if progress:
      print "\tTimeout", block_size, sim.step_num, sim.num_loops
    return Exit_Condition.UNKNOWN, "Time_Out", sim.get_nonzeros(), sim.step_num

  elif sim.op_state == Turing_Machine.INF_REPEAT:
    if progress:
      global max_step2inf, max_loop2inf
      max_step2inf = max(max_step2inf, sim.step_num)
      max_loop2inf = max(max_loop2inf, sim.num_loops)
      print "\tInfinite", block_size, (sim.step_num, max_step2inf),
      print (sim.num_loops, max_loop2inf)
    return (Exit_Condition.INFINITE, "Macro_Simulator",
            block_size, len(sim.prover.rules), sim.prover.num_recursive_rules,
            sim.step_num, sim.num_loops)

  elif sim.op_state == Turing_Machine.HALT:
    if progress:
      print "\tHalted", sim.get_nonzeros(), sim.step_num
    return (Exit_Condition.HALT, sim.get_nonzeros(), sim.step_num,
            "Macro_Simulator", block_size, len(sim.prover.rules),
            sim.prover.num_recursive_rules, sim.num_loops)

  elif sim.op_state == Turing_Machine.UNDEFINED:
    if progress:
      print "\tUndefined", sim.get_nonzeros(), sim.step_num
    # sim.op_details[0][0 & 1] stores the symbol and state that we halted on.
    return (Exit_Condition.UNDEF_CELL,
            int(sim.op_details[0][1]), int(sim.op_details[0][0]),
            sim.get_nonzeros(), sim.step_num, "Macro_Simulator", block_size,
            len(sim.prover.rules), sim.prover.num_recursive_rules, sim.num_loops)

  else:
    raise Exception, "unexpected op_state"
コード例 #6
0
def run_options(ttable, options, stats=None):
    """Run the Accelerated Turing Machine Simulator, running a few simple filters
  first and using intelligent blockfinding."""
    if options.steps == 0:
        options.steps = INF

    if options.time:
        # Note: We cannot practically use time.clock() because it often
        # only has 10ms granularity.
        start_time = time.time()
        pre_sim_end_time = start_time + (options.time / 10)
        end_time = start_time + options.time
    else:
        start_time = end_time = None

    ## Test for quickly for infinite machine
    if Reverse_Engineer_Filter.test(ttable):
        return Exit_Condition.INFINITE, ("Reverse_Engineer", )

    ## Construct the Macro Turing Machine (Backsymbol-k-Block-Macro-Machine)
    m = Turing_Machine.make_machine(ttable)

    block_size = options.block_size
    if not block_size:
        # If no explicit block-size given, use inteligent software to find one
        block_size = Block_Finder.block_finder(m,
                                               options,
                                               end_time=pre_sim_end_time)

    # Do not create a 1-Block Macro-Machine (just use base machine)
    if block_size != 1:
        m = Turing_Machine.Block_Macro_Machine(m, block_size)
    if options.backsymbol:
        m = Turing_Machine.Backsymbol_Macro_Machine(m)

    if options.ctl:
        CTL_config = setup_CTL(m, options.bf_limit1, end_time=pre_sim_end_time)

        # Run CTL filters unless machine halted
        if CTL_config:
            CTL_config_copy = copy.deepcopy(CTL_config)
            if CTL1.CTL(m, CTL_config_copy, end_time=pre_sim_end_time):
                return Exit_Condition.INFINITE, ("CTL_A*", )

            CTL_config_copy = copy.deepcopy(CTL_config)
            if CTL2.CTL(m, CTL_config_copy, end_time=pre_sim_end_time):
                return Exit_Condition.INFINITE, ("CTL_A*_B", )

    ## Set up the simulator
    sim = Simulator.Simulator(m, options, end_time=end_time)

    ## Run the simulator
    while (sim.step_num < options.steps
           and sim.op_state == Turing_Machine.RUNNING
           and sim.tape.compressed_size() <= options.tape_limit):
        sim.step()
        if sim.op_state == Turing_Machine.TIME_OUT and sim.step_num == 0:
            sim.op_state = Turing_Machine.RUNNING
            sim.end_time += options.time

    # TODO(shawn): pass the stats object into sim so we don't have to copy.
    if stats:
        stats.num_rules += len(sim.prover.rules)
        stats.num_recursive_rules += sim.prover.num_recursive_rules
        stats.num_collatz_rules += sim.prover.num_collatz_rules
        stats.num_failed_proofs += sim.prover.num_failed_proofs

    ## Resolve end conditions and return relevent info.
    if sim.tape.compressed_size() > options.tape_limit:
        return Exit_Condition.OVER_TAPE, (sim.tape.compressed_size(),
                                          sim.step_num)

    elif sim.op_state == Turing_Machine.TIME_OUT:
        return Exit_Condition.TIME_OUT, (time.time() - start_time,
                                         sim.step_num)

    elif sim.op_state == Turing_Machine.RUNNING:
        return Exit_Condition.MAX_STEPS, (sim.step_num, )

    elif sim.op_state == Turing_Machine.HALT:
        return Exit_Condition.HALT, (sim.step_num, sim.get_nonzeros())

    elif sim.op_state == Turing_Machine.INF_REPEAT:
        return Exit_Condition.INFINITE, (sim.inf_reason, )

    elif sim.op_state == Turing_Machine.UNDEFINED:
        on_symbol, on_state = sim.op_details[0][:2]
        return Exit_Condition.UNDEF_CELL, (on_state, on_symbol, sim.step_num,
                                           sim.get_nonzeros())

    raise Exception, (sim.op_state, ttable, sim)