-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTapirNode.hpp
271 lines (229 loc) · 7.94 KB
/
TapirNode.hpp
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
262
263
264
265
266
267
268
269
270
271
/** @file TapirNode.hpp
*
* Defines TapirNode, an abstract class that can be used to implement the
* TAPIR solver as a ROS node.
*/
#ifndef TAPIR_NODE_HPP_
#define TAPIR_NODE_HPP_
#include <memory> // For unique_ptr
#include <iostream> // for cout
#include <ros/ros.h>
#include <ros/package.h> // For finding package path
#include "global.hpp"
#include "solver/Agent.hpp"
#include "solver/Solver.hpp"
#include "solver/Simulator.hpp"
#include "solver/BeliefTree.hpp"
#include "solver/BeliefNode.hpp"
template <class Options, class Model, class Observation, class Action>
class TapirNode {
protected:
std::unique_ptr<ros::NodeHandle> node_;
solver::Agent* agent_;
Model* solverModel_;
std::unique_ptr<solver::Solver> solver_;
std::unique_ptr<solver::Simulator> simulator_;
Options options_;
RandomGenerator solverGen_;
RandomGenerator simulatorGen_;
bool internalSimulation_;
std::string rosNamespace_;
long stepNumber_;
bool finished_;
std::unique_ptr<Observation> lastObservation_;
std::unique_ptr<Action> lastAction_;
/** Return a current observation. Left for user implementation */
virtual std::unique_ptr<Observation> getObservation() {
return nullptr;
}
/** Apply an action. Left for user implementation */
virtual void applyAction(const Action& action) {}
/** Load options from file. problemPath should be path to folder containing
* configuration files, default <ROS package path>/problems/<problemName>
* cfgFile is the cfg file to load, the default is default.cfg
*/
virtual void loadOptions(std::string problemPath,
std::string cfgFile = "default.cfg") {
std::string cfgPath = problemPath + "/" + cfgFile;
std::unique_ptr<options::OptionParser> parser = Options::makeParser(false);
try {
parser->setOptions(&options_);
parser->parseCfgFile(cfgPath);
parser->finalize();
// Change directory, as code assumes this location for e.g.
// loading maps from file
tapir::change_directory(problemPath);
} catch (options::OptionParsingException const &e) {
std::cerr << e.what() << std::endl;
}
}
/** Initialise models, solver, and simulator (if required). The Simulator
* class is only used if internalSimulation == true, in this case
* resulting observations and states are generated.
* If generatePolicy is set to false, a starting policy is not
* automatically generated. solver_->improvePolicy must then be manually
* called before starting.
*/
virtual void initTapir(bool generatePolicy = true) {
// If seed is not specified in options, seed using current time
if (options_.seed == 0) {
options_.seed = std::time(nullptr);
}
solverGen_.seed(options_.seed);
solverGen_.discard(10);
// Init solver
std::cout << "Global seed: " << options_.seed << std::endl;
std::unique_ptr<Model> solverModel = std::make_unique<Model>(
&solverGen_, std::make_unique<Options>(options_));
solver_ = std::make_unique<solver::Solver>(std::move(solverModel));
solver_->initializeEmpty();
solverModel_ = static_cast<Model *>(solver_->getModel());
// Generate policy
if (generatePolicy) {
double totT;
double tStart;
tStart = tapir::clock_ms();
solver_->improvePolicy();
totT = tapir::clock_ms() - tStart;
std::cout << "Total solving time: " << totT << "ms" << std::endl;
}
// Init simulator
if (internalSimulation_) {
simulatorGen_.seed(options_.seed);
simulatorGen_.discard(10000);
std::unique_ptr<Model> simModel = std::make_unique<Model>(
&simulatorGen_, std::make_unique<Options>(options_));
simulator_ = std::make_unique<solver::Simulator>(
std::move(simModel), solver_.get(), options_.areDynamic);
if (options_.hasChanges) {
simulator_->loadChangeSequence(options_.changesPath);
}
simulator_->setMaxStepCount(options_.nSimulationSteps);
agent_ = simulator_->getAgent();
} else {
agent_ = new solver::Agent(solver_.get());
}
}
/** Tapir processing, typically called in a timer loop */
virtual void processTapir() {
if (internalSimulation_) {
finished_ = !simulator_->stepSimulation();
return;
}
std::stringstream prevStream;
solver::BeliefNode *currentBelief = agent_->getCurrentBelief();
if (options_.hasVerboseOutput) {
std::cout << std::endl << std::endl << "t-" << stepNumber_ << std::endl;
std::cout << "Belief #" << currentBelief->getId() << std::endl;
solver::HistoricalData *data = currentBelief->getHistoricalData();
if (data != nullptr) {
std::cout << std::endl;
std::cout << *data;
std::cout << std::endl;
}
prevStream << "Before:" << std::endl;
solver_->printBelief(currentBelief, prevStream);
}
if (stepNumber_ != 0) {
// Get observation
lastObservation_ = getObservation();
if (options_.hasVerboseOutput) {
std::cout << "Observation: " << *lastObservation_ << std::endl;
}
// Update belief
solver_->replenishChild(currentBelief, *lastAction_, *lastObservation_);
agent_->updateBelief(*lastAction_, *lastObservation_);
// If we're pruning on every step, we do it now.
if (options_.pruneEveryStep) {
double pruningTimeStart = tapir::clock_ms();
long nSequencesDeleted = solver_->pruneSiblings(currentBelief);
long pruningTime = tapir::clock_ms() - pruningTimeStart;
if (options_.hasVerboseOutput) {
std::cout << "Pruned " << nSequencesDeleted << " sequences in ";
std::cout << pruningTime << "ms." << std::endl;
}
}
if (currentBelief->getNumberOfParticles() == 0) {
debug::show_message("ERROR: Resulting belief has zero particles!!");
}
// Improve policy
currentBelief = agent_->getCurrentBelief();
solver_->improvePolicy(currentBelief);
if (options_.hasVerboseOutput) {
std::stringstream newStream;
newStream << "After:" << std::endl;
solver_->printBelief(currentBelief, newStream);
while (prevStream.good() || newStream.good()) {
std::string s1, s2;
std::getline(prevStream, s1);
std::getline(newStream, s2);
std::cout << s1 << std::setw(40 - s1.size()) << "";
std::cout << s2 << std::setw(40 - s2.size()) << "";
std::cout << std::endl;
}
}
}
stepNumber_++;
// Get preferred action. Needs to be cast into problem's action
std::unique_ptr<solver::Action> sa = agent_->getPreferredAction();
if (sa == nullptr) {
std::cout << "ERROR: Could not choose an action." << std::endl;
} else {
if (options_.hasVerboseOutput) {
std::cout << "Action: " << *sa << std::endl;
}
lastAction_.reset(static_cast<Action *>(sa.release()));
applyAction(*lastAction_);
}
}
/** Handle model changes */
void handleChanges(
std::vector<std::unique_ptr<solver::ModelChange>> const &changes,
bool resetTree) {
bool hasDynamicChanges = options_.areDynamic;
if (internalSimulation_) {
simulator_->handleChanges(changes, hasDynamicChanges, resetTree);
}
if (hasDynamicChanges) {
solver_->setChangeRoot(agent_->getCurrentBelief());
} else {
solver_->setChangeRoot(nullptr);
}
if (resetTree) {
solver_->getModel()->applyChanges(changes, nullptr);
} else {
// The model only needs to inform the solver of changes if we intend
// to keep the policy.
solver_->getModel()->applyChanges(changes, solver_.get());
}
// Apply the changes, or simply reset the tree.
if (resetTree) {
solver_->resetTree(agent_->getCurrentBelief());
agent_->setCurrentBelief(solver_->getPolicy()->getRoot());
} else {
solver_->applyChanges();
}
}
public:
TapirNode(std::string rosNamespace = "") :
internalSimulation_(false),
agent_(nullptr),
solverModel_(nullptr),
stepNumber_(0),
finished_(false),
rosNamespace_(rosNamespace) {
}
virtual ~TapirNode() {
if (!internalSimulation_) {
delete agent_;
}
}
virtual void initialiseBase(std::string problemName) {
node_ = std::make_unique<ros::NodeHandle>(rosNamespace_);
std::string problemPath = ros::package::getPath("tapir") +
"/problems/" + problemName;
loadOptions(problemPath);
initTapir();
}
};
#endif /* TAPIR_NODE_HPP_ */