gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
Assignment.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
19#pragma once
20
21#include <functional>
22#include <iostream>
23#include <map>
24#include <sstream>
25#include <utility>
26#include <vector>
27
28namespace gtsam {
29
36template <class L>
37class Assignment : public std::map<L, size_t> {
45 static std::string DefaultFormatter(const L& x) {
46 std::stringstream ss;
47 ss << x;
48 return ss.str();
49 }
50
51 public:
52 using std::map<L, size_t>::operator=;
53
54 // Define the implicit default constructor.
55 Assignment() = default;
56
57 // Construct from initializer list.
58 Assignment(std::initializer_list<std::pair<const L, size_t>> init)
59 : std::map<L, size_t>{init} {}
60
61 void print(const std::string& s = "Assignment: ",
62 const std::function<std::string(L)>& labelFormatter =
63 &DefaultFormatter) const {
64 std::cout << s << ": ";
65 for (const typename Assignment::value_type& keyValue : *this) {
66 std::cout << "(" << labelFormatter(keyValue.first) << ", "
67 << keyValue.second << ")";
68 }
69 std::cout << std::endl;
70 }
71
72 bool equals(const Assignment& other, double tol = 1e-9) const {
73 return (*this == other);
74 }
75
88 template <typename Derived = Assignment<L>>
89 static std::vector<Derived> CartesianProduct(
90 const std::vector<std::pair<L, size_t>>& keys) {
91 std::vector<Derived> allPossValues;
92 Derived values;
93 typedef std::pair<L, size_t> DiscreteKey;
94 for (const DiscreteKey& key : keys)
95 values[key.first] = 0; // Initialize from 0
96 while (1) {
97 allPossValues.push_back(values);
98 size_t j = 0;
99 for (j = 0; j < keys.size(); j++) {
100 L idx = keys[j].first;
101 values[idx]++;
102 if (values[idx] < keys[j].second) break;
103 // Wrap condition
104 values[idx] = 0;
105 }
106 if (j == keys.size()) break;
107 }
108 return allPossValues;
109 }
110}; // Assignment
111
112} // namespace gtsam
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Template to create a binary predicate.
Definition Testable.h:111
An assignment from labels to value index (size_t).
Definition Assignment.h:37
static std::vector< Derived > CartesianProduct(const std::vector< std::pair< L, size_t > > &keys)
Get Cartesian product consisting all possible configurations.
Definition Assignment.h:89