Go to the documentation of this file.
32 DecisionTreeFactor::DecisionTreeFactor() {}
36 const ADT& potentials)
70 return (
a == 0 ||
b == 0) ? 0 : (
a /
b);
107 for (
Key j :
f.keys()) cs[
j] =
f.cardinality(
j);
110 keys.reserve(cs.size());
111 for (
const auto&
key : cs) {
123 if (nrFrontals >
size()) {
124 throw invalid_argument(
125 "DecisionTreeFactor::combine: invalid number of frontal "
127 std::to_string(nrFrontals) +
", nr.keys=" + std::to_string(
size()));
133 for (
i = 0;
i < nrFrontals;
i++) {
140 for (;
i <
keys_.size();
i++) {
144 return std::make_shared<DecisionTreeFactor>(dkeys,
result);
150 if (frontalKeys.size() >
size()) {
151 throw invalid_argument(
152 "DecisionTreeFactor::combine: invalid number of frontal "
154 std::to_string(frontalKeys.size()) +
", nr.keys=" +
155 std::to_string(
size()));
161 for (
i = 0;
i < frontalKeys.size();
i++) {
162 Key j = frontalKeys[
i];
183 return std::make_shared<DecisionTreeFactor>(dkeys,
result);
196 std::vector<std::pair<DiscreteValues, double>>
result;
197 for (
const auto& assignment : assignments) {
208 std::vector<double> probs;
219 std::set<Key> assignment_keys;
220 for (
auto&& [k,
_] :
a) {
221 assignment_keys.insert(k);
225 std::vector<Key> diff;
226 std::set_difference(allKeys.begin(), allKeys.end(),
227 assignment_keys.begin(), assignment_keys.end(),
228 std::back_inserter(diff));
231 size_t nrAssignments = 1;
232 for (
auto&& k : diff) {
236 probs.insert(probs.end(), nrAssignments,
p);
249 std::stringstream
ss;
250 ss << std::setw(4) << std::setprecision(2) << std::fixed <<
v;
257 bool showZero)
const {
264 bool showZero)
const {
270 bool showZero)
const {
283 ss << keyFormatter(
key) <<
"|";
289 for (
size_t j = 0;
j <
size();
j++)
ss <<
":-:|";
294 for (
const auto& kv :
rows) {
296 auto assignment = kv.first;
298 size_t index = assignment.at(
key);
301 ss << kv.second <<
"|\n";
312 ss <<
"<div>\n<table class='DecisionTreeFactor'>\n <thead>\n";
317 ss <<
"<th>" << keyFormatter(
key) <<
"</th>";
319 ss <<
"<th>value</th></tr>\n";
322 ss <<
" </thead>\n <tbody>\n";
326 for (
const auto& kv :
rows) {
328 auto assignment = kv.first;
330 size_t index = assignment.at(
key);
333 ss <<
"<td>" << kv.second <<
"</td>";
336 ss <<
" </tbody>\n</table>\n</div>";
342 const vector<double>&
table)
357 std::vector<double>
v_;
366 std::push_heap(
v_.begin(),
v_.end(), std::greater<double>{});
371 for (
size_t i = 0;
i <
n; ++
i) {
373 std::push_heap(
v_.begin(),
v_.end(), std::greater<double>{});
379 std::pop_heap(
v_.begin(),
v_.end(), std::greater<double>{});
380 double x =
v_.back();
394 std::cout << (
s.empty() ?
"" :
s +
" ");
395 for (
size_t i = 0;
i <
v_.size();
i++) {
396 std::cout <<
v_.at(
i);
397 if (
v_.size() > 1 &&
i <
v_.size() - 1) std::cout <<
", ";
399 std::cout << std::endl;
406 size_t size()
const {
return v_.size(); }
412 std::set<Key> allKeys = this->
labels();
417 std::set<Key> assignment_keys;
418 for (
auto&& [k,
_] :
a) {
419 assignment_keys.insert(k);
423 std::vector<Key> diff;
424 std::set_difference(allKeys.begin(), allKeys.end(),
425 assignment_keys.begin(), assignment_keys.end(),
426 std::back_inserter(diff));
429 size_t nrAssignments = 1;
430 for (
auto&& k : diff) {
436 if (min_heap.
empty()) {
440 for (
size_t i = 0;
i <
std::min(nrAssignments,
N); ++
i) {
445 if (
p > min_heap.
top()) {
446 if (min_heap.
size() ==
N) {
460 return min_heap.
top();
465 const size_t N = maxNrAssignments;
471 auto thresholdFunc = [threshold, &total,
N](
const double&
value) {
482 if (value < threshold || total >=
N) {
Annotation for function names.
Min-Heap class to help with pruning. The top element is always the smallest value.
void push(double x)
Push value onto the heap.
double computeThreshold(const size_t N) const
Compute the probability value which is the threshold above which only N leaves are present.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
DecisionTreeFactor apply(Unary op) const
std::set< Key > labels() const
DecisionTreeFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
const KeyFormatter & formatter
double error(const DiscreteValues &values) const override
Calculate error for DiscreteValues x, is -log(probability).
const EIGEN_DEVICE_FUNC LogReturnType log() const
DiscreteKeys is a set of keys that can be assembled using the & operator.
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
double pop()
Pop the top value of the heap.
ofstream os("timeSchurFactors.csv")
const_iterator begin() const
std::shared_ptr< DecisionTreeFactor > shared_ptr
void print(const std::string &s="")
Print the heap as a sequence.
static std::stringstream ss
A thin wrapper around std::set that uses boost's fast_pool_allocator.
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
bool empty() const
Return true if heap is empty.
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
bool fpEqual(double a, double b, double tol, bool check_relative_also)
void dot(std::ostream &os, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const
void print(const std::string &s="DecisionTreeFactor:\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
void visitWith(Func f) const
Visit all leaves in depth-first fashion.
virtual double evaluate(const Assignment< Key > &values) const override
DecisionTree apply(const Unary &op) const
const_iterator end() const
std::function< double(const double, const double)> Binary
const gtsam::Symbol key('X', 0)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
double top()
Return the top value of the heap without popping it.
size_t cardinality(Key j) const
shared_ptr combine(size_t nrFrontals, Binary op) const
KeyVector keys_
The keys involved in this factor.
DiscreteValues::Names Names
Translation table from values to strings.
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
const KeyVector & keys() const
Access the factor's involved variable keys.
std::pair< Key, size_t > DiscreteKey
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
Array< int, Dynamic, 1 > v
std::vector< std::pair< DiscreteValues, double > > enumerate() const
Enumerate all values into a map from values to double.
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
MinHeap()
Default constructor.
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
static std::string valueFormatter(const double &v)
std::uint64_t Key
Integer nonlinear key type.
constexpr descr< N - 1 > _(char const (&text)[N])
std::function< double(const double &)> Unary
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
bool contains(const Key &key) const
Check if key exists in ordering.
void push(double x, size_t n)
Push value x, n number of times.
std::vector< double > probabilities() const
Get all the probabilities in order of assignment values.
static double safe_div(const double &a, const double &b)
size_t size() const
Return the size of the heap.
virtual bool equals(const DiscreteFactor &lf, double tol=1e-9) const
equals
gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:26