gtsam/gtsam_unstable/discrete/SingleValue.h

81 lines
2.3 KiB
C
Raw Normal View History

2012-04-16 06:35:28 +08:00
/*
* SingleValue.h
* @brief domain constraint
* @date Feb 6, 2012
* @author Frank Dellaert
*/
#pragma once
2021-11-18 23:54:00 +08:00
#include <gtsam_unstable/discrete/Constraint.h>
2012-04-16 06:35:28 +08:00
namespace gtsam {
2021-11-18 23:17:49 +08:00
/**
* SingleValue constraint: ensures a variable takes on a certain value.
* This could of course also be implemented by changing its `Domain`.
*/
class GTSAM_UNSTABLE_EXPORT SingleValue: public Constraint {
size_t cardinality_; /// < Number of values
size_t value_; ///< allowed value
DiscreteKey discreteKey() const {
return DiscreteKey(keys_[0],cardinality_);
}
2021-11-18 23:17:49 +08:00
public:
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
typedef boost::shared_ptr<SingleValue> shared_ptr;
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
/// Construct from key, cardinality, and given value.
SingleValue(Key key, size_t n, size_t value) :
Constraint(key), cardinality_(n), value_(value) {
}
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
/// Construct from DiscreteKey and given value.
SingleValue(const DiscreteKey& dkey, size_t value) :
Constraint(dkey.first), cardinality_(dkey.second), value_(value) {
}
// print
void print(const std::string& s = "",
const KeyFormatter& formatter = DefaultKeyFormatter) const override;
/// equals
bool equals(const DiscreteFactor& other, double tol) const override {
if(!dynamic_cast<const SingleValue*>(&other))
return false;
else {
const SingleValue& f(static_cast<const SingleValue&>(other));
return (cardinality_==f.cardinality_) && (value_==f.value_);
}
}
/// Calculate value
double operator()(const Values& values) const override;
/// Convert into a decisiontree
DecisionTreeFactor toDecisionTreeFactor() const override;
/// Multiply into a decisiontree
DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
/*
* Ensure Arc-consistency: just sets domain[j] to {value_}
* @param j domain to be checked
* @param domains all other domains
*/
bool ensureArcConsistency(size_t j,
std::vector<Domain>* domains) const override;
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
/// Partially apply known values
Constraint::shared_ptr partiallyApply(const Values& values) const override;
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
/// Partially apply known values, domain version
Constraint::shared_ptr partiallyApply(
const std::vector<Domain>& domains) const override;
};
2012-04-16 06:35:28 +08:00
2021-11-18 23:17:49 +08:00
} // namespace gtsam