Skip to content

Commit 4314124

Browse files
committed
feat: added an example using actionSample
1 parent 291540a commit 4314124

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

examples/actionSample.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Gymie from '../dist'
2+
import { Continuous, Discrete } from '../dist/Env'
3+
4+
const wsApi = 'http://0.0.0.0:5000/gym'
5+
const envId = 'LunarLander-v2'
6+
7+
;(async () => {
8+
const gymie = new Gymie()
9+
await gymie.connect(wsApi)
10+
11+
const env = await gymie.make<Continuous, Discrete>(envId)
12+
const space = await env.actionSpace()
13+
console.log('Action Space:', space)
14+
15+
const initialState = await env.reset()
16+
console.log('Initial State:\n', initialState)
17+
18+
let totalReward = 0
19+
20+
console.log('Running episode...')
21+
while (true) {
22+
const action = await env.actionSample()
23+
console.log('Action:', action)
24+
25+
const [nextState, reward, done, _] = await env.step(action)
26+
27+
console.log('Next State:\n', nextState)
28+
console.log('Reward:', reward)
29+
30+
totalReward += reward
31+
32+
if (done) {
33+
console.log('Done')
34+
break
35+
}
36+
37+
console.log('---')
38+
}
39+
40+
console.log('Episode Reward:', totalReward)
41+
42+
await env.close()
43+
gymie.close()
44+
})()

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"run-python-agent": "python runs/run_random_agent.py",
2626
"prerun-node-agent": "npm run start-server && npm run build",
2727
"run-node-agent": "node runs/runRandomAgent.js",
28+
"postrun-node-agent": "npm run kill-server 5000",
29+
"prerun-action-sample": "npm run start-server",
30+
"run-action-sample": "ts-node examples/actionSample.ts",
31+
"postrun-action-sample": "npm run kill-server 5000",
2832
"predocs": "rm -rf docs",
2933
"docs": "typedoc"
3034
},

runs/runRandomAgent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function mean(list) {
99
return list.reduce((acc, total) => acc + total , 0) / list.length
1010
}
1111

12-
(async () => {
12+
;(async () => {
1313
const gymie = new Gymie()
1414
await gymie.connect(wsApi)
1515

src/Env.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const wsApi = 'http://0.0.0.0:5000/gym'
66
const envId = 'CartPole-v1'
77
const envIdContinuous = 'MountainCarContinuous-v0'
88

9+
// TODO: Mock the server, WebSocketClient & WebSocketConnection
10+
911
interface Setup<O extends Space, A extends Space> {
1012
gymie: Gymie,
1113
env: Env<O, A>

src/Gymie.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Gymie from './Gymie'
44
const wsApi = 'http://0.0.0.0:5000/gym'
55
const envId = 'CartPole-v1'
66

7+
// TODO: Mock the server, WebSocketClient & WebSocketConnection
8+
79
test('Gymie#connect - Server running', async t => {
810
const gymie = new Gymie()
911

0 commit comments

Comments
 (0)