inference

release/4.3a0
kartik arcot 2023-01-12 09:16:58 -08:00
parent 0bf269f854
commit 3bac1efd1f
13 changed files with 67 additions and 64 deletions

View File

@ -155,7 +155,7 @@ class GTSAM_EXPORT DiscreteFactorGraph
* @return DiscreteBayesNet encoding posterior P(X|Z)
*/
DiscreteBayesNet sumProduct(
OptionalOrderingType orderingType = boost::none) const;
OptionalOrderingType orderingType = {}) const;
/**
* @brief Implement the sum-product algorithm
@ -172,7 +172,7 @@ class GTSAM_EXPORT DiscreteFactorGraph
* @return DiscreteLookupDAG DAG with lookup tables
*/
DiscreteLookupDAG maxProduct(
OptionalOrderingType orderingType = boost::none) const;
OptionalOrderingType orderingType = {}) const;
/**
* @brief Implement the max-product algorithm
@ -189,7 +189,7 @@ class GTSAM_EXPORT DiscreteFactorGraph
* @return DiscreteValues : MPE
*/
DiscreteValues optimize(
OptionalOrderingType orderingType = boost::none) const;
OptionalOrderingType orderingType = {}) const;
/**
* @brief Find the maximum probable explanation (MPE) by doing max-product.

View File

@ -25,7 +25,6 @@
#include <gtsam/base/treeTraversal-inst.h>
#include <gtsam/base/timing.h>
#include <boost/optional.hpp>
#include <fstream>
namespace gtsam {

View File

@ -178,7 +178,7 @@ namespace gtsam {
this->conditional()->endParents());
auto separatorMarginal =
p_Cp.marginalMultifrontalBayesNet(Ordering(indicesS), function);
cachedSeparatorMarginal_.reset(*separatorMarginal);
cachedSeparatorMarginal_ = *separatorMarginal;
}
}
@ -217,7 +217,7 @@ namespace gtsam {
}
//Delete CachedShortcut for this clique
cachedSeparatorMarginal_ = boost::none;
cachedSeparatorMarginal_ = {};
}
}

View File

@ -21,10 +21,10 @@
#include <gtsam/inference/Ordering.h>
#include <gtsam/base/types.h>
#include <gtsam/base/FastVector.h>
#include <boost/optional.hpp>
#include <string>
#include <mutex>
#include <optional>
namespace gtsam {
@ -102,7 +102,7 @@ namespace gtsam {
/// @}
/// This stores the Cached separator marginal P(S)
mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
mutable std::optional<FactorGraphType> cachedSeparatorMarginal_;
/// This protects Cached seperator marginal P(S) from concurrent read/writes
/// as many the functions which access it are const (hence the mutable)
/// leading to the false impression that these const functions are thread-safe
@ -174,7 +174,7 @@ namespace gtsam {
*/
void deleteCachedShortcuts();
const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
const std::optional<FactorGraphType>& cachedSeparatorMarginal() const {
std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
return cachedSeparatorMarginal_;
}
@ -194,7 +194,7 @@ namespace gtsam {
/** Non-recursive delete cached shortcuts and marginals - internal only. */
void deleteCachedShortcutsNonRecursive() {
std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
cachedSeparatorMarginal_ = boost::none;
cachedSeparatorMarginal_ = {};
}
private:

View File

@ -40,7 +40,7 @@ void DotWriter::digraphPreamble(ostream* os) const {
}
void DotWriter::drawVariable(Key key, const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position,
const std::optional<Vector2>& position,
ostream* os) const {
// Label the node with the label from the KeyFormatter
*os << " var" << key << "[label=\"" << keyFormatter(key)
@ -54,7 +54,7 @@ void DotWriter::drawVariable(Key key, const KeyFormatter& keyFormatter,
*os << "];\n";
}
void DotWriter::DrawFactor(size_t i, const boost::optional<Vector2>& position,
void DotWriter::DrawFactor(size_t i, const std::optional<Vector2>& position,
ostream* os) {
*os << " factor" << i << "[label=\"\", shape=point";
if (position) {
@ -76,26 +76,28 @@ static void ConnectVariableFactor(Key key, const KeyFormatter& keyFormatter,
}
/// Return variable position or none
boost::optional<Vector2> DotWriter::variablePos(Key key) const {
boost::optional<Vector2> result = boost::none;
std::optional<Vector2> DotWriter::variablePos(Key key) const {
std::optional<Vector2> result = {};
// Check position hint
Symbol symbol(key);
auto hint = positionHints.find(symbol.chr());
if (hint != positionHints.end())
result.reset(Vector2(symbol.index(), hint->second));
if (hint != positionHints.end()) {
result = Vector2(symbol.index(), hint->second);
}
// Override with explicit position, if given.
auto pos = variablePositions.find(key);
if (pos != variablePositions.end())
result.reset(pos->second);
if (pos != variablePositions.end()) {
result = pos->second;
}
return result;
}
void DotWriter::processFactor(size_t i, const KeyVector& keys,
const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position,
const std::optional<Vector2>& position,
ostream* os) const {
if (plotFactorPoints) {
if (binaryEdges && keys.size() == 2) {

View File

@ -25,6 +25,7 @@
#include <iosfwd>
#include <map>
#include <set>
#include <optional>
namespace gtsam {
@ -80,20 +81,20 @@ struct GTSAM_EXPORT DotWriter {
/// Create a variable dot fragment.
void drawVariable(Key key, const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position,
const std::optional<Vector2>& position,
std::ostream* os) const;
/// Create factor dot.
static void DrawFactor(size_t i, const boost::optional<Vector2>& position,
static void DrawFactor(size_t i, const std::optional<Vector2>& position,
std::ostream* os);
/// Return variable position or none
boost::optional<Vector2> variablePos(Key key) const;
std::optional<Vector2> variablePos(Key key) const;
/// Draw a single factor, specified by its index i and its variable keys.
void processFactor(size_t i, const KeyVector& keys,
const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position,
const std::optional<Vector2>& position,
std::ostream* os) const;
};

View File

@ -20,8 +20,8 @@
#include <boost/shared_ptr.hpp>
#include <functional>
#include <optional>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <gtsam/inference/Ordering.h>
#include <gtsam/inference/VariableIndex.h>
@ -92,7 +92,7 @@ namespace gtsam {
typedef boost::optional<const VariableIndex&> OptionalVariableIndex;
/// Typedef for an optional ordering type
typedef boost::optional<Ordering::OrderingType> OptionalOrderingType;
typedef std::optional<Ordering::OrderingType> OptionalOrderingType;
/** Do sequential elimination of all variables to produce a Bayes net. If an ordering is not
* provided, the ordering provided by COLAMD will be used.
@ -111,13 +111,13 @@ namespace gtsam {
* \code
* VariableIndex varIndex(graph); // Build variable index
* Data data = otherFunctionUsingVariableIndex(graph, varIndex); // Other code that uses variable index
* boost::shared_ptr<GaussianBayesNet> result = graph.eliminateSequential(EliminateQR, varIndex, boost::none);
* boost::shared_ptr<GaussianBayesNet> result = graph.eliminateSequential(EliminateQR, varIndex, std::nullopt);
* \endcode
* */
boost::shared_ptr<BayesNetType> eliminateSequential(
OptionalOrderingType orderingType = boost::none,
OptionalOrderingType orderingType = {},
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do sequential elimination of all variables to produce a Bayes net.
*
@ -130,13 +130,13 @@ namespace gtsam {
* \code
* VariableIndex varIndex(graph); // Build variable index
* Data data = otherFunctionUsingVariableIndex(graph, varIndex); // Other code that uses variable index
* boost::shared_ptr<GaussianBayesNet> result = graph.eliminateSequential(myOrdering, EliminateQR, varIndex, boost::none);
* boost::shared_ptr<GaussianBayesNet> result = graph.eliminateSequential(myOrdering, EliminateQR, varIndex, std::nullopt);
* \endcode
* */
boost::shared_ptr<BayesNetType> eliminateSequential(
const Ordering& ordering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do multifrontal elimination of all variables to produce a Bayes tree. If an ordering is not
* provided, the ordering will be computed using either COLAMD or METIS, dependeing on
@ -151,13 +151,13 @@ namespace gtsam {
* \code
* VariableIndex varIndex(graph); // Build variable index
* Data data = otherFunctionUsingVariableIndex(graph, varIndex); // Other code that uses variable index
* boost::shared_ptr<GaussianBayesTree> result = graph.eliminateMultifrontal(EliminateQR, boost::none, varIndex);
* boost::shared_ptr<GaussianBayesTree> result = graph.eliminateMultifrontal(EliminateQR, {}, varIndex);
* \endcode
* */
boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
OptionalOrderingType orderingType = boost::none,
OptionalOrderingType orderingType = {},
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do multifrontal elimination of all variables to produce a Bayes tree. If an ordering is not
* provided, the ordering will be computed using either COLAMD or METIS, dependeing on
@ -171,7 +171,7 @@ namespace gtsam {
boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
const Ordering& ordering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do sequential elimination of some variables, in \c ordering provided, to produce a Bayes net
* and a remaining factor graph. This computes the factorization \f$ p(X) = p(A|B) p(B) \f$,
@ -181,7 +181,7 @@ namespace gtsam {
eliminatePartialSequential(
const Ordering& ordering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do sequential elimination of the given \c variables in an ordering computed by COLAMD to
* produce a Bayes net and a remaining factor graph. This computes the factorization \f$ p(X)
@ -191,7 +191,7 @@ namespace gtsam {
eliminatePartialSequential(
const KeyVector& variables,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do multifrontal elimination of some variables, in \c ordering provided, to produce a Bayes
* tree and a remaining factor graph. This computes the factorization \f$ p(X) = p(A|B) p(B)
@ -201,7 +201,7 @@ namespace gtsam {
eliminatePartialMultifrontal(
const Ordering& ordering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Do multifrontal elimination of the given \c variables in an ordering computed by COLAMD to
* produce a Bayes tree and a remaining factor graph. This computes the factorization \f$ p(X)
@ -211,7 +211,7 @@ namespace gtsam {
eliminatePartialMultifrontal(
const KeyVector& variables,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Compute the marginal of the requested variables and return the result as a Bayes net. Uses
* COLAMD marginalization ordering by default
@ -225,7 +225,7 @@ namespace gtsam {
boost::shared_ptr<BayesNetType> marginalMultifrontalBayesNet(
boost::variant<const Ordering&, const KeyVector&> variables,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Compute the marginal of the requested variables and return the result as a Bayes net.
* @param variables Determines the variables whose marginal to compute, if provided as an
@ -241,7 +241,7 @@ namespace gtsam {
boost::variant<const Ordering&, const KeyVector&> variables,
const Ordering& marginalizedVariableOrdering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Compute the marginal of the requested variables and return the result as a Bayes tree. Uses
* COLAMD marginalization order by default
@ -255,7 +255,7 @@ namespace gtsam {
boost::shared_ptr<BayesTreeType> marginalMultifrontalBayesTree(
boost::variant<const Ordering&, const KeyVector&> variables,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Compute the marginal of the requested variables and return the result as a Bayes tree.
* @param variables Determines the variables whose marginal to compute, if provided as an
@ -271,13 +271,13 @@ namespace gtsam {
boost::variant<const Ordering&, const KeyVector&> variables,
const Ordering& marginalizedVariableOrdering,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
/** Compute the marginal factor graph of the requested variables. */
boost::shared_ptr<FactorGraphType> marginal(
const KeyVector& variables,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const;
OptionalVariableIndex variableIndex = {}) const;
private:
@ -301,8 +301,8 @@ namespace gtsam {
/** @deprecated orderingType specified first for consistency */
boost::shared_ptr<BayesNetType> GTSAM_DEPRECATED eliminateSequential(
const Eliminate& function,
OptionalVariableIndex variableIndex = boost::none,
OptionalOrderingType orderingType = boost::none) const {
OptionalVariableIndex variableIndex = {},
OptionalOrderingType orderingType = {}) const {
return eliminateSequential(orderingType, function, variableIndex);
}
@ -318,8 +318,8 @@ namespace gtsam {
/** @deprecated orderingType specified first for consistency */
boost::shared_ptr<BayesTreeType> GTSAM_DEPRECATED eliminateMultifrontal(
const Eliminate& function,
OptionalVariableIndex variableIndex = boost::none,
OptionalOrderingType orderingType = boost::none) const {
OptionalVariableIndex variableIndex = {},
OptionalOrderingType orderingType = {}) const {
return eliminateMultifrontal(orderingType, function, variableIndex);
}
@ -328,7 +328,7 @@ namespace gtsam {
boost::variant<const Ordering&, const KeyVector&> variables,
boost::none_t,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const {
OptionalVariableIndex variableIndex = {}) const {
return marginalMultifrontalBayesNet(variables, function, variableIndex);
}
@ -337,7 +337,7 @@ namespace gtsam {
boost::variant<const Ordering&, const KeyVector&> variables,
boost::none_t,
const Eliminate& function = EliminationTraitsType::DefaultEliminate,
OptionalVariableIndex variableIndex = boost::none) const {
OptionalVariableIndex variableIndex = {}) const {
return marginalMultifrontalBayesTree(variables, function, variableIndex);
}
#endif

View File

@ -155,7 +155,7 @@ void FactorGraph<FACTOR>::dot(std::ostream& os,
const auto& factor = at(i);
if (factor) {
const KeyVector& factorKeys = factor->keys();
writer.processFactor(i, factorKeys, keyFormatter, boost::none, &os);
writer.processFactor(i, factorKeys, keyFormatter, {}, &os);
}
}

View File

@ -28,6 +28,7 @@
#include <cassert>
#include <stdexcept>
#include <optional>
namespace gtsam {

View File

@ -34,7 +34,7 @@ Vector2 GraphvizFormatting::findBounds(const Values& values,
min.y() = std::numeric_limits<double>::infinity();
for (const Key& key : keys) {
if (values.exists(key)) {
boost::optional<Vector2> xy = extractPosition(values.at(key));
std::optional<Vector2> xy = extractPosition(values.at(key));
if (xy) {
if (xy->x() < min.x()) min.x() = xy->x();
if (xy->y() < min.y()) min.y() = xy->y();
@ -44,7 +44,7 @@ Vector2 GraphvizFormatting::findBounds(const Values& values,
return min;
}
boost::optional<Vector2> GraphvizFormatting::extractPosition(
std::optional<Vector2> GraphvizFormatting::extractPosition(
const Value& value) const {
Vector3 t;
if (const GenericValue<Pose2>* p =
@ -62,7 +62,7 @@ boost::optional<Vector2> GraphvizFormatting::extractPosition(
const Eigen::Ref<const Vector3> p_3d(p->value());
t = p_3d;
} else {
return boost::none;
return {};
}
} else if (const GenericValue<Pose3>* p =
dynamic_cast<const GenericValue<Pose3>*>(&value)) {
@ -71,7 +71,7 @@ boost::optional<Vector2> GraphvizFormatting::extractPosition(
dynamic_cast<const GenericValue<Point3>*>(&value)) {
t = p->value();
} else {
return boost::none;
return {};
}
double x, y;
switch (paperHorizontalAxis) {
@ -121,11 +121,11 @@ boost::optional<Vector2> GraphvizFormatting::extractPosition(
return Vector2(x, y);
}
boost::optional<Vector2> GraphvizFormatting::variablePos(const Values& values,
std::optional<Vector2> GraphvizFormatting::variablePos(const Values& values,
const Vector2& min,
Key key) const {
if (!values.exists(key)) return DotWriter::variablePos(key);
boost::optional<Vector2> xy = extractPosition(values.at(key));
std::optional<Vector2> xy = extractPosition(values.at(key));
if (xy) {
xy->x() = scale * (xy->x() - min.x());
xy->y() = scale * (xy->y() - min.y());
@ -133,11 +133,11 @@ boost::optional<Vector2> GraphvizFormatting::variablePos(const Values& values,
return xy;
}
boost::optional<Vector2> GraphvizFormatting::factorPos(const Vector2& min,
std::optional<Vector2> GraphvizFormatting::factorPos(const Vector2& min,
size_t i) const {
if (factorPositions.size() == 0) return boost::none;
if (factorPositions.size() == 0) return {};
auto it = factorPositions.find(i);
if (it == factorPositions.end()) return boost::none;
if (it == factorPositions.end()) return {};
auto pos = it->second;
return Vector2(scale * (pos.x() - min.x()), scale * (pos.y() - min.y()));
}

View File

@ -53,14 +53,14 @@ struct GTSAM_EXPORT GraphvizFormatting : public DotWriter {
Vector2 findBounds(const Values& values, const KeySet& keys) const;
/// Extract a Vector2 from either Vector2, Pose2, Pose3, or Point3
boost::optional<Vector2> extractPosition(const Value& value) const;
std::optional<Vector2> extractPosition(const Value& value) const;
/// Return affinely transformed variable position if it exists.
boost::optional<Vector2> variablePos(const Values& values, const Vector2& min,
std::optional<Vector2> variablePos(const Values& values, const Vector2& min,
Key key) const;
/// Return affinely transformed factor position if it exists.
boost::optional<Vector2> factorPos(const Vector2& min, size_t i) const;
std::optional<Vector2> factorPos(const Vector2& min, size_t i) const;
};
} // namespace gtsam

View File

@ -129,7 +129,7 @@ void NonlinearFactorGraph::dot(std::ostream& os, const Values& values,
// Create factors and variable connections
size_t i = 0;
for (const KeyVector& factorKeys : structure) {
writer.processFactor(i++, factorKeys, keyFormatter, boost::none, &os);
writer.processFactor(i++, factorKeys, keyFormatter, {}, &os);
}
} else {
// Create factors and variable connections

View File

@ -213,7 +213,7 @@ TEST(BayesTree, shortcutCheck) {
// Check if all the cached shortcuts are cleared
rootClique->deleteCachedShortcuts();
for (SymbolicBayesTree::sharedClique& clique : allCliques) {
bool notCleared = clique->cachedSeparatorMarginal().is_initialized();
bool notCleared = clique->cachedSeparatorMarginal().has_value();
CHECK(notCleared == false);
}
EXPECT_LONGS_EQUAL(0, (long)rootClique->numCachedSeparatorMarginals());