# Path: difflogic/envs/algorithmic/sort_envs.py
# class ListSortingEnv(SimpleRLEnvBase):
#   """Environment for sorting a random permutation.
# 
#   Args:
#     nr_numbers: The number of numbers in the array.
#   """
# 
#   def __init__(self, nr_numbers):
#     super().__init__()
#     self._nr_numbers = nr_numbers
#     self._array = None
# 
#   @notnone_property
#   def array(self):
#     return self._array
# 
#   @property
#   def nr_numbers(self):
#     return self._nr_numbers
# 
#   def get_state(self):
#     """Compute the state given the array."""
#     x, y = np.meshgrid(self.array, self.array)
#     number_relations = np.stack([x < y, x == y, x > y], axis=-1).astype('float')
#     index = np.array(list(range(self._nr_numbers)))
#     x, y = np.meshgrid(index, index)
#     position_relations = np.stack([x < y, x == y, x > y],
#                                   axis=-1).astype('float')
#     return np.concatenate([number_relations, position_relations], axis=-1)
# 
#   def _calculate_optimal(self):
#     """Calculate the optimal number of steps for sorting the array."""
#     a = self._array
#     b = [0 for i in range(len(a))]
#     cnt = 0
#     for i, x in enumerate(a):
#       if b[i] == 0:
#         j = x
#         b[i] = 1
#         while b[j] == 0:
#           b[j] = 1
#           j = a[j]
#         assert i == j
#         cnt += 1
#     return len(a) - cnt
# 
#   def _restart(self):
#     """Restart: Generate a random permutation."""
#     self._array = random.permutation(self._nr_numbers)
#     self._set_current_state(self.get_state())
#     self.optimal = self._calculate_optimal()
# 
#   def _action(self, action):
#     """action is a tuple (i, j), perform this action leads to the swap."""
#     a = self._array
#     i, j = action
#     x, y = a[i], a[j]
#     a[i], a[j] = y, x
#     self._set_current_state(self.get_state())
#     for i in range(self._nr_numbers):
#       if a[i] != i:
#         return 0, False
#     return 1, True
# 
# Path: difflogic/envs/utils.py
# class MapActionProxy(ProxyRLEnvBase):
#   def __init__(self, other, mapping):
#   def mapping(self):
#   def map_action(self, action):
#   def _get_action_space(self):
#   def _action(self, action):
# def get_action_mapping(n, exclude_self=True):
# def get_action_mapping_blocksworld(nr_blocks, exclude_self=True):
# 
# Path: difflogic/envs/utils.py
# class MapActionProxy(ProxyRLEnvBase):
#   """RL Env proxy to map actions using provided mapping function."""
# 
#   def __init__(self, other, mapping):
#     super().__init__(other)
#     self._mapping = mapping
# 
#   @property
#   def mapping(self):
#     return self._mapping
# 
#   def map_action(self, action):
#     assert action < len(self._mapping)
#     return self._mapping[action]
# 
#   def _get_action_space(self):
#     return DiscreteActionSpace(len(self._mapping))
# 
#   def _action(self, action):
#     return self.proxy.action(self.map_action(action))


from jaclearn.rl.proxy import LimitLengthProxy
from .sort_envs import ListSortingEnv
from ..utils import get_action_mapping_sorting
from ..utils import MapActionProxy
#! /usr/bin/env python3
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Quick access for algorithmic environments."""



__all__ = ['get_sort_env', 'make']


def get_sort_env(n, exclude_self=True):
  env_cls = ListSortingEnv
  p = env_cls(n)
  p = LimitLengthProxy(p, n * 2)
  mapping = get_action_mapping_sorting(n, exclude_self=exclude_self)

# Path: difflogic/envs/algorithmic/sort_envs.py
# class ListSortingEnv(SimpleRLEnvBase):
#   """Environment for sorting a random permutation.
# 
#   Args:
#     nr_numbers: The number of numbers in the array.
#   """
# 
#   def __init__(self, nr_numbers):
#     super().__init__()
#     self._nr_numbers = nr_numbers
#     self._array = None
# 
#   @notnone_property
#   def array(self):
#     return self._array
# 
#   @property
#   def nr_numbers(self):
#     return self._nr_numbers
# 
#   def get_state(self):
#     """Compute the state given the array."""
#     x, y = np.meshgrid(self.array, self.array)
#     number_relations = np.stack([x < y, x == y, x > y], axis=-1).astype('float')
#     index = np.array(list(range(self._nr_numbers)))
#     x, y = np.meshgrid(index, index)
#     position_relations = np.stack([x < y, x == y, x > y],
#                                   axis=-1).astype('float')
#     return np.concatenate([number_relations, position_relations], axis=-1)
# 
#   def _calculate_optimal(self):
#     """Calculate the optimal number of steps for sorting the array."""
#     a = self._array
#     b = [0 for i in range(len(a))]
#     cnt = 0
#     for i, x in enumerate(a):
#       if b[i] == 0:
#         j = x
#         b[i] = 1
#         while b[j] == 0:
#           b[j] = 1
#           j = a[j]
#         assert i == j
#         cnt += 1
#     return len(a) - cnt
# 
#   def _restart(self):
#     """Restart: Generate a random permutation."""
#     self._array = random.permutation(self._nr_numbers)
#     self._set_current_state(self.get_state())
#     self.optimal = self._calculate_optimal()
# 
#   def _action(self, action):
#     """action is a tuple (i, j), perform this action leads to the swap."""
#     a = self._array
#     i, j = action
#     x, y = a[i], a[j]
#     a[i], a[j] = y, x
#     self._set_current_state(self.get_state())
#     for i in range(self._nr_numbers):
#       if a[i] != i:
#         return 0, False
#     return 1, True
# 
# Path: difflogic/envs/utils.py
# class MapActionProxy(ProxyRLEnvBase):
#   def __init__(self, other, mapping):
#   def mapping(self):
#   def map_action(self, action):
#   def _get_action_space(self):
#   def _action(self, action):
# def get_action_mapping(n, exclude_self=True):
# def get_action_mapping_blocksworld(nr_blocks, exclude_self=True):
# 
# Path: difflogic/envs/utils.py
# class MapActionProxy(ProxyRLEnvBase):
#   """RL Env proxy to map actions using provided mapping function."""
# 
#   def __init__(self, other, mapping):
#     super().__init__(other)
#     self._mapping = mapping
# 
#   @property
#   def mapping(self):
#     return self._mapping
# 
#   def map_action(self, action):
#     assert action < len(self._mapping)
#     return self._mapping[action]
# 
#   def _get_action_space(self):
#     return DiscreteActionSpace(len(self._mapping))
# 
#   def _action(self, action):
#     return self.proxy.action(self.map_action(action))


# Path: difflogic/envs/algorithmic/quickaccess.py
from jaclearn.rl.proxy import LimitLengthProxy
from .sort_envs import ListSortingEnv
from ..utils import get_action_mapping_sorting
from ..utils import MapActionProxy

#! /usr/bin/env python3
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Quick access for algorithmic environments."""



__all__ = ['get_sort_env', 'make']


def get_sort_env(n, exclude_self=True):
  env_cls = ListSortingEnv
  p = env_cls(n)
  p = LimitLengthProxy(p, n * 2)
  mapping = get_action_mapping_sorting(n, exclude_self=exclude_self)

  p = MapActionProxy(p, mapping)