-
Notifications
You must be signed in to change notification settings - Fork 0
/
Episode.h
202 lines (168 loc) · 5.62 KB
/
Episode.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
#pragma once
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <chrono>
#include <numeric>
#include "Board64.h"
#include "Action.h"
#include "Agent.h"
class Statistic;
class Agent;
class Episode {
friend class statistic;
public:
struct Move {
Action code;
reward_t reward;
time_t time;
board_t board;
Move(board_t board = 0, Action code = {}, reward_t reward = 0, time_t time = 0,
std::array<int, 4> bag = {0, 4, 4, 4}) : board(board),
code(code),
reward(reward),
time(time) {}
operator Action() const { return code; }
friend std::ostream &operator<<(std::ostream &out, const Move &m) {
out << m.code;
if (m.reward) out << '[' << std::dec << m.reward << ']';
if (m.time) out << '(' << std::dec << m.time << ')';
return out;
}
friend std::istream &operator>>(std::istream &in, Move &m) {
in >> m.code;
m.reward = 0;
m.time = 0;
if (in.peek() == '[') {
in.ignore(1);
in >> std::dec >> m.reward;
in.ignore(1);
}
if (in.peek() == '(') {
in.ignore(1);
in >> std::dec >> m.time;
in.ignore(1);
}
return in;
}
};
Episode() : ep_state(InitialState()), ep_score(0), ep_time(0) { ep_moves.reserve(10000); }
Board64 &state() { return ep_state; }
const Board64 &state() const { return ep_state; }
reward_t score() const { return ep_score; }
std::vector<Move> GetMoves() const { return ep_moves; }
void OpenEpisode(const std::string &tag) {
ep_open = {tag, millisec()};
}
void CloseEpisode(const std::string &tag) {
ep_close = {tag, millisec()};
}
bool ApplyAction(Action move) {
reward_t reward = move.Apply(state());
if (reward == -1) return false;
ep_moves.emplace_back(state().GetBoard(), move, reward, millisec() - ep_time);
ep_score += reward;
return true;
}
Agent &TakeTurns(Agent &player, Agent &evil) {
ep_time = millisec();
return (std::max(step() + 1, size_t(9)) % 2) ? evil : player;
}
Agent &TakeLastTurns(Agent &play, Agent &evil) {
return TakeTurns(evil, play);
}
public:
size_t step(unsigned who = -1u) const {
int size = ep_moves.size(); // 'int' is important for handling 0
switch (who) {
case Action::Slide::type_:
return (size - 1) / 2;
case Action::Place::type_:
return (size - (size - 1) / 2);
default:
return size;
}
}
time_t time(unsigned who = -1u) const {
time_t time = 0;
size_t i = 2;
switch (who) {
case Action::Place::type_:
if (ep_moves.size()) time += ep_moves[0].time, i = 1;
// no break;
case Action::Slide::type_:
while (i < ep_moves.size()) time += ep_moves[i].time, i += 2;
break;
default:
time = ep_close.when - ep_open.when;
break;
}
return time;
}
std::vector<Action> actions(unsigned who = -1u) const {
std::vector<Action> res;
size_t i = 2;
switch (who) {
case Action::Place::type_:
if (ep_moves.size()) res.push_back(ep_moves[0]), i = 1;
// no break;
case Action::Slide::type_:
while (i < ep_moves.size()) res.push_back(ep_moves[i]), i += 2;
break;
default:
res.assign(ep_moves.begin(), ep_moves.end());
break;
}
return res;
}
public:
friend std::ostream &operator<<(std::ostream &out, const Episode &ep) {
out << ep.ep_open << '|';
for (const Move &mv : ep.ep_moves) out << mv;
out << '|' << ep.ep_close;
return out;
}
friend std::istream &operator>>(std::istream &in, Episode &ep) {
ep = {};
std::string token;
std::getline(in, token, '|');
std::stringstream(token) >> ep.ep_open;
std::getline(in, token, '|');
for (std::stringstream moves(token); !moves.eof(); moves.peek()) {
ep.ep_moves.emplace_back();
moves >> ep.ep_moves.back();
ep.ep_score += Action(ep.ep_moves.back()).Apply(ep.ep_state);
}
std::getline(in, token, '|');
std::stringstream(token) >> ep.ep_close;
return in;
}
protected:
struct Meta {
std::string tag;
time_t when;
Meta(const std::string &tag = "N/A", time_t when = 0) : tag(tag), when(when) {}
friend std::ostream &operator<<(std::ostream &out, const Meta &m) {
return out << m.tag << "@" << std::dec << m.when;
}
friend std::istream &operator>>(std::istream &in, Meta &m) {
return std::getline(in, m.tag, '@') >> std::dec >> m.when;
}
};
static Board64 InitialState() {
return {};
}
static time_t millisec() {
auto now = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
}
private:
Board64 ep_state;
reward_t ep_score;
std::vector<Move> ep_moves;
time_t ep_time;
Meta ep_open;
Meta ep_close;
};