AIToolbox
A library that offers tools for AI problem solving.
|
Go to the documentation of this file. 1 #ifndef AI_TOOLBOX_POMDP_WITNESS_HEADER_FILE
2 #define AI_TOOLBOX_POMDP_WITNESS_HEADER_FILE
4 #include <unordered_set>
6 #include <boost/functional/hash.hpp>
62 Witness(
unsigned horizon,
double tolerance);
115 std::tuple<double, ValueFunction>
operator()(
const M & model);
123 template <
typename ProjectionsRow>
124 void addDefaultEntry(
const ProjectionsRow & projs);
132 template <
typename ProjectionsRow>
133 void addVariations(
const ProjectionsRow & projs,
const VEntry & variated);
139 std::vector<MDP::Values> agenda_;
140 std::unordered_set<VObs, boost::hash<VObs>> triedVectors_;
149 std::vector<VList> U(A);
153 unsigned timestep = 0;
160 size_t reserveSize = 1;
167 double variation = tolerance_ * 2;
168 while ( timestep < horizon_ && ( !useTolerance || variation > tolerance_ ) ) {
172 reserveSize = std::max(reserveSize, 2 * v[timestep-1].size());
177 auto projections = project(v[timestep-1]);
179 size_t finalWSize = 0;
180 for (
size_t a = 0; a < A; ++a ) {
184 triedVectors_.clear();
192 addDefaultEntry(projections[a]);
195 while ( !agenda_.empty() ) {
196 const auto witness = lp.
findWitness(agenda_.back());
202 addVariations(projections[a], U[a].back());
205 if ( ++counter == reserveSize ) {
213 finalWSize += U[a].size();
216 w.reserve(finalWSize);
219 for (
size_t a = 0; a < A; ++a )
220 w.insert(std::end(w), std::make_move_iterator(std::begin(U[a])), std::make_move_iterator(std::end(U[a])));
224 const auto begin = std::begin(w);
225 const auto end = std::end (w);
226 w.erase(prune(begin, end,
unwrap), end);
228 v.emplace_back(std::move(w));
231 if ( useTolerance ) {
236 return std::make_tuple(useTolerance ? variation : 0.0, v);
239 template <
typename ProjectionsRow>
240 void Witness::addDefaultEntry(
const ProjectionsRow & projs) {
244 for (
size_t o = 0; o < O; ++o )
245 v.noalias() += projs[o][0].values;
247 triedVectors_.emplace(O, 0);
248 agenda_.emplace_back(std::move(v));
251 template <
typename ProjectionsRow>
252 void Witness::addVariations(
const ProjectionsRow & projs,
const VEntry & variated) {
254 auto vObs = variated.observations;
255 const auto & vValues = variated.values;
257 for (
size_t o = 0; o < O; ++o ) {
258 const size_t skip = vObs[o];
260 for (
size_t i = 0; i < projs[o].size(); ++i ) {
261 if ( i == skip )
continue;
264 if ( triedVectors_.find(vObs) != std::end(triedVectors_) )
continue;
266 triedVectors_.insert(vObs);
268 auto v = vValues - projs[o][skip].values + projs[o][i].values;
269 agenda_.emplace_back(std::move(v));