-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakePOMDPAgent.wppl
545 lines (457 loc) · 16.9 KB
/
makePOMDPAgent.wppl
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// HELPER FUNCTIONS
var hasUtilityPriorBelief = function(agentParams) {
if (_.has(agentParams, 'utility') && _.has(agentParams, 'priorBelief')) {
var utility = agentParams.utility;
var priorBelief = agentParams.priorBelief;
return _.isFunction(utility);
}
return false;
};
var hasTransitionObserve = function(world) {
if (_.has(world, 'transition') && _.has(world, 'observe')) {
var transition = world.transition;
var observe = world.observe;
return _.isFunction(transition) && _.isFunction(observe);
}
return false;
};
// Test whether belief has right form for fastUpdateBelief, i.e. it's not
// an Dist but an object with 'manifestState' and 'latentStateDist' properties
var isDistOverLatent = function(belief) {
return (
_.has(belief, 'manifestState') &&
_.has(belief, 'latentStateDist') &&
!_.has(belief, 'support') &&
belief.latentStateDist.sample);
};
// Convert priorBelief from Dist over full states to Dist over latents
var toDistOverLatent = function(belief) {
var manifestState = belief.support()[0].manifestState;
var latentStateDist = Infer({
method: 'enumerate'
}, function() {
return sample(belief).latentState;
})
return {
manifestState: manifestState,
latentStateDist: latentStateDist
};
};
// Construct functions needed for running the POMDP agent
// Most of the code is for the manifest-latent optimization
var getPOMDPFunctions = function(params, world) {
// Sample a state from belief: works for general states and manifestLatent states.
var sampleBelief = function(belief) {
if (isDistOverLatent(belief)) {
return {
manifestState: belief.manifestState,
latentState: sample(belief.latentStateDist)
};
} else {
return sample(belief);
};
};
// ----------------------------
// Manifest-latent Optimization
// Helper functions
var getBeliefToActions = function(manifestStateToActions) {
return function(belief) {
var manifestState = isDistOverLatent(belief) ? belief.manifestState :
sample(belief).manifestState;
return manifestStateToActions(manifestState);
};
};
var getFullObserve = function(observeLatent) {
return function(state) {
return {
manifestState: state.manifestState,
observation: observeLatent(state)
};
};
};
// Agent gets an obervation in starting state without having taken an action
var isNullAction = function(action) {
return action == 'noAction';
};
// Optimized belief update for manifest/latent POMDPs
var getUpdateBeliefManifestLatent = function(transition, observe) {
var isDeltaDist = function(dist) {
return dist.support().length === 1;
};
return dp.cache(
function(belief_, observation, action) {
// If belief has form of "Dist over {manifest:, latent:}" then we convert
// to form of "{manifest:, Dist over latent}".
var belief = isDistOverLatent(belief_) ? belief_ : toDistOverLatent(belief_);
// we set the manifest state to the observed manifest state
var newManifestState = observation.manifestState;
// Don't update latent when you don't need to
if (observation.observation === 'noObservation' ||
isDeltaDist(belief.latentStateDist)) {
return extend(belief, {
manifestState: newManifestState
});
} else {
// Sample latent, update state, compare predicted observation from state
// to actual observation.
var newLatentDist = Infer({
method: 'enumerate'
}, function() {
var latentState = sample(belief.latentStateDist);
var state = {
manifestState: belief.manifestState,
latentState
};
var predictedNextState = isNullAction(action) ? state : transition(state, action);
var predictedObservation = observe(predictedNextState);
condition(_.isEqual(predictedObservation, observation));
return latentState;
});
return {
manifestState: newManifestState,
latentStateDist: newLatentDist
};
}
});
};
// updateBelief function for general POMDPs (no manifest/latent structure)
var getUpdateBeliefSimple = function(transition, observe) {
return dp.cache(
function(belief, observation, action) {
return Infer({
method: 'enumerate'
}, function() {
var state = sample(belief);
var predictedNextState = isNullAction(action) ? state : transition(state, action);
var predictedObservation = observe(predictedNextState);
condition(_.isEqual(predictedObservation, observation));
return predictedNextState;
});
});
};
var useManifestLatent = !_.isUndefined(params.useManifestLatent) ?
params.useManifestLatent :
stateHasManifestLatent(sampleBelief(params.priorBelief))
if (useManifestLatent) {
assert.ok(stateHasManifestLatent(sampleBelief(params.priorBelief)),
'state is not manifestLatent in form')
}
if (useManifestLatent) {
var observe = getFullObserve(world.observe);
return {
sampleBelief: sampleBelief,
useManifestLatent: true,
observe: observe,
beliefToActions: getBeliefToActions(world.manifestStateToActions),
updateBelief: getUpdateBeliefManifestLatent(world.transition, observe),
shouldTerminate: function(state) {
return state.manifestState.terminateAfterAction;
}
};
};
return {
sampleBelief: sampleBelief,
useManifestLatent: false,
observe: world.observe,
beliefToActions: world.beliefToActions,
updateBelief: getUpdateBeliefSimple(world.transition, world.observe),
shouldTerminate: function(state) {
return state.terminateAfterAction;
}
};
};
// --------------------------------------------------------------------
// Optimal POMDP agent
var makePOMDPAgentOptimal = function(params, world) {
assert.ok(hasUtilityPriorBelief(params) && hasTransitionObserve(world),
'makePOMDPAgent params and world');
// set defaults
var defaults = {
recurseOnStateOrBelief: 'belief',
alpha: 1000,
};
var params = extend(defaults, params);
var utility = params.utility;
var transition = world.transition;
var POMDPFunctions = getPOMDPFunctions(params, world);
var observe = POMDPFunctions.observe;
var beliefToActions = POMDPFunctions.beliefToActions;
var updateBelief = POMDPFunctions.updateBelief;
var shouldTerminate = POMDPFunctions.shouldTerminate;
var sampleBelief = POMDPFunctions.sampleBelief;
// RECURSE ON BELIEF (BELLMAN STYLE)
var act = dp.cache(
function(belief) {
return Infer({
method: 'enumerate'
}, function() {
var action = uniformDraw(beliefToActions(belief));
var eu = expectedUtilityBelief(belief, action);
factor(params.alpha * eu);
return action;
});
});
var expectedUtilityBelief = dp.cache(
function(belief, action) {
return expectation(
Infer({
method: 'enumerate'
}, function() {
var state = sampleBelief(belief);
var u = utility(state, action);
assert.ok(_.isFinite(utility(state, action)), 'utility is not finite. state: ' +
JSON.stringify(state));
if (shouldTerminate(state)) {
return u;
} else {
var nextState = transition(state, action);
var nextObservation = observe(nextState);
var nextBelief = updateBelief(belief, nextObservation, action);
var nextAction = sample(act(nextBelief));
var futureU = expectedUtilityBelief(nextBelief, nextAction);
return u + futureU;
}
}));
});
// RECURSE ON STATE (FIXES THE LATENT STATE)
var act_recState = dp.cache(
function(belief) {
return Infer({
method: 'enumerate'
}, function() {
var action = uniformDraw(beliefToActions(belief));
var eu = expectedUtilityBelief_recState(belief, action);
factor(params.alpha * eu);
return action;
});
});
var expectedUtilityBelief_recState = dp.cache(
function(belief, action) {
return expectation(
Infer({
method: 'enumerate'
}, function() {
var state = sampleBelief(belief);
return expectedUtilityState_recState(belief, state, action);
}));
});
var expectedUtilityState_recState = dp.cache(
function(belief, state, action) {
return expectation( // need this for caching
Infer({
method: 'enumerate'
}, function() {
var u = utility(state, action);
assert.ok(_.isFinite(utility(state, action)), 'utility is not finite. state: ' +
JSON.stringify(state));
if (shouldTerminate(state)) {
return u;
} else {
var nextState = transition(state, action);
var nextObservation = observe(nextState);
var nextBelief = updateBelief(belief, nextObservation, action);
var nextAction = sample(act(nextBelief));
var futureU = expectedUtilityState_recState(nextBelief, nextState, nextAction);
return u + futureU;
}
}));
});
var act = params.recurseOnStateOrBelief === 'belief' ? act : act_recState;
var expectedUtilityBelief = params.recurseOnStateOrBelief === 'belief' ?
expectedUtilityBelief : expectedUtilityBelief_recState;
return {
act: act,
expectedUtilityBelief: expectedUtilityBelief,
params: params,
updateBelief: updateBelief,
POMDPFunctions: POMDPFunctions,
};
};
// --------------------------------------------------------------------
// Sub-optimal POMDP agent
var makePOMDPAgentDelay = function(params, world) {
assert.ok(hasUtilityPriorBelief(params) && hasTransitionObserve(world),
'makePOMDPAgent params and world');
var defaults = {
alpha: 1000,
recurseOnStateOrBelief: 'belief',
discount: 0,
sophisticatedOrNaive: 'naive',
noDelays: true,
updateMyopic: false,
rewardMyopic: false
};
var params = extend(defaults, params);
if (params.rewardMyopic || params.updateMyopic) {
assert.ok(params.noDelays === false && params.sophisticatedOrNaive === 'naive',
'rewardMyopic and updateMyopic require Naive agent with delays');
}
assert.ok(params.rewardMyopic === false || params.updateMyopic === false,
"one of rewardMyopic and updateMyopic must be false");
// Variables for methods
var transition = world.transition;
var utility = params.utility;
var POMDPFunctions = getPOMDPFunctions(params, world);
var observe = POMDPFunctions.observe;
var beliefToActions = POMDPFunctions.beliefToActions;
var sampleBelief = POMDPFunctions.sampleBelief;
var _updateBelief = POMDPFunctions.updateBelief;
var updateBelief = function(belief, observation, action, delay) {
if (params.updateMyopic && (delay > params.updateMyopic.bound)) {
// update manifestState (assuming no possibility that isNullAction(action))
var nextBelief = Infer({
method: 'enumerate'
}, function() {
var state = sampleBelief(belief);
return transition(state, action);
});
return POMDPFunctions.useManifestLatent ? toDistOverLatent(nextBelief) : nextBelief;
} else {
return _updateBelief(belief, observation, action);
};
};
// Update the *delay* parameter in *expectedUtility* for sampling
// actions and future utilities
var transformDelay = function(delay) {
var table = {
naive: delay + 1,
sophisticated: 0
};
return params.noDelays ? 0 : table[params.sophisticatedOrNaive];
};
var incrementDelay = function(delay) {
return params.noDelays ? 0 : delay + 1;
};
// Define the discount function to be used
var discountFunction = params.discountFunction ? params.discountFunction :
function(delay) {
return 1.0 / (1 + params.discount * delay);
};
// Termination condition for *expectedUtility*
var shouldTerminate = function(state, delay) {
var terminateAfterAction = POMDPFunctions.useManifestLatent ?
state.manifestState.terminateAfterAction :
state.terminateAfterAction;
if (terminateAfterAction) {
return true;
}
if (params.rewardMyopic) {
return delay >= params.rewardMyopic.bound;
}
return false;
};
// RECURSE ON BELIEF (BELLMAN STYLE)
var act = dp.cache(
function(belief, delay) {
assert.ok(_.isFinite(delay), 'act: delay non-finite. delay: ' + delay);
return Infer({
method: 'enumerate'
}, function() {
var action = uniformDraw(beliefToActions(belief));
var eu = expectedUtilityBelief(belief, action, delay);
factor(params.alpha * eu);
return action;
});
});
var expectedUtilityBelief = dp.cache(
function(belief, action, delay) {
return expectation(
Infer({
method: 'enumerate'
}, function() {
var state = sampleBelief(belief);
assert.ok(_.isFinite(utility(state, action)), 'utility is not finite. state: ' +
JSON.stringify(state));
var u = discountFunction(delay) * utility(state, action);
if (shouldTerminate(state, delay)) {
return u;
} else {
var nextState = transition(state, action);
var nextObservation = observe(nextState);
var transformedDelay = transformDelay(delay);
var nextBelief = updateBelief(belief, nextObservation, action, transformedDelay);
var nextAction = sample(act(nextBelief, transformedDelay));
var futureU = expectedUtilityBelief(nextBelief, nextAction, incrementDelay(delay));
return u + futureU;
}
}));
});
// RECURSE ON STATE (FIXES THE LATENT STATE)
var act_recState = dp.cache(
function(belief, delay) {
assert.ok(_.isFinite(delay), 'act: delay non-finite. delay: ' + delay);
return Infer({
method: 'enumerate'
}, function() {
var action = uniformDraw(beliefToActions(belief));
var eu = expectedUtilityBelief_recState(belief, action, delay);
factor(params.alpha * eu);
return action;
});
});
var expectedUtilityBelief_recState = dp.cache(
function(belief, action, delay) {
return expectation(
Infer({
method: 'enumerate'
}, function() {
var state = sampleBelief(belief);
return expectedUtilityState_recState(belief, state, action, delay);
}));
});
var expectedUtilityState_recState = dp.cache(
function(belief, state, action, delay) {
return expectation( // need this for caching
Infer({
method: 'enumerate'
}, function() {
assert.ok(_.isFinite(utility(state, action)), 'utility is not finite. state: ' +
JSON.stringify(state));
var u = discountFunction(delay) * utility(state, action);
if (shouldTerminate(state, delay)) {
return u;
} else {
var nextState = transition(state, action);
var nextObservation = observe(nextState);
var transformedDelay = transformDelay(delay);
var nextBelief = updateBelief(belief, nextObservation, action,
transformedDelay);
var nextAction = sample(act_recState(nextBelief, transformedDelay));
var futureU = expectedUtilityState_recState(nextBelief, nextState,
nextAction,
incrementDelay(delay));
return u + futureU;
}
}));
});
var act = params.recurseOnStateOrBelief === 'belief' ? act : act_recState;
var expectedUtility = params.recurseOnStateOrBelief === 'belief' ?
expectedUtilityBelief : expectedUtilityBelief_recState;
return { act, updateBelief, expectedUtility, params, POMDPFunctions };
};
// General POMDP wrappers
var isOptimalPOMDPAgent = function(agentParams) {
var optimalProperties = function() {
return !(_.has(agentParams, 'noDelays') ||
_.has(agentParams, 'discount') ||
_.has(agentParams, 'myopic-observations'));
}
return _.isUndefined(agentParams.optimal) ? optimalProperties() : agentParams.optimal;
};
var isManualPOMDPAgent = function(agentParams) {
return (_.has(agentParams, 'act') &&
_.has(agentParams, 'updateBelief') &&
_.has(agentParams, 'params') &&
_.has(agentParams.params, 'priorBelief'));
}
var makePOMDPAgent = function(params, world) {
if (isManualPOMDPAgent(params)) {
return extend(params, {
POMDPFunctions: getPOMDPFunctions(params.params, world)
});
} else {
return (isOptimalPOMDPAgent(params) ?
makePOMDPAgentOptimal(params, world) :
makePOMDPAgentDelay(params, world));
}
};