gtsam
Loading...
Searching...
No Matches
Constraint.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
23#include <gtsam/dllexport.h>
24
25#include <map>
26
27namespace gtsam {
28
29class Domain;
30using Domains = std::map<Key, Domain>;
31
36class GTSAM_EXPORT Constraint : public DiscreteFactor {
37 public:
38 typedef std::shared_ptr<Constraint> shared_ptr;
39
40 protected:
43
46
49
51 template <class KeyIterator>
52 Constraint(KeyIterator beginKey, KeyIterator endKey)
53 : DiscreteFactor(beginKey, endKey) {}
54
55 public:
58
61
63 ~Constraint() override {}
64
68
76 virtual bool ensureArcConsistency(Key j, Domains* domains) const = 0;
77
79 virtual shared_ptr partiallyApply(const DiscreteValues&) const = 0;
80
82 virtual shared_ptr partiallyApply(const Domains&) const = 0;
83
85 virtual DiscreteFactor::shared_ptr operator*(double s) const override {
86 return this->toDecisionTreeFactor() * s;
87 }
88
90 DecisionTreeFactor operator*(const DecisionTreeFactor& dtf) const override {
91 return this->toDecisionTreeFactor() * dtf;
92 }
93
96 const DiscreteFactor::shared_ptr& df) const override {
97 return this->toDecisionTreeFactor() / df;
98 }
99
101 uint64_t nrValues() const override { return 1; };
102
103 DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override {
104 return toDecisionTreeFactor().sum(nrFrontals);
105 }
106
108 return toDecisionTreeFactor().sum(keys);
109 }
110
112 double max() const override { return toDecisionTreeFactor().max(); }
113
114 DiscreteFactor::shared_ptr max(size_t nrFrontals) const override {
115 return toDecisionTreeFactor().max(nrFrontals);
116 }
117
119 return toDecisionTreeFactor().max(keys);
120 }
121
124 throw std::runtime_error("Constraint::error not implemented");
125 }
126
129 const DiscreteValues& assignment) const override {
130 throw std::runtime_error("Constraint::restrict not implemented");
131 }
132
136
138 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
139 const Names& names = {}) const override {
140 return "`Constraint` on " + std::to_string(size()) + " variables\n";
141 }
142
144 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
145 const Names& names = {}) const override {
146 return "<p>Constraint on " + std::to_string(size()) + " variables</p>";
147 }
148
150};
151// DiscreteFactor
152
153} // namespace gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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
An algebraic decision tree fixes the range of a DecisionTree to double.
Definition AlgebraicDecisionTree.h:41
virtual DiscreteFactor::shared_ptr operator*(double s) const override
Multiply by a scalar.
Definition Constraint.h:85
~Constraint() override
Virtual destructor.
Definition Constraint.h:63
virtual shared_ptr partiallyApply(const Domains &) const =0
Partially apply known values, domain version.
Constraint(const KeyVector &js)
Construct n-way constraint factor.
Definition Constraint.h:48
Constraint()
Default constructor for I/O.
DiscreteFactor::shared_ptr max(size_t nrFrontals) const override
Create new factor by maximizing over all values with the same separator.
Definition Constraint.h:114
DiscreteFactor::shared_ptr sum(const Ordering &keys) const override
Create new factor by summing all values with the same separator values.
Definition Constraint.h:107
DiscreteFactor::shared_ptr operator/(const DiscreteFactor::shared_ptr &df) const override
divide by DiscreteFactor::shared_ptr f (safely)
Definition Constraint.h:95
Constraint(Key j1, Key j2)
Construct binary constraint factor.
Definition Constraint.h:45
DiscreteFactor::shared_ptr restrict(const DiscreteValues &assignment) const override
Compute error for each assignment and return as a tree.
Definition Constraint.h:128
virtual shared_ptr partiallyApply(const DiscreteValues &) const =0
Partially apply known values.
double max() const override
Find the max value.
Definition Constraint.h:112
Constraint(KeyIterator beginKey, KeyIterator endKey)
Construct from a key iterator range.
Definition Constraint.h:52
AlgebraicDecisionTree< Key > errorTree() const override
Compute error for each assignment and return as a tree.
Definition Constraint.h:123
DecisionTreeFactor operator*(const DecisionTreeFactor &dtf) const override
Multiply by a DecisionTreeFactor and return a DecisionTreeFactor.
Definition Constraint.h:90
DiscreteFactor::shared_ptr max(const Ordering &keys) const override
Create new factor by maximizing over all values with the same separator.
Definition Constraint.h:118
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
Definition Constraint.h:138
DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override
Create new factor by summing all values with the same separator values.
Definition Constraint.h:103
Constraint(Key j)
Construct unary constraint factor.
Definition Constraint.h:42
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
Definition Constraint.h:144
uint64_t nrValues() const override
Get the number of non-zero values contained in this factor.
Definition Constraint.h:101
virtual bool ensureArcConsistency(Key j, Domains *domains) const =0
Ensure Arc-consistency by checking every possible value of domain j.
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:42
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition DiscreteFactor.h:46
DiscreteValues::Names Names
Translation table from values to strings.
Definition DiscreteFactor.h:194
DiscreteFactor()
Default constructor creates empty factor.
Definition DiscreteFactor.h:65
A map from keys to values.
Definition DiscreteValues.h:34
The Domain class represents a constraint that restricts the possible values a particular variable,...
Definition Domain.h:21
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
Definition Ordering.h:33