-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
335 lines (283 loc) · 11.4 KB
/
main.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
#include "main.hpp"
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include "agent.hpp"
#include "environment.hpp"
#include "search.hpp"
#include "util.hpp"
#include "predict.hpp"
// Globals for controlling experiments
static int total_cycles_mult_g;
static int def_total_cycles_g;
static int global_cycles_g = 0;
static int next_explore_switch_g;
static double explore_rate_g, explore_decay_g;
static bool explore_g;
// Streams for logging
namespace aixi {
std::ofstream log; // A verbose human-readable log
}
std::ofstream compactLog; // A compact comma-separated value log
void processOptions(std::ifstream &in, options_t &options);
Environment * getEnvFromOptions(options_t options);
void setGlobalOptions(options_t options);
// The main agent/environment interaction loop
void mainLoop(Agent &ai, Environment &env, options_t &options) {
// Determine termination lifetime
bool terminate_check = options.count("terminate-lifetime") > 0;
lifetime_t terminate_lifetime;
bool explored = false;
if (terminate_check) {
strExtract(options["terminate-lifetime"], terminate_lifetime);
assert(0 <= terminate_lifetime);
}
// Agent/environment interaction loop
action_t action = 0;
int cycle = 1;
bool dobreak = false;
while (true) {
// check for agent termination
if (terminate_check && ai.lifetime() > terminate_lifetime) {
aixi::log << "info: terminating lifetime" << std::endl;
break;
}
// Get a percept from the environment
percept_t observation = env.getObservation();
percept_t reward = env.getReward();
// Reset the UCT
ai.searchTreeReset();
// Update agent's environment model with the new percept
ai.modelUpdate(observation, reward);
// If the episode is finished, still do a modelUpdate with action
if (env.isFinished()) {
dobreak = true;
}
// Determine best exploitive action, or explore_g
if (global_cycles_g == next_explore_switch_g) {
if (explore_g) {
std::cout << "Starting evaluation phase: " << global_cycles_g
<< std::endl;
explore_g = false;
next_explore_switch_g += def_total_cycles_g
* total_cycles_mult_g / 10;
} else {
std::cout << "Starting training phase: " << global_cycles_g
<< std::endl;
explore_g = true;
next_explore_switch_g += def_total_cycles_g
* total_cycles_mult_g / 5;
}
}
explored = false;
// Either explore or search
if (explore_g && rand01() < explore_rate_g) {
explored = true;
action = ai.genRandomAction();
} else {
// We need to accumulate some history before calling search
if (ai.historySize() >= ai.maxTreeDepth()) {
action = search(ai);
} else {
action = ai.genRandomAction();
}
}
// Update agent's environment model with the chosen action
ai.modelUpdate(action);
// Log this turn
aixi::log << "action: " << action << std::endl;
aixi::log << "explored: " << (explored ? "yes" : "no") << std::endl;
aixi::log << "explore_rate_g: " << explore_rate_g << std::endl;
aixi::log << "cycle: " << cycle << std::endl;
aixi::log << "global_cycle: " << global_cycles_g << std::endl;
aixi::log << "observation: " << observation << std::endl;
aixi::log << "reward: " << reward << std::endl;
aixi::log << "total reward: " << ai.reward() << std::endl;
aixi::log << "average reward: " << ai.averageReward() << std::endl;
aixi::log << "Search tree size: "
<< ai.searchTree()->getDecisionNodeInfo() << std::endl;
aixi::log << "Global cycle number: " << global_cycles_g << std::endl;
// Log the data in a more compact form
compactLog << global_cycles_g << ", " << cycle << ", " << observation
<< ", " << reward << ", " << action << ", " << explore_g << ", "
<< explored << ", " << explore_rate_g << ", " << ai.reward()
<< ", " << ai.averageReward() << ", " << env.isFinished()
<< std::endl;
// Break out before performing another action, since the environment is finished.
if (dobreak) {
break;
}
// Do the action!
env.performAction(action);
if (explore_g)
explore_rate_g *= explore_decay_g;
cycle++;
global_cycles_g++;
}
}
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 3) {
std::cerr << "ERROR: Incorrect number of arguments" << std::endl;
std::cerr
<< "The first argument should indicate the location of the configuration file and the second (optional) argument should indicate the location of the configuration file of the second game to run."
<< std::endl;
return -1;
}
// Set up logging
std::string log_file = "log";
aixi::log.open((log_file + ".log").c_str());
compactLog.open((log_file + ".csv").c_str());
// Print header to compactLog
compactLog
<< "global_cycle, cycle, observation, reward, action, explore_on, explored, explore_rate_g, total reward, average reward, end of game"
<< std::endl;
options_t options;
// Default configuration values
options["ct-depth"] = "3"; // max context tree depth
options["agent-horizon"] = "16"; // agent max search horizon
options["exploration"] = "0"; // do not explore_g
options["explore-decay"] = "1.0"; // exploration rate does not decay
options["timeout"] = "0.5"; // timeout
options["UCB-weight"] = "1.41"; // UCB weight
options["def-total-cycles"] = "1000"; // total number of cycles for 1 experiment
// Read configuration options
std::ifstream conf(argv[1]);
if (!conf.is_open()) {
std::cerr << "ERROR: Could not open file '" << argv[1]
<< "' now exiting" << std::endl;
return -1;
}
processOptions(conf, options);
conf.close();
// Read the optional second config, if required
options_t options1;
if (argc == 3) {
std::ifstream conf1(argv[2]);
if (!conf1.is_open()) {
std::cerr << "ERROR: Could not open file '" << argv[2]
<< "' now exiting" << std::endl;
return -1;
}
processOptions(conf1, options1);
conf1.close();
}
// Set up the environment
Environment * env = getEnvFromOptions(options);
// Set up the agent
Agent ai(options);
// Set the global experiment options
setGlobalOptions(options);
// Run the main agent/environment interaction loop
while (global_cycles_g < 2 * def_total_cycles_g * total_cycles_mult_g) {
mainLoop(ai, *env, options);
env->envReset();
ai.searchTreeReset();
}
// Run on game 2, then again on game 1.
if (argc == 3) {
std::cout << "Finished game 1, starting game 2" << std::endl;
aixi::log << "-----------------" << std::endl;
compactLog << "-----------------" << std::endl;
global_cycles_g = 0;
ai.setOptions(options1);
setGlobalOptions(options1);
env = getEnvFromOptions(options1);
while (global_cycles_g < 2 * def_total_cycles_g * total_cycles_mult_g) {
mainLoop(ai, *env, options1);
env->envReset();
ai.searchTreeReset();
}
global_cycles_g = 0;
ai.setOptions(options);
std::cout << "Finished game 2, returning to game 1" << std::endl;
aixi::log << "-----------------" << std::endl;
compactLog << "-----------------" << std::endl;
setGlobalOptions(options);
env = getEnvFromOptions(options);
while (global_cycles_g < 2 * def_total_cycles_g * total_cycles_mult_g) {
mainLoop(ai, *env, options);
env->envReset();
ai.searchTreeReset();
}
}
std::cout << "Done!" << std::endl;
aixi::log.close();
compactLog.close();
return 0;
}
// Populate the 'options' map based on 'key=value' pairs from an input stream
void processOptions(std::ifstream &in, options_t &options) {
std::string line;
size_t pos;
for (int lineno = 1; in.good(); lineno++) {
std::getline(in, line);
// Ignore # comments
if ((pos = line.find('#')) != std::string::npos) {
line = line.substr(0, pos);
}
// Remove whitespace
while ((pos = line.find(" ")) != std::string::npos)
line.erase(line.begin() + pos);
while ((pos = line.find("\t")) != std::string::npos)
line.erase(line.begin() + pos);
// Split into key/value pair at the first '='
pos = line.find('=');
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
// Check that we have parsed a valid key/value pair. Warn on failure or
// set the appropriate option on success.
if (pos == std::string::npos) {
std::cerr << "WARNING: processOptions skipping line " << lineno
<< " (no '=')" << std::endl;
} else if (key.size() == 0) {
std::cerr << "WARNING: processOptions skipping line " << lineno
<< " (no key)" << std::endl;
} else if (value.size() == 0) {
std::cerr << "WARNING: processOptions skipping line " << lineno
<< " (no value)" << std::endl;
} else {
options[key] = value; // Success!
std::cout << "OPTION: '" << key << "' = '" << value << "'"
<< std::endl;
}
}
}
// Construct the environmnet required by the options
Environment * getEnvFromOptions(options_t options) {
std::string environment_name = options["environment"];
if (environment_name == "coin-flip") {
return new CoinFlip(options);
} else if (environment_name == "cheese-maze") {
return new CheeseMaze(options);
} else if (environment_name == "extended-tiger") {
return new ExtTiger(options);
} else if (environment_name == "tictactoe") {
return new TicTacToe(options);
} else if (environment_name == "biased-rock-paper-scissor") {
return new BRockPaperScissors(options);
} else if (environment_name == "pacman") {
return new Pacman(options);
} else {
std::cerr << "ERROR: unknown environment '" << environment_name << "'"
<< std::endl;
return NULL;
}
}
// Helper function to extract global experiment variables from options
void setGlobalOptions(options_t options) {
// Determine experiment length options
strExtract(options["total-cycles-mult"], total_cycles_mult_g);
strExtract(options["def-total-cycles"], def_total_cycles_g);
next_explore_switch_g = global_cycles_g
+ (def_total_cycles_g * total_cycles_mult_g / 5);
// Determine exploration options
explore_g = options.count("exploration") > 0;
if (explore_g) {
strExtract(options["exploration"], explore_rate_g);
strExtract(options["explore-decay"], explore_decay_g);
assert(0.0 <= explore_rate_g && explore_rate_g <= 1.0);
assert(0.0 <= explore_decay_g && explore_decay_g <= 1.0);
}
}