This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
teaching_task.cpp
232 lines (198 loc) · 7.1 KB
/
teaching_task.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
// Copyright (c) 2017 Baidu Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "teaching_task.h"
#include "simulator_util.h"
namespace simulator {
namespace py = boost::python;
void Task::init_py_task() {
try {
// check if there is a python file with this name
auto mod = py::import(name_.c_str());
py_task_ = mod.attr(name_.c_str())(game_->get_py_env());
} catch (...) {
PyErr_Print();
}
}
void Task::register_stages() {
CHECK(PyObject_HasAttrString(py_task_.ptr(), "get_stage_names"))
<< "Python function get_stage_names() is undefined!";
std::vector<std::string> stage_names;
try {
py::list py_stage_names = py::extract<py::list>(py_task_.attr("get_stage_names")());
for (int i = 0; i < py::len(py_stage_names); i ++) {
stage_names.push_back(py::extract<std::string>(py_stage_names[i]));
}
} catch (...) {
PyErr_Print();
LOG(FATAL) << "Error get stage names";
}
for (const auto& name : stage_names) {
stages_[name] = [this, name]() {
return py_stage(name);
};
}
}
size_t Task::total_possible_sentences() {
try {
return py::extract<int>(py_task_.attr("total_possible_sentences")());
} catch (...) {
PyErr_Print();
LOG(FATAL) << "Error total possible sentences";
}
return 0;
}
std::string Task::py_stage(const std::string& stage_name) {
CHECK(PyObject_HasAttrString(py_task_.ptr(), stage_name.c_str()))
<< "Python task stage is undefined: " << stage_name;
std::string next_stage = "";
double reward = 0;
std::string sentence = "";
// pre-stage: update the python env with simulator changes
std::vector<Entity> entities;
game_->get_all_entities(entities);
py::list py_entities; // a list of py::dict
for (const auto& e : entities) {
py_entities.append(e.to_py_dict());
}
try {
py::object env = game_->get_py_env();
env.attr("update_entities_from_cpp")(py_entities);
auto agent_sent = game_->get_agent_sent_from_buffer();
env.attr("update_agent_sentence_from_cpp")(agent_sent.c_str());
auto action_success = game_->get_agent_action_successful_from_buffer();
env.attr("update_agent_action_success_from_cpp")(action_success);
auto game_event = game_->get_events_of_game();
env.attr("update_game_event_from_cpp")(game_event.c_str());
// during the stage
py::list ret = py::extract<py::list>(py_task_.attr(stage_name.c_str())());
// post-stage: the teacher might have changed the environment
if (env.attr("env_changed")()) {
game_->update_environment();
}
std::string event = py::extract<std::string>(py_task_.attr("get_event")());
game_->record_event_in_buffer(event);
CHECK_EQ(py::len(ret), 3) << "Incorrect length of stage returns";
next_stage = py::extract<std::string>(ret[0]);
reward = py::extract<double>(ret[1]);
sentence = py::extract<std::string>(ret[2]);
} catch (...) {
PyErr_Print();
LOG(FATAL) << "Error py_stage";
}
give_reward(reward);
teacher_speak(sentence);
return next_stage;
}
void Task::teacher_speak(const std::string& sentence) {
if (game_->can_record_teacher_sent_in_buffer()) {
game_->record_teacher_sent_in_buffer(sentence);
game_->record_teacher_sent_type_in_buffer(name_);
} else {
// (task_groups_exclusive = false)
// TODO: if sentence is not spoken, then the task
// perhaps should not proceed to the next stage.
}
}
void Task::run_stage() {
CHECK_GT(stages_.count(current_stage_), 0) << "Unrecognized stage name: "
<< current_stage_;
current_stage_ = stages_[current_stage_]();
}
void Task::obtain_performance(BenchmarkRes& br) {
try {
py::tuple perf = py::extract<py::tuple>(py_task_.attr("obtain_performance")());
br.successes = py::extract<int>(perf[0]);
br.failures = py::extract<int>(perf[1]);
br.success_steps = py::extract<int>(perf[2]);
} catch (...) {
PyErr_Print();
LOG(FATAL) << "Error obtaining performance";
}
}
void TaskGroup::add_task(const std::string& task, double weight) {
CHECK_GT(weight, 0) << "A task must have a positive weight";
CHECK_EQ(task.find(name_), 0)
<< "Task group name must be a prefix of the task name:" << name_ << " "
<< task;
TaskPtr task_ptr = std::make_shared<Task>(task, game_);
task_list_.push_back(task_ptr);
if (task_weights_.empty()) {
task_weights_.push_back(weight);
} else {
task_weights_.push_back(task_weights_.back() + weight);
}
}
void TaskGroup::report_task_performance(
std::unordered_map<std::string, BenchmarkRes>& benchmark) {
for (auto task : task_list_) {
auto task_name = task->name();
BenchmarkRes br;
task->obtain_performance(br);
auto& p = benchmark[task_name];
if (benchmark.count(task_name) == 0) {
p = br;
} else {
p += br;
}
}
}
void TaskGroup::reset() {
// For now, we only need to untrack the busy task.
// We will do a lazy reset later when a task
// is picked as the busy task.
busy_task_ = nullptr;
}
bool TaskGroup::is_idle() {
if (!busy_task_) {
return true;
} else { // a task that was busy might be idle now; we need to check
if (busy_task_->is_idle()) {
busy_task_ = nullptr;
return true;
} else {
return false;
}
}
}
std::string TaskGroup::current_stage() {
if (busy_task_) {
return name_ + " | Task-> " + busy_task_->current_stage();
} else {
return name_ + " | All tasks: idle";
}
}
void TaskGroup::run_stage() {
auto sample_task = [&]() {
int idx = -1;
if (schedule_ == "weighted") {
idx = util::simple_importance_sampling(task_weights_);
} else { // random
idx = util::get_rand_ind(task_list_.size());
}
return idx;
};
if (is_idle()) {
// when idle, randomly sample a task
busy_task_ = task_list_[sample_task()];
// Here we do a lazy reset of the busy task,
// only before we are running that task.
busy_task_->reset();
}
busy_task_->run_stage();
}
size_t TaskGroup::total_possible_sentences() {
size_t total = 0;
for (auto task : task_list_) {
total += task->total_possible_sentences();
}
return total;
}
} // namespace simulator