gtsam
Loading...
Searching...
No Matches
DiscreteValues.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
17
18#pragma once
19
22#include <gtsam/inference/Key.h>
23
24#include <map>
25#include <string>
26#include <vector>
27
28namespace gtsam {
29
34class GTSAM_EXPORT DiscreteValues : public Assignment<Key> {
35 public:
36 using Base = Assignment<Key>; // base class
37
40 using Assignment::Assignment; // all constructors
41
42 // Define the implicit default constructor.
43 DiscreteValues() = default;
44
45 // Construct from assignment.
46 explicit DiscreteValues(const Base& a) : Base(a) {}
47
48 // Construct from initializer list.
49 DiscreteValues(std::initializer_list<std::pair<const Key, size_t>> init)
50 : Assignment<Key>{init} {}
51
55
57 void print(const std::string& s = "",
58 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
59
61 bool equals(const DiscreteValues& x, double tol = 1e-9) const;
62
66
68 friend std::ostream& operator<<(std::ostream& os, const DiscreteValues& x);
69
70 // insert in base class;
71 std::pair<iterator, bool> insert(const value_type& value) {
72 return Base::insert(value);
73 }
74
82 DiscreteValues& insert(const std::pair<Key, size_t>& assignment);
83
91 DiscreteValues& insert(const DiscreteValues& values);
92
102 DiscreteValues& update(const DiscreteValues& values);
103
111 DiscreteValues& insert_or_assign(const DiscreteValues& values);
112
119 bool contains(Key key) const { return this->find(key) != this->end(); }
120
127 DiscreteValues filter(const DiscreteKeys& keys) const {
128 DiscreteValues result;
129 for (const auto& [key, _] : keys) {
130 if (auto it = this->find(key); it != this->end())
131 result[key] = it->second;
132 }
133 return result;
134 }
135
143 DiscreteKeys result;
144 for (const auto& [key, cardinality] : keys) {
145 if (!this->contains(key)) result.emplace_back(key, cardinality);
146 }
147 return result;
148 }
149
157 static std::vector<DiscreteValues> CartesianProduct(
158 const DiscreteKeys& keys) {
160 }
161
165
167 using Names = std::map<Key, std::vector<std::string>>;
168
170 static std::string Translate(const Names& names, Key key, size_t index);
171
179 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
180 const Names& names = {}) const;
181
189 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
190 const Names& names = {}) const;
191
193};
194
196inline std::vector<DiscreteValues> cartesianProduct(const DiscreteKeys& keys) {
198}
199
201std::string GTSAM_EXPORT
202markdown(const DiscreteValues& values,
203 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
204 const DiscreteValues::Names& names = {});
205
207std::string GTSAM_EXPORT
208html(const DiscreteValues& values,
209 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
210 const DiscreteValues::Names& names = {});
211
212// traits
213template <>
214struct traits<DiscreteValues> : public Testable<DiscreteValues> {};
215
216} // namespace gtsam
specialized key for discrete variables
An assignment from labels to a discrete value index (size_t).
Global functions in a separate testing namespace.
Definition chartTesting.h:28
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition DiscreteValues.cpp:160
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:155
std::vector< DiscreteValues > cartesianProduct(const DiscreteKeys &keys)
Free version of CartesianProduct.
Definition DiscreteValues.h:196
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
static std::vector< AssignmentType > CartesianProduct(const std::vector< std::pair< Key, size_t > > &keys)
Definition Assignment.h:89
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:41
A map from keys to values.
Definition DiscreteValues.h:34
DiscreteValues & insert_or_assign(const DiscreteValues &values)
Insert each element of values if it does not already exist, else assign it.
Definition DiscreteValues.cpp:96
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a markdown table.
Definition DiscreteValues.cpp:114
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition DiscreteValues.h:167
DiscreteKeys missingKeys(const DiscreteKeys &keys) const
Return the keys that are not present in the DiscreteValues object.
Definition DiscreteValues.h:142
DiscreteValues filter(const DiscreteKeys &keys) const
Filter values by keys.
Definition DiscreteValues.h:127
friend std::ostream & operator<<(std::ostream &os, const DiscreteValues &x)
ostream operator:
Definition DiscreteValues.cpp:37
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
Definition DiscreteValues.cpp:104
DiscreteValues & update(const DiscreteValues &values)
Update values with corresponding keys from another DiscreteValues object.
Definition DiscreteValues.cpp:82
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
Definition DiscreteValues.h:157
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print required by Testable.
Definition DiscreteValues.cpp:43
bool contains(Key key) const
Check if the DiscreteValues contains the given key.
Definition DiscreteValues.h:119
bool equals(const DiscreteValues &x, double tol=1e-9) const
equals required by Testable for unit testing.
Definition DiscreteValues.cpp:51
The Factor::error simply extracts the.