Removed copy/paste convert

release/4.3a0
Frank Dellaert 2022-01-01 22:10:54 -05:00
parent 731cff746b
commit 7f3f332d09
3 changed files with 37 additions and 67 deletions

View File

@ -108,9 +108,12 @@ namespace gtsam {
/** Convert */
template<typename M>
AlgebraicDecisionTree(const AlgebraicDecisionTree<M>& other,
const std::map<M, L>& map) {
this->root_ = this->template convert<M, double>(other.root_, map,
Ring::id);
const std::map<M, L>& map) {
std::function<L(const M&)> map_function = [&map](const M& label) -> L {
return map.at(label);
};
std::function<double(const double&)> op = Ring::id;
this->root_ = this->template convert(other.root_, op, map_function);
}
/** sum */

View File

@ -453,20 +453,24 @@ namespace gtsam {
root_ = compose(functions.begin(), functions.end(), label);
}
/*********************************************************************************/
template<typename L, typename Y>
template<typename M, typename X>
DecisionTree<L, Y>::DecisionTree(const DecisionTree<M, X>& other,
const std::map<M, L>& map, std::function<Y(const X&)> op) {
root_ = convert(other.root_, map, op);
}
/*********************************************************************************/
template <typename L, typename Y>
template <typename X>
DecisionTree<L, Y>::DecisionTree(const DecisionTree<L, X>& other,
std::function<Y(const X&)> op) {
root_ = convert(other.root_, op);
auto map = [](const L& label) { return label; };
root_ = convert<L, X>(other.root_, op, map);
}
/*********************************************************************************/
template<typename L, typename Y>
template<typename M, typename X>
DecisionTree<L, Y>::DecisionTree(const DecisionTree<M, X>& other,
const std::map<M, L>& map, std::function<Y(const X&)> op) {
std::function<L(const M&)> map_function = [&map](const M& label) -> L {
return map.at(label);
};
root_ = convert<M, X>(other.root_, op, map_function);
}
/*********************************************************************************/
@ -579,12 +583,11 @@ namespace gtsam {
}
/*********************************************************************************/
template<typename L, typename Y>
template<typename M, typename X>
template <typename L, typename Y>
template <typename M, typename X>
typename DecisionTree<L, Y>::NodePtr DecisionTree<L, Y>::convert(
const typename DecisionTree<M, X>::NodePtr& f, const std::map<M, L>& map,
std::function<Y(const X&)> op) {
const typename DecisionTree<M, X>::NodePtr& f,
std::function<Y(const X&)> op, std::function<L(const M&)> map) {
typedef DecisionTree<M, X> MX;
typedef typename MX::Leaf MXLeaf;
typedef typename MX::Choice MXChoice;
@ -602,50 +605,18 @@ namespace gtsam {
"DecisionTree::Convert: Invalid NodePtr");
// get new label
M oldLabel = choice->label();
L newLabel = map.at(oldLabel);
const M oldLabel = choice->label();
const L newLabel = map(oldLabel);
// put together via Shannon expansion otherwise not sorted.
std::vector<LY> functions;
for(const MXNodePtr& branch: choice->branches()) {
LY converted(convert<M, X>(branch, map, op));
LY converted(convert<M, X>(branch, op, map));
functions += converted;
}
return LY::compose(functions.begin(), functions.end(), newLabel);
}
/*********************************************************************************/
template<typename L, typename Y>
template<typename X>
typename DecisionTree<L, Y>::NodePtr DecisionTree<L, Y>::convert(
const typename DecisionTree<L, X>::NodePtr& f,
std::function<Y(const X&)> op) {
typedef DecisionTree<L, X> LX;
typedef typename LX::Leaf LXLeaf;
typedef typename LX::Choice LXChoice;
typedef typename LX::NodePtr LXNodePtr;
typedef DecisionTree<L, Y> LY;
// ugliness below because apparently we can't have templated virtual functions
// If leaf, apply unary conversion "op" and create a unique leaf
const LXLeaf* leaf = dynamic_cast<const LXLeaf*> (f.get());
if (leaf) return NodePtr(new Leaf(op(leaf->constant())));
// Check if Choice
boost::shared_ptr<const LXChoice> choice = boost::dynamic_pointer_cast<const LXChoice> (f);
if (!choice) throw std::invalid_argument(
"DecisionTree::Convert: Invalid NodePtr");
// put together via Shannon expansion otherwise not sorted.
std::vector<LY> functions;
for(const LXNodePtr& branch: choice->branches()) {
LY converted(convert<X>(branch, op));
functions += converted;
}
return LY::compose(functions.begin(), functions.end(), choice->label());
}
/*********************************************************************************/
template <typename L, typename Y>
bool DecisionTree<L, Y>::equals(const DecisionTree& other, double tol,

View File

@ -127,15 +127,11 @@ namespace gtsam {
template<typename It, typename ValueIt>
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const;
/** Convert to a different type */
template<typename M, typename X> NodePtr
convert(const typename DecisionTree<M, X>::NodePtr& f, const std::map<M,
L>& map, std::function<Y(const X&)> op);
/** Convert only node to a different type */
template <typename X>
NodePtr convert(const typename DecisionTree<L, X>::NodePtr& f,
const std::function<Y(const X&)> op);
/// Convert to a different type, will not convert label if map empty.
template <typename M, typename X>
NodePtr convert(const typename DecisionTree<M, X>::NodePtr& f,
std::function<Y(const X&)> op,
std::function<L(const M&)> map);
public:
@ -168,16 +164,16 @@ namespace gtsam {
DecisionTree(const L& label, //
const DecisionTree& f0, const DecisionTree& f1);
/** Convert from a different type */
template<typename M, typename X>
DecisionTree(const DecisionTree<M, X>& other,
const std::map<M, L>& map, std::function<Y(const X&)> op);
/** Convert only nodes from a different type */
/** Convert from a different type. */
template <typename X>
DecisionTree(const DecisionTree<L, X>& other,
std::function<Y(const X&)> op);
/** Convert from a different type, also transate labels via map. */
template<typename M, typename X>
DecisionTree(const DecisionTree<M, X>& other,
const std::map<M, L>& map, std::function<Y(const X&)> op);
/// @}
/// @name Testable
/// @{