-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive.js
More file actions
313 lines (270 loc) · 9.76 KB
/
adaptive.js
File metadata and controls
313 lines (270 loc) · 9.76 KB
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
require('./webppl.min.js');
var _isFunction = function(obj) {
return (obj && obj.constructor &&
obj.call && obj.apply);
};
var makeSkeleton = function(infraThunk) {
return {
common: infraThunk,
// Functionality that I want in all AOED methods
commonUtils: function() {
},
initializePrior: function() {
// The initial prior implied by args.M
// TODO: If oed switches to reusable construct prior function, then
// switch this too
var M = args.M,
inferM1 = (args.infer && args.infer.M1) || Enumerate;
return inferM1(function() {
var m = M(); // jshint ignore:line
return {name: m.name, func: m};
});
},
suggestExperiment: function() {
var mPrior = globalStore.mPrior,
usePredictiveY = globalStore.usePredictiveY,
returnKL = globalStore.returnKL,
cache = globalStore.cache;
var eigs = EIG({
mPrior: mPrior,
X: args.X,
Y: args.Y,
infer: args.infer,
usePredictiveY: usePredictiveY,
returnKL: returnKL,
cache: cache
});
return getBestExpt(eigs.support());
},
suggestAll: function() {
var mPrior = globalStore.mPrior,
usePredictiveY = globalStore.usePredictiveY,
returnKL = globalStore.returnKL,
cache = globalStore.cache;
var eigs = EIG({
mPrior: mPrior,
X: args.X,
Y: args.Y,
infer: args.infer,
usePredictiveY: usePredictiveY,
returnKL: returnKL,
cache: cache
});
return eigs.support();
},
// Exactly like suggestAll, but returns the full experiment marginal
// with probabilities, in case probabilities of sampling Xs matter
suggestAllWithProbs: function() {
var mPrior = globalStore.mPrior,
usePredictiveY = globalStore.usePredictiveY,
returnKL = globalStore.returnKL,
cache = globalStore.cache;
var eigs = EIG({
mPrior: mPrior,
X: args.X,
Y: args.Y,
infer: args.infer,
usePredictiveY: usePredictiveY,
returnKL: returnKL,
cache: cache
});
return eigs;
},
updateBeliefs: function() {
var x = globalStore.x,
y = globalStore.y,
mPrior = globalStore.mPrior,
cache = globalStore.cache;
// FIXME: These functions really ought to be converted to args
// objects
var prune = globalStore.prune || {},
pruneMin = prune.min || Infinity,
keepPercent = prune.keepPercent || 1.0;
var res = updatePosterior({
mPrior: mPrior,
x: x,
y: y,
infer: args.infer,
cache: cache
});
if (res.mPosterior.support().length > pruneMin) {
var pruned = pruneModels(res.mPosterior, keepPercent);
return {
mPosterior: pruned,
AIG: res.AIG
};
} else {
return res;
}
},
cache: function() {
return cacheScores(args);
}
};
};
var getThunkBody = function(thunk) {
var thunkStr = thunk.toString();
// Remove function() { ... };
return thunkStr.slice(
thunkStr.indexOf("{") + 1, thunkStr.lastIndexOf("}")
);
};
var compileSkeleton = function(skeleton) {
// Common to all functions
var commonInfraStr = (_isFunction(skeleton.common)) ?
getThunkBody(skeleton.common) :
skeleton.common;
var commonUtilsStr = getThunkBody(skeleton.commonUtils);
var commonStr = commonInfraStr + commonUtilsStr;
// Individual functions
var suggestStr = getThunkBody(skeleton.suggestExperiment);
var suggestAllStr = getThunkBody(skeleton.suggestAll);
var suggestAllProbsStr = getThunkBody(skeleton.suggestAllWithProbs);
var updateStr = getThunkBody(skeleton.updateBeliefs);
var initialStr = getThunkBody(skeleton.initializePrior);
var cacheStr = getThunkBody(skeleton.cache);
// Concat common with the other objects
var suggestSrc = webppl.compile(commonStr + suggestStr);
var updateSrc = webppl.compile(commonStr + updateStr);
var initialSrc = webppl.compile(commonStr + initialStr);
var suggestAllSrc = webppl.compile(commonStr + suggestAllStr);
var suggestAllProbsSrc = webppl.compile(commonStr + suggestAllProbsStr);
var cacheSrc = webppl.compile(commonStr + cacheStr);
var handleRunError = function(e) {
// Just log it for now?
throw e;
};
// XXX: Switch .cli to .web when using browser
var runner = util.trampolineRunners.cli(handleRunError);
// Need to get a container to store return values
// XXX: Not sure if this will cause an asynchronous problem?
// After plenty of testing, it doesn't seem to
// result -> ((store, returnValue) -> ())
// side effect of return value func: result has returnValue property
var makeStoreFunc = function(res) {
return function(store, returnValue) {
res.returnValue = returnValue;
};
};
var aoed = {};
var suggest = function(mPrior, args) {
var globalStore = {
mPrior: mPrior,
usePredictiveY: !!args.usePredictiveY,
returnKL: !!args.returnKL,
cache: args.cache
};
var _code = eval.call({}, suggestSrc)(runner);
var res = {};
_code(globalStore, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.suggest = suggest;
var suggestAll = function(mPrior, args) {
var globalStore = {
mPrior: mPrior,
usePredictiveY: !!args.usePredictiveY,
returnKL: !!args.returnKL,
cache: args.cache
};
var _code = eval.call({}, suggestAllSrc)(runner);
var res = {};
_code(globalStore, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.suggestAll = suggestAll;
var suggestAllWithProbs = function(mPrior, args) {
var globalStore = {
mPrior: mPrior,
usePredictiveY: !!args.usePredictiveY,
returnKL: !!args.returnKL,
cache: args.cache
};
var _code = eval.call({}, suggestAllProbsSrc)(runner);
var res = {};
_code(globalStore, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.suggestAllWithProbs = suggestAllWithProbs;
var update = function(mPrior, x, y, opts) {
var globalStore = {
mPrior: mPrior,
x: x,
y: y,
prune: opts && opts.prune,
cache: opts && opts.cache
};
var _code = eval.call({}, updateSrc)(runner);
var res = {};
_code(globalStore, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.update = update;
var cache = function() {
var _code = eval.call({}, cacheSrc)(runner);
var res = {};
_code({}, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.cache = cache;
// Functions for compiling and running arbitrary code with infrastructure
// and utils
var compile = function(wpplStr) {
return webppl.compile(commonStr + wpplStr);
};
aoed.compile = compile;
// Helper for getting the body of a thunk
var compileThunk = function(thunk) {
// Add infrastructure and utils
var wpplStr = getThunkBody(thunk);
return compile(wpplStr);
};
aoed.compileThunk = compileThunk;
var run = function(wpplSrc, globalStore) {
var _code = eval.call({}, wpplSrc)(runner);
var res = {};
_code(globalStore, makeStoreFunc(res), '');
return res.returnValue;
};
aoed.run = run;
// For one-off convenience
var compileAndRun = function(wpplStr, globalStore) {
var wpplSrc = compile(wpplStr);
return run(wpplSrc, globalStore);
};
aoed.compileAndRun = compileAndRun;
// For getting variables defined in the thunk
var retrieve = function(name) {
return compileAndRun('return ' + name + ';', {});
};
aoed.retrieve = retrieve;
// Actually call the initialize source code to get the first model prior
var initialPrior = eval.call({}, initialSrc)(runner);
initialPrior({}, function(store, returnValue) {
aoed.initialPrior = returnValue;
}, '');
return aoed;
};
var AOED = function(infraThunk) {
// Important: reflection must work on whatever platform this is running on.
// Aside from that, the function must also have an args argument.
// The lamest assert that these two issues are true is to search for "args"
// as a substring.
// XXX: There might be workarounds, e.g. have a separate args thunk that
// returns an object, etc.
var commonStr = (_isFunction(infraThunk)) ?
infraThunk.toString() :
infraThunk; // Otherwise assume already string
// Check if the string is good to go
if (commonStr.indexOf("args") === -1) {
throw "No `args` string detected in thunk, " +
"or reflection isn't working. Check that your thunk's toString returns " +
"something useful.";
}
var skeleton = makeSkeleton(infraThunk);
return compileSkeleton(skeleton);
};
module.exports = {
AOED: AOED,
getThunkBody: getThunkBody
};