original_docstring: Do a breadth-first search of all simulator states.
docstring: Do a breadth-first search of all simulator states.
docstring_tokens: Do a breadth - first search of all simulator states .
code: def explicate_graph(simulator, verbose=True):
      """Do a breadth-first search of all simulator states."""
      # set up data structures
      children_of_state = {}
      to_expand = queue.Queue()
  
      # add the initial state
      init_state, _ = reset_sim(simulator)
      to_expand.put(init_state)
      children_of_state[init_state] = []
  
      # do BFS
      while not to_expand.empty():
          # fetch next node and expand
          new_node = to_expand.get_nowait()
          for action, child in enumerate_children(simulator, new_node):
              if child in children_of_state:
                  # either we've expanded it or we're going to expand it
                  continue
              to_expand.put(child)
              children_of_state[child] = []
              children_of_state[new_node].append((action, child))
  
          # debug print
          if verbose:
              # tells us whenever we advance by ~1/10th of the current number of
              # expansions (i.e. will tell us about expansions 10, 20, 30, …,
              # 100, 200, 300, …, 1000, 1100, 1200, …)
              n = len(children_of_state)
              n_log10 = int(math.log(n) / math.log(10))
              debug_print = n > 10 and n % int(10**n_log10) == 0
              if debug_print:
                  print(f'Have seen {len(children_of_state)} states, of which '
                        f'{to_expand.qsize()} need to be expanded')
  
      # more debug prints
      if verbose:
          print(f'Search finished. Saw {len(children_of_state)} states.')
  
      with open('states.json.tmp', 'w') as fp:
          fp.write('[\n')
          state_tups = sorted(
              dataclasses.astuple(state)
              for state in children_of_state.keys())
          is_first = True
          for state_tup in state_tups:
              json_tup = json.dumps(state_tup)
              if not is_first:
                  fp.write(',\n')
              fp.write('  ')
              fp.write(json_tup)
              is_first = False
          fp.write('\n]')
          try:
              # unlink in case states.json exists already
              os.unlink('states.json')
          except FileNotFoundError:
              pass
          os.rename('states.json.tmp', 'states.json')
  
      return children_of_state
code_tokens: def explicate_graph ( simulator , verbose = True ) : children_of_state = { } to_expand = queue . Queue ( ) init_state , _ = reset_sim ( simulator ) to_expand . put ( init_state ) children_of_state [ init_state ] = [ ] while not to_expand . empty ( ) : new_node = to_expand . get_nowait ( ) for action , child in enumerate_children ( simulator , new_node ) : if child in children_of_state : continue to_expand . put ( child ) children_of_state [ child ] = [ ] children_of_state [ new_node ] . append ( ( action , child ) ) if verbose : n = len ( children_of_state ) n_log10 = int ( math . log ( n ) / math . log ( 10 ) ) debug_print = n > 10 and n % int ( 10 ** n_log10 ) == 0 if debug_print : print ( f'Have seen {len(children_of_state)} states, of which ' f'{to_expand.qsize()} need to be expanded' ) if verbose : print ( f'Search finished. Saw {len(children_of_state)} states.' ) with open ( 'states.json.tmp' , 'w' ) as fp : fp . write ( '[\n' ) state_tups = sorted ( dataclasses . astuple ( state ) for state in children_of_state . keys ( ) ) is_first = True for state_tup in state_tups : json_tup = json . dumps ( state_tup ) if not is_first : fp . write ( ',\n' ) fp . write ( '  ' ) fp . write ( json_tup ) is_first = False fp . write ( '\n]' ) try : os . unlink ( 'states.json' ) except FileNotFoundError : pass os . rename ( 'states.json.tmp' , 'states.json' ) return children_of_state
short_docstring: Do a breadth-first search of all simulator states.
short_docstring_tokens: Do a breadth - first search of all simulator states .
comment: """Do a breadth-first search of all simulator states."""$SEP$# set up data structures$SEP$# add the initial state$SEP$# do BFS$SEP$# fetch next node and expand$SEP$# either we've expanded it or we're going to expand it$SEP$# debug print$SEP$# tells us whenever we advance by ~1/10th of the current number of$SEP$# expansions (i.e. will tell us about expansions 10, 20, 30, …,$SEP$# 100, 200, 300, …, 1000, 1100, 1200, …)$SEP$# more debug prints$SEP$# unlink in case states.json exists already