-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdifficulty.js
435 lines (362 loc) · 9.94 KB
/
difficulty.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
423
424
425
426
427
428
429
430
431
432
433
434
435
//EXPERIMENT FUNCTIONS
Grader = function(parent, streak_length, max_value){
this.Es = [];
this.signal = new Phaser.Signal();
this.streak_length= streak_length;
this.parent = parent;
this.streak = 'NaN';
db_ref = firebase.database().ref("settings")
.child(firebase.auth().currentUser.uid)
.child(parent.task)
.child("durations");
db_ref.on('value', function(snapshot) {
this.set_max_value(snapshot.val());
}, function(error) {}, this);
this.set_max_value = function (durations) {
console.log(durations);
this.max_value = durations['stimulus'] * 1000;
}
this.grade = function(user_resp, CRESP, RT) {
if (user_resp == CRESP) {
ACC = 1;
}
else {ACC = 0}
this.ACC = ACC;
//check for timeout
if (RT != "NA") {
this.easyness = ACC - (RT / this.max_value);
}
else {
this.easyness = -1;
}
this.Es.push(this.easyness);
l = this.Es.length;
streak_sum = 0;
for (var i=1; i<= this.streak_length; i++) {
streak_sum += this.Es[l - i];
}
if (this.Es.length >= this.streak_length) {
this.streak = streak_sum / this.streak_length;
this.parent.logger.inputData('avg_easyness', this.streak);
}
//log ye relevant data
this.parent.logger.inputData('CRESP', CRESP);
this.parent.logger.inputData('user_resp', user_resp);
this.parent.logger.inputData('ACC', ACC);
this.parent.logger.inputData('RT', RT);
this.parent.logger.inputData('performance', this.easyness);
this.parent.logger.inputData('practice', this.parent.practice.practice);
this.parent.logger.inputData('mobile', this.parent.mobile);
//log this trial's worth of data
this.parent.logger.sendData(this.parent.trial_clock.trial);
this.signal.dispatch("grade"); //TODO - do we even use this anymore?
debug = RT + " " + ACC + " " + this.easyness + " " + this.streak;
//console.log(debug);
}
};
Param_Space = function(params, names, signal, logger, grader) {
this.params = params;
this.names = names;
this.stuck = false;
this.score = 0;
this.logger = logger;
this.grader = grader;
//initialize indexes
this.indexes = [];
items = [];
for (var i=0; i<params.length; i++) {
this.indexes[i] = 0;
items.push(i);
}
//create a give_items objects
this.give_diff = new GiveItems(items, 2);
this.signal = signal;
this.normed_indexes = function () {
ni = [];
for (var i=0; i<params.length; i++) {
j = this.indexes[i] / (this.params[i].length-1);
ni.push(j);
}
return ni;
}
this.adjust = function (increments, random) {
//if we have an increment for each parameter
//adjust them all
random = true;
if (increments.length == this.params.length) {
//determine whether we want to retain the order
for (var i=0; i<this.params.length; i++) {
if (random == true) {
this.indexes[this.give_diff.next()] += increments[i];
} else {
this.indexes[i] += increments[i];
}
}
}
//otherwise, use give_diff to select which params(s)
//to adjust
else {
for (var i=0; i<increments.length; i++) {
this.indexes[this.give_diff.next()] += increments[i];
}
}
//now check this indexes
this.check_indexes();
}
this.search = function () {
if (this.new_xs.length > 1 && this.new_ys.length > 1) {
//$('#console').html(this.new_xs[0] + " " + this.new_ys[1]);
this.indexes[0] = this.new_xs.shift();
this.indexes[1] = this.new_ys.shift();
if (this.indexes.length == 3) {
this.indexes[2] = this.new_zs.shift();
}
}
else {
this.signal.dispatch('end_task');
}
this.check_indexes();
}
this.check_indexes = function () {
//ensure that the indexes have not passed into illegal values
maxed_sum = 0;
for (var i=0; i<this.params.length; i++) {
//hold at max if too high
if (this.indexes[i] > this.params[i].length - 1) {
this.indexes[i] = this.params[i].length - 1;
maxed_sum += 1;
}
//increase to 0 if too low
if (this.indexes[i] < 0) {
this.indexes[i] = 0;
}
ni = this.normed_indexes();
//TODO log the normed index
label = "diff" + (i + 1);
this.logger.inputData(label, ni[i]);
}
//if all params are maxed, call this the sticking point
//$('#console').html(maxed_sum + ' ' + this.indexes.length);
if (this.stuck==true) {
//
}
else {
if (maxed_sum == this.indexes.length) {
this.sticking_point();
}
}
}
this.sticking_point = function() {
this.stuck = true;
this.new_xs = [];
this.new_ys = [];
this.new_zs = [];
ni = this.normed_indexes();
if (this.indexes.length == 2) {
//save the person's score
this.score = (ni[0] + ni[1]) / 2;
this.make_square();
}
else if (this.indexes.length == 3) {
//save the person's score
this.score = (ni[0] + ni[1] + ni[2]) / 3;
this.make_cube();
}
else {
$('#console').html('Too many difficulty dimensions');
this.score = 'NA'
}
this.logger.log('summary', {'score' : this.score, 'ni': ni})
this.search();
}
this.make_cube = function () {
//make a cube around the sticking point
x = this.indexes[0];
y = this.indexes[1];
z = this.indexes[2];
//front right
this.new_xs.push(x + 1);
this.new_ys.push(y + 1);
this.new_zs.push(z + 1);
this.new_xs.push(x + 1);
this.new_ys.push(y);
this.new_zs.push(z + 1);
this.new_xs.push(x + 1);
this.new_ys.push(y-1);
this.new_zs.push(z + 1);
//mid right
this.new_xs.push(x + 1);
this.new_ys.push(y + 1);
this.new_zs.push(z);
this.new_xs.push(x + 1);
this.new_ys.push(y);
this.new_zs.push(z);
this.new_xs.push(x + 1);
this.new_ys.push(y-1);
this.new_zs.push(z);
//back right
this.new_xs.push(x + 1);
this.new_ys.push(y + 1);
this.new_zs.push(z - 1);
this.new_xs.push(x + 1);
this.new_ys.push(y);
this.new_zs.push(z - 1);
this.new_xs.push(x + 1);
this.new_ys.push(y-1);
this.new_zs.push(z - 1);
//front left
this.new_xs.push(x - 1);
this.new_ys.push(y + 1);
this.new_zs.push(z + 1);
this.new_xs.push(x - 1);
this.new_ys.push(y);
this.new_zs.push(z + 1);
this.new_xs.push(x - 1);
this.new_ys.push(y-1);
this.new_zs.push(z + 1);
//mid left
this.new_xs.push(x - 1);
this.new_ys.push(y + 1);
this.new_zs.push(z);
this.new_xs.push(x - 1);
this.new_ys.push(y);
this.new_zs.push(z);
this.new_xs.push(x - 1);
this.new_ys.push(y-1);
this.new_zs.push(z);
//back left
this.new_xs.push(x - 1);
this.new_ys.push(y + 1);
this.new_zs.push(z - 1);
this.new_xs.push(x - 1);
this.new_ys.push(y);
this.new_zs.push(z - 1);
this.new_xs.push(x - 1);
this.new_ys.push(y-1);
this.new_zs.push(z - 1);
//front mid
this.new_xs.push(x);
this.new_ys.push(y + 1);
this.new_zs.push(z + 1);
this.new_xs.push(x);
this.new_ys.push(y);
this.new_zs.push(z + 1);
this.new_xs.push(x);
this.new_ys.push(y-1);
this.new_zs.push(z + 1);
//mid mid
this.new_xs.push(x);
this.new_ys.push(y + 1);
this.new_zs.push(z);
this.new_xs.push(x);
this.new_ys.push(y-1);
this.new_zs.push(z);
//back mid
this.new_xs.push(x);
this.new_ys.push(y + 1);
this.new_zs.push(z - 1);
this.new_xs.push(x);
this.new_ys.push(y);
this.new_zs.push(z - 1);
this.new_xs.push(x);
this.new_ys.push(y-1);
this.new_zs.push(z - 1);
}
this.make_square = function () {
//make a square around the sticking point
x = this.indexes[0];
y = this.indexes[1];
//right
this.new_xs.push(x + 1);
this.new_ys.push(y + 1);
this.new_xs.push(x + 1);
this.new_ys.push(y);
this.new_xs.push(x + 1);
this.new_ys.push(y - 1);
//center
this.new_xs.push(x);
this.new_ys.push(y + 1);
this.new_xs.push(x);
this.new_ys.push(y - 1);
//left
this.new_xs.push(x-1);
this.new_ys.push(y + 1);
this.new_xs.push(x-1);
this.new_ys.push(y);
this.new_xs.push(x-1);
this.new_ys.push(y - 1);
}
this.make_lines = function() {
//find slope of difficulty line
m = this.indexes[1] / this.indexes[0];
//draw new set of points, one above and one below difficulty line
for (var x = this.indexes[0] + 1; x >= 0; x--) {
this.new_xs.push(x);
this.new_ys.push(Math.round(m * x) + 1);
this.new_xs.push(x);
this.new_ys.push(Math.round(m * x) - 1);
}
}
this.get = function (p) {
index = this.indexes[p];
value = this.params[p][Math.round(index)];
this.logger.inputData(this.names[p], value);
return value;
}
this.reset = function () {
this.grader.Es = [];
for (var i=0; i<this.params.length; i++) {
this.indexes[i] = 0;
}
}
};
Difficulty = function(parent, params, names, search_params) {
this.param_space = new Param_Space(params, names, parent.signal, parent.logger, parent.grader);
this.search_params = search_params;
this.parent = parent;
this.adjust = function(){
easyness = this.parent.grader.easyness;
//easier to assume we are not stuck, can overwrite later
this.parent.logger.inputData('stuck', false);
//if we have already found the sticking point
if (this.param_space.stuck == true) {
this.param_space.search();
this.parent.logger.inputData('stuck', true);
}
else {
//sticking point?
if (this.parent.grader.streak < 0) {
this.param_space.sticking_point();
this.parent.logger.inputData('stuck', true);
}
//otherwise
else {
//first check accuracy
if (easyness < 0 ) {
this.param_space.adjust(this.search_params[0]);
}
else {
//if we are just practicing, do not increase the params by much
if (this.parent.practice == true) {
this.param_space.adjust([1,0]);
}
else {
if (easyness > this.search_params[1][0]) {
this.param_space.adjust(this.search_params[1][1]);
}
else if (easyness > this.search_params[2][0]) {
this.param_space.adjust(this.search_params[2][1]);
}
else if (easyness > this.search_params[3][0]) {
//just hang out in this space...
this.param_space.adjust(this.search_params[3][1], true);
}
else {
this.param_space.adjust([-1, -1]);
}
}
}
}
}
}
};