This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.h
261 lines (236 loc) · 7.16 KB
/
agent.h
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
#pragma once
#include "action.h"
#include "board.h"
#include <algorithm>
#include <array>
#include <map>
#include <random>
#include <sstream>
#include <string>
#include <type_traits>
class agent {
public:
agent(const std::string &args = "") {
std::stringstream ss("name=unknown role=unknown " + args);
for (std::string pair; ss >> pair;) {
std::string key = pair.substr(0, pair.find('='));
std::string value = pair.substr(pair.find('=') + 1);
meta[key] = {value};
}
}
virtual ~agent() {}
virtual void open_episode(const std::string &flag = "") {}
virtual void close_episode(const std::string &flag = "") {}
virtual action take_action(const board &, unsigned) { return action(); }
virtual bool check_for_win(const board &) { return false; }
public:
virtual std::string property(const std::string &key) const {
return meta.at(key);
}
virtual void notify(const std::string &msg) {
meta[msg.substr(0, msg.find('='))] = {msg.substr(msg.find('=') + 1)};
}
virtual std::string name() const { return property("name"); }
virtual std::string role() const { return property("role"); }
protected:
typedef std::string key;
struct value {
std::string value;
operator std::string() const { return value; }
template <typename numeric,
typename = typename std::enable_if<
std::is_arithmetic<numeric>::value, numeric>::type>
operator numeric() const {
return numeric(std::stod(value));
}
};
std::map<key, value> meta;
};
class random_agent : public agent {
public:
random_agent(const std::string &args = "") : agent(args) {
if (meta.find("seed") != meta.end())
engine.seed(int(meta["seed"]));
}
virtual ~random_agent() {}
protected:
std::default_random_engine engine;
};
template <class _IntType, size_t _Size> class bag_int_distribution {
public:
bag_int_distribution() { std::iota(std::begin(bag_), std::end(bag_), 1); }
void reset() { index_ = _Size; }
_IntType operator()(std::default_random_engine engine) {
if (index_ == _Size) {
std::shuffle(std::begin(bag_), std::end(bag_), engine);
index_ = 0;
}
return bag_[index_++];
}
private:
std::array<_IntType, _Size> bag_;
size_t index_ = _Size;
};
/*struct bitbag_int_distribution {
void reset() { bag = 0; }
board::tile_t operator()(std::default_random_engine engine) {
if (bag == 0) {
std::shuffle(std::begin(index), std::end(index), engine);
bag = 0b111;
}
for (unsigned idx : index) {
if ((bag >> idx) & 1u) {
bag &= ~(1u << idx);
return idx + 1u;
}
}
assert(false);
}
unsigned bag;
std::array<unsigned, 3> index{0u, 1u, 2u};
};*/
/**
* random environment
* add a new random tile to an empty cell
*/
class rndenv : public random_agent {
public:
rndenv(const std::string &args = "")
: random_agent("name=random role=environment " + args), popup() {}
action init_action(size_t step) {
if (step == 0) {
popup.reset();
std::shuffle(std::begin(init_space), std::end(init_space), engine);
}
board::tile_t tile = popup(engine);
return action::place(init_space[step], tile);
}
virtual action take_action(const board &after, unsigned move_) {
auto &cur = space[move_];
std::shuffle(std::begin(cur), std::end(cur), engine);
for (unsigned pos : cur) {
if (after(pos) != 0)
continue;
board::tile_t tile = popup(engine);
return action::place(pos, tile);
}
return action();
}
private:
std::array<unsigned, 16> init_space{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u,
8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u};
std::array<unsigned, 4> space[4]{{12u, 13u, 14u, 15u},
{0u, 4u, 8u, 12u},
{0u, 1u, 2u, 3u},
{3u, 7u, 11u, 15u}};
bag_int_distribution<board::tile_t, 3> popup;
};
/**
* dummy player
* select a legal action randomly
*/
class player : public random_agent {
public:
player(const std::string &args = "")
: random_agent("name=dummy role=player " + args),
opcode({0u, 1u, 2u, 3u}) {}
virtual action take_action(const board &before, unsigned) {
std::shuffle(std::begin(opcode), std::end(opcode), engine);
for (unsigned op : opcode) {
board::reward_t reward = board(before).slide(op);
if (reward != -1)
return action::slide(op);
}
return action();
}
private:
std::array<unsigned, 4> opcode;
};
/**
* greedy player
* select a greedy action
*/
class greedy_player : public random_agent {
public:
greedy_player(const std::string &args = "")
: random_agent("name=greedy role=player " + args) {}
virtual action take_action(const board &before, unsigned = 4) {
board::reward_t reward[4] = {
board(before).slide(0u), board(before).slide(1u),
board(before).slide(2u), board(before).slide(3u)};
board::reward_t *max_reward = std::max_element(reward, reward + 4);
if (*max_reward != -1) {
return action::slide(max_reward - reward);
}
return action();
}
};
/**
* random environment for search
* add a new random tile to an empty cell
*/
class search_env : public random_agent {
public:
search_env(const std::string &args = "")
: random_agent("name=search_env role=environment " + args) {}
virtual action take_action(const board &after, unsigned move_) {
auto &cur = space[move_];
std::shuffle(std::begin(cur), std::end(cur), engine);
for (unsigned pos : cur) {
if (after(pos) != 0)
continue;
std::shuffle(std::begin(bag), std::end(bag), engine);
board::tile_t tile = bag[0];
return action::place(pos, tile);
}
return action();
}
void reset() { bag = {1, 2, 3}; }
void remove(unsigned tile) {
if (bag.empty())
reset();
bag.erase(std::remove(std::begin(bag), std::end(bag), tile), std::end(bag));
}
private:
std::array<unsigned, 4> space[4]{{12u, 13u, 14u, 15u},
{0u, 4u, 8u, 12u},
{0u, 1u, 2u, 3u},
{3u, 7u, 11u, 15u}};
std::vector<unsigned> bag;
};
/**
* deep greedy player
* select a greedy action by deep search
*/
class deep_greedy_player : public random_agent {
public:
deep_greedy_player(const std::string &args = "")
: random_agent("name=deep_greedy role=player " + args) {}
virtual action take_action(const board &before, unsigned) {
board::reward_t reward[4] = {}, rew;
for (size_t op = 0; op < 3; ++op) {
board cur(before);
if ((reward[op] = cur.slide(op)) == -1)
continue;
env.reset();
unsigned move_ = op;
for (size_t depth = 0; depth < 3; ++depth) {
env.take_action(cur, move_).apply(cur);
action move = player.take_action(cur);
rew = move.apply(cur);
if (rew == -1)
break;
reward[op] += rew;
move_ = move.event() & 0b11;
}
}
board::reward_t *max_reward = std::max_element(reward, reward + 4);
if (*max_reward != -1) {
return action::slide(max_reward - reward);
}
return action();
}
private:
greedy_player player;
search_env env;
};