This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmemory_chain.py
112 lines (92 loc) · 3.76 KB
/
memory_chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# pylint: disable=g-bad-file-header
# Copyright 2019 DeepMind Technologies Limited. All Rights Reserved.
#
# 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
#
# http://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.
# ============================================================================
"""Simple diagnostic memory challenge.
Observation is given by n+1 pixels: (context, time_to_live).
Context will only be nonzero in the first step, when it will be +1 or -1 iid
by component. All actions take no effect until time_to_live=0, then the agent
must repeat the observations that it saw bit-by-bit.
"""
from typing import Optional
from bsuite.environments import base
import dm_env
from dm_env import specs
import numpy as np
class MemoryChain(base.Environment):
"""Memory Chain environment, implementing the environment API."""
def __init__(self,
memory_length: int,
num_bits: int = 1,
seed: Optional[int] = None):
"""Builds the memory chain environment."""
super(MemoryChain, self).__init__()
self._memory_length = memory_length
self._num_bits = num_bits
self._rng = np.random.RandomState(seed)
# Contextual information per episode
self._timestep = 0
self._context = self._rng.binomial(1, 0.5, num_bits)
self._query = self._rng.randint(num_bits)
# Logging info
self._total_perfect = 0
self._total_regret = 0
self._episode_mistakes = 0
# bsuite experiment length.
self.bsuite_num_episodes = 10_000 # Overridden by experiment load().
def _get_observation(self):
"""Observation of form [time, query, num_bits of context]."""
obs = np.zeros(shape=(1, self._num_bits + 2), dtype=np.float32)
# Show the time, on every step.
obs[0, 0] = 1 - self._timestep / self._memory_length
# Show the query, on the last step
if self._timestep == self._memory_length - 1:
obs[0, 1] = self._query
# Show the context, on the first step
if self._timestep == 0:
obs[0, 2:] = 2 * self._context - 1
return obs
def _step(self, action: int) -> dm_env.TimeStep:
observation = self._get_observation()
self._timestep += 1
if self._timestep - 1 < self._memory_length:
# On all but the last step provide a reward of 0.
return dm_env.transition(reward=0., observation=observation)
if self._timestep - 1 > self._memory_length:
raise RuntimeError('Invalid state.') # We shouldn't get here.
if action == self._context[self._query]:
reward = 1.
self._total_perfect += 1
else:
reward = -1.
self._total_regret += 2.
return dm_env.termination(reward=reward, observation=observation)
def _reset(self) -> dm_env.TimeStep:
self._timestep = 0
self._episode_mistakes = 0
self._context = self._rng.binomial(1, 0.5, self._num_bits)
self._query = self._rng.randint(self._num_bits)
observation = self._get_observation()
return dm_env.restart(observation)
def observation_spec(self):
return specs.Array(
shape=(1, self._num_bits + 2), dtype=np.float32, name='observation')
def action_spec(self):
return specs.DiscreteArray(2, name='action')
def _save(self, observation):
self._raw_observation = (observation * 255).astype(np.uint8)
def bsuite_info(self):
return dict(
total_perfect=self._total_perfect,
total_regret=self._total_regret)