-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuctAI.js
352 lines (313 loc) · 9.56 KB
/
uctAI.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
var AITools = require('./AITools.js');
var fs = require('fs');
var myTeam = null;
// Hack for undefined object.length
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
function setTeam(team){
myTeam = team;
}
// Uses Pseudo-static member model.BitArrayMembers
// If we start using bit arrays for anything but pokemon, we could overflow our int.
function BitArray(){
this.keys = model.BitArrayKeys;
this.shift = 0; for (k in this.keys) this.shift += this.keys[k].width;
this.data = 0;
this.set = function(key, value, maxValue)
{
if (isNaN(value)) value = 0;
//if (isNaN(maxValue)) maxValue = 1;
if (!(key in this.keys))
{
var width = Math.floor(Math.log(maxValue) / Math.log(2)) + 1;
this.keys[key] = {'shift':this.shift, 'width':width};
this.shift += width;
}
this.data |= value << this.keys[key].shift;
}
this.get = function(key)
{
if (!(key in this.keys)) throw "Key not valid";
var mask = this.keys[key].width << this.keys[key].shift;
value = (this.data & (mask)) >> this.keys[key].shift
return value;
}
}
// Returns {mySide:[BitArray-per-pokemon], yourSide:[BitArray-per-pokemon]}
function DehashState(state){
var output = {myPokemon:[], yourPokemon:[]};
var split = state.split(',');
for (i = 0; i < 6; i++){
p = new BitArray()
p.data = +split[i];
output.myPokemon.push(p);
}
for (i = 6; i < split.length; i++){
p = new BitArray()
p.data = +split[i];
output.yourPokemon.push(p);
}
return output
}
// Returns a number representing a pokemon's state
function HashPokemon(pokemon, isActive){
var output = new BitArray();
if (isActive) output.set('active', 1, 1);
var c_numHpBuckets = 4;
percenthp = pokemon.hp / pokemon.maxhp * 100;
bucketHP = Math.floor(percenthp / (101 / c_numHpBuckets));
output.set('hp', bucketHP, c_numHpBuckets);
var c_numStatBuckets = 4;
bucketAtk = Math.floor(pokemon.baseStats.atk / (256 / c_numStatBuckets));
output.set('atk', bucketAtk, c_numStatBuckets);
bucketDef = Math.floor(pokemon.baseStats.def / (256 / c_numStatBuckets));
output.set('def', bucketDef, c_numStatBuckets);
bucketSpA = Math.floor(pokemon.baseStats.spa / (256 / c_numStatBuckets));
output.set('spa', bucketSpA, c_numStatBuckets);
bucketSpD = Math.floor(pokemon.baseStats.spd / (256 / c_numStatBuckets));
output.set('spd', bucketSpD, c_numStatBuckets);
types = {"None": 0, "Dark": 1, "Dragon": 2, "Electric": 3, "Fighting": 4, "Fire": 5, "Flying": 6, "Ghost": 7, "Grass": 8, "Ground": 9,"Ice": 10, "Normal": 11, "Poison": 12, "Psychic": 13, "Rock": 14, "Steel": 15, "Water": 16, "Bug": 17};
type1 = pokemon.types[0][0].toUpperCase() + pokemon.types[0].slice(1);
output.set('type1', types[type1], Object.size(types));
if (pokemon.types[1]){
type2 = pokemon.types[1][0].toUpperCase() + pokemon.types[1].slice(1);
output.set('type2', types[type2], Object.size(types));
} else output.set('type2', types.length, Object.size(types)); // Set none
statuses = {'': 0, 'psn' : 1, 'slp': 2, 'tox': 3, 'brn': 4, 'frz': 5}
output.set('status', statuses[pokemon.status], Object.size(statuses));
return output.data;
}
// Returns twelve pokemon state numbers joined by ,
function HashState(battle){
var hashVal = '';
myPokes = battle.mySide.pokemon.slice(0);
myPokes.sort(function (a, b) { return ((a.species < b.species) ? -1 : ((a.species > b.species) ? 1 : 0)); });
myActive = battle.mySide.active[0];
for (pIdx in myPokes)
{
active = false;
if (myActive) active = (myPokes[pIdx].species == myActive.species);
myPokes[pIdx] = HashPokemon(myPokes[pIdx], active );
}
yourPokes = battle.yourSide.pokemon.slice(0);
yourPokes.sort(function (a, b) { return ((a.species < b.species) ? -1 : ((a.species > b.species) ? 1 : 0)); });
yourActive = battle.yourSide.active[0];
for (pIdx in yourPokes)
{
active = false;
if (yourActive) active = (yourPokes[pIdx].species == yourActive.species);
yourPokes[pIdx] = HashPokemon(yourPokes[pIdx], active);
}
var output = myPokes.join() + "," + yourPokes.join();
console.log(output);
return output;
}
// Contains a list of states in tree, and a transition
// function that isn't really necessary. Should be rolled into state list
function ModelStruct(){
this.BitArrayKeys = {}
this.wins = 0;
this.loses = 0;
// transition[state][action][statep] = #visits
this.transition = {};
// state: {cumulative reward, cnt}
this.states = {};
// Returns possible states and their visit numbers
this.getTransitionProb = function(state, action) {
if ((state in this.transition) && (action in this.transition[state]))
{
return this.transition[state][action];
}
return [];
}
this.addTransition = function(state, action, statep){
if (!(state in this.transition)) this.transition[state] = {};
if (!(action in this.transition[state])) this.transition[state][action] = {};
if (!(statep in this.transition[state][action])) this.transition[state][action][statep] = 0;
this.transition[state][action][statep]++;
}
this.getActionCount = function(state, action){
output = 0;
if ((state in this.transition) && (action in this.transition[state])) {
for (idx in this.transition[state][action]) {
output += this.transition[state][action][idx];
}
}
return output;
}
this.addState = function(state, reward){
if (state in this.states)
this.states[state].reward = (this.states[state].reward * this.states[state].count + reward) / (this.states[state].count + 1);
else
this.states[state] = {'reward': reward, 'count':0};
this.states[state].count++;
}
this.reward = function(state)
{
battle = DehashState(state); // battle.(my/your)Pokemon = [BitArrays]
output = 0;
for( p in battle.myPokemon){
output += battle.myPokemon[p].get('hp');
}
for( p in battle.yourPokemon){
output -= battle.yourPokemon[p].get('hp');
}
output -= 6 - battle.yourPokemon.length;
return output;
}
this.fold = function(coeff){
// TODO
}
}
var model = new ModelStruct();
var firstStateAction; // Action to add to our "tree"
var nodesToUpdate = []; // Nodes to update the reward of
var cumulativeReward = 0;
var lastState, lastAction;
function randomInt(){
var bytes = require('crypto').randomBytes(4);
var inter = bytes[0] +
(bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);
return inter > 0 ? inter : -inter;
}
function argMax(set){
y = 0;
x = set[0];
for (i in set) if (x < set[i]) { x = set[i]; y = i; }
return y;
}
function maxVal(set){
x = set[0];
for (i in set) if (x < set[i]) x = set[i];
return x;
}
// Runs just before next game. After add(Win/Lose)
function postGame(){
if (!learn) return;
// firstStateAction can be null if we never leave the tree...
// It doesn't hurt anything, so it almost seems useful?
model.addState(firstStateAction, cumulativeReward);
for (idx in nodesToUpdate)
model.addState(nodesToUpdate[idx], cumulativeReward);
nodesToUpdate = [];
firstStateAction = null;
cumulativeReward = 0;
}
function addWin(){
model.wins++;
cumulativeReward = 10;
}
function addLose(){
model.loses++;
cumulativeReward = -1;//-1;
}
function chooseMove(room, actions){
var c_c = 10; // expected horizon?
var currentstate = HashState(room.battle);
cumulativeReward += model.reward(currentstate);
console.log(cumulativeReward);
if (currentstate in model.states)
{ // Tree policy
nodesToUpdate.push(currentstate);
qVals = []
for (actIdx in actions)
{
var action = actions[actIdx];
q = model.states[currentstate].reward;
cnt = model.states[currentstate].count;
actCnt = model.getActionCount(currentstate, action.id);
if (greedy)
ex = 0;
else
ex = c_c * Math.sqrt(Math.log(cnt) / actCnt);
qVals.push(q+ex);
}
currentaction = actions[argMax(qVals)];
}
else
{
if (!firstStateAction) firstStateAction = currentstate;
rIdx = Math.floor(Math.random() * actions.length);
currentaction = actions[rIdx];
}
// Learning
if (lastState && learn) {
model.addTransition(lastState, lastAction, currentstate);
}
lastAction = currentaction.id;
lastState = currentstate;
if (currentaction.type != "switch")
{
console.log("Using " + currentaction.id);
room.formUseMove(currentaction.id);
}else{
console.log("Switching to " + currentaction.idx + ": " + currentaction.id);
room.formSwitchTo(currentaction.idx);
}
console.log();
};
function SaveState(){
if (learn)
{
if (fs.existsSync(randomFile))
{
fs.unlinkSync(randomFile);
}
lastAction = null;
var output = model;
var fd = fs.writeFileSync(randomFile, JSON.stringify(output));
console.log("Wrote to: " + randomFile);
}
};
function loadState(filename){
if (fs.existsSync(filename))
{
console.log("Loading " + filename);
var data = JSON.parse(fs.readFileSync(filename, 'utf8'));
model = $.extend(true, new ModelStruct(), data);
}
}
var randomFile = "aidata" + randomInt() + ".json";
var glie = false;
var greedy = false;
var learn = true;
if (process.argv.length > 2){
for (i = 2; i < process.argv.length; i++)
{
var arg = process.argv[i];
if (arg.indexOf('-') < 0){
try{
loadState(arg);
randomFile = arg;
}
catch(e){console.log(e);}
}else{
switch(arg.slice(1))
{
case 'glie':
console.log("GLIE mode enabled");
glie = true;
break;
case 'greedy':
console.log("Greedy mode enabled");
greedy = true;
break;
case 'learn':
console.log("Learning disabled");
learn = false;
break;
}
}
}
}
module.exports.setTeam = setTeam;
module.exports.SaveState = SaveState;
module.exports.chooseMove = chooseMove;
module.exports.addWin = addWin;
module.exports.addLose = addLose;
module.exports.postGame = postGame;