-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.cpp
350 lines (280 loc) · 9.59 KB
/
agent.cpp
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
#include "agent.hpp"
#include <cassert>
#include <cmath>
#include "predict.hpp"
#include "search.hpp"
#include "util.hpp"
// construct a learning agent from the command line arguments
void Agent::setOptions(options_t & options) {
std::string s;
strExtract(options["agent-actions"], m_actions);
strExtract(options["agent-horizon"], m_horizon);
strExtract(options["observation-bits"], m_obs_bits);
strExtract(options["ct-depth"], m_max_tree_depth);
strExtract<unsigned int>(options["reward-bits"], m_rew_bits);
strExtract(options["timeout"], m_timeout);
strExtract(options["UCB-weight"], m_UCBWeight);
for (unsigned int i = 1, c = 1; i < m_actions; i *= 2, c++) {
m_actions_bits = c;
}
m_time_cycle = 0;
m_total_reward = 0.0;
obsrew_t o_r = std::make_pair(NULL, NULL);
m_st = new DecisionNode(o_r);
}
Agent::Agent(options_t & options) {
std::string s;
strExtract(options["agent-actions"], m_actions);
strExtract(options["agent-horizon"], m_horizon);
strExtract(options["observation-bits"], m_obs_bits);
strExtract(options["ct-depth"], m_max_tree_depth);
strExtract<unsigned int>(options["reward-bits"], m_rew_bits);
strExtract(options["timeout"], m_timeout);
strExtract(options["UCB-weight"], m_UCBWeight);
// calculate the number of bits needed to represent the action
for (unsigned int i = 1, c = 1; i < m_actions; i *= 2, c++) {
m_actions_bits = c;
}
m_ct = new ContextTree(strExtract<unsigned int>(options["ct-depth"]));
// build a new uct
obsrew_t o_r = std::make_pair(NULL, NULL);
m_st = new DecisionNode(o_r);
reset();
}
// destruct the agent and the corresponding context tree
Agent::~Agent(void) {
if (m_ct)
delete m_ct;
}
// current lifetime of the agent in cycles
lifetime_t Agent::lifetime(void) const {
return m_time_cycle;
}
double Agent::UCBWeight(void) const {
return m_UCBWeight;
}
// the total accumulated reward across an agent's lifespan
reward_t Agent::reward(void) const {
return m_total_reward;
}
// the average reward received by the agent at each time step
reward_t Agent::averageReward(void) const {
return lifetime() > 0 ? reward() / reward_t(lifetime() + 1) : 0.0;
}
// maximum reward in a single time instant
reward_t Agent::maxReward(void) const {
return reward_t((1 << m_rew_bits) - 1);
}
// minimum reward in a single time instant
reward_t Agent::minReward(void) const {
return 0.0;
}
// number of distinct actions
unsigned int Agent::numActions(void) const {
return m_actions;
}
// Get the Context tree depth
size_t Agent::maxTreeDepth(void) {
return m_max_tree_depth;
}
// Calculate the probability of next symbol
double Agent::getProbNextSymbol(void) {
return pow(2, m_ct->getLogProbNextSymbolGivenH(1));
}
// the length of the stored history for an agent
size_t Agent::historySize(void) const {
return m_ct->historySize();
}
// length of the search horizon used by the agent
size_t Agent::horizon(void) const {
return m_horizon;
}
// generate an action uniformly at random
action_t Agent::genRandomAction(void) {
return randRange(m_actions);
}
// Generate a percept distributed according
// to our history statistics
percept_t* Agent::genPercept(void) const {
percept_t *percept = new percept_t[2];
symbol_list_t symbol_list;
// Generate the observation and reward block
m_ct->genRandomSymbols(symbol_list, m_obs_bits + m_rew_bits);
m_ct->revertHistory(m_obs_bits + m_rew_bits);
// Decode the (observation, reward) percept from symbol list
percept[0] = decode(symbol_list, m_obs_bits);
for (int i = 0; i < m_rew_bits; i++) {
symbol_list[i] = symbol_list[i + m_obs_bits];
}
percept[1] = decode(symbol_list, m_rew_bits);
return percept;
}
// generate a percept distributed to our history statistics, and
// update our mixture environment model with it
percept_t* Agent::genPerceptAndUpdate(void) {
percept_t* percept = new percept_t[2];
symbol_list_t symbol_list(m_obs_bits + m_rew_bits);
// Generate the observation and reward block and update the Context tree
m_ct->genRandomSymbolsAndUpdate(symbol_list, m_obs_bits + m_rew_bits);
// Decode the (observation, reward) percept from symbol list
percept[0] = decode(symbol_list, m_obs_bits);
for (int i = 0; i < m_rew_bits; i++) {
symbol_list[i] = symbol_list[i + m_obs_bits];
}
percept[1] = decode(symbol_list, m_rew_bits);
// Update other properties
m_total_reward += percept[1];
m_last_update_percept = true;
return percept;
}
// Update the agent's internal model of the world after receiving a percept
void Agent::modelUpdate(percept_t observation, percept_t reward) {
// Update internal model
symbol_list_t percept;
encodePercept(percept, observation, reward);
if (m_ct->historySize() >= m_ct->depth()) {
// Update the context tree with the percept
m_ct->update(percept);
} else {
// Populate the history for initial context
m_ct->updateHistory(percept);
}
// Update other properties
m_total_reward += reward;
m_last_update_percept = true;
}
// Update the agent's internal model of the world after performing an action
void Agent::modelUpdate(action_t action) {
assert(isActionOk(action));
assert(m_last_update_percept == true);
// Update internal model
symbol_list_t action_syms;
encodeAction(action_syms, action);
m_ct->updateHistory(action_syms);
m_time_cycle++;
m_last_update_percept = false;
}
// revert the agent's internal model of the world
// to that of a previous time cycle, false on failure
bool Agent::modelRevert(const ModelUndo &mu) {
int n_cycles = m_time_cycle - mu.lifetime();
// Revert the context tree to the restoration point
for (int i = 0; i < n_cycles; i++) {
for (int j = 0; j < m_obs_bits + m_rew_bits; j++) {
// Revert the perpcept for each cycle
m_ct->revert();
m_ct->revertHistory(m_ct->historySize() - 1);
}
m_ct->revertHistory(m_ct->historySize() - m_actions_bits);
}
// Revert the time cycle and total reward
m_time_cycle = mu.lifetime();
m_total_reward = mu.reward();
return true;
}
// Reset the agent
void Agent::reset(void) {
m_ct->clear();
m_time_cycle = 0;
m_total_reward = 0.0;
}
// Start a new episode
void Agent::newEpisode(void) {
m_time_cycle = 0;
m_total_reward = 0.0;
m_ct->resetHistory();
}
// Get the time out
double Agent::timeout(void) {
return m_timeout;
}
// probability of selecting an action according to the
// agent's internal model of it's own behaviour
double Agent::getPredictedActionProb(action_t action) {
double log_probability = 0.0;
for (int i = 0; i < m_actions_bits; i++) {
log_probability += m_ct->getLogProbNextSymbolGivenHWithUpdate(
1 & action);
action /= 2;
}
return pow(2, log_probability);
}
// get the agent's probability of receiving a particular percept
double Agent::perceptProbability(percept_t observation,
percept_t reward) const {
double log_probability = 0.0;
for (int i = 0; i < m_obs_bits; i++) {
// Calculate the log probability of seeing the observation bits
log_probability += m_ct->getLogProbNextSymbolGivenHWithUpdate(
1 & observation);
observation /= 2;
}
for (int i = 0; i < m_rew_bits; i++) {
// Calculate the log probability of seeing the reward bits
log_probability += m_ct->getLogProbNextSymbolGivenHWithUpdate(
1 & reward);
reward /= 2;
}
return pow(2, log_probability);
}
// Return context tree
ContextTree * Agent::contextTree() {
return m_ct;
}
// action sanity check
bool Agent::isActionOk(action_t action) const {
return action < m_actions;
}
// reward sanity check
bool Agent::isRewardOk(reward_t reward) const {
return reward >= minReward() && reward <= maxReward();
}
// Encodes an action as a list of symbols
void Agent::encodeAction(symbol_list_t &symlist, action_t action) const {
symlist.clear();
encode(symlist, action, m_actions_bits);
}
// Encodes a percept (observation, reward) as a list of symbols
void Agent::encodePercept(symbol_list_t &symlist, percept_t observation,
percept_t reward) const {
symlist.clear();
encode(symlist, observation, m_obs_bits);
encode(symlist, reward, m_rew_bits);
}
// Decodes the observation from a list of symbols
action_t Agent::decodeAction(const symbol_list_t &symlist) const {
return decode(symlist, m_actions_bits);
}
// Decodes the reward from a list of symbols
percept_t Agent::decodeReward(const symbol_list_t &symlist) const {
return decode(symlist, m_rew_bits);
}
// return the search tree
DecisionNode * Agent::searchTree() {
return m_st;
}
// reset the search tree to a new root node
void Agent::searchTreeReset() {
delete m_st;
obsrew_t o_r = std::make_pair(NULL, NULL);
m_st = new DecisionNode(o_r);
}
// prune the tree to the subtree of the root corresponding to
// the given action
void Agent::searchTreePrune(action_t action, obsrew_t obsrew) {
ChanceNode * chance_node = m_st->getChild(action);
if (chance_node != 0) {
searchTree()->pruneAllBut(action);
DecisionNode * new_root = chance_node->getChild(obsrew);
if (new_root != 0) {
chance_node->pruneAllBut(obsrew);
m_st = new_root;
}
}
}
// used to revert an agent to a previous state
ModelUndo::ModelUndo(const Agent &agent) {
m_lifetime = agent.lifetime();
m_reward = agent.reward();
m_history_size = agent.historySize();
}