<commit_before># -*- coding:utf8 -*-
# File   : base.py
# Author : <NAME>
# Email  : <EMAIL>
# Date   : 3/18/17
# 
# This file is part of TensorArtist

from tartist import random
from tartist.core.utils.cache import cached_property
import numpy as np
import collections

__all__ = [
    'RLEnvironBase', 'SimpleRLEnvironBase', 'ProxyRLEnvironBase',
    'ActionSpaceBase', 'DiscreteActionSpace', 'ContinuousActionSpace'
]


class RLEnvironBase(object):
    def __init__(self):
        self._stats = collections.defaultdict(list)

    @property
    def stats(self):
        return self._stats

    def append_stat(self, name, value):
        self._stats[name].append(value)
        return self

    def clear_stats(self):
        self._stats = collections.defaultdict(list)
        return self

    @cached_property
    def action_space(self):
        return self._get_action_space()

    @property
    def current_state(self):
        return self._get_current_state()

    def action(self, action):
        return self._action(action)

    def restart(self):
        return self._restart()

    def finish(self):
        return self._finish()

    def play_one_episode(self, func, ret_states=False):
        states = []

        self.restart()
        while True:
            state = self.current_state
            action = func(state)
            r, is_over = self.action(action)
            if ret_states:
                states.append(state)
            if is_over:
                self.finish()
                break

        if ret_states:
            states.append(self.current_state)
            return states

    def _get_action_space(self):
        return None

    def _get_current_state(self):
        return None

    def _action(self, action):
        raise NotImplementedError()

    def _restart(self):
        raise NotImplementedError()

    def _finish(self):
        pass


class SimpleRLEnvironBase(RLEnvironBase):
    _current_state = None

    def __init__(self):
        super().__init__()
        self._reward_history = []

    def _get_current_state(self):
        return self._current_state

    def _set_current_state(self, state):
        self._current_state = state

    def action(self, action):
        r, is_over = self._action(action)
        self._reward_history.append(r) 
        return r, is_over

    def restart(self):
        rc = self._restart()
        self._reward_history = []
        return rc

    def finish(self):
        rc = self._finish()
        self.append_stat('score', sum(self._reward_history))
        return rc


class ProxyRLEnvironBase(RLEnvironBase):
    def __init__(self, other):
        super().__init__()
        self.__proxy = other

    @property
    def proxy(self):
        return self.__proxy

    @property
    def stats(self):
        return self.__proxy.stats

    def append_stat(self, name, value):
        self.__proxy.append_stat(name)
        return self

    def clear_stats(self):
        self.__proxy.clear_stats()
        return self

    @property
    def action_space(self):
        return self.__proxy.action_space

    # directly override the action_space to disable cache
    # def _get_action_space(self):
    #     return self.__proxy.action_space

    def _get_current_state(self):
        return self.__proxy.current_state

    def _action(self, action):
        return self.__proxy.action(action)

    def _restart(self):
        return self.__proxy.restart()

    def _finish(self):
        return self.__proxy.finish()


class ActionSpaceBase(object):
    def __init__(self, action_meanings=None):
        self.__rng = random.gen_rng()
        self._action_meanings = action_meanings

    @property
    def rng(self):
        return self.__rng

    @property
    def action_meanings(self):
        return self._action_meanings

    def sample(self, theta=None):
        return self._sample(theta)

    def _sample(self, theta=None):
        return None
<commit_msg>[dev-mjy] add rl.env.unwrapped<commit_after># -*- coding:utf8 -*-
# File   : base.py
# Author : <NAME>
# Email  : <EMAIL>
# Date   : 3/18/17
# 
# This file is part of TensorArtist

from tartist import random
from tartist.core.utils.cache import cached_property
import numpy as np
import collections

__all__ = [
    'RLEnvironBase', 'SimpleRLEnvironBase', 'ProxyRLEnvironBase',
    'ActionSpaceBase', 'DiscreteActionSpace', 'ContinuousActionSpace'
]


class RLEnvironBase(object):
    def __init__(self):
        self._stats = collections.defaultdict(list)

    @property
    def stats(self):
        return self._stats

    def append_stat(self, name, value):
        self._stats[name].append(value)
        return self

    def clear_stats(self):
        self._stats = collections.defaultdict(list)
        return self

    @cached_property
    def action_space(self):
        return self._get_action_space()

    @property
    def current_state(self):
        return self._get_current_state()

    def action(self, action):
        return self._action(action)

    def restart(self):
        return self._restart()

    def finish(self):
        return self._finish()

    def play_one_episode(self, func, ret_states=False):
        states = []

        self.restart()
        while True:
            state = self.current_state
            action = func(state)
            r, is_over = self.action(action)
            if ret_states:
                states.append(state)
            if is_over:
                self.finish()
                break

        if ret_states:
            states.append(self.current_state)
            return states

    def _get_action_space(self):
        return None

    def _get_current_state(self):
        return None

    def _action(self, action):
        raise NotImplementedError()

    def _restart(self):
        raise NotImplementedError()

    def _finish(self):
        pass

    @property
    def unwrapped(self):
        return self


class SimpleRLEnvironBase(RLEnvironBase):
    _current_state = None

    def __init__(self):
        super().__init__()
        self._reward_history = []

    def _get_current_state(self):
        return self._current_state

    def _set_current_state(self, state):
        self._current_state = state

    def action(self, action):
        r, is_over = self._action(action)
        self._reward_history.append(r) 
        return r, is_over

    def restart(self):
        rc = self._restart()
        self._reward_history = []
        return rc

    def finish(self):
        rc = self._finish()
        self.append_stat('score', sum(self._reward_history))
        return rc


class ProxyRLEnvironBase(RLEnvironBase):
    def __init__(self, other):
        super().__init__()
        self.__proxy = other

    @property
    def proxy(self):
        return self.__proxy

    @property
    def stats(self):
        return self.__proxy.stats

    def append_stat(self, name, value):
        self.__proxy.append_stat(name)
        return self

    def clear_stats(self):
        self.__proxy.clear_stats()
        return self

    @property
    def action_space(self):
        return self.__proxy.action_space

    # directly override the action_space to disable cache
    # def _get_action_space(self):
    #     return self.__proxy.action_space

    def _get_current_state(self):
        return self.__proxy.current_state

    def _action(self, action):
        return self.__proxy.action(action)

    def _restart(self):
        return self.__proxy.restart()

    def _finish(self):
        return self.__proxy.finish()

    @property
    def unwrapped(self):
        return self.proxy.unwrapped


class ActionSpaceBase(object):
    def __init__(self, action_meanings=None):
        self.__rng = random.gen_rng()
        self._action_meanings = action_meanings

    @property
    def rng(self):
        return self.__rng

    @property
    def action_meanings(self):
        return self._action_meanings

    def sample(self, theta=None):
        return self._sample(theta)

    def _sample(self, theta=None):
        return None

{'fraction_non_alphanumeric': 0.09156238384338991, 'fraction_numerical': 0.0014868046090942882, 'mean_word_length': 2.360532889258951, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2189621', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2349, 'n_words': 542}