def visualize_mcts(root: mzcore.Node, history: Sequence[HistoryEntry],
                   grammar: state_tree.NQCFG, min_visit_count: int) -> str:
  """Visualize the MCTS corresponding to `root` and `history`.

  The returned string can be visualized using the `forest` LaTex package
  (https://ctan.org/pkg/forest?lang=en).

  Args:
    root:  Root node for the Monte-Carlo Tree search.
    history:  History from the Agent's state.
    grammar:  Underlying grammar.
    min_visit_count:  Only visualize a node if it got visited at least that many
      times.  If set to 0, all children - even unexplored - will be visualized,
      which might become unwieldy.  Setting it to 1 will visualize all paths
      actually taken.

  Returns:
    A `forest`-package compatible string representing the Monte-Carlo Tree
    search tree.
  """

  edge_str_template = (
      ', edge '
      'label={{node[midway,fill=white,font=\\scriptsize,align=right]{{\\textcolor{{red}}{{{}}}'
      ' \\tiny{{\\textcolor{{blue}}{{{:.2f}}}}} \\\\\\ '
      '\\tiny{{\\textcolor{{magenta}}{{{:.2f}}}}}}}}}')

  def to_latex_tree(node: VisNode) -> str:
    edge_str = edge_str_template.format(
        node.visit_count, node.reward,
        node.qvalue) if node.visit_count > 0 else ''
    label = '{} : {}'.format(
        ' '.join(
            [str(nonterminal).replace('_', '') for nonterminal in node.stack]),
        ' '.join([
            str(leaf).replace('[', '{[').replace(']', ']}')
            for leaf in node.leaves
        ]))
    if not node.children or all([
        child.visit_count < min_visit_count for child in node.children.values()
    ]) or not node.stack:
      return '[{}{} ]'.format(label, edge_str)
    children_strs = []
    for child in node.children.values():
      if child.visit_count >= min_visit_count:
        children_strs.append(to_latex_tree(child))
    latex_str = '[{}{} {} ]'.format(label, edge_str,
                                    ' '.join(children_strs)).replace(
                                        '##', '@@')
    return latex_str

  root = VisNode.from_root_with_history(root, history, grammar)
  tree_str = to_latex_tree(root)

  return tree_str
Visualize the MCTS corresponding to `root` and `history`.

  The returned string can be visualized using the `forest` LaTex package
  (https://ctan.org/pkg/forest?lang=en).

  Args:
    root:  Root node for the Monte-Carlo Tree search.
    history:  History from the Agent's state.
    grammar:  Underlying grammar.
    min_visit_count:  Only visualize a node if it got visited at least that many
      times.  If set to 0, all children - even unexplored - will be visualized,
      which might become unwieldy.  Setting it to 1 will visualize all paths
      actually taken.

  Returns:
    A `forest`-package compatible string representing the Monte-Carlo Tree
    search tree.
  
language/search_agents/muzero/types.py