-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakeGridWorldPOMDP.wppl
218 lines (173 loc) · 7.71 KB
/
makeGridWorldPOMDP.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
// function that builds a gridworld POMDP
// arg is a gridworld MDP, which is an object with features (an array of
// arrays of the features at each point), xLim, yLim, feature (a function of
// a (manifest) state that returns the features of that state), transition
// (a transition function on manifest states), actions, and stateToActions.
// noReverse must be false in the MDP.
// manifest states in this POMDP are objects with loc (an array of two
// numbers), timeLeft, terminateAfterAction, and timeAtRestraurant - the same as states in gridworld MDPs.
// latent states are an object listing whether each restaurant type is open.
// transition acts the same as in gridworld MDPs, not touching the latent state,
// unless the transition would take the agent into a closed restaurant, in which
// case the agent stays put.
// observe tells the agent which adjacent restaurants are open. If the agent is not
// adjacent to any restaurants, they get 'noObservation'.
var makeGridWorldPOMDP = function(options) {
var gridworld = makeGridWorldMDP(options).world;
var actions = gridworld.actions;
var feature = gridworld.feature;
// returns an array of restaurants that neighbour a state
var neighbourRestaurants = function(manifestState) {
var loc = manifestState.loc;
var updateStateLoc = function(manifestState, newLoc) {
return extend(manifestState, {
loc: newLoc
});
};
var potentialneighbourStates = [
updateStateLoc(manifestState, [loc[0] - 1, loc[1]]),
updateStateLoc(manifestState, [loc[0] + 1, loc[1]]),
updateStateLoc(manifestState, [loc[0], loc[1] + 1]),
updateStateLoc(manifestState, [loc[0], loc[1] - 1]),
];
var neighbourStates = filter(function(manifestState) {
return inGrid_(gridworld, manifestState.loc);
}, potentialneighbourStates);
var isRestaurant = function(manifestState) {
return feature(manifestState).name;
};
return filter(isRestaurant, neighbourStates);
};
// returns a subobject of latentState that gives the status of adjacent restaurants
var observe = function(state) {
var neighbourRests = neighbourRestaurants(state.manifestState);
if (_.isEmpty(neighbourRests)) {
return 'noObservation';
} else {
var restaurantNames = map(function(manifestState) {
return feature(manifestState).name;
}, neighbourRests);
return map(function(name) {
return [name, state.latentState[name]];
},
restaurantNames);
}
};
var _mdpTransition = gridworld.transition;
var mdpTransition = function(state, action) {
// transition the manifest state as it would be transitioned in the gridworld
// mdp, leave the latent state alone
var newManifestState = _mdpTransition(state.manifestState, action);
return {
manifestState: newManifestState,
latentState: state.latentState
};
};
var transition = function(state, action) {
assert.ok(stateHasManifestLatent(state), 'transition state arg');
var proposedNewState = mdpTransition(state, action);
var newFeatureName = feature(proposedNewState.manifestState).name;
// if proposedNewState is a restaurant that is closed, stay put, but increment
// time. otherwise, change to proposed new state.
if (newFeatureName && !proposedNewState.latentState[newFeatureName]) {
return {
manifestState: advanceStateTime(state.manifestState),
latentState: state.latentState
};
} else {
return proposedNewState;
}
};
var manifestStateToActions = function(manifestState) {
var neighbourRestaurantArray = neighbourRestaurants(manifestState);
if (neighbourRestaurantArray.length == 0) {
var manifestStateToActions_ = gridworld.stateToActions;
return manifestStateToActions_(manifestState);
} else {
return gridworld.actions;
}
};
return {
manifestStateToActions: manifestStateToActions,
transition: transition,
observe: observe,
feature: feature,
MDPWorld: gridworld
};
};
var inferGridWorldPOMDP = function(world, startState, baseParams, trueAgentParams, prior, agentTypeAndFunctions, trajectoryOrOffPolicy, numRejectionSamples) {
var makeAgent = agentTypeAndFunctions.makeAgent;
var simulate = agentTypeAndFunctions.simulate;
var beliefOrBeliefDelay = agentTypeAndFunctions.type;
var isJointPrior = prior.isJoint;
// try just not defining things instead of having them as 'undefined'
var jointPrior = isJointPrior ? prior.jointPrior : undefined;
var priorUtilityTable = isJointPrior ? undefined : prior.priorUtilityTable;
var priorAgentPrior = isJointPrior ? undefined : prior.priorAgentPrior;
if (isJointPrior) {
assert.ok(stateHasManifestLatent(startState) && jointPrior.sample,
'inferGridworld args');
} else {
assert.ok(stateHasManifestLatent(startState) &&
priorUtilityTable.sample &&
priorAgentPrior.sample,
'inferGridworld args');
}
assert.ok(trajectoryOrOffPolicy == 'trajectory' || trajectoryOrOffPolicy == 'offPolicy',
'trajectoryOrOffPolicy is not valid');
assert.ok(_.isNumber(numRejectionSamples) && isPOMDPWithManifestLatent(world), 'inferGridWorld args');
// get observations using trueAgentParams
var agent = makeAgent(trueAgentParams, world);
var observedStateAction = simulatePOMDP(startState, world, agent, 'stateAction');
var observe = agent.POMDPFunctions.observe;
assert.ok(stateHasManifestLatent(observedStateAction[0][0]), 'fullstate in trajectory for inferGridWorld');
return Infer({
method: 'enumerate'
}, function() {
var utilityAndPrior = isJointPrior ? sample(jointPrior) : null;
var utilityTable = isJointPrior ? utilityAndPrior.utilityTable :
sample(priorUtilityTable);
var utility = makeRestaurantUtilityFunction(world, utilityTable);
var priorBelief = isJointPrior ? utilityAndPrior.priorBelief :
sample(priorAgentPrior);
var params = extend(baseParams, {
utility: utility,
priorBelief: priorBelief
});
var agent = makeAgent(params, world);
var agentAct = agent.act;
var agentUpdateBelief = agent.updateBelief;
// Factor on whole sampled trajectory (SLOW IF TRANSITIONS NOT DETERMINISTIC OR IF NUM SAMPLES HIGH)
var factorOnTrajectory = function() {
var trajectoryDist = Infer({
method: 'rejection',
samples: numRejectionSamples
}, function() {
return simulatePOMDP(startState, world, agent, 'states')
});
factor(trajectoryDist.score(map(first, observedStateAction)));
};
// Move agent through observed sequence
var factorSequenceOffPolicy = function(currentBelief, previousAction, timeIndex) {
if (timeIndex < observedStateAction.length) {
// Go to next world state and sample observation from that state
var state = observedStateAction[timeIndex][0];
var observation = observe(state);
// Update agent's internal state and get action Dist
var delay = 0;
var nextBelief = beliefOrBeliefDelay == 'belief' ?
agentUpdateBelief(currentBelief, observation, previousAction) :
agentUpdateBelief(currentBelief, observation, previousAction, delay);
var nextActionDist = beliefOrBeliefDelay == 'belief' ?
agentAct(nextBelief) : agentAct(nextBelief, delay);
var observedAction = observedStateAction[timeIndex][1];
factor(nextActionDist.score(observedAction));
// condition on next world state, passing through updated internal state
factorSequenceOffPolicy(nextBelief, observedAction, timeIndex + 1);
}
};
var doInfer = (trajectoryOrOffPolicy == 'trajectory') ? factorOnTrajectory() :
factorSequenceOffPolicy(priorBelief, 'noAction', 0);
return { utilityTable, priorBelief };
});
};