-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_actor_thread_loop.h
More file actions
107 lines (92 loc) · 2.74 KB
/
Copy pathenv_actor_thread_loop.h
File metadata and controls
107 lines (92 loc) · 2.74 KB
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
#ifndef RLCC_ENV_ACTOR_THREAD_LOOP_H
#define RLCC_ENV_ACTOR_THREAD_LOOP_H
#include "rela/thread_loop.h"
#include "rlcc/env_actor.h"
namespace rlcc {
class EnvActorThreadLoop : public rela::ThreadLoop {
public:
EnvActorThreadLoop(std::vector<std::shared_ptr<EnvActor>> env_actors,
int num_game_per_env = -1, int thread_idx = -1,
bool verbose = false)
: thread_idx_(thread_idx),
env_actors_(env_actors),
num_game_per_env_(num_game_per_env),
verbose_(verbose) {}
void mainLoop() override {
while (!terminated()) {
if (paused()) {
waitUntilResume();
}
if (verbose_) {
std::cout << "Before observe before act" << std::endl;
std::cout << "Terminal count: " << env_actors_[0]->TerminalCount()
<< std::endl;
std::cout << "Current Env:\n"
<< env_actors_[0]->GetEnv()->ToString() << std::endl;
// std::cout << "Current Env:\n"
// << env_actors_[1]->GetEnv()->ToString() << std::endl;
}
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
ea->ObserveBeforeAct();
}
}
if (verbose_) {
std::cout << "Before act" << std::endl;
}
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
ea->Act();
}
}
if (verbose_)
std::cout << "Before observe after act" << std::endl;
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
ea->ObserveAfterAct();
}
}
if (verbose_)
std::cout << "Before send experience" << std::endl;
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
ea->SendExperience();
}
}
if (verbose_)
std::cout << "Before post send experience" << std::endl;
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
ea->PostSendExperience();
}
}
bool all_finished = true;
for (auto &ea : env_actors_) {
if (!EnvFinished(*ea)) {
all_finished = false;
break;
}
}
if (all_finished) {
break;
}
loop_count_++;
if (verbose_) {
std::cout << "Thread " << thread_idx_ << ", Loop count: " << loop_count_
<< std::endl;
}
}
}
private:
const int thread_idx_;
std::vector<std::shared_ptr<EnvActor>> env_actors_;
int num_game_per_env_;
int loop_count_ = 0;
bool verbose_;
//Check is an env actor is finished, useful when eval.
bool EnvFinished(const EnvActor &ea) {
return (num_game_per_env_ > 0 && ea.TerminalCount() >= num_game_per_env_);
}
};
} // namespace rlcc
#endif /* RLCC_ENV_ACTOR_THREAD_LOOP_H */