/
q_learning.js
255 lines (231 loc) · 5.22 KB
/
q_learning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import Tensor from '../util/tensor.js'
import { RLEnvironmentBase, RLRealRange, RLIntRange } from '../rl/base.js'
/**
* Base class for Q-table
*/
export class QTableBase {
/**
* @param {RLEnvironmentBase} env Environment
* @param {number} [resolution] Resolution
*/
constructor(env, resolution = 20) {
this._env = env
this._resolution = resolution
this._state_sizes = env.states.map(s => {
if (Array.isArray(s)) {
return s.length
} else {
return s.toArray(resolution).length
}
})
this._action_sizes = env.actions.map(a => {
if (Array.isArray(a)) {
return a.length
} else {
return a.toArray(resolution).length
}
})
this._sizes = [...this._state_sizes, ...this._action_sizes]
this._tensor = new Tensor(this._sizes)
this._table = this._tensor.value
}
/**
* Tensor
*
* @type {Tensor}
*/
get tensor() {
return this._tensor
}
/**
* States
*
* @type {(*[] | RLRealRange | RLIntRange)[]}
*/
get states() {
return this._env.states
}
/**
* Actions
*
* @type {(*[] | RLRealRange | RLIntRange)[]}
*/
get actions() {
return this._env.actions
}
/**
* Resolution
*
* @type {number}
*/
get resolution() {
return this._resolution
}
_state_index(state) {
return state.map((s, i) => {
const si = this.states[i]
if (Array.isArray(si)) {
return si.indexOf(s)
} else if (si instanceof RLIntRange) {
return si.indexOf(s, this.resolution)
} else if (si instanceof RLRealRange) {
return si.indexOf(s, this.resolution)
} else {
throw 'Not implemented'
}
})
}
_state_value(index) {
return index.map((s, i) => {
const si = this.states[i]
if (Array.isArray(si)) {
return si[s]
} else if (si instanceof RLIntRange) {
return si.toArray(this.resolution)[s]
} else if (si instanceof RLRealRange) {
return (s * (si.max - si.min)) / this.resolution + si.min
} else {
throw 'Not implemented'
}
})
}
_action_index(action) {
return action.map((a, i) => {
const ai = this.actions[i]
if (Array.isArray(ai)) {
return ai.indexOf(a)
} else if (ai instanceof RLRealRange) {
return ai.indexOf(a, this.resolution)
} else {
throw 'Not implemented'
}
})
}
_action_value(index) {
return index.map((a, i) => {
const ai = this.actions[i]
if (Array.isArray(ai)) {
return ai[a]
} else if (ai instanceof RLRealRange) {
return (a * (ai.max - ai.min)) / this.resolution + ai.min
} else {
throw 'Not implemented'
}
})
}
_to_position(size, index) {
let s = 0
for (let i = 0; i < index.length; i++) {
s = s * size[i] + index[i]
}
let e = s + 1
for (let i = index.length; i < size.length; i++) {
s *= size[i]
e *= size[i]
}
return [s, e]
}
_to_index(size, position) {
const a = Array(size.length)
for (let i = size.length - 1; i >= 0; i--) {
a[i] = position % size[i]
position = Math.floor(position / size[i])
}
return a
}
_q(state, action) {
if (!action) {
const [s, e] = this._to_position(this._sizes, state)
return [this._table.slice(s, e), [s, e]]
}
const [s] = this._to_position(this._sizes, [...state, ...action])
return [this._table[s], s]
}
/**
* Returns Q-table as array.
*
* @returns {*[]} Nested array
*/
toArray() {
return this._tensor.toArray()
}
/**
* Returns the best action.
*
* @param {*[]} state Current states
* @returns {*[]} Action
*/
best_action(state) {
const [q] = this._q(this._state_index(state))
const mv = Math.max(...q)
const midx = []
for (let i = 0; i < q.length; i++) {
if (q[i] === mv) midx.push(i)
}
let m = midx[Math.floor(Math.random() * midx.length)]
const a = this._to_index(this._action_sizes, m)
return this._action_value(a)
}
}
class QTable extends QTableBase {
constructor(env, resolution = 20, alpha = 0.2, gamma = 0.99) {
super(env, resolution)
this._alpha = alpha
this._gamma = gamma
}
update(action, state, next_state, reward) {
action = this._action_index(action)
state = this._state_index(state)
next_state = this._state_index(next_state)
const [next_q] = this._q(next_state)
const next_max_q = Math.max(...next_q)
const [q_value, qs] = this._q(state, action)
this._table[qs] += this._alpha * (reward + this._gamma * next_max_q - q_value)
}
}
/**
* Q-learning agent
*/
export default class QAgent {
/**
* @param {RLEnvironmentBase} env Environment
* @param {number} [resolution] Resolution
*/
constructor(env, resolution = 20) {
this._env = env
this._table = new QTable(env, resolution)
}
/**
* Returns a score.
*
* @returns {Array<Array<Array<number>>>} Score values
*/
get_score() {
return this._table.toArray()
}
/**
* Returns a action.
*
* @param {*[]} state Current states
* @param {number} greedy_rate Greedy rate
* @returns {*[]} Action
*/
get_action(state, greedy_rate = 0.002) {
if (Math.random() > greedy_rate) {
return this._table.best_action(state)
} else {
return this._env.sample_action(this)
}
}
/**
* Update model.
*
* @param {*[]} action Action
* @param {*[]} state Current state
* @param {*[]} next_state Next state
* @param {number} reward Reward
*/
update(action, state, next_state, reward) {
this._table.update(action, state, next_state, reward)
}
}