-
Notifications
You must be signed in to change notification settings - Fork 1
/
episode.h
182 lines (165 loc) · 4.55 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
/**
* Framework for 2048 & 2048-Like Games (C++ 11)
* episode.h: Data structure for storing an episode
*
* Author: Hung Guei
* Computer Games and Intelligence (CGI) Lab, NYCU, Taiwan
* https://cgilab.nctu.edu.tw/
*/
#pragma once
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <chrono>
#include <numeric>
#include "board.h"
#include "action.h"
#include "agent.h"
class episode {
public:
episode() : ep_state(initial_state()), ep_score(0), ep_time(0) { ep_moves.reserve(10000); }
public:
board& state() { return ep_state; }
const board& state() const { return ep_state; }
board::score score() const { return ep_score; }
void open_episode(const std::string& tag) {
ep_open = { tag, millisec() };
}
void close_episode(const std::string& tag) {
ep_close = { tag, millisec() };
}
bool apply_action(action move) {
board::reward reward = move.apply(state());
if (reward == -1) return false;
ep_moves.emplace_back(move, reward, millisec() - ep_time);
ep_score += reward;
return true;
}
agent& take_turns(agent& slide, agent& place) {
ep_time = millisec();
return step() < 2 || step() % 2 ? place : slide;
}
agent& last_turns(agent& slide, agent& place) {
return step() > 2 ? take_turns(place, slide) : place;
}
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 move {
action code;
board::reward reward;
time_t time;
move(action code = {}, board::reward reward = 0, time_t time = 0) : 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;
}
};
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 board initial_state() {
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:
board ep_state;
board::score ep_score;
std::vector<move> ep_moves;
time_t ep_time;
meta ep_open;
meta ep_close;
};