Skip to content

Commit

Permalink
Merge pull request #85 from jjfiv/pip-setup
Browse files Browse the repository at this point in the history
Pip setup
  • Loading branch information
etosch committed Mar 17, 2019
2 parents eba2e32 + 95c8186 commit 917a8cd
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 315 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ ctoybox/*.model.data-00000-of-00001
ctoybox/*.model.index
ctoybox/*.model.meta
ctoybox/checkpoint
ctoybox/*.model
ctoybox/*.modeltoybox/build
toybox/dist
ctoybox/toybox/build
ctoybox/toybox/dist
ctoybox/toybox/toybox.egg-info
ctoybox/toybox/toybox/_native__lib.so
3 changes: 3 additions & 0 deletions ctoybox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "ctoybox"
version = "0.1.0"
authors = ["Emma 'Frank' Tosch <[email protected]>"]
build = "build.rs"

[lib]
name = "ctoybox"
Expand All @@ -19,3 +20,5 @@ toybox = {path = "../toybox", version="*"}
version = "*"
path = "../core"

[build-dependencies]
cbindgen = "0.5"
1 change: 1 addition & 0 deletions ctoybox/REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ tensorflow
gym[atari]
tensorboard
pygame
cffi

13 changes: 13 additions & 0 deletions ctoybox/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// from https://github.com/getsentry/milksnake
extern crate cbindgen;

use std::env;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut config: cbindgen::Config = Default::default();
config.language = cbindgen::Language::C;
cbindgen::generate_with_config(&crate_dir, config)
.unwrap()
.write_to_file("../target/ctoybox.h");
}
15 changes: 9 additions & 6 deletions ctoybox/human_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@
break
key_state = pygame.key.get_pressed()
player_input = Input()
player_input.left = key_state[K_LEFT] or key_state[K_a]
player_input.right = key_state[K_RIGHT] or key_state[K_d]
player_input.up = key_state[K_UP] or key_state[K_w]
player_input.down = key_state[K_DOWN] or key_state[K_s]
player_input.button1 = key_state[K_z] or key_state[K_SPACE]
player_input.button2 = key_state[K_x] or key_state[K_RSHIFT] or key_state[K_LSHIFT]

# Explicitly casting to bools because in some versions, the RHS gets converted
# to ints, causing problems when we load into the associated rust structs.
player_input.left = bool(key_state[K_LEFT] or key_state[K_a])
player_input.right = bool(key_state[K_RIGHT] or key_state[K_d])
player_input.up = bool(key_state[K_UP] or key_state[K_w])
player_input.down = bool(key_state[K_DOWN] or key_state[K_s])
player_input.button1 = bool(key_state[K_z] or key_state[K_SPACE])
player_input.button2 = bool(key_state[K_x] or key_state[K_RSHIFT] or key_state[K_LSHIFT])

tb.apply_action(player_input)
if args.query is not None:
print(args.query, tb.query_state_json(args.query, args.query_args))
Expand Down
18 changes: 11 additions & 7 deletions ctoybox/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::WrapSimulator;
use super::WrapState;
use libc::c_char;
use libc::{c_char, c_void};
use serde_json;
use std::boxed::Box;
use std::ffi::{CStr, CString};
Expand Down Expand Up @@ -215,16 +215,20 @@ pub extern "C" fn state_apply_ale_action(state_ptr: *mut WrapState, input: i32)
}

#[no_mangle]
pub extern "C" fn state_apply_action(state_ptr: *mut WrapState, input_ptr: *mut Input) {
pub extern "C" fn state_apply_action(state_ptr: *mut WrapState, input_ptr: *const c_char) {
let &mut WrapState { ref mut state } = unsafe {
assert!(!state_ptr.is_null());
&mut *state_ptr
};
let input = unsafe {
let input_ptr = unsafe {
assert!(!input_ptr.is_null());
&mut *input_ptr
CStr::from_ptr(input_ptr)
};
state.update_mut(*input);
let input_str = input_ptr
.to_str()
.expect("Could not create input string from pointer");
let input: Input = serde_json::from_str(input_str).expect("Could not input string to Input");
state.update_mut(input);
}

#[no_mangle]
Expand All @@ -246,15 +250,15 @@ pub extern "C" fn state_score(state_ptr: *mut WrapState) -> i32 {
}

#[no_mangle]
pub extern "C" fn state_to_json(state_ptr: *mut WrapState) -> *const c_char {
pub extern "C" fn state_to_json(state_ptr: *mut WrapState) -> *mut c_void {
let &mut WrapState { ref mut state } = unsafe {
assert!(!state_ptr.is_null());
&mut *state_ptr
};

let json: String = state.to_json();
let cjson: CString = CString::new(json).expect("Conversion to CString should succeed!");
CString::into_raw(cjson)
CString::into_raw(cjson) as *mut c_void
}

#[no_mangle]
Expand Down
8 changes: 5 additions & 3 deletions ctoybox/start_python
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/bash
#pip3 install -q -r REQUIREMENTS.txt --user
export PYTHONPATH=${PWD}/baselines:${PWD}/toybox:${PYTHONPATH}:${HOME}/toybox/ctoybox/baselines:${HOME}/toybox/ctoybox/toybox
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PWD}/../target/release:${HOME}/toybox/target/release:${HOME}/dev/toybox/target/release
# RUN THIS command FROM CTOYBOX.
pip3 install -q -r REQUIREMENTS.txt --user
export PYTHONPATH=${PWD}/baselines:${PWD}/toybox:${PYTHONPATH}
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PWD}/../target/release
export LIBCTOYBOX=${PWD}/..
echo "PYTHONPATH $PYTHONPATH"
echo "LD_LIBRARY_PATH $LD_LIBRARY_PATH"
echo "LIBCTOYBOX $LIBCTOYBOX"
Expand Down
31 changes: 27 additions & 4 deletions ctoybox/toybox/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
# From https://github.com/getsentry/milksnake
from setuptools import setup

setup(name='openai_shim',
version='0.0.1',
install_requires=['gym'] # And any other dependencies foo needs
)
def build_native(spec):
# build an example rust library
build = spec.add_external_build(
cmd=['cargo', 'build', '--release'],
path='.'
)

spec.add_cffi_module(
module_path='toybox._native',
dylib=lambda: build.find_dylib('ctoybox', in_path='../../target/release'),
header_filename=lambda: build.find_header('ctoybox.h', in_path='../../target'),
rtld_flags=['NOW', 'NODELETE']
)

setup(
name='toybox',
version='0.1.0',
packages=['toybox', 'toybox.envs', 'toybox.interventions', 'toybox.sample_tests'],
zip_safe=False,
platforms='any',
setup_requires=['milksnake'],
install_requires=['milksnake'],
milksnake_tasks=[
build_native
]
)
42 changes: 24 additions & 18 deletions ctoybox/toybox/toybox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
from gym.envs.registration import register
import toybox.toybox as toybox
import toybox.envs as envs
import toybox.interventions as interventions
import toybox.sample_tests as sample_tests

# Updated to use v4 to be analogous with the ALE versioning
register(
id='BreakoutToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:BreakoutEnv',
nondeterministic=True
)
try:
from gym.envs.registration import register

register(
id='AmidarToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:AmidarEnv',
nondeterministic=False
)
# Updated to use v4 to be analogous with the ALE versioning
register(
id='BreakoutToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:BreakoutEnv',
nondeterministic=True
)

register(
id='SpaceInvadersToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:SpaceInvadersEnv',
nondeterministic=False
)
register(
id='AmidarToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:AmidarEnv',
nondeterministic=False
)

print("Loaded Toybox environments.")
register(
id='SpaceInvadersToyboxNoFrameskip-v4',
entry_point='toybox.envs.atari:SpaceInvadersEnv',
nondeterministic=False
)

print("Registered Toybox environments with gym.")

except:
# ModuleNotFoundError only in 3.6 and above
print("Loaded Toybox environments.")
Loading

0 comments on commit 917a8cd

Please sign in to comment.