forked from ShangtongZhang/DeepRL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
73 lines (61 loc) · 2.31 KB
/
task.py
File metadata and controls
73 lines (61 loc) · 2.31 KB
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
#######################################################################
# Copyright (C) 2017 Shangtong Zhang(zhangshangtong.cpp@gmail.com) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
import gym
import sys
import numpy as np
from atari_wrapper import *
class BasicTask:
def __init__(self):
self.normalized_state = True
def normalize_state(self, state):
return state
def reset(self):
state = self.env.reset()
if self.normalized_state:
return self.normalize_state(state)
return state
def step(self, action):
next_state, reward, done, info = self.env.step(action)
if self.normalized_state:
next_state = self.normalize_state(next_state)
return next_state, np.sign(reward), done, info
class MountainCar(BasicTask):
name = 'MountainCar-v0'
success_threshold = -110
def __init__(self):
BasicTask.__init__(self)
self.env = gym.make(self.name)
self.env._max_episode_steps = sys.maxsize
class CartPole(BasicTask):
name = 'CartPole-v0'
success_threshold = 195
def __init__(self):
BasicTask.__init__(self)
self.env = gym.make(self.name)
class LunarLander(BasicTask):
name = 'LunarLander-v2'
success_threshold = 200
def __init__(self):
BasicTask.__init__(self)
self.env = gym.make(self.name)
class PixelAtari(BasicTask):
def __init__(self, name, no_op, frame_skip, normalized_state=True,
frame_size=84, success_threshold=1000):
BasicTask.__init__(self)
self.normalized_state = normalized_state
self.name = name
self.success_threshold = success_threshold
env = gym.make(name)
assert 'NoFrameskip' in env.spec.id
env = EpisodicLifeEnv(env)
env = NoopResetEnv(env, noop_max=no_op)
env = MaxAndSkipEnv(env, skip=frame_skip)
if 'FIRE' in env.unwrapped.get_action_meanings():
env = FireResetEnv(env)
env = ProcessFrame(env, frame_size)
self.env = ClippedRewardsWrapper(env)
def normalize_state(self, state):
return np.asarray(state, dtype=np.float32) / 255.0