-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfof.js
165 lines (138 loc) · 3.74 KB
/
fof.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
var time = [0]
var rewards = [0]
var avg_rewards = [0]
var probs = [0.5, 0.5]
var learning_rate = 0.25
var agent = "Random"
function updateProbs(probs, choice, learning_rate) {
var p = [0, 0]
p[0] = learning_rate * (1.0 - choice) + (1.0 - learning_rate) * probs[0]
p[1] = learning_rate * choice + (1.0 - learning_rate) * probs[1]
p[0] = p[0] / (p[0] + p[1])
p[1] = p[1] / (p[0] + p[1])
return p
}
function reset() {
rewards = [0]
avg_rewards = [0]
time = [0]
probs = [0.5, 0.5]
plot(time, rewards, avg_rewards)
document.getElementById("revealed").style.display = "none"
document.getElementById("reveal").style.display = "block"
document.getElementById("probs").style.display = "none"
document.getElementById("des").value = "Describe how you think each agent works."
}
function choose(agent, probs) {
if (agent == "Good") {
if (probs[0] > probs[1]) {
return 0
} else {
return 1
}
return sample(probs)
}
if (agent == "Evil") {
if (probs[0] > probs[1]) {
return 1
} else {
return 0
}
}
if (agent == "Random") {
return Math.round(Math.random())
}
}
function sample(probs) {
x = Math.random()
if (x > probs[0]) {
return 1
} else {
return 0
}
}
function getReward(choice, ans) {
if (choice == ans) {
return 1
} else {
return -1
}
}
function step(choice) {
var ans = choose(agent, probs)
console.log(ans)
console.log(agent)
var r = getReward(choice, ans)
console.log(learning_rate)
probs = updateProbs(probs, choice, learning_rate)
document.getElementById("p").innerHTML = "[ " + probs[0].toFixed(3) + ", " + probs[1].toFixed(3) + " ]"
rewards.push(r)
var t = time.length + 1
time.push(t)
var n = avg_rewards.length
if (n == 1) {
var ar = r
} else {
var ar = (r + n * avg_rewards[n - 1]) / (n + 1)
}
avg_rewards.push(ar)
plot(time, rewards, avg_rewards)
}
function plot(time, rewards, avg_rewards) {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: time,
datasets: [{
label: "Rewards",
data: rewards,
borderWidth: 1
},
{
label: "Avg Rewards",
data: avg_rewards,
borderWidth: 1,
borderColor: "#FF0000",
backgroundColor: "rgba(1,1,1,0)"
}
]
},
options: {
animation: {
duration: 0
},
scales: {
yAxes: [{
ticks: {
beginAtZero: false
}
}]
}
}
});
}
document.getElementById("reset").onclick = reset
document.getElementById("reveal").onclick = function () {
document.getElementById("revealed").style.display = "block"
document.getElementById("reveal").style.display = "none"
document.getElementById("probs").style.display = "block"
}
document.getElementById("Agent1").onclick = function () {
agent = "Random"
}
document.getElementById("Agent2").onclick = function () {
agent = "Evil"
}
document.getElementById("Agent3").onclick = function () {
agent = "Good"
}
document.getElementById("box1").onclick = function () {
step(0)
}
document.getElementById("box2").onclick = function () {
step(1)
}
document.getElementById("lrc").onclick = function () {
learning_rate = parseFloat(document.getElementById("lr").value)
}