-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
166 lines (134 loc) · 3.56 KB
/
ai.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
///////////////////////////////////////////////
// //
// AI Module //
// //
///////////////////////////////////////////////
var ai = (function (){
// Game variables
var learnRate = 0.15;
var moves = ["L", "R"];
var points = {"L": -1, "R": 1};
var gameTree = new Tree("", null);
var history = "";
// Tree class to store playing history
function Tree(path, parent){
this.path = path;
this.parent = parent;
this.children = [];
this.weight = {};
for (var i = 0; i < moves.length; i++){
this.weight[moves[i]] = 1;
}
this.subtreeWeight = 1;
}
Tree.prototype.addChildren = function (){
for (var i = 0; i < moves.length; i++){
var newPath = this.path + moves[i];
this.children.push(new Tree(newPath, this));
}
}
Tree.prototype.search = function (path){
if (path == this.path){
return this;
}else{
if (this.children.length == 0){
this.addChildren();
}
for (var i = 0; i < this.children.length; i++){
var child = this.children[i];
if (path.indexOf(child.path) == 0){
return child.search(path);
}
}
}
}
Tree.prototype.meanWeight = function (){
var sumWeight = 0;
for (var i = 0; i < moves.length; i++){
sumWeight += this.weight[moves[i]];
}
return sumWeight / moves.length;
}
Tree.prototype.meanPred = function (){
var sumPred = 0;
for (var i = 0; i < moves.length; i++){
sumPred += this.weight[moves[i]] * points[moves[i]];
}
return sumPred / moves.length;
}
Tree.prototype.multChildSubtreeWeight = function (){
var product = 1;
for (var i = 0; i < this.children.length; i++){
product *= this.children[i].subtreeWeight;
}
return product;
}
Tree.prototype.updateWeight = function (playerMove){
for (var i = 0; i < moves.length; i++){
this.weight[moves[i]] *= Math.exp(-learnRate * Math.abs(points[playerMove] - points[moves[i]]));
}
}
Tree.prototype.updateSubtreeWeight = function (){
if (this.children.length == 0){
this.subtreeWeight = this.meanWeight();
}else if (this.path != ""){
this.subtreeWeight = this.meanWeight() + this.multChildSubtreeWeight();
}else{
this.subtreeWeight = this.multChildSubtreeWeight();
}
}
// Helper functions
function onPath(node){
if (history.indexOf(node.path) == 0){
return false;
}
return true;
}
function getPrediction(){
var prediction;
var currentNode = gameTree.search(history);
var subtreePred = currentNode.meanPred();
if (currentNode.path == ""){
subtreePred = 2 * Math.random() - 1;
}
while (currentNode.path != ""){
currentNode = currentNode.parent;
nonPathChild = currentNode.children.filter(onPath)[0];
if (currentNode.path == ""){
subtreePred *= nonPathChild.subtreeWeight;
}else{
subtreePred = subtreePred * nonPathChild.subtreeWeight + currentNode.meanPred();
}
}
prediction = subtreePred / currentNode.subtreeWeight;
if (prediction >= 0){
return moves[1];
}else{
return moves[0];
}
}
function updateWeights(playerMove){
var currentNode = gameTree.search(history);
while (currentNode){
currentNode.updateWeight(playerMove);
currentNode.updateSubtreeWeight();
currentNode = currentNode.parent;
}
}
function updateHistory(playerMove){
history = playerMove + history;
}
function resetAI(){
history = "";
gameTree = new Tree("", null);
}
// Public methods
return {
predict: getPrediction,
reset: resetAI,
update: function (playerMove){
updateWeights(playerMove);
updateHistory(playerMove);
}
}
})()