- Notifications
You must be signed in to change notification settings - Fork0
WebSocket client that consumes an API wrapping OpenAI Gym or gym-like environments such as Gym Retro or Unity ML-Agents.
License
jscriptcoder/Gymie-Client
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
WebSocket client that consumes an API wrappingOpenAI Gym or Gym-like environments such asGym Retro orUnity ML-Agents. Currently the best server is its counterpartGymie-Server 😉
Gymie-Client is available as aNPM package, and can installed as a dependency as usual:
$ npm install gymie
You can also clone the repo and npm-link the library as follows, although there isn't really a good readon to do it this way, unless you wanna contribute to the library and test it locally.
$ git clone https://github.com/jscriptcoder/Gymie-ClientCloning into 'Gymie-Client'......$ cd Gymie-Client/$ npm linkgymie@0.x.y preinstall /path/to/Gymie-Client...$ cd ~/path/to/project$ npm link gymie/path/to/project/node_modules/gymie -> /usr/local/lib/node_modules/gymie -> /path/to/Gymie-Client
During the installationGymie-Server will also be installed. It's important to note that Gymie-Server requires Python>=3.6, so I suggest to conda-create an environment with such version if it's not already installed... or upgrade Python to at least this version.
Gymie-Client communicates with a server through WebSockets. This server will provide Gymie with an API to access the underlying Python library to create and interact with an environment. As mentioned before, this client comes with itscounterpart server. You can start the server from the command line:
$ python -m gymie --host 0.0.0.0 --port 5000(84581) wsgi starting up on http://0.0.0.0:5000
Once the server is running, Gymie-Client can start interacting with it as follows:
importGymiefrom'gymie'constgymie=newGymie()awaitgymie.connect('http://0.0.0.0:5000')// connects to the serverconstenv=awaitgymie.make('LunarLander-v2')// instantiates an environment// accesing the underlying Gym-like library.constspace=awaitenv.actionSpace()constinitialState=awaitenv.reset()constrandomAction=awaitenv.actionSample()
Complete API documentation can be found here:Gymie-Client API(generated byTypeDoc)
In theprevious section we already saw how to import gymie, connect to the server, instantiate an environment and call a few API methods. Let's go a bit more in detail with a complete example of a random agent interacting with an environment:
importGymiefrom'gymie'import{Continuous,Discrete}from'gymie/Env'import{ConnectFailed,NoConnected,ConnectionError,ConnectionClosed}from'gymie/errors'constwsApi='http://0.0.0.0:5000/gym'constenvId='LunarLander-v2'const{ log}=console;(async()=>{constgymie=newGymie()try{// Connects to the serverawaitgymie.connect(wsApi)// Instantiates the environment, in this case it's got// a continuous state and discrete action space.constenv=awaitgymie.make<Continuous,Discrete>(envId)constspace=awaitenv.actionSpace()log('Action Space:',space)// => Action Space: { name: 'Discrete', n: number }constinitialState=awaitenv.reset()log('Initial State:\n',initialState)// => Initial State: number[]letstep=0lettotalReward=0// Running loop: runs an episode until `done = true`log('---- START episode ----')while(true){log(`\nStep:${++step}`)// Samples a random actionconstaction=awaitenv.actionSample()log('Action:',action)// => Action: number from [0..n)// Performs a step on the environment given the actionconst[nextState,reward,done,_]=awaitenv.step(action)log('Next State:\n',nextState)// => Next State: number[]log(`Reward:${reward}`)// => Next State: numbertotalReward+=rewardif(done){log('---- END episode ----')break}}log(`\nEpisode Reward:${totalReward}\n`)awaitenv.close()}catch(err){switch(true){// This could happen while trying to connect to the servercaseerrinstanceofConnectFailed:break// There is no connection and we try to instantiate an environmentcaseerrinstanceofNoConnected:break// There was a socket errorcaseerrinstanceofConnectionError:break// Server closed the connection. Code and reason comes in the messagecaseerrinstanceofConnectionClosed:break}}finally{gymie.close()}})()
All unit-tests live next to the code they're testing, under the extensionsrc/*.test.ts
. You can run all the tests by executing:
$ npm test
MIT License - Copyright (c) 2020 Francisco Ramos
About
WebSocket client that consumes an API wrapping OpenAI Gym or gym-like environments such as Gym Retro or Unity ML-Agents.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.