-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathq-learning.js
196 lines (156 loc) · 5.32 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
var QLearning = (function () {
// s(playerPose + fruitPose + size + trail) = state of the current position
// act(s) = best action so far
// rew = instant reward of taking this step
// s'(s, act) = new state
// Q(s, act) += LR * (rew + DF*max(Q(s',*)) - Q(s,act))
var qTable = {};
var learningRate = 0.85; // Learning Rate
var discountFactor = 0.9; // Discount Factor of Future Rewards
var randomize = 0.05; // Randomization Rate on Action
var availableActions = ['up', 'down', 'left', 'right'];
var score = 0;
var missed = 0;
var intervalID;
var defaultLoopsPerInterval = 1200;
var fullSetOfStates = false;
var whichStateNow = function () {
let tileCount = Snake.info.tileCount;
let player = Snake.data.player;
let fruit = Snake.data.fruit;
let fruitRelativePose = { x:0, y:0 };
let trail = Snake.data.trail();
let trailRelativePose = [];
fruitRelativePose.x = fruit.x - player.x;
while(fruitRelativePose.x < 0) fruitRelativePose.x += tileCount;
while(fruitRelativePose.x > tileCount) fruitRelativePose.x -= tileCount;
fruitRelativePose.y = fruit.y - player.y;
while(fruitRelativePose.y < 0) fruitRelativePose.y += tileCount;
while(fruitRelativePose.y > tileCount) fruitRelativePose.y -= tileCount;
var stateName = fruitRelativePose.x + ',' + fruitRelativePose.y;
// + ',' + trail.length;
const maxLength = (fullSetOfStates ? trail.length : 1);
for(let index = 0; index < maxLength; index++) {
if (trailRelativePose[index] == undefined) trailRelativePose.push({ x:0, y:0 });
trailRelativePose[index].x = trail[index].x - player.x;
while(trailRelativePose[index].x < 0) trailRelativePose[index].x += tileCount;
while(trailRelativePose[index].x > tileCount) trailRelativePose[index].x -= tileCount;
trailRelativePose[index].y = trail[index].y - player.y;
while (trailRelativePose[index].y < 0) trailRelativePose[index].y += tileCount;
while (trailRelativePose[index].y > tileCount) trailRelativePose[index].y -= tileCount;
stateName += ',' + trailRelativePose[index].x + ',' + trailRelativePose[index].y;
}
return stateName;
};
var whichTable = function (s) {
if(qTable[s] == undefined ) {
qTable[s] = { 'up':0, 'down':0, 'left':0, 'right':0 };
}
return qTable[s];
}
var bestAction = function (s) {
let q = whichTable(s);
if(Math.random() < randomize){
let random = Math.floor(Math.random() * availableActions.length);
return availableActions[random];
}
let maxValue = q[availableActions[0]];
let choseAction = availableActions[0];
let actionsZero = [];
for(let i = 0; i < availableActions.length; i++) {
if(q[availableActions[i]] == 0) actionsZero.push(availableActions[i]);
if(q[availableActions[i]] > maxValue){
maxValue = q[availableActions[i]];
choseAction = availableActions[i];
}
}
if(maxValue == 0){
let random = Math.floor(Math.random() * actionsZero.length);
choseAction = actionsZero[random];
}
return choseAction;
}
var updateQTable = function (state0, state1, reward, act) {
var q0 = whichTable(state0);
var q1 = whichTable(state1);
var newValue = reward + discountFactor * Math.max(q1.up, q1.down, q1.left, q1.right) - q0[act];
qTable[state0][act] = q0[act] + learningRate * newValue;
}
function Algorithm () {
var currentState = whichStateNow();
var action = bestAction(currentState);
Snake.action(action);
var instantReward = Snake.loop();
var nextState = whichStateNow();
updateQTable(currentState, nextState, instantReward, action);
if(instantReward > 0) score += Math.trunc(instantReward);
if(instantReward < 0) missed += Math.trunc(instantReward);
}
return {
run: function () {
clearInterval(intervalID);
intervalID = setInterval(Algorithm, 1000/15);
},
stop: function () {
clearInterval(intervalID);
},
startTrain: function (loopsPerInterval) {
clearInterval(intervalID);
const loops = loopsPerInterval ? loopsPerInterval : defaultLoopsPerInterval;
intervalID = setInterval(() => {
for (let index = 0; index < loops; index++) {
Algorithm();
}
}, 1000/15);
},
stopTrain: function () {
clearInterval(intervalID);
},
reset: function () {
qTable = {};
score = 0;
missed = 0;
},
changeConst: {
LearningRate: function (lr) {
learningRate = lr;
},
DiscountFactor: function (df) {
discountFactor = df;
},
Randomization: function (rand) {
randomize = rand;
},
FullSetOfStates: function (fullSet) {
fullSetOfStates = fullSet;
}
},
changeFPS: function (fps) {
clearInterval(intervalID);
intervalID = setInterval(Algorithm, 1000/fps);
},
changeSpeed: function (ms) {
clearInterval(intervalID);
intervalID = setInterval(Algorithm, ms);
},
info: {
score: function () {
return score;
},
missed: function () {
return missed;
}
},
qTable: {
show: function () {
console.table(qTable);
},
export: function () {
return qTable;
},
import: function (newQ) {
qTable = newQ;
}
}
}
})();