gtsam 4.1.1
gtsam
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 <iostream>
22#include <vector>
23#include <map>
24
25
26namespace gtsam {
27
33 template<class L>
34 class Assignment: public std::map<L, size_t> {
35 public:
36 void print(const std::string& s = "Assignment: ") const {
37 std::cout << s << ": ";
38 for(const typename Assignment::value_type& keyValue: *this)
39 std::cout << "(" << keyValue.first << ", " << keyValue.second << ")";
40 std::cout << std::endl;
41 }
42
43 bool equals(const Assignment& other, double tol = 1e-9) const {
44 return (*this == other);
45 }
46 }; //Assignment
47
48
61 template<typename L>
62 std::vector<Assignment<L> > cartesianProduct(
63 const std::vector<std::pair<L, size_t> >& keys) {
64 std::vector<Assignment<L> > allPossValues;
65 Assignment<L> values;
66 typedef std::pair<L, size_t> DiscreteKey;
67 for(const DiscreteKey& key: keys)
68 values[key.first] = 0; //Initialize from 0
69 while (1) {
70 allPossValues.push_back(values);
71 size_t j = 0;
72 for (j = 0; j < keys.size(); j++) {
73 L idx = keys[j].first;
74 values[idx]++;
75 if (values[idx] < keys[j].second)
76 break;
77 //Wrap condition
78 values[idx] = 0;
79 }
80 if (j == keys.size())
81 break;
82 }
83 return allPossValues;
84 }
85
86} // namespace gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
std::vector< Assignment< L > > cartesianProduct(const std::vector< std::pair< L, size_t > > &keys)
Get Cartesian product consisting all possible configurations.
Definition: Assignment.h:62
Template to create a binary predicate.
Definition: Testable.h:111
An assignment from labels to value index (size_t).
Definition: Assignment.h:34