Skip to content

Commit 00fb4c7

Browse files
authored
Update README.md
1 parent 8858153 commit 00fb4c7

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,84 @@ Complete API documentation can be found here: [Gymie-Client API](https://jscript
7575
In the [previous section](#how-to-run-the-client-and-server) 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:
7676

7777
```ts
78-
// TODO
78+
import Gymie from '../dist'
79+
import { Continuous, Discrete } from '../dist/Env'
80+
import {
81+
ConnectFailed,
82+
NoConnected,
83+
ConnectionError,
84+
ConnectionClosed
85+
} from '../dist/errors'
86+
87+
const wsApi = 'http://0.0.0.0:5000/gym'
88+
const envId = 'LunarLander-v2'
89+
const { log } = console
90+
91+
;(async () => {
92+
const gymie = new Gymie()
93+
94+
try {
95+
// Connect to the server
96+
await gymie.connect(wsApi)
97+
98+
// Instantiates the environment, in this case it's got
99+
// a continuous state and discrete action space.
100+
const env = await gymie.make<Continuous, Discrete>(envId)
101+
const space = await env.actionSpace()
102+
log('Action Space:', space) // => Action Space: n
103+
104+
const initialState = await env.reset()
105+
log('Initial State:\n', initialState) // => Initial State: number[]
106+
107+
let step = 0
108+
let totalReward = 0
109+
110+
// Running loop: runs an episode until `done = true`
111+
log('---- START episode ----')
112+
while (true) {
113+
114+
log(`\nStep: ${++step}`)
115+
116+
// Samples a random action
117+
const action = await env.actionSample()
118+
log('Action:', action) // => Action: number from [0..n)
119+
120+
// Performs a step on the environment given the action
121+
const [nextState, reward, done, _] = await env.step(action)
122+
123+
log('Next State:\n', nextState) // => Next State: number[]
124+
log(`Reward: ${reward}`) // => Next State: number
125+
126+
totalReward += reward
127+
128+
if (done) {
129+
log('---- END episode ----')
130+
break
131+
}
132+
}
133+
134+
console.log(`\nEpisode Reward: ${totalReward}\n`)
135+
136+
await env.close()
137+
138+
} catch(err) {
139+
switch(true) {
140+
// This could happen when trying to connect to the server
141+
case err instanceof ConnectFailed: break
142+
143+
// There is no connection and we try to instantiate an environment
144+
case err instanceof NoConnected: break
145+
146+
// There was a socket error
147+
case err instanceof ConnectionError: break
148+
149+
// Server closed the connection. Code and reason comes in the message
150+
case err instanceof ConnectionClosed: break
151+
}
152+
}
153+
154+
gymie.close()
155+
})()
79156
```
80157

81158
## Testing Gymie

0 commit comments

Comments
 (0)