Created
September 2, 2018 09:00
-
-
Save markroxor/7c8041e098bc3e008bec2f73e3525fd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var gymHTTPClient_1 = require("/home/markroxor/Documents/RL/gym-http-api/binding-js/dist/lib/gymHTTPClient"); | |
var RandomDiscreteAgent = (function () { | |
function RandomDiscreteAgent(n) { | |
this.n = n; | |
} | |
RandomDiscreteAgent.prototype.act = function (observation, reward, done) { | |
return Math.floor(Math.random() * this.n); | |
}; | |
return RandomDiscreteAgent; | |
}()); | |
var client = new gymHTTPClient_1.default("http://127.0.0.1:5000"), envID = "CartPole-v0", numTrials = 3, outDir = "/tmp/random-agent-results", episodeCount = 100, maxSteps = 200; | |
var instanceID = undefined, agent = undefined; | |
client.envCreate(envID) | |
.then(function (reply) { | |
instanceID = reply.instance_id; | |
return client.envActionSpaceInfo(instanceID); | |
}).then(function (reply) { | |
agent = new RandomDiscreteAgent(reply.info["n"]); | |
return client.envMonitorStart(instanceID, outDir, true); | |
}).then(function () { | |
return actOutEpisode(0) | |
.then(function (done) { | |
client.envMonitorClose(instanceID).then(function (value) { return; }); | |
}); | |
}).then(function () { | |
console.log("Successfully ran example agent. Now trying to upload " + | |
"results to the scoreboard. If this fails, you likely need to " + | |
"set OPENAI_GYM_API_KEY=<your_api_key>"); | |
return client.upload(outDir); | |
}).catch(function (error) { | |
console.log("Example agent failed. Got error: " + error); | |
}); | |
function actOutEpisode(episode) { | |
return new Promise(function (resolve, reject) { | |
if (episode >= episodeCount) { | |
resolve(true); | |
} | |
else { | |
client.envReset(instanceID) | |
.then(function (reply) { return actOutStep(0, 0, reply.observation, false); }) | |
.then(function (done) { return resolve(actOutEpisode(episode + 1)); }); | |
} | |
}); | |
} | |
function actOutStep(step, reward, observation, done) { | |
return new Promise(function (resolve, reject) { | |
if (step >= maxSteps) { | |
resolve(null); | |
} | |
else { | |
var action = agent.act(observation, reward, done); | |
client.envStep(instanceID, action, true) | |
.then(function (reply) { | |
if (reply.done) { | |
resolve(null); | |
} | |
else { | |
resolve(actOutStep(step + 1, reply.reward, reply.observation, reply.done)); | |
} | |
}).catch(function (error) { throw error; }); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment