Fix constraints

release/4.3a0
Frank Dellaert 2025-01-30 21:13:40 -05:00
parent a24246ab63
commit 2b6545233a
6 changed files with 36 additions and 5 deletions

View File

@ -136,9 +136,6 @@ class GTSAM_EXPORT DiscreteFactor : public Factor {
/** /**
* @brief Multiply in a DiscreteFactor and return the result as * @brief Multiply in a DiscreteFactor and return the result as
* DiscreteFactor, both via shared pointers. * DiscreteFactor, both via shared pointers.
*
* @param df DiscreteFactor shared_ptr
* @return DiscreteFactor::shared_ptr
*/ */
virtual DiscreteFactor::shared_ptr multiply( virtual DiscreteFactor::shared_ptr multiply(
const DiscreteFactor::shared_ptr& df) const = 0; const DiscreteFactor::shared_ptr& df) const = 0;
@ -170,7 +167,7 @@ class GTSAM_EXPORT DiscreteFactor : public Factor {
* *
* @return DiscreteFactor::shared_ptr * @return DiscreteFactor::shared_ptr
*/ */
DiscreteFactor::shared_ptr scale() const; virtual DiscreteFactor::shared_ptr scale() const;
/** /**
* Get the number of non-zero values contained in this factor. * Get the number of non-zero values contained in this factor.

View File

@ -72,6 +72,11 @@ class GTSAM_UNSTABLE_EXPORT AllDiff : public Constraint {
/// Partially apply known values, domain version /// Partially apply known values, domain version
Constraint::shared_ptr partiallyApply( Constraint::shared_ptr partiallyApply(
const Domains&) const override; const Domains&) const override;
// Scale just returns the same constraint.
DiscreteFactor::shared_ptr scale() const override {
return std::make_shared<AllDiff>(*this);
}
}; };
} // namespace gtsam } // namespace gtsam

View File

@ -96,6 +96,11 @@ class BinaryAllDiff : public Constraint {
AlgebraicDecisionTree<Key> errorTree() const override { AlgebraicDecisionTree<Key> errorTree() const override {
throw std::runtime_error("BinaryAllDiff::error not implemented"); throw std::runtime_error("BinaryAllDiff::error not implemented");
} }
// Scale just returns the same constraint.
DiscreteFactor::shared_ptr scale() const override {
return std::make_shared<BinaryAllDiff>(*this);
}
}; };
} // namespace gtsam } // namespace gtsam

View File

@ -29,7 +29,9 @@ class Domain;
using Domains = std::map<Key, Domain>; using Domains = std::map<Key, Domain>;
/** /**
* Base class for constraint factors * Base class for constraint factors.
* This class is used to represent constraints on discrete variables.
* The values are always either 0 or 1, with at least one 1.
* Derived classes include SingleValue, BinaryAllDiff, and AllDiff. * Derived classes include SingleValue, BinaryAllDiff, and AllDiff.
*/ */
class GTSAM_UNSTABLE_EXPORT Constraint : public DiscreteFactor { class GTSAM_UNSTABLE_EXPORT Constraint : public DiscreteFactor {
@ -80,6 +82,16 @@ class GTSAM_UNSTABLE_EXPORT Constraint : public DiscreteFactor {
/// Partially apply known values, domain version /// Partially apply known values, domain version
virtual shared_ptr partiallyApply(const Domains&) const = 0; virtual shared_ptr partiallyApply(const Domains&) const = 0;
/// Multiply in a DecisionTreeFactor.
DecisionTreeFactor operator*(const DecisionTreeFactor& df) const override {
throw std::logic_error("Constraint::operator* not implemented");
}
/// Multiply with scalar just returns the constraint.
DiscreteFactor::shared_ptr operator*(double /* s*/) const override {
throw std::logic_error("Constraint::operator* not implemented");
}
/// Multiply factors, DiscreteFactor::shared_ptr edition /// Multiply factors, DiscreteFactor::shared_ptr edition
DiscreteFactor::shared_ptr multiply( DiscreteFactor::shared_ptr multiply(
const DiscreteFactor::shared_ptr& df) const override { const DiscreteFactor::shared_ptr& df) const override {
@ -104,6 +116,8 @@ class GTSAM_UNSTABLE_EXPORT Constraint : public DiscreteFactor {
return toDecisionTreeFactor().sum(keys); return toDecisionTreeFactor().sum(keys);
} }
double max() const override { return 1.0; }
DiscreteFactor::shared_ptr max(size_t nrFrontals) const override { DiscreteFactor::shared_ptr max(size_t nrFrontals) const override {
return toDecisionTreeFactor().max(nrFrontals); return toDecisionTreeFactor().max(nrFrontals);
} }

View File

@ -114,6 +114,11 @@ class GTSAM_UNSTABLE_EXPORT Domain : public Constraint {
/// Partially apply known values, domain version /// Partially apply known values, domain version
Constraint::shared_ptr partiallyApply(const Domains& domains) const override; Constraint::shared_ptr partiallyApply(const Domains& domains) const override;
// Scale just returns the same constraint.
DiscreteFactor::shared_ptr scale() const override {
return std::make_shared<Domain>(*this);
}
}; };
} // namespace gtsam } // namespace gtsam

View File

@ -77,6 +77,11 @@ class GTSAM_UNSTABLE_EXPORT SingleValue : public Constraint {
/// Partially apply known values, domain version /// Partially apply known values, domain version
Constraint::shared_ptr partiallyApply( Constraint::shared_ptr partiallyApply(
const Domains& domains) const override; const Domains& domains) const override;
// Scale just returns the same constraint.
DiscreteFactor::shared_ptr scale() const override {
return std::make_shared<SingleValue>(*this);
}
}; };
} // namespace gtsam } // namespace gtsam