forked from NVlabs/GA3C
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Environment.py
108 lines (91 loc) · 4 KB
/
Environment.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
# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
if sys.version_info >= (3,0):
from queue import Queue
else:
from Queue import Queue
import numpy as np
#import scipy.misc as misc
from Config import Config
from GameManager import GameManager
class Environment:
def __init__(self):
self.game = GameManager(Config.MAP)
self.nb_frames = Config.STACKED_FRAMES
self.frame_q = Queue(maxsize=self.nb_frames)
self.previous_state = None
self.current_state = None
self.total_reward = 0
self.reset()
def is_running(self):
return self.game.is_running()
@staticmethod
def _rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
@staticmethod
def _preprocess(image):
#image = Environment._rgb2gray(image)
#image = misc.imresize(image, [Config.IMAGE_HEIGHT, Config.IMAGE_WIDTH], 'bilinear')
image = image.astype(np.float32) / 255.
return image
def _get_current_state_no_stacking(self):
if not self.frame_q.full():
return None # frame queue is not full yet.
return np.array(list(self.frame_q.queue)[0])
def _get_current_state(self):
if not self.frame_q.full():
return None # frame queue is not full yet.
x_ = [np.array(i) for i in list(self.frame_q.queue)]
x_ = np.concatenate(x_, axis=2)
#x_ = np.array(self.frame_q.queue)
#x_ = np.transpose(x_, [1, 2, 3, 0]) # move channels
return x_
def _update_frame_q(self, frame):
if self.frame_q.full():
self.frame_q.get()
self.frame_q.put(frame)
# the state is no longer just the image, but a concatenation of
# image and auxiliary inputs. We can't use the same _preprocess()
#image = Environment._preprocess(frame)
#self.frame_q.put(image)
def get_num_actions(self):
return GameManager.get_num_actions()
def reset(self):
self.total_reward = 0
self.frame_q.queue.clear()
self.game.reset()
self._update_frame_q(self.game.get_state())
self.previous_state = self.current_state = None
def step(self, action):
reward, is_running = self.game.step(action)
self.total_reward += reward
self.previous_state = self.current_state
if is_running:
observation = self.game.get_state()
self._update_frame_q(observation)
self.current_state = self._get_current_state_no_stacking()
return reward, is_running