<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.builtins import object, next, range

import logging
import logging.config
import pkgutil
import random
import time
import types
from math import log, sqrt


__version__ = pkgutil.get_data(__name__, "VERSION").decode("utf-8").strip()
INF = float("inf")
logger = logging.getLogger(__name__)
debug = logger.debug
info = logger.info
warn = logger.warn


def run(root, time_limit=INF, iter_limit=INF, pruning=None,
        rng_seed=None, rng_state=None, log_iter_interval=1000):
    """
    Monte Carlo Tree Search for **minimization** problems.

    Note:
        Objective functions (and bounds) for maximization problems must be multiplied by -1.

    Arguments:
        root (TreeNode): the root of the search tree.
        time_limit (float): maximum CPU time allowed.
        iter_limit (int): maximum number of iterations.
        pruning (bool or None): make the search use/not use pruning if true/false. If `None` is
            given (default), auto-detects pruning settings from root node.
        rng_seed: an object to pass to `random.seed()`. Does not seed the RNG if no value is given.
        rng_state: an RNG state tuple, as obtained from `random.getstate()`. Can be used to set a
            particular RNG state at the start of the search.
        log_iter_interval (int): interval, in number of iterations, between automatic log messages.

    Returns:
        `Solutions` object containing the best solution found by the search, as well as the list
        of incumbent solutions during the search.
    """
    if pruning is None:
        # Guess pruning by comparing the root node's bound() method with the bound() method from
        # the base TreeNode class.
        original_bound_meth = types.MethodType(TreeNode.bound, root)
        pruning = root.bound != original_bound_meth
    if rng_seed is not None:
        info("Seeding RNG with {}...".format(rng_seed))
        random.seed(rng_seed)
    if rng_state is not None:
        random.setstate(rng_state)
    info("Pruning is {}.".format("enabled" if pruning else "disabled"))

    t0 = time.clock()  # initial cpu time
    sols = Solutions()  # object used to keep track of our best/worst solutions
    for sol in root.simulations():  # run simulations from root and
        root.backpropagate(sol)  # backpropagate the solutions
        sols.update(sol)
    t = time.clock() - t0  # cpu time elapsed
    i = 0  # iteration count

    try:<commit_msg>Change to simpler way to autodetect pruning<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.builtins import object, next, range

import logging
import logging.config
import pkgutil
import random
import time
from math import log, sqrt


__version__ = pkgutil.get_data(__name__, "VERSION").decode("utf-8").strip()
INF = float("inf")
logger = logging.getLogger(__name__)
debug = logger.debug
info = logger.info
warn = logger.warn


def run(root, time_limit=INF, iter_limit=INF, pruning=None,
        rng_seed=None, rng_state=None, log_iter_interval=1000):
    """
    Monte Carlo Tree Search for **minimization** problems.

    Note:
        Objective functions (and bounds) for maximization problems must be multiplied by -1.

    Arguments:
        root (TreeNode): the root of the search tree.
        time_limit (float): maximum CPU time allowed.
        iter_limit (int): maximum number of iterations.
        pruning (bool or None): make the search use/not use pruning if true/false. If `None` is
            given (default), auto-detects pruning settings from root node.
        rng_seed: an object to pass to `random.seed()`. Does not seed the RNG if no value is given.
        rng_state: an RNG state tuple, as obtained from `random.getstate()`. Can be used to set a
            particular RNG state at the start of the search.
        log_iter_interval (int): interval, in number of iterations, between automatic log messages.

    Returns:
        `Solutions` object containing the best solution found by the search, as well as the list
        of incumbent solutions during the search.
    """
    if pruning is None:
        # Guess pruning by comparing the bound() method from the root node's class with the
        # bound() method from the base TreeNode class.
        pruning = type(root).bound != TreeNode.bound
    if rng_seed is not None:
        info("Seeding RNG with {}...".format(rng_seed))
        random.seed(rng_seed)
    if rng_state is not None:
        random.setstate(rng_state)
    info("Pruning is {}.".format("enabled" if pruning else "disabled"))

    t0 = time.clock()  # initial cpu time
    sols = Solutions()  # object used to keep track of our best/worst solutions
    for sol in root.simulations():  # run simulations from root and
        root.backpropagate(sol)  # backpropagate the solutions
        sols.update(sol)
    t = time.clock() - t0  # cpu time elapsed
    i = 0  # iteration count

    try:
{'fraction_non_alphanumeric': 0.07985305491105955, 'fraction_numerical': 0.0034802784222737818, 'mean_word_length': 3.155020080321285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '344605', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1426, 'n_words': 583}