This repository was archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
422 lines (399 loc) · 14.3 KB
/
Player.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Player instance
*
* @constructor
* @param {Position} _position The starting position of this player
* @param {Environment} _env The environment this player exists in
* @property {Position} position The starting position of this player
* @property {Environment} env The environment this player exists in
* @property {QLearner} qlearner The associated lerner
* @property {Boolean} goalReached Set to `true` if the players's goal is reached
* @author Christian Vogt <[email protected]>
* @todo document methods and properties
*/
function Player(_position, _env) {
checkInstance(_position, Position);
checkInstance(_env, Environment);
this.position = _position;
this.startPosition = JSON.stringify(_position);
this.env = _env;
this.qlearner = new QLearner(this);
this.goalReached = false;
/**
* Move the Player with help of the learner
*
*/
this.move = function(_cb) {
this.qlearner.move(_cb);
};
/**
* Moves this Player up
*
* @returns {Player} Me
*/
this.moveUp = function() {
this.position = this.env.getResultingPosition(this.position, this.position.up());
return this;
};
/**
* Moves this Player right
*
* @returns {Player} Me
*/
this.moveRight = function() {
this.position = this.env.getResultingPosition(this.position, this.position.right());
return this;
};
/**
* Moves this Player down
*
* @returns {Player} Me
*/
this.moveDown = function() {
this.position = this.env.getResultingPosition(this.position, this.position.down());
return this;
};
/**
* Moves this Player left
*
* @returns {Player} Me
*/
this.moveLeft = function() {
this.position = this.env.getResultingPosition(this.position, this.position.left());
return this;
};
/**
* Doesnt do anything
*
* @returns {Player} Me
*/
this.stay = function() {
return this;
};
/**
* Performs the given action, and returns the resulting Position.
*
* @param {ACTIONS} _action The Action to perform
* @returns {Position} The resulting position
* @todo why do we call `reward()` here?
*/
this.performAction = function(_action) {
switch (_action) {
case ACTIONS.up: this.moveUp(); break;
case ACTIONS.right: this.moveRight(); break;
case ACTIONS.down: this.moveDown(); break;
case ACTIONS.left: this.moveLeft(); break;
}
this.reward();
return this.position;
};
/**
* Determine if the player is stuck, i.e. cannot move anywhere
*
* @returns {Boolean} `true` if the player is stuck
*/
this.amIStuck = function() {
var pos;
pos = this.env.getResultingPosition(this.position, this.position.up());
if (!pos.equals(this.position)) return false;
pos = this.env.getResultingPosition(this.position, this.position.right());
if (!pos.equals(this.position)) return false;
pos = this.env.getResultingPosition(this.position, this.position.down());
if (!pos.equals(this.position)) return false;
pos = this.env.getResultingPosition(this.position, this.position.left());
if (!pos.equals(this.position)) return false;
return true;
};
/**
* Identifies the player in the environment. This is used by the learner to build the `ACTIONS` table.
* Can be overridden by agents that shall perform differently.
*
* @returns {String} the hash of this player
*/
this.hash = function() {
var hash = this.position.hash();
return hash;
};
/**
* Reward function that helps the learner to determine if a actions has been a good or bad choice.
* Should be overriden by agents that shall perform differently.
*
* @returns {number} the given reward
*/
this.reward = function() {
return 0;
};
/**
* Shuffle this players position.
*
* @returns {Position} The resulting Position
*/
this.shufflePosition = function() {
var x = Math.floor(Math.random() * this.env.width);
var y = Math.floor(Math.random() * this.env.height);
this.position = this.env.getResultingPosition(this.position, new Position(x, y));
//return this.position;
};
}
/**
* Standard hunter. Gets rewarded when the distance to a victim is down to 1.
*
* @namespace Player.Hunter
* @constructor
* @extends Player
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function Hunter(_position, _env) {
Player.call(this, _position, _env);
this.reward = function() {
for(var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof Victim) {
// find victims
var victim = this.env.players[i];
if ( this.position.distance(victim.position) <= 1 ) { // distance to victim <1? then win!
this.goalReached = true;
return 100;
}
}
}
// this.goalReached = false;
return -1;
};
}
Hunter.prototype = Object.create(Player.prototype);
Hunter.prototype.constructor = Hunter;
/**
* Senses victims in the area that are in sight (determined by the `sight` value that is the maximum distance as of `Position.distance()`)
*
* @namespace Player.SensingHunter
* @constructor
* @extends Hunter
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function SensingHunter(_position, _env) {
Hunter.call(this, _position, _env);
this.sight = 0;
this.hash = function() {
var hash = this.position.hash();
for (var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof Victim && this.sense(this.env.players[i]) !== false) {
hash += this.sense(this.env.players[i]);
}
}
return hash;
};
this.sense = function(_player) {
if (this.position.distance(_player.position) <= this.sight) {
return this.position.directionTo(_player.position);
} else {
return false;
}
};
}
SensingHunter.prototype = Object.create(Hunter.prototype);
SensingHunter.prototype.constructor = SensingHunter;
/**
* Randomly moving victim.
*
* @namespace Player.Victim
* @constructor
* @extends Player
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function Victim(_position, _env) {
Player.call(this, _position, _env);
this.move = function() {
this.performAction(Math.floor(Math.random()*Object.keys(ACTIONS).length));
};
}
Victim.prototype = Object.create(Player.prototype);
Victim.prototype.constructor = Victim;
/**
* This player does not move at all but maps all action to "stay".
*
* @namespace Player.StillVictim
* @constructor
* @extends Victim
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function StillVictim(_position, _env) {
Victim.call(this, _position, _env);
this.performAction = function(_action) {
// this.stay();
return new Position(this.position.posx, this.position.posy);
};
}
StillVictim.prototype = Object.create(Victim.prototype);
StillVictim.prototype.constructor = StillVictim;
/**
* Bad ass hunter! Knows about the distance and direction to the victim
*
* @namespace Player.OptimizedSensingHunter
* @constructor
* @extends SensingHunter
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function OptimizedSensingHunter(_position, _env) {
SensingHunter.call(this, _position, _env);
this.sense = function(_player) {
if (this.position.distance(_player.position) <= this.sight) {
var result = this.position.directionTo(_player.position);
result += this.position.distance(_player.position);
return result;
} else {
return false;
}
};
}
OptimizedSensingHunter.prototype = Object.create(SensingHunter.prototype);
OptimizedSensingHunter.prototype.constructor = OptimizedSensingHunter;
/**
* Senses victims and other hunters, sends directions to other TeamHunters
*
* @namespace Player.TeamHunter
* @constructor
* @extends SensingHunter
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function TeamHunter(_position, _env) {
SensingHunter.call(this, _position, _env);
this.askOtherTeamHunter = function(_victim) {
var results = [];
for (var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof TeamHunter && this.env.players[i] !== this) {
results.push(this.env.players[i].sense(_victim));
}
}
return results;
};
this.hash = function() {
var hash = this.position.hash(); // my position
for (var i=0; i<this.env.players.length; i++) {
hash += this.sense(this.env.players[i]);
if (this.env.players[i] instanceof Victim && this.sense(this.env.players[i]) !== false) {
//hash += this.sense(this.env.players[i]); // my sensing
var direction = this.askOtherTeamHunter(this.env.players[i]); // the other hunters senses
if (direction) hash += direction;
else hash += 'notSensed';
}
}
return hash;
};
}
TeamHunter.prototype = Object.create(SensingHunter.prototype);
TeamHunter.prototype.constructor = TeamHunter;
/**
* Tries to stuck the victim
*
* @namespace Player.StuckTeamHunter
* @constructor
* @extends TeamHunter
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function StuckTeamHunter(_position, _env) {
TeamHunter.call(this, _position, _env);
this.reward = function() {
for(var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof Victim) {
// find victims
var victim = this.env.players[i];
if ( this.position.distance(victim.position) <= 1 && (victim.amIStuck() === true)) { // distance to victim <=1? then check if the victim cannot move anymore
/*if ( !this.env.getResultingPosition(victim.position, victim.position.up()).equals(victim.position) ) return 5;
if ( !this.env.getResultingPosition(victim.position, victim.position.right()).equals(victim.position) ) return 5;
if ( !this.env.getResultingPosition(victim.position, victim.position.down()).equals(victim.position) ) return 5;
if ( !this.env.getResultingPosition(victim.position, victim.position.left()).equals(victim.position) ) return 5;*/
this.goalReached = true;
return 100;
} else if ( this.position.distance(victim.position) <= 3 ) {
//return 1;
}
}
}
// this.goalReached = false;
return -1;
};
this.hash = function() {
var hash = '';
this.sight = 99;
for (var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof Victim && this.sense(this.env.players[i]) !== false) {
hash += 'victim';
hash += this.position.distance(this.env.players[i].position); // distance to victim
hash += this.sense(this.env.players[i]); // direction to victim
hash += this.askOtherTeamHunter(this.env.players[i]).toString(); // the other hunters directions to victim
}
/*if (this.env.players[i] instanceof Hunter) {
hash += 'hunter';
hash += this.sense(this.env.players[i]); // direction to my team
hash += this.position.distance(this.env.players[i].position); // distance to my team
}*/
}
return hash;
};
}
StuckTeamHunter.prototype = Object.create(TeamHunter.prototype);
StuckTeamHunter.prototype.constructor = StuckTeamHunter;
/**
* Controllable by keyboard
*
* @namespace Player.ManualVictim
* @constructor
* @extends StillVictim
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function ManualVictim(_position, _env) {
StillVictim.call(this, _position, _env);
this.move = function(){};
this.shufflePosition = function() {
};
var self = this;
window.addEventListener("keydown", function(evt){
if (evt.keyIdentifier === 'Up') {
evt.preventDefault();
self.moveUp();
} else if (evt.keyIdentifier === 'Right') {
evt.preventDefault();
self.moveRight();
} else if (evt.keyIdentifier === 'Down') {
evt.preventDefault();
self.moveDown();
} else if (evt.keyIdentifier === 'Left') {
evt.preventDefault();
self.moveLeft();
}
}, false);
}
ManualVictim.prototype = Object.create(Victim.prototype);
ManualVictim.prototype.constructor = ManualVictim;
/**
* This hunter has a huge stateTable because it hashes its position with the position of the victim.
* Use with care as it will take forever to train him.
*
* @namepsace Player.Hunter
* @constructor
* @extends Hunter
* @param {Position} _position the starting position of this player
* @param {Environment} _env the environment this player exists in
*/
function AllKnowingHunter(_position, _env) {
Hunter.call(this, _position, _env);
this.hash = function() {
var hash = this.position.hash();
for (var i=0; i<this.env.players.length; i++) {
if (this.env.players[i] instanceof Victim) {
hash += this.env.players[i].position.hash();
}
}
return hash;
};
}
AllKnowingHunter.prototype = Object.create(Hunter.prototype);
AllKnowingHunter.prototype.constructor = AllKnowingHunter;