-
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathale_python_interface.hpp
More file actions
196 lines (175 loc) · 8.56 KB
/
Copy pathale_python_interface.hpp
File metadata and controls
196 lines (175 loc) · 8.56 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* *****************************************************************************
* A.L.E (Arcade Learning Environment)
* Copyright (c) 2009-2013 by Yavar Naddaf, Joel Veness, Marc G. Bellemare and
* the Reinforcement Learning and Artificial Intelligence Laboratory
* Released under the GNU General Public License; see License.txt for details.
*
* Based on: Stella -- "An Atari 2600 VCS Emulator"
* Copyright (c) 1995-2007 by Bradford W. Mott and the Stella team
*
* *****************************************************************************
* ale_python_interface.hpp
*
* Bindings for the ALE Python Interface.
*
**************************************************************************** */
#ifndef __ALE_PYTHON_INTERFACE_HPP__
#define __ALE_PYTHON_INTERFACE_HPP__
#include <sstream>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "ale_interface.hpp"
#include "version.hpp"
namespace py = pybind11;
using namespace py::literals;
namespace ale {
class ALEPythonInterface : public ALEInterface {
public:
using ALEInterface::ALEInterface;
void getScreen(py::array_t<pixel_t, py::array::c_style>& buffer);
void getScreenRGB(py::array_t<pixel_t, py::array::c_style>& buffer);
void getScreenGrayscale(py::array_t<pixel_t, py::array::c_style>& buffer);
py::array_t<pixel_t, py::array::c_style> getScreen();
py::array_t<pixel_t, py::array::c_style> getScreenRGB();
py::array_t<pixel_t, py::array::c_style> getScreenGrayscale();
inline py::tuple getScreenDims() {
const ALEScreen& screen = ALEInterface::getScreen();
return py::make_tuple(screen.height(), screen.width());
}
inline uint32_t getRAMSize() { return ALEInterface::getRAM().size(); }
const py::array_t<uint8_t, py::array::c_style> getRAM();
void getRAM(py::array_t<uint8_t, py::array::c_style>& buffer);
};
} // namespace ale
inline std::vector<ale::Action> convert(std::vector<uint32_t> a){
std::vector<ale::Action> v(a.size());
for (size_t i = 0; i < a.size(); i++) {
v[i] = (ale::Action)(a[i]);
}
return v;
}
PYBIND11_MODULE(ale_py, m) {
m.attr("__version__") = py::str(ALE_VERSION_STR);
#ifdef __USE_SDL
m.attr("SDL") = py::bool_(true);
#else
m.attr("SDL") = py::bool_(false);
#endif
py::enum_<ale::Action>(m, "Action")
.value("NOOP", ale::PLAYER_A_NOOP)
.value("FIRE", ale::PLAYER_A_FIRE)
.value("UP", ale::PLAYER_A_UP)
.value("RIGHT", ale::PLAYER_A_RIGHT)
.value("LEFT", ale::PLAYER_A_LEFT)
.value("DOWN", ale::PLAYER_A_DOWN)
.value("UPRIGHT", ale::PLAYER_A_UPRIGHT)
.value("UPLEFT", ale::PLAYER_A_UPLEFT)
.value("DOWNRIGHT", ale::PLAYER_A_DOWNRIGHT)
.value("DOWNLEFT", ale::PLAYER_A_DOWNLEFT)
.value("UPFIRE", ale::PLAYER_A_UPFIRE)
.value("RIGHTFIRE", ale::PLAYER_A_RIGHTFIRE)
.value("LEFTFIRE", ale::PLAYER_A_LEFTFIRE)
.value("DOWNFIRE", ale::PLAYER_A_DOWNFIRE)
.value("UPRIGHTFIRE", ale::PLAYER_A_UPRIGHTFIRE)
.value("UPLEFTFIRE", ale::PLAYER_A_UPLEFTFIRE)
.value("DOWNRIGHTFIRE", ale::PLAYER_A_DOWNRIGHTFIRE)
.value("DOWNLEFTFIRE", ale::PLAYER_A_DOWNLEFTFIRE)
.export_values();
py::enum_<ale::Logger::mode>(m, "LoggerMode")
.value("Info", ale::Logger::mode::Info)
.value("Warning", ale::Logger::mode::Warning)
.value("Error", ale::Logger::mode::Error)
.export_values();
py::class_<ale::ALEState>(m, "ALEState")
.def(py::init<>())
.def(py::init<const ale::ALEState&, const std::string&>())
.def(py::init<const std::string&>())
.def("equals", &ale::ALEState::equals)
.def("getFrameNumber", &ale::ALEState::getFrameNumber)
.def("getEpisodeFrameNumber", &ale::ALEState::getEpisodeFrameNumber)
.def("getDifficulty", &ale::ALEState::getDifficulty)
.def("getCurrentMode", &ale::ALEState::getCurrentMode)
.def("serialize", &ale::ALEState::serialize)
.def("__eq__", &ale::ALEState::equals)
.def(py::pickle(
[](ale::ALEState& a) {
return py::make_tuple(py::bytes(a.serialize()));
},
[](py::tuple t) {
if (t.size() != 1)
throw std::runtime_error("Invalid ALEState state...");
ale::ALEState state(t[0].cast<std::string>());
return state;
}));
py::class_<ale::ALEPythonInterface>(m, "ALEInterface")
.def(py::init<>())
.def("getString", &ale::ALEPythonInterface::getString)
.def("getInt", &ale::ALEPythonInterface::getInt)
.def("getBool", &ale::ALEPythonInterface::getBool)
.def("getFloat", &ale::ALEPythonInterface::getFloat)
.def("setString", &ale::ALEPythonInterface::setString)
.def("setInt", &ale::ALEPythonInterface::setInt)
.def("setBool", &ale::ALEPythonInterface::setBool)
.def("setFloat", &ale::ALEPythonInterface::setFloat)
.def("loadROM", &ale::ALEPythonInterface::loadROM)
.def_static("isSupportedRom", &ale::ALEPythonInterface::isSupportedRom)
.def("act", [](ale::ALEPythonInterface & ale, uint32_t a){ return ale.act((ale::Action)(a)); })
.def("act", [](ale::ALEPythonInterface & ale, ale::Action a){ return ale.act(a); })
.def("act", [](ale::ALEPythonInterface & ale, std::vector<ale::Action> a){ return ale.act(a); })
.def("act", [](ale::ALEPythonInterface & ale, std::vector<uint32_t> a){ return ale.act(convert(a)); })
.def("game_over", &ale::ALEPythonInterface::game_over)
.def("reset_game", &ale::ALEPythonInterface::reset_game)
.def("numPlayersActive", &ale::ALEPythonInterface::numPlayersActive)
.def("getAvailableModes", &ale::ALEPythonInterface::getAvailableModes)
.def("getAvailableModes", [](ale::ALEPythonInterface & ale){ return ale.getAvailableModes(); })
.def("setMode", &ale::ALEPythonInterface::setMode)
.def("getAvailableDifficulties",
&ale::ALEPythonInterface::getAvailableDifficulties)
.def("setDifficulty", &ale::ALEPythonInterface::setDifficulty)
.def("getLegalActionSet", &ale::ALEPythonInterface::getLegalActionSet)
.def("getMinimalActionSet", &ale::ALEPythonInterface::getMinimalActionSet)
.def("getFrameNumber", &ale::ALEPythonInterface::getFrameNumber)
.def("lives", &ale::ALEPythonInterface::lives)
.def("allLives", &ale::ALEPythonInterface::allLives)
.def("getEpisodeFrameNumber",
&ale::ALEPythonInterface::getEpisodeFrameNumber)
.def("getScreen", (void (ale::ALEPythonInterface::*)(
py::array_t<ale::pixel_t, py::array::c_style>&)) &
ale::ALEPythonInterface::getScreen)
.def("getScreen", (py::array_t<ale::pixel_t, py::array::c_style>(
ale::ALEPythonInterface::*)()) &
ale::ALEPythonInterface::getScreen)
.def("getScreenRGB",
(void (ale::ALEPythonInterface::*)(
py::array_t<ale::pixel_t, py::array::c_style>&)) &
ale::ALEPythonInterface::getScreenRGB)
.def("getScreenRGB", (py::array_t<ale::pixel_t, py::array::c_style>(
ale::ALEPythonInterface::*)()) &
ale::ALEPythonInterface::getScreenRGB)
.def("getScreenGrayscale",
(void (ale::ALEPythonInterface::*)(
py::array_t<ale::pixel_t, py::array::c_style>&)) &
ale::ALEPythonInterface::getScreenGrayscale)
.def("getScreenGrayscale",
(py::array_t<ale::pixel_t, py::array::c_style>(
ale::ALEPythonInterface::*)()) &
ale::ALEPythonInterface::getScreenGrayscale)
.def("getScreenDims", &ale::ALEPythonInterface::getScreenDims)
.def("getRAMSize", &ale::ALEPythonInterface::getRAMSize)
.def("getRAM", (const py::array_t<uint8_t, py::array::c_style> (
ale::ALEPythonInterface::*)()) &
ale::ALEPythonInterface::getRAM)
.def("getRAM", (void (ale::ALEPythonInterface::*)(
py::array_t<uint8_t, py::array::c_style>&)) &
ale::ALEPythonInterface::getRAM)
.def("saveState", &ale::ALEPythonInterface::saveState)
.def("loadState", &ale::ALEPythonInterface::loadState)
.def("cloneState", &ale::ALEPythonInterface::cloneState)
.def("restoreState", &ale::ALEPythonInterface::restoreState)
.def("cloneSystemState", &ale::ALEPythonInterface::cloneSystemState)
.def("restoreSystemState", &ale::ALEPythonInterface::restoreSystemState)
.def("saveScreenPNG", &ale::ALEPythonInterface::saveScreenPNG)
.def_static("setLoggerMode", &ale::Logger::setMode);
}
#endif // __ALE_PYTHON_INTERFACE_HPP__