├── License.txt ├── README.md ├── ale_python_interface ├── __init__.py ├── ale_c_wrapper.cpp ├── ale_c_wrapper.h ├── ale_python_interface.py ├── atari_ntsc_rgb_palette.h └── build.sh ├── examples ├── ale_python_test1.py ├── ale_python_test2.py ├── ale_python_test_pygame.py └── ale_python_test_pygame_player.py └── setup.py /License.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | Appendix: How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 19yy 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) 19yy name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Library General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A python interface for the Arcade Learning Environment 2 | 3 | This provides a python library to interface with the arcade learning environment that can be found here: 4 | http://www.arcadelearningenvironment.org/ 5 | 6 | This library hooks into the shared object file for the arcade learning environment and bypasses using the slower FIFO interface. 7 | It is designed to be fast. Example code is provided that demonstrates an agent that can be controlled from the keyboard. 8 | 9 | Installation instructions and usage documentation can be found on the github wiki linked below: 10 | https://github.com/bbitmaster/ale_python_interface/wiki 11 | 12 | This library was written by Ben Goodrich, as part of the Machine Intelligence Lab at The University of Tennessee. 13 | 14 | http://mil.engr.utk.edu/ 15 | -------------------------------------------------------------------------------- /ale_python_interface/__init__.py: -------------------------------------------------------------------------------- 1 | from ale_python_interface import * 2 | -------------------------------------------------------------------------------- /ale_python_interface/ale_c_wrapper.cpp: -------------------------------------------------------------------------------- 1 | //all code is currently in the .h file 2 | #include "ale_c_wrapper.h" 3 | 4 | -------------------------------------------------------------------------------- /ale_python_interface/ale_c_wrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef __ALE_C_WRAPPER_H__ 2 | #define __ALE_C_WRAPPER_H__ 3 | 4 | #include 5 | 6 | extern "C" { 7 | //declares int rgb_palette[256] 8 | #include "atari_ntsc_rgb_palette.h" 9 | ALEInterface *ALE_new(){ 10 | return new ALEInterface();} 11 | 12 | void ALE_del(ALEInterface *ale){delete ale;} 13 | 14 | const char *getString(ALEInterface *ale,const char *key){return ale->getString(key).c_str();} 15 | int getInt(ALEInterface *ale,const char *key){return ale->getInt(key);} 16 | bool getBool(ALEInterface *ale,const char *key){return ale->getBool(key);} 17 | float getFloat(ALEInterface *ale,const char *key){return ale->getFloat(key);} 18 | 19 | void setString(ALEInterface *ale,const char *key,const char *value){ale->set(key,value);} 20 | void setInt(ALEInterface *ale,const char *key,int value){ale->set(key,value);} 21 | void setBool(ALEInterface *ale,const char *key,bool value){ale->set(key,value);} 22 | void setFloat(ALEInterface *ale,const char *key,float value){ale->set(key,value);} 23 | 24 | void loadROM(ALEInterface *ale,const char *rom_file){ale->loadROM(rom_file);} 25 | int getLegalActionSize(ALEInterface *ale){return ale->getLegalActionSet().size();} 26 | 27 | void getLegalActionSet(ALEInterface *ale,int *actions){ 28 | ActionVect action_vect = ale->getLegalActionSet(); 29 | const int sz = (int)ale->getLegalActionSet().size(); 30 | for(int i = 0;i < ale->getLegalActionSet().size();i++){ 31 | actions[i] = action_vect[i]; 32 | } 33 | } 34 | 35 | int getMinimalActionSize(ALEInterface *ale){return ale->getMinimalActionSet().size();} 36 | 37 | void getMinimalActionSet(ALEInterface *ale,int *actions){ 38 | ActionVect action_vect = ale->getMinimalActionSet(); 39 | const int sz = (int)ale->getMinimalActionSet().size(); 40 | for(int i = 0;i < ale->getMinimalActionSet().size();i++){ 41 | actions[i] = action_vect[i]; 42 | } 43 | } 44 | 45 | int getFrameNumber(ALEInterface *ale){return ale->getFrameNumber();} 46 | int getEpisodeFrameNumber(ALEInterface *ale){return ale->getEpisodeFrameNumber();} 47 | 48 | int getScreenWidth(ALEInterface *ale){return ale->getScreen().width();} 49 | int getScreenHeight(ALEInterface *ale){return ale->getScreen().height();} 50 | 51 | int getScreen(ALEInterface *ale,unsigned char *screen_data){ 52 | int w = ale->getScreen().width(); 53 | int h = ale->getScreen().height(); 54 | pixel_t *ale_screen_data = (pixel_t *)ale->getScreen().getArray(); 55 | memcpy(screen_data,ale_screen_data,w*h*sizeof(pixel_t)); 56 | } 57 | 58 | int getScreenRGB(ALEInterface *ale,int *screen_data){ 59 | int w = ale->getScreen().width(); 60 | int h = ale->getScreen().height(); 61 | pixel_t *ale_screen_data = (pixel_t *)ale->getScreen().getArray(); 62 | for(int i = 0;i < w*h;i++){ 63 | screen_data[i] = rgb_palette[ale_screen_data[i]]; 64 | } 65 | } 66 | 67 | int getRAMSize(ALEInterface *ale){return ale->getRAM().size();} 68 | 69 | void getRAM(ALEInterface *ale,unsigned char *ram){ 70 | unsigned char *ale_ram = ale->getRAM().array(); 71 | int size = ale->getRAM().size(); 72 | memcpy(ram,ale_ram,size*sizeof(unsigned char)); 73 | } 74 | 75 | int act(ALEInterface *ale,int action){return ale->act((Action)action);} 76 | bool game_over(ALEInterface *ale){return ale->game_over();} 77 | void reset_game(ALEInterface *ale){ale->reset_game();} 78 | 79 | void set(ALEInterface *ale,const char *key,const char*value){ale->set(key,value);} 80 | } 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /ale_python_interface/ale_python_interface.py: -------------------------------------------------------------------------------- 1 | # ale_python_interface.py 2 | # Author: Ben Goodrich 3 | # This directly implements a python version of the arcade learning environment interface. 4 | # It requires the C wrapper library to be built and on shared object path, as "ale_c_wrapper.so" 5 | 6 | from ctypes import cdll 7 | import numpy as np 8 | from numpy.ctypeslib import as_ctypes 9 | import os 10 | 11 | ale_lib = cdll.LoadLibrary(os.path.join( 12 | os.path.dirname(__file__),'ale_c_wrapper.so')) 13 | 14 | class ALEInterface(object): 15 | def __init__(self): 16 | self.obj = ale_lib.ALE_new() 17 | 18 | def getString(self,key): 19 | return ale_lib.getString(self.obj,key) 20 | def getInt(self,key): 21 | return ale_lib.getInt(self.obj,key) 22 | def getBool(self,key): 23 | return ale_lib.getBool(self.obj,key) 24 | def getFloat(self,key): 25 | return ale_lib.getFloat(self.obj,key) 26 | 27 | def set(self,key,value): 28 | if(type(value) == str): 29 | ale_lib.setString(self.obj,key,value) 30 | elif(type(value) == int): 31 | ale_lib.setInt(self.obj,key,value) 32 | elif(type(value) == bool): 33 | ale_lib.setBool(self.obj,key,value) 34 | elif(type(value) == float): 35 | ale_lib.setFloar(self.obj,key,value) 36 | 37 | def loadROM(self,rom_file): 38 | ale_lib.loadROM(self.obj,rom_file) 39 | 40 | def act(self,action): 41 | return ale_lib.act(self.obj,int(action)) 42 | 43 | def game_over(self): 44 | return ale_lib.game_over(self.obj) 45 | 46 | def reset_game(self): 47 | ale_lib.reset_game(self.obj) 48 | 49 | def getLegalActionSet(self): 50 | act_size = ale_lib.getLegalActionSize(self.obj) 51 | act = np.zeros((act_size),dtype=np.int32) 52 | ale_lib.getLegalActionSet(self.obj,as_ctypes(act)) 53 | return act 54 | 55 | def getMinimalActionSet(self): 56 | act_size = ale_lib.getMinimalActionSize(self.obj) 57 | act = np.zeros((act_size),dtype=np.int32) 58 | ale_lib.getMinimalActionSet(self.obj,as_ctypes(act)) 59 | return act 60 | 61 | def getFrameNumber(self): 62 | return ale_lib.getFrameNumber(self.obj) 63 | 64 | def getEpisodeFrameNumber(self): 65 | return ale_lib.getEpisodeFrameNumber(self.obj) 66 | 67 | def getScreenDims(self): 68 | """returns a tuple that contains (screen_width,screen_height) 69 | """ 70 | width = ale_lib.getScreenWidth(self.obj) 71 | height = ale_lib.getScreenHeight(self.obj) 72 | return (width,height) 73 | 74 | 75 | def getScreen(self,screen_data=None): 76 | """This function fills screen_data with the RAW Pixel data 77 | screen_data MUST be a numpy array of uint8/int8. This could be initialized like so: 78 | screen_data = np.array(w*h,dtype=np.uint8) 79 | Notice, it must be width*height in size also 80 | If it is None, then this function will initialize it 81 | Note: This is the raw pixel values from the atari, before any RGB palette transformation takes place 82 | """ 83 | if(screen_data is None): 84 | width = ale_lib.getScreenWidth(self.obj) 85 | height = ale_lib.getScreenWidth(self.obj) 86 | screen_data = np.zeros(width*height,dtype=np.uint8) 87 | ale_lib.getScreen(self.obj,as_ctypes(screen_data)) 88 | return screen_data 89 | 90 | def getScreenRGB(self,screen_data=None): 91 | """This function fills screen_data with the data 92 | screen_data MUST be a numpy array of uint32/int32. This can be initialized like so: 93 | screen_data = np.array(w*h,dtype=np.uint32) 94 | Notice, it must be width*height in size also 95 | If it is None, then this function will initialize it 96 | """ 97 | if(screen_data is None): 98 | width = ale_lib.getScreenWidth(self.obj) 99 | height = ale_lib.getScreenWidth(self.obj) 100 | screen_data = np.zeros(width*height,dtype=np.uint32) 101 | ale_lib.getScreenRGB(self.obj,as_ctypes(screen_data)) 102 | return screen_data 103 | 104 | def getRAMSize(self): 105 | return ale_lib.getRAMSize(self.obj) 106 | 107 | def getRAM(self,ram=None): 108 | """This function grabs the atari RAM. 109 | ram MUST be a numpy array of uint8/int8. This can be initialized like so: 110 | ram = np.array(ram_size,dtype=uint8) 111 | Notice: It must be ram_size where ram_size can be retrieved via the getRAMSize function. 112 | If it is None, then this function will initialize it. 113 | """ 114 | if(ram is None): 115 | ram_size = ale_lib.getRAMSize(self.obj) 116 | ram = np.zeros(ram_size,dtype=np.uint8) 117 | ale_lib.getRAM(self.obj,as_ctypes(ram)) 118 | 119 | def __del__(self): 120 | ale_lib.ALE_del(self.obj) 121 | -------------------------------------------------------------------------------- /ale_python_interface/atari_ntsc_rgb_palette.h: -------------------------------------------------------------------------------- 1 | //taken from Stella in Arcade-Learning-Environment/src/emucore/console.cxx 2 | static const int rgb_palette[256] = { 3 | 0x000000, 0, 0x4a4a4a, 0, 0x6f6f6f, 0, 0x8e8e8e, 0, 4 | 0xaaaaaa, 0, 0xc0c0c0, 0, 0xd6d6d6, 0, 0xececec, 0, 5 | 0x484800, 0, 0x69690f, 0, 0x86861d, 0, 0xa2a22a, 0, 6 | 0xbbbb35, 0, 0xd2d240, 0, 0xe8e84a, 0, 0xfcfc54, 0, 7 | 0x7c2c00, 0, 0x904811, 0, 0xa26221, 0, 0xb47a30, 0, 8 | 0xc3903d, 0, 0xd2a44a, 0, 0xdfb755, 0, 0xecc860, 0, 9 | 0x901c00, 0, 0xa33915, 0, 0xb55328, 0, 0xc66c3a, 0, 10 | 0xd5824a, 0, 0xe39759, 0, 0xf0aa67, 0, 0xfcbc74, 0, 11 | 0x940000, 0, 0xa71a1a, 0, 0xb83232, 0, 0xc84848, 0, 12 | 0xd65c5c, 0, 0xe46f6f, 0, 0xf08080, 0, 0xfc9090, 0, 13 | 0x840064, 0, 0x97197a, 0, 0xa8308f, 0, 0xb846a2, 0, 14 | 0xc659b3, 0, 0xd46cc3, 0, 0xe07cd2, 0, 0xec8ce0, 0, 15 | 0x500084, 0, 0x68199a, 0, 0x7d30ad, 0, 0x9246c0, 0, 16 | 0xa459d0, 0, 0xb56ce0, 0, 0xc57cee, 0, 0xd48cfc, 0, 17 | 0x140090, 0, 0x331aa3, 0, 0x4e32b5, 0, 0x6848c6, 0, 18 | 0x7f5cd5, 0, 0x956fe3, 0, 0xa980f0, 0, 0xbc90fc, 0, 19 | 0x000094, 0, 0x181aa7, 0, 0x2d32b8, 0, 0x4248c8, 0, 20 | 0x545cd6, 0, 0x656fe4, 0, 0x7580f0, 0, 0x8490fc, 0, 21 | 0x001c88, 0, 0x183b9d, 0, 0x2d57b0, 0, 0x4272c2, 0, 22 | 0x548ad2, 0, 0x65a0e1, 0, 0x75b5ef, 0, 0x84c8fc, 0, 23 | 0x003064, 0, 0x185080, 0, 0x2d6d98, 0, 0x4288b0, 0, 24 | 0x54a0c5, 0, 0x65b7d9, 0, 0x75cceb, 0, 0x84e0fc, 0, 25 | 0x004030, 0, 0x18624e, 0, 0x2d8169, 0, 0x429e82, 0, 26 | 0x54b899, 0, 0x65d1ae, 0, 0x75e7c2, 0, 0x84fcd4, 0, 27 | 0x004400, 0, 0x1a661a, 0, 0x328432, 0, 0x48a048, 0, 28 | 0x5cba5c, 0, 0x6fd26f, 0, 0x80e880, 0, 0x90fc90, 0, 29 | 0x143c00, 0, 0x355f18, 0, 0x527e2d, 0, 0x6e9c42, 0, 30 | 0x87b754, 0, 0x9ed065, 0, 0xb4e775, 0, 0xc8fc84, 0, 31 | 0x303800, 0, 0x505916, 0, 0x6d762b, 0, 0x88923e, 0, 32 | 0xa0ab4f, 0, 0xb7c25f, 0, 0xccd86e, 0, 0xe0ec7c, 0, 33 | 0x482c00, 0, 0x694d14, 0, 0x866a26, 0, 0xa28638, 0, 34 | 0xbb9f47, 0, 0xd2b656, 0, 0xe8cc63, 0, 0xfce070, 0 35 | }; 36 | 37 | 38 | -------------------------------------------------------------------------------- /ale_python_interface/build.sh: -------------------------------------------------------------------------------- 1 | g++ -g -fPIC -c ale_c_wrapper.cpp -o ale_c_wrapper.o -lale 2 | g++ -g ale_c_wrapper.o -shared -o ale_c_wrapper.so -lale 3 | -------------------------------------------------------------------------------- /examples/ale_python_test1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # ale_python_test1.py 4 | # Author: Ben Goodrich 5 | # 6 | # This is a direct port to python of the shared library example from ALE provided in 7 | # doc/examples/sharedLibraryInterfaceExample.cpp 8 | 9 | import sys 10 | from ale_python_interface import ALEInterface 11 | import numpy as np 12 | 13 | if(len(sys.argv) < 2): 14 | print("Usage ./ale_python_test1.py ") 15 | sys.exit() 16 | 17 | ale = ALEInterface() 18 | 19 | max_frames_per_episode = ale.getInt("max_num_frames_per_episode"); 20 | ale.set("random_seed",123) 21 | 22 | random_seed = ale.getInt("random_seed") 23 | print("random_seed: " + str(random_seed)) 24 | 25 | ale.loadROM(sys.argv[1]) 26 | legal_actions = ale.getLegalActionSet() 27 | 28 | for episode in range(10): 29 | total_reward = 0.0 30 | while not ale.game_over(): 31 | a = legal_actions[np.random.randint(legal_actions.size)] 32 | reward = ale.act(a); 33 | total_reward += reward 34 | print("Episode " + str(episode) + " ended with score: " + str(total_reward)) 35 | ale.reset_game() 36 | 37 | -------------------------------------------------------------------------------- /examples/ale_python_test2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # ale_python_test1.py 4 | # Author: Ben Goodrich 5 | # 6 | # This modified ale_python_test1.py to do more extensive tests of the python interface 7 | # by calling more functions 8 | import sys 9 | from ale_python_interface import ALEInterface 10 | import numpy as np 11 | 12 | if(len(sys.argv) < 2): 13 | print("Usage ./ale_python_test2.py ") 14 | sys.exit() 15 | 16 | ale = ALEInterface() 17 | 18 | max_frames_per_episode = ale.getInt("max_num_frames_per_episode"); 19 | ale.set("random_seed",123) 20 | 21 | random_seed = ale.getInt("random_seed") 22 | print("random_seed: " + str(random_seed)) 23 | 24 | ale.loadROM(sys.argv[1]) 25 | legal_actions = ale.getMinimalActionSet() 26 | 27 | for episode in range(10): 28 | total_reward = 0.0 29 | while not ale.game_over(): 30 | a = legal_actions[np.random.randint(legal_actions.size)] 31 | reward = ale.act(a); 32 | total_reward += reward 33 | episode_frame_number = ale.getEpisodeFrameNumber() 34 | frame_number = ale.getFrameNumber() 35 | print("Frame Number: " + str(frame_number) + " Episode Frame Number: " + str(episode_frame_number)) 36 | print("Episode " + str(episode) + " ended with score: " + str(total_reward)) 37 | ale.reset_game() 38 | 39 | -------------------------------------------------------------------------------- /examples/ale_python_test_pygame.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # ale_python_test_pygame.py 4 | # Author: Ben Goodrich 5 | # 6 | # This modified ale_python_test1.py to display screen contents using pygame 7 | import sys 8 | from ale_python_interface import ALEInterface 9 | import numpy as np 10 | import pygame 11 | 12 | if(len(sys.argv) < 2): 13 | print("Usage ./ale_python_test_pygame.py ") 14 | sys.exit() 15 | 16 | ale = ALEInterface() 17 | 18 | max_frames_per_episode = ale.getInt("max_num_frames_per_episode"); 19 | ale.set("random_seed",123) 20 | 21 | random_seed = ale.getInt("random_seed") 22 | print("random_seed: " + str(random_seed)) 23 | 24 | ale.loadROM(sys.argv[1]) 25 | legal_actions = ale.getMinimalActionSet() 26 | 27 | (screen_width,screen_height) = ale.getScreenDims() 28 | print("width/height: " +str(screen_width) + "/" + str(screen_height)) 29 | 30 | #init pygame 31 | pygame.init() 32 | screen = pygame.display.set_mode((screen_width,screen_height)) 33 | pygame.display.set_caption("Arcade Learning Environment Random Agent Display") 34 | 35 | pygame.display.flip() 36 | 37 | episode = 0 38 | total_reward = 0.0 39 | while(episode < 10): 40 | exit=False 41 | for event in pygame.event.get(): 42 | if event.type == pygame.QUIT: 43 | exit=True 44 | break; 45 | if(exit): 46 | break 47 | 48 | a = legal_actions[np.random.randint(legal_actions.size)] 49 | reward = ale.act(a); 50 | total_reward += reward 51 | 52 | numpy_surface = np.frombuffer(screen.get_buffer(),dtype=np.int32) 53 | ale.getScreenRGB(numpy_surface) 54 | pygame.display.flip() 55 | if(ale.game_over()): 56 | episode_frame_number = ale.getEpisodeFrameNumber() 57 | frame_number = ale.getFrameNumber() 58 | print("Frame Number: " + str(frame_number) + " Episode Frame Number: " + str(episode_frame_number)) 59 | print("Episode " + str(episode) + " ended with score: " + str(total_reward)) 60 | ale.reset_game() 61 | total_reward = 0.0 62 | episode = episode + 1 63 | 64 | -------------------------------------------------------------------------------- /examples/ale_python_test_pygame_player.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # ale_python_test_pygame_player.py 4 | # Author: Ben Goodrich 5 | # 6 | # This modified ale_python_test_pygame.py to provide a fully interactive experience allowing the player 7 | # to play. RAM Contents, current action, and reward are also displayed. 8 | # keys are: 9 | # arrow keys -> up/down/left/right 10 | # z -> fire button 11 | import sys 12 | from ale_python_interface import ALEInterface 13 | import numpy as np 14 | import pygame 15 | 16 | key_action_tform_table = ( 17 | 0, #00000 none 18 | 2, #00001 up 19 | 5, #00010 down 20 | 2, #00011 up/down (invalid) 21 | 4, #00100 left 22 | 7, #00101 up/left 23 | 9, #00110 down/left 24 | 7, #00111 up/down/left (invalid) 25 | 3, #01000 right 26 | 6, #01001 up/right 27 | 8, #01010 down/right 28 | 6, #01011 up/down/right (invalid) 29 | 3, #01100 left/right (invalid) 30 | 6, #01101 left/right/up (invalid) 31 | 8, #01110 left/right/down (invalid) 32 | 6, #01111 up/down/left/right (invalid) 33 | 1, #10000 fire 34 | 10, #10001 fire up 35 | 13, #10010 fire down 36 | 10, #10011 fire up/down (invalid) 37 | 12, #10100 fire left 38 | 15, #10101 fire up/left 39 | 17, #10110 fire down/left 40 | 15, #10111 fire up/down/left (invalid) 41 | 11, #11000 fire right 42 | 14, #11001 fire up/right 43 | 16, #11010 fire down/right 44 | 14, #11011 fire up/down/right (invalid) 45 | 11, #11100 fire left/right (invalid) 46 | 14, #11101 fire left/right/up (invalid) 47 | 16, #11110 fire left/right/down (invalid) 48 | 14 #11111 fire up/down/left/right (invalid) 49 | ) 50 | 51 | if(len(sys.argv) < 2): 52 | print("Usage ./ale_python_test_pygame_player.py ") 53 | sys.exit() 54 | 55 | ale = ALEInterface() 56 | 57 | max_frames_per_episode = ale.getInt("max_num_frames_per_episode"); 58 | ale.set("random_seed",123) 59 | 60 | random_seed = ale.getInt("random_seed") 61 | print("random_seed: " + str(random_seed)) 62 | 63 | ale.loadROM(sys.argv[1]) 64 | legal_actions = ale.getMinimalActionSet() 65 | print legal_actions 66 | 67 | (screen_width,screen_height) = ale.getScreenDims() 68 | print("width/height: " +str(screen_width) + "/" + str(screen_height)) 69 | 70 | (display_width,display_height) = (1024,420) 71 | 72 | #init pygame 73 | pygame.init() 74 | screen = pygame.display.set_mode((display_width,display_height)) 75 | pygame.display.set_caption("Arcade Learning Environment Player Agent Display") 76 | 77 | game_surface = pygame.Surface((screen_width,screen_height)) 78 | 79 | pygame.display.flip() 80 | 81 | #init clock 82 | clock = pygame.time.Clock() 83 | 84 | episode = 0 85 | total_reward = 0.0 86 | while(episode < 10): 87 | 88 | #get the keys 89 | keys = 0 90 | pressed = pygame.key.get_pressed() 91 | keys |= pressed[pygame.K_UP] 92 | keys |= pressed[pygame.K_DOWN] <<1 93 | keys |= pressed[pygame.K_LEFT] <<2 94 | keys |= pressed[pygame.K_RIGHT] <<3 95 | keys |= pressed[pygame.K_z] <<4 96 | a = key_action_tform_table[keys] 97 | reward = ale.act(a); 98 | total_reward += reward 99 | 100 | #clear screen 101 | screen.fill((0,0,0)) 102 | 103 | #get atari screen pixels and blit them 104 | numpy_surface = np.frombuffer(game_surface.get_buffer(),dtype=np.int32) 105 | ale.getScreenRGB(numpy_surface) 106 | del numpy_surface 107 | screen.blit(pygame.transform.scale2x(game_surface),(0,0)) 108 | 109 | #get RAM 110 | ram_size = ale.getRAMSize() 111 | ram = np.zeros((ram_size),dtype=np.uint8) 112 | ale.getRAM(ram) 113 | 114 | 115 | #Display ram bytes 116 | font = pygame.font.SysFont("Ubuntu Mono",32) 117 | text = font.render("RAM: " ,1,(255,208,208)) 118 | screen.blit(text,(330,10)) 119 | 120 | font = pygame.font.SysFont("Ubuntu Mono",25) 121 | height = font.get_height()*1.2 122 | 123 | line_pos = 40 124 | ram_pos = 0 125 | while(ram_pos < 128): 126 | ram_string = ''.join(["%02X "%ram[x] for x in range(ram_pos,min(ram_pos+16,128))]) 127 | text = font.render(ram_string,1,(255,255,255)) 128 | screen.blit(text,(340,line_pos)) 129 | line_pos += height 130 | ram_pos +=16 131 | 132 | #display current action 133 | font = pygame.font.SysFont("Ubuntu Mono",32) 134 | text = font.render("Current Action: " + str(a) ,1,(208,208,255)) 135 | height = font.get_height()*1.2 136 | screen.blit(text,(330,line_pos)) 137 | line_pos += height 138 | 139 | #display reward 140 | font = pygame.font.SysFont("Ubuntu Mono",30) 141 | text = font.render("Total Reward: " + str(total_reward) ,1,(208,255,255)) 142 | screen.blit(text,(330,line_pos)) 143 | 144 | pygame.display.flip() 145 | 146 | #process pygame event queue 147 | exit=False 148 | for event in pygame.event.get(): 149 | if event.type == pygame.QUIT: 150 | exit=True 151 | break; 152 | if(pressed[pygame.K_q]): 153 | exit = True 154 | if(exit): 155 | break 156 | 157 | #delay to 60fps 158 | clock.tick(60.) 159 | 160 | if(ale.game_over()): 161 | episode_frame_number = ale.getEpisodeFrameNumber() 162 | frame_number = ale.getFrameNumber() 163 | print("Frame Number: " + str(frame_number) + " Episode Frame Number: " + str(episode_frame_number)) 164 | print("Episode " + str(episode) + " ended with score: " + str(total_reward)) 165 | ale.reset_game() 166 | total_reward = 0.0 167 | episode = episode + 1 168 | 169 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | module1 = Extension('ale_python_interface.ale_c_wrapper', 4 | libraries = ['ale'], 5 | sources=['ale_python_interface/ale_c_wrapper.cpp']) 6 | setup(name = 'ale_python_interface', 7 | description = 'Python Arcade Learning Environment Interface', 8 | url='https://github.com/bbitmaster/ale_python_interface', 9 | author='Ben Goodrich', 10 | license='GPL', 11 | ext_modules = [module1], 12 | packages=['ale_python_interface']) 13 | 14 | --------------------------------------------------------------------------------