Skip to content

Instantly share code, notes, and snippets.

@ShigekiKarita
Created July 10, 2018 00:37
Show Gist options
  • Select an option

  • Save ShigekiKarita/7e6ecd626e883819e1ece73a0bed6f57 to your computer and use it in GitHub Desktop.

Select an option

Save ShigekiKarita/7e6ecd626e883819e1ece73a0bed6f57 to your computer and use it in GitHub Desktop.
/// based on https://github.com/openai/gym-http-api/blob/master/binding-rust/src/lib.rs
module gym;
import std.exception : enforce;
import std.json : JSONValue, parseJSON;
import std.conv : to;
import std.stdio : writeln;
enum bool isSpace(T) = is(typeof({ T.from(JSONValue.init); }));
/// Discrete Space
struct Discrete {
long n;
alias n this;
static from(T: JSONValue)(scope auto ref T info) {
enforce(info["name"].str == "Discrete");
return Discrete(info["n"].integer);
}
}
///
unittest {
static assert(isSpace!Discrete);
auto s = `{
"name": "Discrete",
"n": 123
}`;
auto j = s.parseJSON;
assert(Discrete.from(j) == Discrete(123));
}
/// Box Space
struct Box {
long[] shape;
double[] high;
double[] low;
static from(T: JSONValue)(scope auto ref T info) {
import std.algorithm : map;
import std.array : array;
enforce(info["name"].str == "Box");
return Box(
info["shape"].array.map!(a => a.integer).array.dup,
info["high"].array.map!(a => a.floating).array.dup,
info["low"].array.map!(a => a.floating).array.dup
);
}
}
///
unittest {
static assert(isSpace!Box);
auto s = `{
"name": "Box",
"shape": [2],
"high": [1.0, 2.0],
"low": [0.0, 0.0]
}`;
auto j = s.parseJSON;
assert(Box.from(j) == Box([2], [1.0, 2.0], [0.0, 0.0]));
}
struct State {
double[] observation;
double reward;
bool done;
JSONValue info;
}
struct Environment(ActionSpace, ObservationSpace) {
GymClient client;
string instanceId;
ActionSpace action;
ObservationSpace observation;
}
struct GymClient {
import std.net.curl;
string address;
this(string address) {
this.address = address;
}
auto env(string name) {
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
JSONValue req = ["env_id": name];
auto instanceId = post(this.address ~ "/v1/envs/", req.toString, http)
.parseJSON["instance_id"].str;
instanceId.writeln;
auto obs = get(this.address ~ "/v1/envs/" ~ instanceId ~ "/observation_space/");
obs.writeln;
auto act = get(this.address ~ "/v1/envs/" ~ instanceId ~ "/action_space/");
act.writeln;
// char[] d;
// http.onReceive = (ubyte[] data) { d ~= cast(char[])(data); return data.length; };
// writeln(req.toString);
// http.setPostData(req.toString, "application/json");
// http.perform();
// writeln(d);
}
}
///
unittest {
GymClient("127.0.0.1:5000").env("CartPole-v0");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment