54#ifndef DOXYGEN_SHOULD_SKIP_THIS
67 template < GUM_Numeric GUM_SCALAR >
72 bool use_binary_join_tree) :
74 _use_binary_join_tree_(use_binary_join_tree) {
77 = &ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_;
78 setRelevantTensorsFinderType(relevant_type);
79 setFindBarrenNodesType(barren_type);
82 _triangulation_ =
new DefaultTriangulation;
85 GUM_CONSTRUCTOR(ShaferShenoyInference);
89 template < GUM_Numeric GUM_SCALAR >
90 ShaferShenoyInference< GUM_SCALAR >::~ShaferShenoyInference() {
92 for (
const auto& pot: _arc_to_created_tensors_)
100 for (
auto pot: _clique_ss_tensor_) {
101 if (_clique_tensors_[pot.first].size() > 1)
delete pot.second;
104 for (
auto potset: _clique_tensors_) {
105 for (
auto pot: potset.second)
110 for (
const auto& pot: _target_posteriors_)
112 for (
const auto& pot: _joint_target_posteriors_)
116 if (_JT_ !=
nullptr)
delete _JT_;
117 if (_junctionTree_ !=
nullptr)
delete _junctionTree_;
118 delete _triangulation_;
121 GUM_DESTRUCTOR(ShaferShenoyInference);
125 template < GUM_Numeric GUM_SCALAR >
126 void ShaferShenoyInference< GUM_SCALAR >::setTriangulation(
127 const Triangulation& new_triangulation) {
128 delete _triangulation_;
129 _triangulation_ = new_triangulation.newFactory();
130 _is_new_jt_needed_ =
true;
131 this->setOutdatedStructureState_();
135 template < GUM_Numeric GUM_SCALAR >
136 const JoinTree* ShaferShenoyInference< GUM_SCALAR >::joinTree() {
137 if (_is_new_jt_needed_) _createNewJT_();
143 template < GUM_Numeric GUM_SCALAR >
144 const JunctionTree* ShaferShenoyInference< GUM_SCALAR >::junctionTree() {
145 if (_is_new_jt_needed_) _createNewJT_();
147 return _junctionTree_;
151 template < GUM_Numeric GUM_SCALAR >
152 void ShaferShenoyInference< GUM_SCALAR >::setRelevantTensorsFinderType(
153 RelevantTensorsFinderType type) {
154 if (type != _find_relevant_tensor_type_) {
156 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
157 _findRelevantTensors_
158 = &ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_;
161 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES :
162 _findRelevantTensors_
163 = &ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation_;
166 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 :
167 _findRelevantTensors_
168 = &ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation3_;
171 case RelevantTensorsFinderType::FIND_ALL :
172 _findRelevantTensors_ = &ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsGetAll_;
177 "setRelevantTensorsFinderType for type " << (
unsigned int)type
178 <<
" is not implemented yet");
181 _find_relevant_tensor_type_ = type;
185 _invalidateAllMessages_();
190 template < GUM_Numeric GUM_SCALAR >
191 void ShaferShenoyInference< GUM_SCALAR >::_setProjectionFunction_(
192 Tensor< GUM_SCALAR > (*proj)(
const Tensor< GUM_SCALAR >&,
const gum::VariableSet&)) {
193 _projection_op_ = proj;
197 _invalidateAllMessages_();
201 template < GUM_Numeric GUM_SCALAR >
202 void ShaferShenoyInference< GUM_SCALAR >::_setCombinationFunction_(
203 Tensor< GUM_SCALAR > (*comb)(
const Tensor< GUM_SCALAR >&,
const Tensor< GUM_SCALAR >&)) {
204 _combination_op_ = comb;
208 _invalidateAllMessages_();
212 template < GUM_Numeric GUM_SCALAR >
213 void ShaferShenoyInference< GUM_SCALAR >::_invalidateAllMessages_() {
215 for (
auto& pot: _separator_tensors_)
216 pot.second =
nullptr;
218 for (
auto& mess_computed: _messages_computed_)
219 mess_computed.second =
false;
222 for (
const auto& pot: _arc_to_created_tensors_)
223 if (pot.second !=
nullptr)
delete pot.second;
224 _arc_to_created_tensors_.clear();
227 for (
const auto& pot: _target_posteriors_)
229 _target_posteriors_.clear();
230 for (
const auto& pot: _joint_target_posteriors_)
232 _joint_target_posteriors_.clear();
235 if (this->isInferenceReady() || this->isInferenceDone()) this->setOutdatedTensorsState_();
239 template < GUM_Numeric GUM_SCALAR >
240 void ShaferShenoyInference< GUM_SCALAR >::setFindBarrenNodesType(FindBarrenNodesType type) {
241 if (type != _barren_nodes_type_) {
245 case FindBarrenNodesType::FIND_BARREN_NODES :
246 case FindBarrenNodesType::FIND_NO_BARREN_NODES :
break;
250 "setFindBarrenNodesType for type " << (
unsigned int)type
251 <<
" is not implemented yet");
254 _barren_nodes_type_ = type;
257 this->setOutdatedStructureState_();
262 template < GUM_Numeric GUM_SCALAR >
263 void ShaferShenoyInference< GUM_SCALAR >::onEvidenceAdded_(
const NodeId
id,
bool isHardEvidence) {
267 if (isHardEvidence || !_graph_.exists(
id)) _is_new_jt_needed_ =
true;
270 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_ADDED);
276 _evidence_changes_[id] = EvidenceChangeType::EVIDENCE_MODIFIED;
282 template < GUM_Numeric GUM_SCALAR >
283 void ShaferShenoyInference< GUM_SCALAR >::onEvidenceErased_(
const NodeId
id,
284 bool isHardEvidence) {
287 if (isHardEvidence) _is_new_jt_needed_ =
true;
290 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_ERASED);
297 if (_evidence_changes_[
id] == EvidenceChangeType::EVIDENCE_ADDED)
298 _evidence_changes_.erase(
id);
299 else _evidence_changes_[id] = EvidenceChangeType::EVIDENCE_ERASED;
305 template < GUM_Numeric GUM_SCALAR >
306 void ShaferShenoyInference< GUM_SCALAR >::onAllEvidenceErased_(
bool has_hard_evidence) {
307 if (has_hard_evidence || !this->hardEvidenceNodes().empty()) _is_new_jt_needed_ =
true;
309 for (
const auto node: this->softEvidenceNodes()) {
311 _evidence_changes_.insert(node, EvidenceChangeType::EVIDENCE_ERASED);
318 if (_evidence_changes_[node] == EvidenceChangeType::EVIDENCE_ADDED)
319 _evidence_changes_.erase(node);
320 else _evidence_changes_[node] = EvidenceChangeType::EVIDENCE_ERASED;
327 template < GUM_Numeric GUM_SCALAR >
328 void ShaferShenoyInference< GUM_SCALAR >::onEvidenceChanged_(
const NodeId
id,
329 bool hasChangedSoftHard) {
330 if (hasChangedSoftHard) _is_new_jt_needed_ =
true;
333 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_MODIFIED);
343 template < GUM_Numeric GUM_SCALAR >
344 void ShaferShenoyInference< GUM_SCALAR >::onModelChanged_(
const GraphicalModel* bn) {}
347 template < GUM_Numeric GUM_SCALAR >
348 void ShaferShenoyInference< GUM_SCALAR >::onMarginalTargetAdded_(
const NodeId
id) {
353 if (!_graph_.exists(
id) && !_hard_ev_nodes_.contains(
id)) { _is_new_jt_needed_ =
true; }
357 template < GUM_Numeric GUM_SCALAR >
358 void ShaferShenoyInference< GUM_SCALAR >::onMarginalTargetErased_(
const NodeId
id) {}
361 template < GUM_Numeric GUM_SCALAR >
362 void ShaferShenoyInference< GUM_SCALAR >::onJointTargetAdded_(
const NodeSet& set) {
364 if (_JT_ ==
nullptr) {
365 _is_new_jt_needed_ =
true;
372 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
373 int elim_number = std::numeric_limits< int >::max();
374 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
375 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
376 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
377 elim_order.insert(JT_elim_order[i], (
int)i);
378 NodeSet unobserved_set(set.size());
379 for (
const auto node: set) {
380 if (!_graph_.exists(node)) {
381 if (!_hard_ev_nodes_.contains(node)) {
382 _is_new_jt_needed_ =
true;
386 unobserved_set.insert(node);
387 if (elim_order[node] < elim_number) {
388 elim_number = elim_order[node];
389 first_eliminated_node = node;
394 if (!unobserved_set.empty()) {
398 const auto clique_id = _node_to_clique_[first_eliminated_node];
399 const auto& clique = _JT_->clique(clique_id);
400 for (
const auto node: unobserved_set) {
401 if (!clique.contains(node)) {
402 _is_new_jt_needed_ =
true;
410 template < GUM_Numeric GUM_SCALAR >
411 void ShaferShenoyInference< GUM_SCALAR >::onJointTargetErased_(
const NodeSet& set) {}
414 template < GUM_Numeric GUM_SCALAR >
415 void ShaferShenoyInference< GUM_SCALAR >::onAllMarginalTargetsAdded_() {
416 for (
const auto node: this->BN().internalDag()) {
421 if (!_graph_.exists(node) && !_hard_ev_nodes_.contains(node)) {
422 _is_new_jt_needed_ =
true;
429 template < GUM_Numeric GUM_SCALAR >
430 void ShaferShenoyInference< GUM_SCALAR >::onAllMarginalTargetsErased_() {}
433 template < GUM_Numeric GUM_SCALAR >
434 void ShaferShenoyInference< GUM_SCALAR >::onAllJointTargetsErased_() {}
437 template < GUM_Numeric GUM_SCALAR >
438 void ShaferShenoyInference< GUM_SCALAR >::onAllTargetsErased_() {}
441 template < GUM_Numeric GUM_SCALAR >
442 bool ShaferShenoyInference< GUM_SCALAR >::_isNewJTNeeded_()
const {
445 if ((_JT_ ==
nullptr) || _is_new_jt_needed_)
return true;
453 const auto& hard_ev_nodes = this->hardEvidenceNodes();
454 for (
const auto node: this->targets()) {
455 if (!_graph_.exists(node) && !hard_ev_nodes.exists(node))
return true;
459 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
460 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
461 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
462 elim_order.insert(JT_elim_order[i], (
int)i);
465 for (
const auto& joint_target: this->jointTargets()) {
468 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
469 int elim_number = std::numeric_limits< int >::max();
470 unobserved_set.clear();
471 for (
const auto node: joint_target) {
472 if (!_graph_.exists(node)) {
473 if (!hard_ev_nodes.exists(node))
return true;
475 unobserved_set.insert(node);
476 if (elim_order[node] < elim_number) {
477 elim_number = elim_order[node];
478 first_eliminated_node = node;
482 if (!unobserved_set.empty()) {
486 const auto clique_id = _node_to_clique_[first_eliminated_node];
487 const auto& clique = _JT_->clique(clique_id);
488 for (
const auto node: unobserved_set) {
489 if (!clique.contains(node))
return true;
496 for (
const auto& change: _evidence_changes_) {
497 if ((change.second == EvidenceChangeType::EVIDENCE_ADDED) && !_graph_.exists(change.first))
506 template < GUM_Numeric GUM_SCALAR >
507 void ShaferShenoyInference< GUM_SCALAR >::_createNewJT_() {
523 const auto& bn = this->BN();
525 for (
const auto node: bn.internalDag())
526 _graph_.addNodeWithId(node);
529 NodeSet target_nodes = this->targets();
530 for (
const auto& nodeset: this->jointTargets()) {
531 target_nodes += nodeset;
539 if ((this->nbrTargets() != bn.internalDag().size())
540 && (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES)) {
543 if (target_nodes.size() != bn.size()) {
544 BarrenNodesFinder finder(&bn.internalDag());
545 finder.setTargets(&target_nodes);
547 NodeSet evidence_nodes(this->evidence().size());
548 for (
const auto& pair: this->evidence()) {
549 evidence_nodes.insert(pair.first);
551 finder.setEvidence(&evidence_nodes);
553 NodeSet barren_nodes = finder.barrenNodes();
556 for (
const auto node: barren_nodes) {
557 _graph_.eraseNode(node);
565 if (this->nbrTargets() != bn.internalDag().size()) {
567 bool dsep_analysis =
false;
568 switch (_find_relevant_tensor_type_) {
569 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
570 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES : {
571 BayesBall::requisiteNodes(bn.internalDag(),
573 this->hardEvidenceNodes(),
574 this->softEvidenceNodes(),
576 dsep_analysis =
true;
579 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 : {
580 dSeparationAlgorithm dsep;
581 dsep.requisiteNodes(bn.internalDag(),
583 this->hardEvidenceNodes(),
584 this->softEvidenceNodes(),
586 dsep_analysis =
true;
589 case RelevantTensorsFinderType::FIND_ALL :
break;
596 for (
auto iter = _graph_.beginSafe(); iter != _graph_.endSafe(); ++iter) {
597 if (!requisite_nodes.contains(*iter) && !this->hardEvidenceNodes().contains(*iter)) {
598 _graph_.eraseNode(*iter);
605 for (
const auto node: _graph_) {
606 const NodeSet& parents = bn.parents(node);
607 for (
auto iter1 = parents.cbegin(); iter1 != parents.cend(); ++iter1) {
612 if (_graph_.existsNode(*iter1)) {
613 _graph_.addEdge(*iter1, node);
616 for (++iter2; iter2 != parents.cend(); ++iter2) {
621 if (_graph_.existsNode(*iter2)) _graph_.addEdge(*iter1, *iter2);
630 for (
const auto& nodeset: this->jointTargets()) {
631 for (
auto iter1 = nodeset.cbegin(); iter1 != nodeset.cend(); ++iter1) {
633 for (++iter2; iter2 != nodeset.cend(); ++iter2) {
634 _graph_.addEdge(*iter1, *iter2);
640 _hard_ev_nodes_ = this->hardEvidenceNodes();
641 for (
const auto node: _hard_ev_nodes_) {
642 _graph_.eraseNode(node);
649 if (_JT_ !=
nullptr)
delete _JT_;
650 if (_junctionTree_ !=
nullptr)
delete _junctionTree_;
652 _triangulation_->setGraph(&_graph_, &(this->domainSizes()));
653 const JunctionTree& triang_jt = _triangulation_->junctionTree();
654 if (_use_binary_join_tree_) {
655 BinaryJoinTreeConverterDefault bjt_converter;
657 _JT_ =
new CliqueGraph(bjt_converter.convert(triang_jt, this->domainSizes(), emptyset));
659 _JT_ =
new CliqueGraph(triang_jt);
661 _junctionTree_ =
new CliqueGraph(triang_jt);
666 _node_to_clique_.clear();
667 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
668 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
669 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
670 elim_order.insert(JT_elim_order[i], (
int)i);
671 const DAG& dag = bn.internalDag();
672 for (
const auto node: _graph_) {
674 NodeId first_eliminated_node = node;
675 int elim_number = elim_order[first_eliminated_node];
677 for (
const auto parent: dag.parents(node)) {
678 if (_graph_.existsNode(parent) && (elim_order[parent] < elim_number)) {
679 elim_number = elim_order[parent];
680 first_eliminated_node = parent;
688 _node_to_clique_.insert(node,
689 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
695 for (
const auto node: _hard_ev_nodes_) {
696 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
697 int elim_number = std::numeric_limits< int >::max();
699 for (
const auto parent: dag.parents(node)) {
700 if (_graph_.exists(parent) && (elim_order[parent] < elim_number)) {
701 elim_number = elim_order[parent];
702 first_eliminated_node = parent;
710 if (elim_number != std::numeric_limits< int >::max()) {
711 _node_to_clique_.insert(node,
712 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
717 _joint_target_to_clique_.clear();
718 for (
const auto& set: this->jointTargets()) {
719 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
720 int elim_number = std::numeric_limits< int >::max();
724 for (
const auto node: set) {
725 if (!_hard_ev_nodes_.contains(node)) {
728 if (elim_order[node] < elim_number) {
729 elim_number = elim_order[node];
730 first_eliminated_node = node;
735 if (elim_number != std::numeric_limits< int >::max()) {
736 _joint_target_to_clique_.insert(
738 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
743 _computeJoinTreeRoots_();
748 for (
const auto& pot: _clique_ss_tensor_) {
749 if (_clique_tensors_[pot.first].size() > 1)
delete pot.second;
751 _clique_ss_tensor_.clear();
752 for (
const auto& potlist: _clique_tensors_)
753 for (
const auto pot: potlist.second)
755 _clique_tensors_.clear();
758 for (
const auto& pot: _arc_to_created_tensors_)
760 _arc_to_created_tensors_.clear();
765 _node_to_hard_ev_projected_CPTs_.clear();
768 _node_to_soft_evidence_.clear();
772 _ScheduleMultiDimSet_ empty_set;
773 for (
const auto node: *_JT_) {
774 _clique_tensors_.insert(node, empty_set);
775 _clique_ss_tensor_.insert(node,
nullptr);
783 _separator_tensors_.clear();
784 _messages_computed_.clear();
785 for (
const auto& edge: _JT_->edges()) {
786 const Arc arc1(edge.first(), edge.second());
787 _separator_tensors_.insert(arc1,
nullptr);
788 _messages_computed_.insert(arc1,
false);
789 const Arc arc2(edge.second(), edge.first());
790 _separator_tensors_.insert(arc2,
nullptr);
791 _messages_computed_.insert(arc2,
false);
795 for (
const auto& pot: _target_posteriors_)
797 _target_posteriors_.clear();
798 for (
const auto& pot: _joint_target_posteriors_)
800 _joint_target_posteriors_.clear();
805 double overall_size = 0;
806 for (
const auto clique: *_JT_) {
807 double clique_size = 1.0;
808 for (
const auto node: _JT_->clique(clique))
809 clique_size *= this->domainSizes()[node];
810 overall_size += clique_size;
812 _use_schedules_ = (overall_size > _schedule_threshold_);
815 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
816 for (
const auto node: this->softEvidenceNodes()) {
817 if (
auto ptr_clique = _node_to_clique_.tryGet(node)) {
818 auto ev_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*evidence[node],
false);
819 _node_to_soft_evidence_.insert(node, ev_pot);
820 _clique_tensors_[*ptr_clique].insert(ev_pot);
828 if (_use_schedules_) {
830 _initializeJTCliques_(schedule);
832 _initializeJTCliques_();
837 _evidence_changes_.clear();
838 _is_new_jt_needed_ =
false;
842 template < GUM_Numeric GUM_SCALAR >
843 void ShaferShenoyInference< GUM_SCALAR >::_initializeJTCliques_() {
844 const auto& bn = this->BN();
845 const DAG& dag = bn.internalDag();
851 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
852 const NodeProperty< Idx >& hard_evidence = this->hardEvidence();
854 for (
const auto node: dag) {
855 if (_graph_.exists(node) || _hard_ev_nodes_.contains(node)) {
856 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
860 const auto& variables = cpt.variablesSequence();
861 bool graph_contains_nodes =
false;
862 for (
const auto var: variables) {
863 NodeId xnode = bn.nodeId(*var);
864 if (_hard_ev_nodes_.contains(xnode)) hard_nodes.insert(xnode);
865 else if (_graph_.exists(xnode)) graph_contains_nodes =
true;
871 if (hard_nodes.empty()) {
872 auto sched_cpt =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(cpt,
false);
873 _clique_tensors_[_node_to_clique_[node]].insert(sched_cpt);
879 if (hard_nodes.size() == variables.size()) {
880 Instantiation inst(cpt);
881 for (Size i = 0; i < hard_nodes.size(); ++i) {
882 inst.chgVal(*variables[i], hard_evidence[bn.nodeId(*(variables[i]))]);
884 _constants_.insert(node, cpt.get(inst));
889 if (!graph_contains_nodes)
continue;
893 _TensorSet_ marg_cpt_set(1 + hard_nodes.size());
894 marg_cpt_set.insert(&cpt);
895 for (
const auto xnode: hard_nodes) {
896 marg_cpt_set.insert(evidence[xnode]);
897 hard_variables.
insert(&(bn.variable(xnode)));
901 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
905 _TensorSet_ new_cpt_list = combine_and_project.execute(marg_cpt_set, hard_variables);
908 if (new_cpt_list.size() != 1) {
909 for (
const auto pot: new_cpt_list)
910 if (!marg_cpt_set.contains(pot))
delete pot;
913 "the projection of a tensor containing " <<
"hard evidence is empty!");
915 auto new_pot =
const_cast< Tensor< GUM_SCALAR >*
>(*(new_cpt_list.begin()));
916 auto projected_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*new_pot));
919 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
920 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
930 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
931 for (
const auto& xpotset: _clique_tensors_) {
932 const auto& potset = xpotset.second;
933 if (potset.size() > 0) {
938 if (potset.size() == 1) {
939 _clique_ss_tensor_[xpotset.first] = *(potset.cbegin());
941 _TensorSet_ p_potset(potset.size());
942 for (
const auto pot: potset)
944 &(
static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot)->multiDim()));
946 Tensor< GUM_SCALAR >* joint
947 =
const_cast< Tensor< GUM_SCALAR >*
>(fast_combination.execute(p_potset));
948 _clique_ss_tensor_[xpotset.first]
949 =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*joint));
957 template < GUM_Numeric GUM_SCALAR >
958 void ShaferShenoyInference< GUM_SCALAR >::_initializeJTCliques_(Schedule& schedule) {
959 const auto& bn = this->BN();
960 const DAG& dag = bn.internalDag();
966 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
967 const NodeProperty< Idx >& hard_evidence = this->hardEvidence();
969 for (
const auto node: dag) {
970 if (_graph_.exists(node) || _hard_ev_nodes_.contains(node)) {
971 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
975 const auto& variables = cpt.variablesSequence();
976 bool graph_contains_nodes =
false;
977 for (
const auto var: variables) {
978 NodeId xnode = bn.nodeId(*var);
979 if (_hard_ev_nodes_.contains(xnode)) hard_nodes.insert(xnode);
980 else if (_graph_.exists(xnode)) graph_contains_nodes =
true;
986 if (hard_nodes.empty()) {
987 auto sched_cpt =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(cpt,
false);
988 _clique_tensors_[_node_to_clique_[node]].insert(sched_cpt);
994 if (hard_nodes.size() == variables.size()) {
995 Instantiation inst(cpt);
996 for (Size i = 0; i < hard_nodes.size(); ++i) {
997 inst.chgVal(*variables[i], hard_evidence[bn.nodeId(*(variables[i]))]);
999 _constants_.insert(node, cpt.get(inst));
1004 if (!graph_contains_nodes)
continue;
1008 _ScheduleMultiDimSet_ marg_cpt_set(1 + hard_nodes.size());
1009 const IScheduleMultiDim* sched_cpt
1010 = schedule.insertTable< Tensor< GUM_SCALAR > >(cpt,
false);
1011 marg_cpt_set.insert(sched_cpt);
1013 for (
const auto xnode: hard_nodes) {
1014 const IScheduleMultiDim* pot
1015 = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[xnode],
false);
1016 marg_cpt_set.insert(pot);
1017 hard_variables.
insert(&(bn.variable(xnode)));
1021 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
1025 _ScheduleMultiDimSet_ new_cpt_list
1026 = combine_and_project.schedule(schedule, marg_cpt_set, hard_variables);
1029 if (new_cpt_list.size() != 1) {
1031 "the projection of a tensor containing " <<
"hard evidence is empty!");
1033 auto projected_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1034 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1035 *new_cpt_list.begin()));
1036 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(projected_pot))
1037 ->makeResultsPersistent(
true);
1038 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
1039 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1044 this->scheduler().execute(schedule);
1051 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1052 for (
const auto& xpotset: _clique_tensors_) {
1053 const auto& potset = xpotset.second;
1054 if (potset.size() > 0) {
1059 if (potset.size() == 1) {
1060 _clique_ss_tensor_[xpotset.first] = *(potset.cbegin());
1063 for (
const auto pot: potset) {
1064 schedule.emplaceScheduleMultiDim(*pot);
1067 auto joint =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1068 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1069 fast_combination.schedule(schedule, potset)));
1070 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(joint))
1071 ->makeResultsPersistent(
true);
1072 _clique_ss_tensor_[xpotset.first] = joint;
1076 this->scheduler().execute(schedule);
1080 template < GUM_Numeric GUM_SCALAR >
1081 void ShaferShenoyInference< GUM_SCALAR >::updateOutdatedStructure_() {
1083 if (_isNewJTNeeded_()) {
1089 updateOutdatedTensors_();
1094 template < GUM_Numeric GUM_SCALAR >
1095 void ShaferShenoyInference< GUM_SCALAR >::_diffuseMessageInvalidations_(
1098 NodeSet& invalidated_cliques) {
1100 invalidated_cliques.insert(to_id);
1103 const Arc arc(from_id, to_id);
1104 bool& message_computed = _messages_computed_[arc];
1105 if (message_computed) {
1106 message_computed =
false;
1107 _separator_tensors_[arc] =
nullptr;
1108 if (_arc_to_created_tensors_.exists(arc)) {
1109 delete _arc_to_created_tensors_[arc];
1110 _arc_to_created_tensors_.erase(arc);
1114 for (
const auto node_id: _JT_->neighbours(to_id)) {
1115 if (node_id != from_id) _diffuseMessageInvalidations_(to_id, node_id, invalidated_cliques);
1122 template < GUM_Numeric GUM_SCALAR >
1123 void ShaferShenoyInference< GUM_SCALAR >::updateOutdatedTensors_() {
1128 NodeProperty< bool > ss_tensor_to_deallocate(_clique_tensors_.size());
1129 for (
const auto& potset: _clique_tensors_) {
1130 ss_tensor_to_deallocate.insert(potset.first, (potset.second.size() > 1));
1140 NodeSet hard_nodes_changed(_hard_ev_nodes_.size());
1141 for (
const auto node: _hard_ev_nodes_)
1142 if (_evidence_changes_.exists(node)) hard_nodes_changed.insert(node);
1144 NodeSet nodes_with_projected_CPTs_changed;
1145 const auto& bn = this->BN();
1146 for (
auto pot_iter = _node_to_hard_ev_projected_CPTs_.beginSafe();
1147 pot_iter != _node_to_hard_ev_projected_CPTs_.endSafe();
1149 for (
const auto var: bn.cpt(pot_iter.key()).variablesSequence()) {
1150 if (hard_nodes_changed.contains(bn.nodeId(*var))) {
1151 nodes_with_projected_CPTs_changed.insert(pot_iter.key());
1152 delete pot_iter.val();
1153 _clique_tensors_[_node_to_clique_[pot_iter.key()]].erase(pot_iter.val());
1154 _node_to_hard_ev_projected_CPTs_.erase(pot_iter);
1168 NodeSet invalidated_cliques(_JT_->size());
1169 for (
const auto& pair: _evidence_changes_) {
1170 if (
auto ptr_clique = _node_to_clique_.tryGet(pair.first)) {
1171 const auto clique = *ptr_clique;
1172 invalidated_cliques.insert(clique);
1173 for (
const auto neighbor: _JT_->neighbours(clique)) {
1174 _diffuseMessageInvalidations_(clique, neighbor, invalidated_cliques);
1181 for (
const auto node: nodes_with_projected_CPTs_changed) {
1182 const auto clique = _node_to_clique_[node];
1183 invalidated_cliques.insert(clique);
1184 for (
const auto neighbor: _JT_->neighbours(clique)) {
1185 _diffuseMessageInvalidations_(clique, neighbor, invalidated_cliques);
1191 for (
const auto clique: invalidated_cliques) {
1192 if (ss_tensor_to_deallocate[clique]) {
1193 delete _clique_ss_tensor_[clique];
1194 _clique_ss_tensor_[clique] =
nullptr;
1203 for (
auto iter = _target_posteriors_.beginSafe(); iter != _target_posteriors_.endSafe();
1205 if (_graph_.exists(iter.key())
1206 && (invalidated_cliques.exists(_node_to_clique_[iter.key()]))) {
1208 _target_posteriors_.erase(iter);
1213 for (
auto iter = _target_posteriors_.beginSafe(); iter != _target_posteriors_.endSafe();
1215 if (hard_nodes_changed.contains(iter.key())) {
1217 _target_posteriors_.erase(iter);
1223 for (
auto iter = _joint_target_posteriors_.beginSafe();
1224 iter != _joint_target_posteriors_.endSafe();
1228 const auto clique = _joint_target_to_clique_.tryGet(iter.key());
1229 if (!clique || invalidated_cliques.exists(*clique)) {
1231 _joint_target_posteriors_.erase(iter);
1234 bool has_unevidenced_node =
false;
1235 for (
const auto node: iter.key()) {
1236 if (!hard_nodes_changed.exists(node)) {
1237 has_unevidenced_node =
true;
1241 if (!has_unevidenced_node) {
1243 _joint_target_posteriors_.erase(iter);
1250 for (
const auto& pot_pair: _node_to_soft_evidence_) {
1251 delete pot_pair.second;
1252 _clique_tensors_[_node_to_clique_[pot_pair.first]].erase(pot_pair.second);
1254 _node_to_soft_evidence_.clear();
1256 const auto& evidence = this->evidence();
1257 for (
const auto node: this->softEvidenceNodes()) {
1258 auto ev_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*evidence[node],
false);
1259 _node_to_soft_evidence_.insert(node, ev_pot);
1260 _clique_tensors_[_node_to_clique_[node]].insert(ev_pot);
1270 if (_use_schedules_) {
1272 for (
const auto node: nodes_with_projected_CPTs_changed) {
1274 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
1275 const auto& variables = cpt.variablesSequence();
1276 _ScheduleMultiDimSet_ marg_cpt_set;
1277 const auto sched_cpt = schedule.insertTable< Tensor< GUM_SCALAR > >(cpt,
false);
1278 marg_cpt_set.insert(sched_cpt);
1281 for (
const auto var: variables) {
1282 NodeId xnode = bn.nodeId(*var);
1283 if (_hard_ev_nodes_.exists(xnode)) {
1284 const auto pot = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[xnode],
false);
1285 marg_cpt_set.insert(pot);
1286 hard_variables.
insert(var);
1291 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
1295 _ScheduleMultiDimSet_ new_cpt_list
1296 = combine_and_project.schedule(schedule, marg_cpt_set, hard_variables);
1299 if (new_cpt_list.size() != 1) {
1301 "the projection of a tensor containing " <<
"hard evidence is empty!");
1303 auto projected_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1304 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*new_cpt_list.begin()));
1305 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(projected_pot))
1306 ->makeResultsPersistent(
true);
1307 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
1308 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1314 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1315 for (
const auto clique: invalidated_cliques) {
1316 const auto& potset = _clique_tensors_[clique];
1318 if (potset.size() > 0) {
1323 if (potset.size() == 1) {
1324 _clique_ss_tensor_[clique] = *(potset.cbegin());
1326 for (
const auto pot: potset)
1327 if (!schedule.existsScheduleMultiDim(pot->id()))
1328 schedule.emplaceScheduleMultiDim(*pot);
1329 auto joint =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1330 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1331 fast_combination.schedule(schedule, potset)));
1332 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(joint))
1333 ->makeResultsPersistent(
true);
1334 _clique_ss_tensor_[clique] = joint;
1338 this->scheduler().execute(schedule);
1340 for (
const auto node: nodes_with_projected_CPTs_changed) {
1342 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
1343 const auto& variables = cpt.variablesSequence();
1344 _TensorSet_ marg_cpt_set(1 + variables.size());
1345 marg_cpt_set.insert(&cpt);
1348 for (
const auto var: variables) {
1349 NodeId xnode = bn.nodeId(*var);
1350 if (_hard_ev_nodes_.exists(xnode)) {
1351 marg_cpt_set.insert(evidence[xnode]);
1352 hard_variables.
insert(var);
1357 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
1361 _TensorSet_ new_cpt_list = combine_and_project.execute(marg_cpt_set, hard_variables);
1364 if (new_cpt_list.size() != 1) {
1365 for (
const auto pot: new_cpt_list)
1366 if (!marg_cpt_set.contains(pot))
delete pot;
1369 "the projection of a tensor containing " <<
"hard evidence is empty!");
1371 Tensor< GUM_SCALAR >* xprojected_pot
1372 =
const_cast< Tensor< GUM_SCALAR >*
>(*new_cpt_list.begin());
1374 =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*xprojected_pot));
1375 delete xprojected_pot;
1376 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
1377 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1383 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1384 for (
const auto clique: invalidated_cliques) {
1385 const auto& potset = _clique_tensors_[clique];
1387 if (potset.size() > 0) {
1392 if (potset.size() == 1) {
1393 _clique_ss_tensor_[clique] = *(potset.cbegin());
1395 _TensorSet_ p_potset(potset.size());
1396 for (
const auto pot: potset)
1398 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot)->multiDim()));
1400 Tensor< GUM_SCALAR >* joint
1401 =
const_cast< Tensor< GUM_SCALAR >*
>(fast_combination.execute(p_potset));
1402 _clique_ss_tensor_[clique]
1403 =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*joint));
1411 const auto& hard_evidence = this->hardEvidence();
1412 for (
auto& node_cst: _constants_) {
1413 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node_cst.first);
1414 const auto& variables = cpt.variablesSequence();
1416 for (
const auto var: variables)
1418 for (
const auto var: variables) {
1419 inst.chgVal(*var, hard_evidence[bn.nodeId(*var)]);
1421 node_cst.second = cpt.get(inst);
1425 _evidence_changes_.clear();
1429 template < GUM_Numeric GUM_SCALAR >
1430 void ShaferShenoyInference< GUM_SCALAR >::_computeJoinTreeRoots_() {
1435 for (
const auto node: this->targets()) {
1436 if (
auto ptr_clique = _node_to_clique_.tryGet(node)) { clique_targets.insert(*ptr_clique); }
1438 for (
const auto& set: this->jointTargets()) {
1439 if (
auto ptr_clique = _joint_target_to_clique_.tryGet(set)) {
1440 clique_targets.insert(*ptr_clique);
1450 template < GUM_Numeric GUM_SCALAR >
1451 void ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsGetAll_(
1452 Set< const IScheduleMultiDim* >& pot_list,
1456 template < GUM_Numeric GUM_SCALAR >
1457 void ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation_(
1458 Set< const IScheduleMultiDim* >& pot_list,
1462 const auto& bn = this->BN();
1463 for (
const auto var: kept_vars) {
1464 kept_ids.insert(bn.nodeId(*var));
1469 BayesBall::requisiteNodes(bn.internalDag(),
1471 this->hardEvidenceNodes(),
1472 this->softEvidenceNodes(),
1474 for (
auto iter = pot_list.beginSafe(); iter != pot_list.endSafe(); ++iter) {
1475 const Sequence< const DiscreteVariable* >& vars = (*iter)->variablesSequence();
1477 for (
const auto var: vars) {
1478 if (requisite_nodes.exists(bn.nodeId(*var))) {
1484 if (!found) { pot_list.erase(iter); }
1489 template < GUM_Numeric GUM_SCALAR >
1490 void ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_(
1491 Set< const IScheduleMultiDim* >& pot_list,
1495 const auto& bn = this->BN();
1496 for (
const auto var: kept_vars) {
1497 kept_ids.insert(bn.nodeId(*var));
1501 BayesBall::relevantTensors(bn,
1503 this->hardEvidenceNodes(),
1504 this->softEvidenceNodes(),
1509 template < GUM_Numeric GUM_SCALAR >
1510 void ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsWithdSeparation3_(
1511 Set< const IScheduleMultiDim* >& pot_list,
1515 const auto& bn = this->BN();
1516 for (
const auto var: kept_vars) {
1517 kept_ids.insert(bn.nodeId(*var));
1521 dSeparationAlgorithm dsep;
1522 dsep.relevantTensors(bn,
1524 this->hardEvidenceNodes(),
1525 this->softEvidenceNodes(),
1530 template < GUM_Numeric GUM_SCALAR >
1531 void ShaferShenoyInference< GUM_SCALAR >::_findRelevantTensorsXX_(
1532 Set< const IScheduleMultiDim* >& pot_list,
1534 switch (_find_relevant_tensor_type_) {
1535 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
1536 _findRelevantTensorsWithdSeparation2_(pot_list, kept_vars);
1539 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES :
1540 _findRelevantTensorsWithdSeparation_(pot_list, kept_vars);
1543 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 :
1544 _findRelevantTensorsWithdSeparation3_(pot_list, kept_vars);
1547 case RelevantTensorsFinderType::FIND_ALL :
1548 _findRelevantTensorsGetAll_(pot_list, kept_vars);
1556 template < GUM_Numeric GUM_SCALAR >
1557 Set< const IScheduleMultiDim* >
1558 ShaferShenoyInference< GUM_SCALAR >::_removeBarrenVariables_(Schedule& schedule,
1559 _ScheduleMultiDimSet_& pot_list,
1564 for (
auto iter = the_del_vars.
beginSafe(); iter != the_del_vars.
endSafe(); ++iter) {
1565 NodeId
id = this->BN().nodeId(**iter);
1566 if (this->hardEvidenceNodes().exists(
id) || this->softEvidenceNodes().exists(
id)) {
1567 the_del_vars.
erase(iter);
1572 HashTable< const DiscreteVariable*, _ScheduleMultiDimSet_ > var2pots(the_del_vars.
size());
1573 _ScheduleMultiDimSet_ empty_pot_set;
1574 for (
const auto pot: pot_list) {
1575 const auto& vars = pot->variablesSequence();
1576 for (
const auto var: vars) {
1577 if (the_del_vars.
exists(var)) {
1578 if (!var2pots.exists(var)) { var2pots.insert(var, empty_pot_set); }
1579 var2pots[var].insert(pot);
1586 HashTable< const IScheduleMultiDim*, gum::VariableSet > pot2barren_var;
1588 for (
const auto& elt: var2pots) {
1589 if (elt.second.size() == 1) {
1590 const IScheduleMultiDim* pot = *(elt.second.begin());
1591 if (!pot2barren_var.exists(pot)) { pot2barren_var.insert(pot, empty_var_set); }
1592 pot2barren_var[pot].insert(elt.first);
1599 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1600 _ScheduleMultiDimSet_ projected_pots;
1601 for (
const auto& elt: pot2barren_var) {
1603 const IScheduleMultiDim* pot = elt.first;
1604 pot_list.erase(pot);
1608 if (pot->variablesSequence().size() != elt.second.size()) {
1609 const IScheduleMultiDim* new_pot = projector.schedule(schedule, pot, elt.second);
1613 pot_list.insert(new_pot);
1614 projected_pots.insert(new_pot);
1618 return projected_pots;
1622 template < GUM_Numeric GUM_SCALAR >
1623 Set< const Tensor< GUM_SCALAR >* >
1624 ShaferShenoyInference< GUM_SCALAR >::_removeBarrenVariables_(_TensorSet_& pot_list,
1629 for (
auto iter = the_del_vars.
beginSafe(); iter != the_del_vars.
endSafe(); ++iter) {
1630 NodeId
id = this->BN().nodeId(**iter);
1631 if (this->hardEvidenceNodes().exists(
id) || this->softEvidenceNodes().exists(
id)) {
1632 the_del_vars.
erase(iter);
1637 HashTable< const DiscreteVariable*, _TensorSet_ > var2pots(the_del_vars.
size());
1638 _TensorSet_ empty_pot_set;
1639 for (
const auto pot: pot_list) {
1640 const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
1641 for (
const auto var: vars) {
1642 if (the_del_vars.
exists(var)) {
1643 if (!var2pots.exists(var)) { var2pots.insert(var, empty_pot_set); }
1644 var2pots[var].insert(pot);
1653 for (
const auto& elt: var2pots) {
1654 if (elt.second.size() == 1) {
1655 const Tensor< GUM_SCALAR >* pot = *(elt.second.begin());
1656 if (!pot2barren_var.exists(pot)) { pot2barren_var.insert(pot, empty_var_set); }
1657 pot2barren_var[pot].insert(elt.first);
1664 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1665 _TensorSet_ projected_pots;
1666 for (
const auto& elt: pot2barren_var) {
1668 const Tensor< GUM_SCALAR >* pot = elt.first;
1669 pot_list.erase(pot);
1673 if (pot->variablesSequence().size() != elt.second.size()) {
1674 const Tensor< GUM_SCALAR >* new_pot = projector.execute(*pot, elt.second);
1675 pot_list.insert(new_pot);
1676 projected_pots.insert(new_pot);
1680 return projected_pots;
1684 template < GUM_Numeric GUM_SCALAR >
1685 void ShaferShenoyInference< GUM_SCALAR >::_collectMessage_(Schedule& schedule,
1688 for (
const auto other: _JT_->neighbours(
id)) {
1689 if ((other != from) && !_messages_computed_[
Arc(other,
id)])
1690 _collectMessage_(schedule, other,
id);
1693 if ((
id != from) && !_messages_computed_[
Arc(
id, from)]) {
1694 _produceMessage_(schedule,
id, from);
1699 template < GUM_Numeric GUM_SCALAR >
1700 void ShaferShenoyInference< GUM_SCALAR >::_collectMessage_(NodeId
id, NodeId from) {
1701 for (
const auto other: _JT_->neighbours(
id)) {
1702 if ((other != from) && !_messages_computed_[
Arc(other,
id)]) _collectMessage_(other,
id);
1705 if ((
id != from) && !_messages_computed_[
Arc(
id, from)]) { _produceMessage_(
id, from); }
1709 template < GUM_Numeric GUM_SCALAR >
1710 const IScheduleMultiDim* ShaferShenoyInference< GUM_SCALAR >::_marginalizeOut_(
1712 Set< const IScheduleMultiDim* > pot_list,
1719 if (pot_list.empty()) {
1720 return new ScheduleMultiDim< Tensor< GUM_SCALAR > >(Tensor< GUM_SCALAR >());
1725 for (
const auto pot: pot_list) {
1726 if (!schedule.existsScheduleMultiDim(pot->id())) schedule.emplaceScheduleMultiDim(*pot);
1731 _ScheduleMultiDimSet_ barren_projected_tensors;
1732 if (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES) {
1733 barren_projected_tensors = _removeBarrenVariables_(schedule, pot_list, del_vars);
1737 _ScheduleMultiDimSet_ new_pot_list;
1738 if (pot_list.size() == 1) {
1739 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1740 auto xpot = projector.schedule(schedule, *(pot_list.begin()), del_vars);
1741 new_pot_list.insert(xpot);
1742 }
else if (pot_list.size() > 1) {
1745 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(_combination_op_,
1747 new_pot_list = combine_and_project.schedule(schedule, pot_list, del_vars);
1753 for (
auto barren_pot: barren_projected_tensors) {
1754 if (!new_pot_list.exists(barren_pot))
1755 schedule.emplaceDeletion(
1756 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>& >(*barren_pot));
1760 if (new_pot_list.empty())
1761 return new ScheduleMultiDim< Tensor< GUM_SCALAR > >(Tensor< GUM_SCALAR >());
1762 if (new_pot_list.size() == 1)
return *(new_pot_list.begin());
1763 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1764 return fast_combination.schedule(schedule, new_pot_list);
1768 template < GUM_Numeric GUM_SCALAR >
1769 const IScheduleMultiDim* ShaferShenoyInference< GUM_SCALAR >::_marginalizeOut_(
1770 Set< const IScheduleMultiDim* >& pot_list,
1774 if (pot_list.empty()) {
1775 return new ScheduleMultiDim< Tensor< GUM_SCALAR > >(Tensor< GUM_SCALAR >());
1778 _TensorSet_ xpot_list(pot_list.size());
1779 for (
auto pot: pot_list)
1781 &(
static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot)->multiDim()));
1788 _TensorSet_ barren_projected_tensors;
1789 if (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES) {
1790 barren_projected_tensors = _removeBarrenVariables_(xpot_list, del_vars);
1794 _TensorSet_ xnew_pot_list;
1795 if (xpot_list.size() == 1) {
1796 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1797 auto xpot = projector.execute(**(xpot_list.begin()), del_vars);
1798 xnew_pot_list.insert(xpot);
1799 }
else if (xpot_list.size() > 1) {
1802 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(_combination_op_,
1804 xnew_pot_list = combine_and_project.execute(xpot_list, del_vars);
1808 const Tensor< GUM_SCALAR >* xres_pot;
1809 ScheduleMultiDim< Tensor< GUM_SCALAR > >* res_pot;
1810 if (xnew_pot_list.size() == 1) {
1811 xres_pot = *(xnew_pot_list.begin());
1812 }
else if (xnew_pot_list.size() > 1) {
1815 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1816 xres_pot = fast_combination.execute(xnew_pot_list);
1817 for (
const auto pot: xnew_pot_list) {
1818 if (!xpot_list.contains(pot) && (pot != xres_pot))
delete pot;
1821 xres_pot =
new Tensor< GUM_SCALAR >();
1825 if (xpot_list.contains(xres_pot))
1826 res_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*xres_pot,
false);
1828 res_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(
1829 std::move(
const_cast< Tensor< GUM_SCALAR >&
>(*xres_pot)));
1836 for (
const auto barren_pot: barren_projected_tensors) {
1837 if (!xnew_pot_list.exists(barren_pot))
delete barren_pot;
1844 template < GUM_Numeric GUM_SCALAR >
1845 void ShaferShenoyInference< GUM_SCALAR >::_produceMessage_(Schedule& schedule,
1849 _ScheduleMultiDimSet_ pot_list;
1850 if (_clique_ss_tensor_[from_id] !=
nullptr) pot_list.insert(_clique_ss_tensor_[from_id]);
1853 for (
const auto other_id: _JT_->neighbours(from_id)) {
1854 if (other_id != to_id) {
1855 const auto separator_pot = _separator_tensors_[
Arc(other_id, from_id)];
1856 if (separator_pot !=
nullptr) pot_list.insert(separator_pot);
1861 const NodeSet& from_clique = _JT_->clique(from_id);
1862 const NodeSet& separator = _JT_->separator(from_id, to_id);
1865 const auto& bn = this->BN();
1867 for (
const auto node: from_clique) {
1868 if (!separator.contains(node)) {
1869 del_vars.
insert(&(bn.variable(node)));
1871 kept_vars.
insert(&(bn.variable(node)));
1877 const IScheduleMultiDim* new_pot = _marginalizeOut_(schedule, pot_list, del_vars, kept_vars);
1880 const Arc arc(from_id, to_id);
1881 if (!pot_list.exists(new_pot)) {
1882 if (!_arc_to_created_tensors_.exists(arc)) {
1883 _arc_to_created_tensors_.insert(arc, new_pot);
1886 auto op = schedule.scheduleMultiDimCreator(new_pot);
1887 if (op !=
nullptr)
const_cast< ScheduleOperator*
>(op)->makeResultsPersistent(
true);
1891 _separator_tensors_[arc] = new_pot;
1892 _messages_computed_[arc] =
true;
1896 template < GUM_Numeric GUM_SCALAR >
1897 void ShaferShenoyInference< GUM_SCALAR >::_produceMessage_(NodeId from_id, NodeId to_id) {
1899 _ScheduleMultiDimSet_ pot_list;
1900 if (_clique_ss_tensor_[from_id] !=
nullptr) pot_list.insert(_clique_ss_tensor_[from_id]);
1903 for (
const auto other_id: _JT_->neighbours(from_id)) {
1904 if (other_id != to_id) {
1905 const auto separator_pot = _separator_tensors_[
Arc(other_id, from_id)];
1906 if (separator_pot !=
nullptr) pot_list.insert(separator_pot);
1911 const NodeSet& from_clique = _JT_->clique(from_id);
1912 const NodeSet& separator = _JT_->separator(from_id, to_id);
1915 const auto& bn = this->BN();
1917 for (
const auto node: from_clique) {
1918 if (!separator.contains(node)) {
1919 del_vars.
insert(&(bn.variable(node)));
1921 kept_vars.
insert(&(bn.variable(node)));
1927 const IScheduleMultiDim* new_pot = _marginalizeOut_(pot_list, del_vars, kept_vars);
1930 const Arc arc(from_id, to_id);
1931 if (!pot_list.exists(new_pot)) {
1932 if (!_arc_to_created_tensors_.exists(arc)) { _arc_to_created_tensors_.insert(arc, new_pot); }
1935 _separator_tensors_[arc] = new_pot;
1936 _messages_computed_[arc] =
true;
1940 template < GUM_Numeric GUM_SCALAR >
1941 void ShaferShenoyInference< GUM_SCALAR >::makeInference_() {
1942 if (_use_schedules_) {
1946 for (
const auto node: this->targets()) {
1950 if (_graph_.exists(node)) {
1951 _collectMessage_(schedule, _node_to_clique_[node], _node_to_clique_[node]);
1959 for (
const auto& set: _joint_target_to_clique_)
1960 _collectMessage_(schedule, set.second, set.second);
1963 this->scheduler().execute(schedule);
1966 for (
const auto node: this->targets()) {
1970 if (_graph_.exists(node)) {
1971 _collectMessage_(_node_to_clique_[node], _node_to_clique_[node]);
1979 for (
const auto& set: _joint_target_to_clique_)
1980 _collectMessage_(set.second, set.second);
1985 template < GUM_Numeric GUM_SCALAR >
1986 Tensor< GUM_SCALAR >*
1987 ShaferShenoyInference< GUM_SCALAR >::unnormalizedJointPosterior_(NodeId
id) {
1988 if (_use_schedules_) {
1990 return _unnormalizedJointPosterior_(schedule,
id);
1992 return _unnormalizedJointPosterior_(
id);
1997 template < GUM_Numeric GUM_SCALAR >
1998 Tensor< GUM_SCALAR >*
1999 ShaferShenoyInference< GUM_SCALAR >::_unnormalizedJointPosterior_(Schedule& schedule,
2001 const auto& bn = this->BN();
2005 if (this->hardEvidenceNodes().
contains(
id)) {
2006 return new Tensor< GUM_SCALAR >(*(this->evidence()[
id]));
2009 auto& scheduler = this->scheduler();
2013 const NodeId clique_of_id = _node_to_clique_[id];
2014 _collectMessage_(schedule, clique_of_id, clique_of_id);
2019 _ScheduleMultiDimSet_ pot_list;
2020 if (_clique_ss_tensor_[clique_of_id] !=
nullptr)
2021 pot_list.insert(_clique_ss_tensor_[clique_of_id]);
2024 for (
const auto other: _JT_->neighbours(clique_of_id))
2025 pot_list.insert(_separator_tensors_[
Arc(other, clique_of_id)]);
2028 const NodeSet& nodes = _JT_->clique(clique_of_id);
2031 for (
const auto node: nodes) {
2032 if (node !=
id) del_vars.
insert(&(bn.variable(node)));
2037 auto resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2038 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2039 _marginalizeOut_(schedule, pot_list, del_vars, kept_vars)));
2040 Tensor< GUM_SCALAR >* joint =
nullptr;
2042 scheduler.execute(schedule);
2046 if (pot_list.exists(resulting_pot)) {
2047 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
2049 joint = resulting_pot->exportMultiDim();
2055 bool nonzero_found =
false;
2056 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2057 if (joint->get(inst)) {
2058 nonzero_found =
true;
2062 if (!nonzero_found) {
2066 "some evidence entered into the Bayes "
2067 "net are incompatible (their joint proba = 0)");
2073 template < GUM_Numeric GUM_SCALAR >
2074 Tensor< GUM_SCALAR >*
2075 ShaferShenoyInference< GUM_SCALAR >::_unnormalizedJointPosterior_(NodeId
id) {
2076 const auto& bn = this->BN();
2080 if (this->hardEvidenceNodes().
contains(
id)) {
2081 return new Tensor< GUM_SCALAR >(*(this->evidence()[
id]));
2086 NodeId clique_of_id = _node_to_clique_[id];
2087 _collectMessage_(clique_of_id, clique_of_id);
2092 _ScheduleMultiDimSet_ pot_list;
2093 if (_clique_ss_tensor_[clique_of_id] !=
nullptr)
2094 pot_list.insert(_clique_ss_tensor_[clique_of_id]);
2097 for (
const auto other: _JT_->neighbours(clique_of_id))
2098 pot_list.insert(_separator_tensors_[
Arc(other, clique_of_id)]);
2101 const NodeSet& nodes = _JT_->clique(clique_of_id);
2104 for (
const auto node: nodes) {
2105 if (node !=
id) del_vars.
insert(&(bn.variable(node)));
2110 auto resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2111 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2112 _marginalizeOut_(pot_list, del_vars, kept_vars)));
2113 Tensor< GUM_SCALAR >* joint =
nullptr;
2117 if (pot_list.exists(resulting_pot)) {
2118 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
2120 joint = resulting_pot->exportMultiDim();
2121 delete resulting_pot;
2127 bool nonzero_found =
false;
2128 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2129 if (joint->get(inst)) {
2130 nonzero_found =
true;
2134 if (!nonzero_found) {
2138 "some evidence entered into the Bayes "
2139 "net are incompatible (their joint proba = 0)");
2145 template < GUM_Numeric GUM_SCALAR >
2146 const Tensor< GUM_SCALAR >& ShaferShenoyInference< GUM_SCALAR >::posterior_(NodeId
id) {
2148 if (_target_posteriors_.exists(
id)) {
return *(_target_posteriors_[id]); }
2151 auto joint = unnormalizedJointPosterior_(
id);
2152 if (joint->sum() != 1)
2154 _target_posteriors_.insert(
id, joint);
2160 template < GUM_Numeric GUM_SCALAR >
2161 Tensor< GUM_SCALAR >*
2162 ShaferShenoyInference< GUM_SCALAR >::unnormalizedJointPosterior_(
const NodeSet& set) {
2163 if (_use_schedules_) {
2165 return _unnormalizedJointPosterior_(schedule, set);
2167 return _unnormalizedJointPosterior_(set);
2172 template < GUM_Numeric GUM_SCALAR >
2173 Tensor< GUM_SCALAR >*
2174 ShaferShenoyInference< GUM_SCALAR >::_unnormalizedJointPosterior_(Schedule& schedule,
2175 const NodeSet& set) {
2178 NodeSet targets = set, hard_ev_nodes;
2179 for (
const auto node: this->hardEvidenceNodes()) {
2180 if (targets.contains(node)) {
2181 targets.erase(node);
2182 hard_ev_nodes.insert(node);
2186 auto& scheduler = this->scheduler();
2190 const auto& evidence = this->evidence();
2191 if (targets.empty()) {
2192 if (set.size() == 1) {
2193 return new Tensor< GUM_SCALAR >(*evidence[*set.begin()]);
2195 _ScheduleMultiDimSet_ pot_list;
2196 for (
const auto node: set) {
2197 auto new_pot_ev = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[node],
false);
2198 pot_list.insert(new_pot_ev);
2202 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2203 const IScheduleMultiDim* pot = fast_combination.schedule(schedule, pot_list);
2204 auto schedule_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2205 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot));
2206 scheduler.execute(schedule);
2207 auto result = schedule_pot->exportMultiDim();
2217 NodeId clique_of_set;
2218 if (
auto ptr_clique = _joint_target_to_clique_.tryGet(set)) {
2219 clique_of_set = *ptr_clique;
2226 for (
const auto node: targets) {
2227 if (!_graph_.exists(node)) {
2229 "The variable " << this->BN().variable(node).name() <<
"(" << node
2230 <<
") does not belong to this optimized inference.")
2236 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
2238 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
2239 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
2240 elim_order.insert(JT_elim_order[i], (
int)i);
2241 NodeId first_eliminated_node = *(targets.begin());
2242 int elim_number = elim_order[first_eliminated_node];
2243 for (
const auto node: targets) {
2244 if (elim_order[node] < elim_number) {
2245 elim_number = elim_order[node];
2246 first_eliminated_node = node;
2250 clique_of_set = _triangulation_->createdJunctionTreeClique(first_eliminated_node);
2254 const NodeSet& clique_nodes = _JT_->clique(clique_of_set);
2255 for (
const auto node: targets) {
2256 if (!clique_nodes.contains(node)) {
2258 this->BN().names(set) <<
"(" << set <<
")"
2259 <<
" is not addressable in this optimized inference.")
2264 _joint_target_to_clique_.
insert(set, clique_of_set);
2268 _collectMessage_(schedule, clique_of_set, clique_of_set);
2273 _ScheduleMultiDimSet_ pot_list;
2274 if (_clique_ss_tensor_[clique_of_set] !=
nullptr) {
2275 auto pot = _clique_ss_tensor_[clique_of_set];
2276 if (!schedule.existsScheduleMultiDim(pot->id())) schedule.emplaceScheduleMultiDim(*pot);
2277 pot_list.insert(_clique_ss_tensor_[clique_of_set]);
2281 for (
const auto other: _JT_->neighbours(clique_of_set)) {
2282 const auto pot = _separator_tensors_[
Arc(other, clique_of_set)];
2283 if (pot !=
nullptr) pot_list.insert(pot);
2288 const NodeSet& nodes = _JT_->clique(clique_of_set);
2291 const auto& bn = this->BN();
2292 for (
const auto node: nodes) {
2293 if (!targets.contains(node)) {
2294 del_vars.
insert(&(bn.variable(node)));
2296 kept_vars.
insert(&(bn.variable(node)));
2302 const IScheduleMultiDim* new_pot = _marginalizeOut_(schedule, pot_list, del_vars, kept_vars);
2303 scheduler.execute(schedule);
2304 ScheduleMultiDim< Tensor< GUM_SCALAR > >* resulting_pot
2305 =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2306 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(new_pot));
2310 Tensor< GUM_SCALAR >* joint =
nullptr;
2311 if (pot_list.exists(resulting_pot)) {
2312 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
2314 joint = resulting_pot->exportMultiDim();
2319 bool nonzero_found =
false;
2320 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2321 if ((*joint)[inst]) {
2322 nonzero_found =
true;
2326 if (!nonzero_found) {
2330 "some evidence entered into the Bayes "
2331 "net are incompatible (their joint proba = 0)");
2337 if (!hard_ev_nodes.empty()) {
2338 _TensorSet_ pot_list;
2339 pot_list.insert(joint);
2340 const auto& hard_evidence = this->evidence();
2341 for (
const auto node: hard_ev_nodes)
2342 pot_list.insert(hard_evidence[node]);
2343 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > combine(_combination_op_);
2344 Tensor< GUM_SCALAR >* new_joint = combine.execute(pot_list);
2353 template < GUM_Numeric GUM_SCALAR >
2354 Tensor< GUM_SCALAR >*
2355 ShaferShenoyInference< GUM_SCALAR >::_unnormalizedJointPosterior_(
const NodeSet& set) {
2358 NodeSet targets = set, hard_ev_nodes;
2359 for (
const auto node: this->hardEvidenceNodes()) {
2360 if (targets.contains(node)) {
2361 targets.erase(node);
2362 hard_ev_nodes.insert(node);
2368 const auto& evidence = this->evidence();
2369 if (targets.empty()) {
2370 if (set.size() == 1) {
2371 return new Tensor< GUM_SCALAR >(*evidence[*set.begin()]);
2373 _TensorSet_ pot_list;
2374 for (
const auto node: set) {
2375 pot_list.insert(evidence[node]);
2379 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2380 const Tensor< GUM_SCALAR >* pot = fast_combination.execute(pot_list);
2382 return const_cast< Tensor< GUM_SCALAR >*
>(pot);
2390 NodeId clique_of_set;
2391 if (
auto ptr_clique = _joint_target_to_clique_.tryGet(set)) {
2392 clique_of_set = *ptr_clique;
2399 for (
const auto node: targets) {
2400 if (!_graph_.exists(node)) {
2402 node <<
" cannot be a query in the optimized inference (w.r.t the declared "
2403 "targets/evidence)")
2409 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
2411 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
2412 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
2413 elim_order.insert(JT_elim_order[i], (
int)i);
2414 NodeId first_eliminated_node = *(targets.begin());
2415 int elim_number = elim_order[first_eliminated_node];
2416 for (
const auto node: targets) {
2417 if (elim_order[node] < elim_number) {
2418 elim_number = elim_order[node];
2419 first_eliminated_node = node;
2423 clique_of_set = _triangulation_->createdJunctionTreeClique(first_eliminated_node);
2426 const NodeSet& clique_nodes = _JT_->clique(clique_of_set);
2427 for (
const auto node: targets) {
2428 if (!clique_nodes.contains(node)) {
2430 this->BN().names(set) <<
"(" << set <<
")"
2431 <<
" is not addressable in this optimized inference.")
2436 _joint_target_to_clique_.
insert(set, clique_of_set);
2440 _collectMessage_(clique_of_set, clique_of_set);
2445 _ScheduleMultiDimSet_ pot_list;
2446 if (_clique_ss_tensor_[clique_of_set] !=
nullptr) {
2447 auto pot = _clique_ss_tensor_[clique_of_set];
2448 if (pot !=
nullptr) pot_list.insert(_clique_ss_tensor_[clique_of_set]);
2452 for (
const auto other: _JT_->neighbours(clique_of_set)) {
2453 const auto pot = _separator_tensors_[
Arc(other, clique_of_set)];
2454 if (pot !=
nullptr) pot_list.insert(pot);
2458 const NodeSet& nodes = _JT_->clique(clique_of_set);
2461 const auto& bn = this->BN();
2462 for (
const auto node: nodes) {
2463 if (!targets.contains(node)) {
2464 del_vars.
insert(&(bn.variable(node)));
2466 kept_vars.
insert(&(bn.variable(node)));
2472 const IScheduleMultiDim* new_pot = _marginalizeOut_(pot_list, del_vars, kept_vars);
2473 ScheduleMultiDim< Tensor< GUM_SCALAR > >* resulting_pot
2474 =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2475 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(new_pot));
2479 Tensor< GUM_SCALAR >* joint =
nullptr;
2480 if (pot_list.exists(resulting_pot)) {
2481 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
2483 joint = resulting_pot->exportMultiDim();
2489 bool nonzero_found =
false;
2490 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2491 if ((*joint)[inst]) {
2492 nonzero_found =
true;
2496 if (!nonzero_found) {
2500 "some evidence entered into the Bayes "
2501 "net are incompatible (their joint proba = 0)");
2507 if (!hard_ev_nodes.empty()) {
2508 _TensorSet_ pot_list;
2509 pot_list.insert(joint);
2510 const auto& hard_evidence = this->evidence();
2511 for (
const auto node: hard_ev_nodes)
2512 pot_list.insert(hard_evidence[node]);
2513 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > combine(_combination_op_);
2514 Tensor< GUM_SCALAR >* new_joint = combine.execute(pot_list);
2523 template < GUM_Numeric GUM_SCALAR >
2524 const Tensor< GUM_SCALAR >&
2525 ShaferShenoyInference< GUM_SCALAR >::jointPosterior_(
const NodeSet& set) {
2527 if (_joint_target_posteriors_.exists(set)) {
return *(_joint_target_posteriors_[set]); }
2530 auto joint = unnormalizedJointPosterior_(set);
2532 _joint_target_posteriors_.insert(set, joint);
2538 template < GUM_Numeric GUM_SCALAR >
2539 const Tensor< GUM_SCALAR >&
2540 ShaferShenoyInference< GUM_SCALAR >::jointPosterior_(
const NodeSet& wanted_target,
2541 const NodeSet& declared_target) {
2543 if (_joint_target_posteriors_.exists(wanted_target))
2544 return *(_joint_target_posteriors_[wanted_target]);
2550 if (!_joint_target_posteriors_.exists(declared_target)) { jointPosterior_(declared_target); }
2553 const auto& bn = this->BN();
2555 for (
const auto node: declared_target)
2556 if (!wanted_target.contains(node)) del_vars.
insert(&(bn.variable(node)));
2558 =
new Tensor< GUM_SCALAR >(_joint_target_posteriors_[declared_target]->sumOut(del_vars));
2562 _joint_target_posteriors_.insert(wanted_target, pot);
2563 if (
auto clique = _joint_target_to_clique_.tryGet(declared_target)) {
2564 const NodeId clique_id = *clique;
2565 if (!_joint_target_to_clique_.exists(wanted_target))
2566 _joint_target_to_clique_.insert(wanted_target, clique_id);
2572 template < GUM_Numeric GUM_SCALAR >
2573 GUM_SCALAR ShaferShenoyInference< GUM_SCALAR >::evidenceProbability() {
2576 RelevantTensorsFinderType old_relevant_type = _find_relevant_tensor_type_;
2581 if (old_relevant_type != RelevantTensorsFinderType::FIND_ALL) {
2582 _find_relevant_tensor_type_ = RelevantTensorsFinderType::FIND_ALL;
2583 _is_new_jt_needed_ =
true;
2584 this->setOutdatedStructureState_();
2588 this->makeInference();
2596 GUM_SCALAR prob_ev = 1;
2597 for (
const auto root: _roots_) {
2599 NodeId node = *(_JT_->clique(root).begin());
2600 Tensor< GUM_SCALAR >* tmp = unnormalizedJointPosterior_(node);
2601 prob_ev *= tmp->sum();
2605 for (
const auto& projected_cpt: _constants_)
2606 prob_ev *= projected_cpt.second;
2609 _find_relevant_tensor_type_ = old_relevant_type;
2614 template < GUM_Numeric GUM_SCALAR >
2615 Tensor< GUM_SCALAR > SSNewmultiTensor(
const Tensor< GUM_SCALAR >& t1,
2616 const Tensor< GUM_SCALAR >& t2) {
2620 template < GUM_Numeric GUM_SCALAR >
2621 Tensor< GUM_SCALAR > SSNewprojTensor(
const Tensor< GUM_SCALAR >& t1,
2623 return t1.sumOut(del_vars);
2626 template < GUM_Numeric GUM_SCALAR >
2627 void ShaferShenoyInference< GUM_SCALAR >::onStateChanged_() {}
The BayesBall algorithm (as described by Schachter).
Implementation of Shafer-Shenoy's algorithm for inference in Bayesian networks.
Detect barren nodes for inference in Bayesian networks.
An algorithm for converting a join tree into a binary join tree.
Exception : a similar element already exists.
<agrum/BN/inference/evidenceInference.h>
Exception : fatal (unknown ?) error.
Class representing the minimal interface for Bayesian network with no numerical data.
Exception : several evidence are incompatible together (proba=0).
Exception: at least one argument passed to a function is not what was expected.
<agrum/BN/inference/jointTargetedInference.h>
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
void erase(const Key &k)
Erases an element from the set.
Size size() const noexcept
Returns the number of elements in the set.
static const iterator_safe & endSafe() noexcept
The usual safe end iterator to parse the set.
ShaferShenoyInference(const IBayesNet< GUM_SCALAR > *BN, RelevantTensorsFinderType=RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS, FindBarrenNodesType barren_type=FindBarrenNodesType::FIND_BARREN_NODES, bool use_binary_join_tree=true)
default constructor
Exception : a looked-for element could not be found.
d-separation analysis (as described in Koller & Friedman 2009)
#define GUM_ERROR(type, msg)
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
GUM_SHARED_PUBLIC bool contains(std::string_view s, std::string_view needle)
true if needle in s
Header files of gum::Instantiation.
Selection of the roots of the connected components of a junction tree.
gum is the global namespace for all aGrUM entities
FindBarrenNodesType
type of algorithm to determine barren nodes
Set< const DiscreteVariable * > VariableSet
NodeSet junctionTreeRoots(const JunctionTree &jt, const GraphicalModel &model, const NodeSet &preferred)
Returns one root for each connected component of a junction tree.
CliqueGraph JoinTree
a join tree is a clique graph satisfying the running intersection property (but some cliques may be i...
CliqueGraph JunctionTree
a junction tree is a clique graph satisfying the running intersection property and such that no cliqu...
RelevantTensorsFinderType
type of algorithm for determining the relevant tensors for combinations using some d-separation analy...