54#ifndef DOXYGEN_SHOULD_SKIP_THIS
70 template < GUM_Numeric GUM_SCALAR >
74 bool use_binary_join_tree) :
76 _use_binary_join_tree_(use_binary_join_tree) {
78 _findRelevantTensors_ = &LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_;
79 setRelevantTensorsFinderType(relevant_type);
80 setFindBarrenNodesType(barren_type);
83 _triangulation_ =
new DefaultTriangulation;
86 GUM_CONSTRUCTOR(LazyPropagation);
90 template < GUM_Numeric GUM_SCALAR >
91 LazyPropagation< GUM_SCALAR >::~LazyPropagation() {
93 for (
const auto& pots: _arc_to_created_tensors_)
94 for (
const auto pot: pots.second)
98 for (
const auto& pots: _clique_tensors_)
99 for (
const auto pot: pots.second)
103 for (
const auto& pot: _target_posteriors_)
105 for (
const auto& pot: _joint_target_posteriors_)
109 if (_JT_ !=
nullptr)
delete _JT_;
110 if (_junctionTree_ !=
nullptr)
delete _junctionTree_;
111 delete _triangulation_;
113 GUM_DESTRUCTOR(LazyPropagation);
117 template < GUM_Numeric GUM_SCALAR >
118 void LazyPropagation< GUM_SCALAR >::setTriangulation(
const Triangulation& new_triangulation) {
119 delete _triangulation_;
120 _triangulation_ = new_triangulation.newFactory();
121 _is_new_jt_needed_ =
true;
122 this->setOutdatedStructureState_();
126 template < GUM_Numeric GUM_SCALAR >
127 const JoinTree* LazyPropagation< GUM_SCALAR >::joinTree() {
128 if (_is_new_jt_needed_) _createNewJT_();
134 template < GUM_Numeric GUM_SCALAR >
135 const JunctionTree* LazyPropagation< GUM_SCALAR >::junctionTree() {
136 if (_is_new_jt_needed_) _createNewJT_();
138 return _junctionTree_;
142 template < GUM_Numeric GUM_SCALAR >
143 void LazyPropagation< GUM_SCALAR >::setRelevantTensorsFinderType(RelevantTensorsFinderType type) {
144 if (type != _find_relevant_tensor_type_) {
146 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
147 _findRelevantTensors_
148 = &LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_;
151 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES :
152 _findRelevantTensors_
153 = &LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation_;
156 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 :
157 _findRelevantTensors_
158 = &LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation3_;
161 case RelevantTensorsFinderType::FIND_ALL :
162 _findRelevantTensors_ = &LazyPropagation< GUM_SCALAR >::_findRelevantTensorsGetAll_;
167 "setRelevantTensorsFinderType for type " << (
unsigned int)type
168 <<
" is not implemented yet");
171 _find_relevant_tensor_type_ = type;
175 _invalidateAllMessages_();
180 template < GUM_Numeric GUM_SCALAR >
181 void LazyPropagation< GUM_SCALAR >::_setProjectionFunction_(
182 Tensor< GUM_SCALAR > (*proj)(
const Tensor< GUM_SCALAR >&,
const gum::VariableSet&)) {
183 _projection_op_ = proj;
187 _invalidateAllMessages_();
191 template < GUM_Numeric GUM_SCALAR >
192 void LazyPropagation< GUM_SCALAR >::_setCombinationFunction_(
193 Tensor< GUM_SCALAR > (*comb)(
const Tensor< GUM_SCALAR >&,
const Tensor< GUM_SCALAR >&)) {
194 _combination_op_ = comb;
198 _invalidateAllMessages_();
202 template < GUM_Numeric GUM_SCALAR >
203 void LazyPropagation< GUM_SCALAR >::_invalidateAllMessages_() {
205 for (
auto& potset: _separator_tensors_)
206 potset.second.clear();
207 for (
auto& mess_computed: _messages_computed_)
208 mess_computed.second =
false;
211 for (
const auto& potset: _arc_to_created_tensors_)
212 for (
const auto pot: potset.second)
214 _arc_to_created_tensors_.clear();
217 for (
const auto& pot: _target_posteriors_)
219 _target_posteriors_.clear();
220 for (
const auto& pot: _joint_target_posteriors_)
222 _joint_target_posteriors_.clear();
225 if (this->isInferenceReady() || this->isInferenceDone()) this->setOutdatedTensorsState_();
229 template < GUM_Numeric GUM_SCALAR >
230 void LazyPropagation< GUM_SCALAR >::setFindBarrenNodesType(FindBarrenNodesType type) {
231 if (type != _barren_nodes_type_) {
235 case FindBarrenNodesType::FIND_BARREN_NODES :
236 case FindBarrenNodesType::FIND_NO_BARREN_NODES :
break;
240 "setFindBarrenNodesType for type " << (
unsigned int)type
241 <<
" is not implemented yet");
244 _barren_nodes_type_ = type;
247 this->setOutdatedStructureState_();
252 template < GUM_Numeric GUM_SCALAR >
253 void LazyPropagation< GUM_SCALAR >::onEvidenceAdded_(
const NodeId
id,
bool isHardEvidence) {
257 if (isHardEvidence || !_graph_.exists(
id)) _is_new_jt_needed_ =
true;
260 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_ADDED);
266 _evidence_changes_[id] = EvidenceChangeType::EVIDENCE_MODIFIED;
272 template < GUM_Numeric GUM_SCALAR >
273 void LazyPropagation< GUM_SCALAR >::onEvidenceErased_(
const NodeId
id,
bool isHardEvidence) {
276 if (isHardEvidence) _is_new_jt_needed_ =
true;
279 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_ERASED);
286 if (_evidence_changes_[
id] == EvidenceChangeType::EVIDENCE_ADDED)
287 _evidence_changes_.erase(
id);
288 else _evidence_changes_[id] = EvidenceChangeType::EVIDENCE_ERASED;
294 template < GUM_Numeric GUM_SCALAR >
295 void LazyPropagation< GUM_SCALAR >::onAllEvidenceErased_(
bool has_hard_evidence) {
296 if (has_hard_evidence || !this->hardEvidenceNodes().empty()) _is_new_jt_needed_ =
true;
298 for (
const auto node: this->softEvidenceNodes()) {
300 _evidence_changes_.insert(node, EvidenceChangeType::EVIDENCE_ERASED);
307 if (_evidence_changes_[node] == EvidenceChangeType::EVIDENCE_ADDED)
308 _evidence_changes_.erase(node);
309 else _evidence_changes_[node] = EvidenceChangeType::EVIDENCE_ERASED;
316 template < GUM_Numeric GUM_SCALAR >
317 void LazyPropagation< GUM_SCALAR >::onEvidenceChanged_(
const NodeId
id,
bool hasChangedSoftHard) {
318 if (hasChangedSoftHard) _is_new_jt_needed_ =
true;
321 _evidence_changes_.insert(
id, EvidenceChangeType::EVIDENCE_MODIFIED);
331 template < GUM_Numeric GUM_SCALAR >
332 void LazyPropagation< GUM_SCALAR >::onModelChanged_(
const GraphicalModel* bn) {}
335 template < GUM_Numeric GUM_SCALAR >
336 void LazyPropagation< GUM_SCALAR >::onMarginalTargetAdded_(
const NodeId
id) {
341 if (!_graph_.exists(
id) && !_hard_ev_nodes_.contains(
id)) { _is_new_jt_needed_ =
true; }
345 template < GUM_Numeric GUM_SCALAR >
346 void LazyPropagation< GUM_SCALAR >::onMarginalTargetErased_(
const NodeId
id) {}
349 template < GUM_Numeric GUM_SCALAR >
350 void LazyPropagation< GUM_SCALAR >::onJointTargetAdded_(
const NodeSet& set) {
352 if (_JT_ ==
nullptr) {
353 _is_new_jt_needed_ =
true;
360 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
361 int elim_number = std::numeric_limits< int >::max();
362 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
363 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
364 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
365 elim_order.insert(JT_elim_order[i], (
int)i);
366 NodeSet unobserved_set(set.size());
367 for (
const auto node: set) {
368 if (!_graph_.exists(node)) {
369 if (!_hard_ev_nodes_.contains(node)) {
370 _is_new_jt_needed_ =
true;
374 unobserved_set.insert(node);
375 if (elim_order[node] < elim_number) {
376 elim_number = elim_order[node];
377 first_eliminated_node = node;
382 if (!unobserved_set.empty()) {
386 const auto clique_id = _node_to_clique_[first_eliminated_node];
387 const auto& clique = _JT_->clique(clique_id);
388 for (
const auto node: unobserved_set) {
389 if (!clique.contains(node)) {
390 _is_new_jt_needed_ =
true;
398 template < GUM_Numeric GUM_SCALAR >
399 void LazyPropagation< GUM_SCALAR >::onJointTargetErased_(
const NodeSet& set) {}
402 template < GUM_Numeric GUM_SCALAR >
403 void LazyPropagation< GUM_SCALAR >::onAllMarginalTargetsAdded_() {
404 for (
const auto node: this->BN().internalDag()) {
409 if (!_graph_.exists(node) && !_hard_ev_nodes_.contains(node)) {
410 _is_new_jt_needed_ =
true;
417 template < GUM_Numeric GUM_SCALAR >
418 void LazyPropagation< GUM_SCALAR >::onAllMarginalTargetsErased_() {}
421 template < GUM_Numeric GUM_SCALAR >
422 void LazyPropagation< GUM_SCALAR >::onAllJointTargetsErased_() {}
425 template < GUM_Numeric GUM_SCALAR >
426 void LazyPropagation< GUM_SCALAR >::onAllTargetsErased_() {}
429 template < GUM_Numeric GUM_SCALAR >
430 bool LazyPropagation< GUM_SCALAR >::_isNewJTNeeded_()
const {
433 if ((_JT_ ==
nullptr) || _is_new_jt_needed_)
return true;
441 const auto& hard_ev_nodes = this->hardEvidenceNodes();
442 for (
const auto node: this->targets()) {
443 if (!_graph_.exists(node) && !hard_ev_nodes.exists(node))
return true;
447 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
448 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
449 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
450 elim_order.insert(JT_elim_order[i], (
int)i);
453 for (
const auto& joint_target: this->jointTargets()) {
456 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
457 int elim_number = std::numeric_limits< int >::max();
458 unobserved_set.clear();
459 for (
const auto node: joint_target) {
460 if (!_graph_.exists(node)) {
461 if (!hard_ev_nodes.exists(node))
return true;
463 unobserved_set.insert(node);
464 if (elim_order[node] < elim_number) {
465 elim_number = elim_order[node];
466 first_eliminated_node = node;
470 if (!unobserved_set.empty()) {
474 const auto clique_id = _node_to_clique_[first_eliminated_node];
475 const auto& clique = _JT_->clique(clique_id);
476 for (
const auto node: unobserved_set) {
477 if (!clique.contains(node))
return true;
484 for (
const auto& change: _evidence_changes_) {
485 if ((change.second == EvidenceChangeType::EVIDENCE_ADDED) && !_graph_.exists(change.first))
494 template < GUM_Numeric GUM_SCALAR >
495 void LazyPropagation< GUM_SCALAR >::_createNewJT_() {
511 const auto& bn = this->BN();
513 for (
const auto node: bn.internalDag())
514 _graph_.addNodeWithId(node);
517 NodeSet target_nodes = this->targets();
518 for (
const auto& nodeset: this->jointTargets()) {
519 target_nodes += nodeset;
527 if ((this->nbrTargets() != bn.internalDag().size())
528 && (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES)) {
531 if (target_nodes.size() != bn.size()) {
532 BarrenNodesFinder finder(&bn.internalDag());
533 finder.setTargets(&target_nodes);
535 NodeSet evidence_nodes(this->evidence().size());
536 for (
const auto& pair: this->evidence()) {
537 evidence_nodes.insert(pair.first);
540 finder.setEvidence(&evidence_nodes);
543 NodeSet barren_nodes = finder.barrenNodes();
546 for (
const auto node: barren_nodes) {
547 _graph_.eraseNode(node);
555 if (this->nbrTargets() != bn.internalDag().size()) {
557 bool dsep_analysis =
false;
558 switch (_find_relevant_tensor_type_) {
559 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
560 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES : {
561 BayesBall::requisiteNodes(bn.internalDag(),
563 this->hardEvidenceNodes(),
564 this->softEvidenceNodes(),
566 dsep_analysis =
true;
569 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 : {
570 dSeparationAlgorithm dsep;
571 dsep.requisiteNodes(bn.internalDag(),
573 this->hardEvidenceNodes(),
574 this->softEvidenceNodes(),
576 dsep_analysis =
true;
579 case RelevantTensorsFinderType::FIND_ALL :
break;
586 for (
auto iter = _graph_.beginSafe(); iter != _graph_.endSafe(); ++iter) {
587 if (!requisite_nodes.contains(*iter) && !this->hardEvidenceNodes().contains(*iter)) {
588 _graph_.eraseNode(*iter);
595 for (
const auto node: _graph_) {
596 const NodeSet& parents = bn.parents(node);
597 for (
auto iter1 = parents.cbegin(); iter1 != parents.cend(); ++iter1) {
602 if (_graph_.existsNode(*iter1)) {
603 _graph_.addEdge(*iter1, node);
606 for (++iter2; iter2 != parents.cend(); ++iter2) {
611 if (_graph_.existsNode(*iter2)) _graph_.addEdge(*iter1, *iter2);
620 for (
const auto& nodeset: this->jointTargets()) {
621 for (
auto iter1 = nodeset.cbegin(); iter1 != nodeset.cend(); ++iter1) {
623 for (++iter2; iter2 != nodeset.cend(); ++iter2) {
624 _graph_.addEdge(*iter1, *iter2);
630 _hard_ev_nodes_ = this->hardEvidenceNodes();
631 for (
const auto node: _hard_ev_nodes_) {
632 _graph_.eraseNode(node);
639 if (_JT_ !=
nullptr)
delete _JT_;
640 if (_junctionTree_ !=
nullptr)
delete _junctionTree_;
642 _triangulation_->setGraph(&_graph_, &(this->domainSizes()));
643 const JunctionTree& triang_jt = _triangulation_->junctionTree();
644 if (_use_binary_join_tree_) {
645 BinaryJoinTreeConverterDefault bjt_converter;
647 _JT_ =
new CliqueGraph(bjt_converter.convert(triang_jt, this->domainSizes(), emptyset));
649 _JT_ =
new CliqueGraph(triang_jt);
651 _junctionTree_ =
new CliqueGraph(triang_jt);
656 _node_to_clique_.clear();
657 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
658 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
659 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
660 elim_order.insert(JT_elim_order[i], (
int)i);
661 const DAG& dag = bn.internalDag();
662 for (
const auto node: _graph_) {
664 NodeId first_eliminated_node = node;
665 int elim_number = elim_order[first_eliminated_node];
667 for (
const auto parent: dag.parents(node)) {
668 if (_graph_.existsNode(parent) && (elim_order[parent] < elim_number)) {
669 elim_number = elim_order[parent];
670 first_eliminated_node = parent;
678 _node_to_clique_.insert(node,
679 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
685 for (
const auto node: _hard_ev_nodes_) {
686 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
687 int elim_number = std::numeric_limits< int >::max();
689 for (
const auto parent: dag.parents(node)) {
690 if (_graph_.exists(parent) && (elim_order[parent] < elim_number)) {
691 elim_number = elim_order[parent];
692 first_eliminated_node = parent;
700 if (elim_number != std::numeric_limits< int >::max()) {
701 _node_to_clique_.insert(node,
702 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
706 _joint_target_to_clique_.clear();
707 for (
const auto& set: this->jointTargets()) {
708 NodeId first_eliminated_node = std::numeric_limits< NodeId >::max();
709 int elim_number = std::numeric_limits< int >::max();
713 for (
const auto node: set) {
714 if (!_hard_ev_nodes_.contains(node)) {
717 if (elim_order[node] < elim_number) {
718 elim_number = elim_order[node];
719 first_eliminated_node = node;
724 if (elim_number != std::numeric_limits< int >::max()) {
725 _joint_target_to_clique_.insert(
727 _triangulation_->createdJunctionTreeClique(first_eliminated_node));
732 _computeJoinTreeRoots_();
737 for (
const auto& potlist: _clique_tensors_)
738 for (
const auto pot: potlist.second)
740 _clique_tensors_.clear();
743 for (
const auto& potlist: _arc_to_created_tensors_)
744 for (
const auto pot: potlist.second)
746 _arc_to_created_tensors_.clear();
751 _node_to_hard_ev_projected_CPTs_.clear();
754 _node_to_soft_evidence_.clear();
758 _ScheduleMultiDimSet_ empty_set;
759 for (
const auto node: *_JT_) {
760 _clique_tensors_.insert(node, empty_set);
769 _separator_tensors_.clear();
770 _messages_computed_.clear();
771 for (
const auto& edge: _JT_->edges()) {
772 const Arc arc1(edge.first(), edge.second());
773 _separator_tensors_.insert(arc1, empty_set);
774 _messages_computed_.insert(arc1,
false);
775 const Arc arc2(edge.second(), edge.first());
776 _separator_tensors_.insert(arc2, empty_set);
777 _messages_computed_.insert(arc2,
false);
781 for (
const auto& pot: _target_posteriors_)
783 _target_posteriors_.clear();
784 for (
const auto& pot: _joint_target_posteriors_)
786 _joint_target_posteriors_.clear();
791 double overall_size = 0;
792 for (
const auto clique: *_JT_) {
793 double clique_size = 1.0;
794 for (
const auto node: _JT_->clique(clique))
795 clique_size *= this->domainSizes()[node];
796 overall_size += clique_size;
798 _use_schedules_ = (overall_size > _schedule_threshold_);
804 if (_use_schedules_) {
806 _initializeJTCliques_(schedule);
808 _initializeJTCliques_();
813 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
814 for (
const auto node: this->softEvidenceNodes()) {
815 if (
auto ptr_clique = _node_to_clique_.tryGet(node)) {
816 auto ev_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*evidence[node],
false);
817 _node_to_soft_evidence_.insert(node, ev_pot);
818 _clique_tensors_[*ptr_clique].insert(ev_pot);
824 _evidence_changes_.clear();
825 _is_new_jt_needed_ =
false;
829 template < GUM_Numeric GUM_SCALAR >
830 void LazyPropagation< GUM_SCALAR >::_initializeJTCliques_() {
831 const auto& bn = this->BN();
832 const DAG& dag = bn.internalDag();
838 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
839 const NodeProperty< Idx >& hard_evidence = this->hardEvidence();
841 for (
const auto node: dag) {
842 if (_graph_.exists(node) || _hard_ev_nodes_.contains(node)) {
843 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
847 const auto& variables = cpt.variablesSequence();
848 bool graph_contains_nodes =
false;
849 for (
const auto var: variables) {
850 NodeId xnode = bn.nodeId(*var);
851 if (_hard_ev_nodes_.contains(xnode)) hard_nodes.insert(xnode);
852 else if (_graph_.exists(xnode)) graph_contains_nodes =
true;
858 if (hard_nodes.empty()) {
859 auto sched_cpt =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(cpt,
false);
860 _clique_tensors_[_node_to_clique_[node]].insert(sched_cpt);
866 if (hard_nodes.size() == variables.size()) {
867 Instantiation inst(cpt);
868 for (Size i = 0; i < hard_nodes.size(); ++i) {
869 inst.chgVal(*variables[i], hard_evidence[bn.nodeId(*(variables[i]))]);
871 _constants_.insert(node, cpt.get(inst));
876 if (!graph_contains_nodes)
continue;
880 _TensorSet_ marg_cpt_set(1 + hard_nodes.size());
881 marg_cpt_set.insert(&cpt);
882 for (
const auto xnode: hard_nodes) {
883 marg_cpt_set.insert(evidence[xnode]);
884 hard_variables.
insert(&(bn.variable(xnode)));
888 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
892 _TensorSet_ new_cpt_list = combine_and_project.execute(marg_cpt_set, hard_variables);
895 if (new_cpt_list.size() != 1) {
896 for (
const auto pot: new_cpt_list) {
897 if (!marg_cpt_set.contains(pot))
delete pot;
900 "the projection of a tensor containing " <<
"hard evidence is empty!");
902 auto new_pot =
const_cast< Tensor< GUM_SCALAR >*
>(*(new_cpt_list.begin()));
903 auto projected_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*new_pot));
906 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
907 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
915 template < GUM_Numeric GUM_SCALAR >
916 void LazyPropagation< GUM_SCALAR >::_initializeJTCliques_(Schedule& schedule) {
917 const auto& bn = this->BN();
918 const DAG& dag = bn.internalDag();
924 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence = this->evidence();
925 const NodeProperty< Idx >& hard_evidence = this->hardEvidence();
927 for (
const auto node: dag) {
928 if (_graph_.exists(node) || _hard_ev_nodes_.contains(node)) {
929 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
933 const auto& variables = cpt.variablesSequence();
934 bool graph_contains_nodes =
false;
935 for (
const auto var: variables) {
936 NodeId xnode = bn.nodeId(*var);
937 if (_hard_ev_nodes_.contains(xnode)) hard_nodes.insert(xnode);
938 else if (_graph_.exists(xnode)) graph_contains_nodes =
true;
944 if (hard_nodes.empty()) {
945 auto sched_cpt =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(cpt,
false);
946 _clique_tensors_[_node_to_clique_[node]].insert(sched_cpt);
952 if (hard_nodes.size() == variables.size()) {
953 Instantiation inst(cpt);
954 for (Size i = 0; i < hard_nodes.size(); ++i) {
955 inst.chgVal(*variables[i], hard_evidence[bn.nodeId(*(variables[i]))]);
957 _constants_.insert(node, cpt.get(inst));
962 if (!graph_contains_nodes)
continue;
966 _ScheduleMultiDimSet_ marg_cpt_set(1 + hard_nodes.size());
967 const IScheduleMultiDim* sched_cpt
968 = schedule.insertTable< Tensor< GUM_SCALAR > >(cpt,
false);
969 marg_cpt_set.insert(sched_cpt);
971 for (
const auto xnode: hard_nodes) {
972 const IScheduleMultiDim* pot
973 = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[xnode],
false);
974 marg_cpt_set.insert(pot);
975 hard_variables.
insert(&(bn.variable(xnode)));
979 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
983 _ScheduleMultiDimSet_ new_cpt_list
984 = combine_and_project.schedule(schedule, marg_cpt_set, hard_variables);
987 if (new_cpt_list.size() != 1) {
989 "the projection of a tensor containing " <<
"hard evidence is empty!");
991 auto projected_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
992 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
993 *new_cpt_list.begin()));
994 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(projected_pot))
995 ->makeResultsPersistent(
true);
996 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
997 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1002 this->scheduler().execute(schedule);
1006 template < GUM_Numeric GUM_SCALAR >
1007 void LazyPropagation< GUM_SCALAR >::updateOutdatedStructure_() {
1009 if (_isNewJTNeeded_()) {
1015 updateOutdatedTensors_();
1020 template < GUM_Numeric GUM_SCALAR >
1021 void LazyPropagation< GUM_SCALAR >::_diffuseMessageInvalidations_(NodeId from_id,
1023 NodeSet& invalidated_cliques) {
1025 invalidated_cliques.insert(to_id);
1028 const Arc arc(from_id, to_id);
1029 bool& message_computed = _messages_computed_[arc];
1030 if (message_computed) {
1031 message_computed =
false;
1032 _separator_tensors_[arc].clear();
1033 if (_arc_to_created_tensors_.exists(arc)) {
1034 _ScheduleMultiDimSet_& arc_created_tensors = _arc_to_created_tensors_[arc];
1035 for (
const auto pot: arc_created_tensors)
1037 arc_created_tensors.clear();
1041 for (
const auto node_id: _JT_->neighbours(to_id)) {
1042 if (node_id != from_id) _diffuseMessageInvalidations_(to_id, node_id, invalidated_cliques);
1049 template < GUM_Numeric GUM_SCALAR >
1050 void LazyPropagation< GUM_SCALAR >::updateOutdatedTensors_() {
1060 NodeSet hard_nodes_changed(_hard_ev_nodes_.size());
1061 for (
const auto node: _hard_ev_nodes_)
1062 if (_evidence_changes_.exists(node)) hard_nodes_changed.insert(node);
1064 NodeSet nodes_with_projected_CPTs_changed;
1065 const auto& bn = this->BN();
1066 for (
auto pot_iter = _node_to_hard_ev_projected_CPTs_.beginSafe();
1067 pot_iter != _node_to_hard_ev_projected_CPTs_.endSafe();
1069 for (
const auto var: bn.cpt(pot_iter.key()).variablesSequence()) {
1070 if (hard_nodes_changed.contains(bn.nodeId(*var))) {
1071 nodes_with_projected_CPTs_changed.insert(pot_iter.key());
1072 delete pot_iter.val();
1073 _clique_tensors_[_node_to_clique_[pot_iter.key()]].erase(pot_iter.val());
1074 _node_to_hard_ev_projected_CPTs_.erase(pot_iter);
1088 NodeSet invalidated_cliques(_JT_->size());
1089 for (
const auto& pair: _evidence_changes_) {
1090 if (
auto ptr_clique = _node_to_clique_.tryGet(pair.first)) {
1091 const auto clique = *ptr_clique;
1092 invalidated_cliques.insert(clique);
1093 for (
const auto neighbor: _JT_->neighbours(clique)) {
1094 _diffuseMessageInvalidations_(clique, neighbor, invalidated_cliques);
1101 for (
const auto node: nodes_with_projected_CPTs_changed) {
1102 const auto clique = _node_to_clique_[node];
1103 invalidated_cliques.insert(clique);
1104 for (
const auto neighbor: _JT_->neighbours(clique)) {
1105 _diffuseMessageInvalidations_(clique, neighbor, invalidated_cliques);
1114 for (
auto iter = _target_posteriors_.beginSafe(); iter != _target_posteriors_.endSafe();
1116 if (_graph_.exists(iter.key())
1117 && (invalidated_cliques.exists(_node_to_clique_[iter.key()]))) {
1119 _target_posteriors_.erase(iter);
1124 for (
auto iter = _target_posteriors_.beginSafe(); iter != _target_posteriors_.endSafe();
1126 if (hard_nodes_changed.contains(iter.key())) {
1128 _target_posteriors_.erase(iter);
1134 for (
auto iter = _joint_target_posteriors_.beginSafe();
1135 iter != _joint_target_posteriors_.endSafe();
1139 const auto clique = _joint_target_to_clique_.tryGet(iter.key());
1140 if (!clique || invalidated_cliques.exists(*clique)) {
1142 _joint_target_posteriors_.erase(iter);
1145 bool has_unevidenced_node =
false;
1146 for (
const auto node: iter.key()) {
1147 if (!hard_nodes_changed.exists(node)) {
1148 has_unevidenced_node =
true;
1152 if (!has_unevidenced_node) {
1154 _joint_target_posteriors_.erase(iter);
1161 for (
const auto& pot_pair: _node_to_soft_evidence_) {
1162 delete pot_pair.second;
1163 _clique_tensors_[_node_to_clique_[pot_pair.first]].erase(pot_pair.second);
1165 _node_to_soft_evidence_.clear();
1167 const auto& evidence = this->evidence();
1168 for (
const auto node: this->softEvidenceNodes()) {
1169 auto ev_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*evidence[node],
false);
1170 _node_to_soft_evidence_.insert(node, ev_pot);
1171 _clique_tensors_[_node_to_clique_[node]].insert(ev_pot);
1181 if (_use_schedules_) {
1183 for (
const auto node: nodes_with_projected_CPTs_changed) {
1185 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
1186 const auto& variables = cpt.variablesSequence();
1187 _ScheduleMultiDimSet_ marg_cpt_set;
1188 const auto sched_cpt = schedule.insertTable< Tensor< GUM_SCALAR > >(cpt,
false);
1189 marg_cpt_set.insert(sched_cpt);
1192 for (
const auto var: variables) {
1193 NodeId xnode = bn.nodeId(*var);
1194 if (_hard_ev_nodes_.exists(xnode)) {
1195 const auto pot = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[xnode],
false);
1196 marg_cpt_set.insert(pot);
1197 hard_variables.
insert(var);
1202 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
1206 _ScheduleMultiDimSet_ new_cpt_list
1207 = combine_and_project.schedule(schedule, marg_cpt_set, hard_variables);
1210 if (new_cpt_list.size() != 1) {
1212 "the projection of a tensor containing " <<
"hard evidence is empty!");
1214 auto projected_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1215 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*new_cpt_list.begin()));
1216 const_cast< ScheduleOperator*
>(schedule.scheduleMultiDimCreator(projected_pot))
1217 ->makeResultsPersistent(
true);
1218 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
1219 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1221 this->scheduler().execute(schedule);
1223 for (
const auto node: nodes_with_projected_CPTs_changed) {
1225 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node);
1226 const auto& variables = cpt.variablesSequence();
1227 _TensorSet_ marg_cpt_set(1 + variables.size());
1228 marg_cpt_set.insert(&cpt);
1231 for (
const auto var: variables) {
1232 NodeId xnode = bn.nodeId(*var);
1233 if (_hard_ev_nodes_.exists(xnode)) {
1234 marg_cpt_set.insert(evidence[xnode]);
1235 hard_variables.
insert(var);
1240 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(
1244 _TensorSet_ new_cpt_list = combine_and_project.execute(marg_cpt_set, hard_variables);
1247 if (new_cpt_list.size() != 1) {
1249 "the projection of a tensor containing " <<
"hard evidence is empty!");
1251 Tensor< GUM_SCALAR >* sched_pot
1252 =
const_cast< Tensor< GUM_SCALAR >*
>(*new_cpt_list.begin());
1253 auto projected_pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(std::move(*sched_pot));
1255 _clique_tensors_[_node_to_clique_[node]].insert(projected_pot);
1256 _node_to_hard_ev_projected_CPTs_.insert(node, projected_pot);
1262 const auto& hard_evidence = this->hardEvidence();
1263 for (
auto& node_cst: _constants_) {
1264 const Tensor< GUM_SCALAR >& cpt = bn.cpt(node_cst.first);
1265 const auto& variables = cpt.variablesSequence();
1266 Instantiation inst(cpt);
1267 for (
const auto var: variables) {
1268 inst.chgVal(*var, hard_evidence[bn.nodeId(*var)]);
1270 node_cst.second = cpt.get(inst);
1274 _evidence_changes_.clear();
1278 template < GUM_Numeric GUM_SCALAR >
1279 void LazyPropagation< GUM_SCALAR >::_computeJoinTreeRoots_() {
1284 for (
const auto node: this->targets()) {
1285 if (
auto ptr_clique = _node_to_clique_.tryGet(node)) { clique_targets.insert(*ptr_clique); }
1287 for (
const auto& set: this->jointTargets()) {
1288 if (
auto ptr_clique = _joint_target_to_clique_.tryGet(set)) {
1289 clique_targets.insert(*ptr_clique);
1299 template < GUM_Numeric GUM_SCALAR >
1300 void LazyPropagation< GUM_SCALAR >::_findRelevantTensorsGetAll_(
1301 Set< const IScheduleMultiDim* >& pot_list,
1305 template < GUM_Numeric GUM_SCALAR >
1306 void LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation_(
1307 Set< const IScheduleMultiDim* >& pot_list,
1311 const auto& bn = this->BN();
1312 for (
const auto var: kept_vars) {
1313 kept_ids.insert(bn.nodeId(*var));
1318 BayesBall::requisiteNodes(bn.internalDag(),
1320 this->hardEvidenceNodes(),
1321 this->softEvidenceNodes(),
1323 for (
auto iter = pot_list.beginSafe(); iter != pot_list.endSafe(); ++iter) {
1324 const Sequence< const DiscreteVariable* >& vars = (*iter)->variablesSequence();
1326 for (
const auto var: vars) {
1327 if (requisite_nodes.exists(bn.nodeId(*var))) {
1333 if (!found) { pot_list.erase(iter); }
1338 template < GUM_Numeric GUM_SCALAR >
1339 void LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation2_(
1340 Set< const IScheduleMultiDim* >& pot_list,
1344 const auto& bn = this->BN();
1345 for (
const auto var: kept_vars) {
1346 kept_ids.insert(bn.nodeId(*var));
1350 BayesBall::relevantTensors(bn,
1352 this->hardEvidenceNodes(),
1353 this->softEvidenceNodes(),
1358 template < GUM_Numeric GUM_SCALAR >
1359 void LazyPropagation< GUM_SCALAR >::_findRelevantTensorsWithdSeparation3_(
1360 Set< const IScheduleMultiDim* >& pot_list,
1364 const auto& bn = this->BN();
1365 for (
const auto var: kept_vars) {
1366 kept_ids.insert(bn.nodeId(*var));
1370 dSeparationAlgorithm dsep;
1371 dsep.relevantTensors(bn,
1373 this->hardEvidenceNodes(),
1374 this->softEvidenceNodes(),
1379 template < GUM_Numeric GUM_SCALAR >
1380 void LazyPropagation< GUM_SCALAR >::_findRelevantTensorsXX_(
1381 Set< const IScheduleMultiDim* >& pot_list,
1383 switch (_find_relevant_tensor_type_) {
1384 case RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS :
1385 _findRelevantTensorsWithdSeparation2_(pot_list, kept_vars);
1388 case RelevantTensorsFinderType::DSEP_BAYESBALL_NODES :
1389 _findRelevantTensorsWithdSeparation_(pot_list, kept_vars);
1392 case RelevantTensorsFinderType::DSEP_KOLLER_FRIEDMAN_2009 :
1393 _findRelevantTensorsWithdSeparation3_(pot_list, kept_vars);
1396 case RelevantTensorsFinderType::FIND_ALL :
1397 _findRelevantTensorsGetAll_(pot_list, kept_vars);
1405 template < GUM_Numeric GUM_SCALAR >
1406 Set< const IScheduleMultiDim* >
1407 LazyPropagation< GUM_SCALAR >::_removeBarrenVariables_(Schedule& schedule,
1408 _ScheduleMultiDimSet_& pot_list,
1413 for (
auto iter = the_del_vars.
beginSafe(); iter != the_del_vars.
endSafe(); ++iter) {
1414 NodeId
id = this->BN().nodeId(**iter);
1415 if (this->hardEvidenceNodes().exists(
id) || this->softEvidenceNodes().exists(
id)) {
1416 the_del_vars.
erase(iter);
1421 HashTable< const DiscreteVariable*, _ScheduleMultiDimSet_ > var2pots(the_del_vars.
size());
1422 _ScheduleMultiDimSet_ empty_pot_set;
1423 for (
const auto pot: pot_list) {
1424 const auto& vars = pot->variablesSequence();
1425 for (
const auto var: vars) {
1426 if (the_del_vars.
exists(var)) {
1427 if (!var2pots.exists(var)) { var2pots.insert(var, empty_pot_set); }
1428 var2pots[var].insert(pot);
1435 HashTable< const IScheduleMultiDim*, gum::VariableSet > pot2barren_var;
1437 for (
const auto& elt: var2pots) {
1438 if (elt.second.size() == 1) {
1439 const IScheduleMultiDim* pot = *(elt.second.begin());
1440 if (!pot2barren_var.exists(pot)) { pot2barren_var.insert(pot, empty_var_set); }
1441 pot2barren_var[pot].insert(elt.first);
1448 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1449 _ScheduleMultiDimSet_ projected_pots;
1450 for (
const auto& elt: pot2barren_var) {
1452 const IScheduleMultiDim* pot = elt.first;
1453 pot_list.erase(pot);
1457 if (pot->variablesSequence().size() != elt.second.size()) {
1458 const IScheduleMultiDim* new_pot = projector.schedule(schedule, pot, elt.second);
1462 pot_list.insert(new_pot);
1463 projected_pots.insert(new_pot);
1467 return projected_pots;
1471 template < GUM_Numeric GUM_SCALAR >
1472 Set< const Tensor< GUM_SCALAR >* >
1473 LazyPropagation< GUM_SCALAR >::_removeBarrenVariables_(_TensorSet_& pot_list,
1478 for (
auto iter = the_del_vars.
beginSafe(); iter != the_del_vars.
endSafe(); ++iter) {
1479 NodeId
id = this->BN().nodeId(**iter);
1480 if (this->hardEvidenceNodes().exists(
id) || this->softEvidenceNodes().exists(
id)) {
1481 the_del_vars.
erase(iter);
1486 HashTable< const DiscreteVariable*, _TensorSet_ > var2pots;
1487 _TensorSet_ empty_pot_set;
1488 for (
const auto pot: pot_list) {
1489 const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
1490 for (
const auto var: vars) {
1491 if (the_del_vars.
exists(var)) {
1492 if (!var2pots.exists(var)) { var2pots.insert(var, empty_pot_set); }
1493 var2pots[var].insert(pot);
1502 for (
const auto& elt: var2pots) {
1503 if (elt.second.size() == 1) {
1504 const Tensor< GUM_SCALAR >* pot = *(elt.second.begin());
1505 if (!pot2barren_var.exists(pot)) { pot2barren_var.insert(pot, empty_var_set); }
1506 pot2barren_var[pot].insert(elt.first);
1513 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1514 _TensorSet_ projected_pots;
1515 for (
const auto& elt: pot2barren_var) {
1517 const Tensor< GUM_SCALAR >* pot = elt.first;
1518 pot_list.erase(pot);
1522 if (pot->variablesSequence().size() != elt.second.size()) {
1523 const Tensor< GUM_SCALAR >* new_pot = projector.execute(*pot, elt.second);
1524 pot_list.insert(new_pot);
1525 projected_pots.insert(new_pot);
1529 return projected_pots;
1533 template < GUM_Numeric GUM_SCALAR >
1534 void LazyPropagation< GUM_SCALAR >::_collectMessage_(Schedule& schedule, NodeId
id, NodeId from) {
1535 for (
const auto other: _JT_->neighbours(
id)) {
1536 if ((other != from) && !_messages_computed_[
Arc(other,
id)])
1537 _collectMessage_(schedule, other,
id);
1540 if ((
id != from) && !_messages_computed_[
Arc(
id, from)]) {
1541 _produceMessage_(schedule,
id, from);
1546 template < GUM_Numeric GUM_SCALAR >
1547 void LazyPropagation< GUM_SCALAR >::_collectMessage_(NodeId
id, NodeId from) {
1548 for (
const auto other: _JT_->neighbours(
id)) {
1549 if ((other != from) && !_messages_computed_[
Arc(other,
id)]) _collectMessage_(other,
id);
1552 if ((
id != from) && !_messages_computed_[
Arc(
id, from)]) { _produceMessage_(
id, from); }
1556 template < GUM_Numeric GUM_SCALAR >
1557 Set< const IScheduleMultiDim* >
1558 LazyPropagation< GUM_SCALAR >::_marginalizeOut_(Schedule& schedule,
1559 Set< const IScheduleMultiDim* > pot_list,
1566 if (pot_list.empty()) {
return _ScheduleMultiDimSet_(); }
1570 for (
const auto pot: pot_list) {
1571 if (!schedule.existsScheduleMultiDim(pot->id())) schedule.emplaceScheduleMultiDim(*pot);
1576 _ScheduleMultiDimSet_ barren_projected_tensors;
1577 if (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES) {
1578 barren_projected_tensors = _removeBarrenVariables_(schedule, pot_list, del_vars);
1582 _ScheduleMultiDimSet_ new_pot_list;
1583 if (pot_list.size() == 1) {
1584 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1585 auto xpot = projector.schedule(schedule, *(pot_list.begin()), del_vars);
1586 new_pot_list.insert(xpot);
1587 }
else if (pot_list.size() > 1) {
1590 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(_combination_op_,
1592 new_pot_list = combine_and_project.schedule(schedule, pot_list, del_vars);
1598 for (
auto barren_pot: barren_projected_tensors) {
1599 if (!new_pot_list.exists(barren_pot))
1600 schedule.emplaceDeletion(
1601 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>& >(*barren_pot));
1604 return new_pot_list;
1608 template < GUM_Numeric GUM_SCALAR >
1609 Set< const IScheduleMultiDim* >
1610 LazyPropagation< GUM_SCALAR >::_marginalizeOut_(Set< const IScheduleMultiDim* >& pot_list,
1614 if (pot_list.empty()) {
return _ScheduleMultiDimSet_(); }
1616 _TensorSet_ xpot_list(pot_list.size());
1617 for (
auto pot: pot_list)
1619 &(
static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot)->multiDim()));
1626 _TensorSet_ barren_projected_tensors;
1627 if (_barren_nodes_type_ == FindBarrenNodesType::FIND_BARREN_NODES) {
1628 barren_projected_tensors = _removeBarrenVariables_(xpot_list, del_vars);
1632 _TensorSet_ xnew_pot_list;
1633 _ScheduleMultiDimSet_ new_pot_list;
1634 if (xpot_list.size() == 1) {
1635 MultiDimProjection< Tensor< GUM_SCALAR > > projector(_projection_op_);
1636 auto xpot = projector.execute(**(xpot_list.begin()), del_vars);
1637 ScheduleMultiDim< Tensor< GUM_SCALAR > >* pot;
1638 if (xpot_list.contains(xpot))
1639 pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*xpot,
false);
1641 pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(
1642 std::move(
const_cast< Tensor< GUM_SCALAR >&
>(*xpot)));
1645 new_pot_list.insert(pot);
1646 }
else if (xpot_list.size() > 1) {
1649 MultiDimCombineAndProjectDefault< Tensor< GUM_SCALAR > > combine_and_project(_combination_op_,
1651 xnew_pot_list = combine_and_project.execute(xpot_list, del_vars);
1653 for (
auto xpot: xnew_pot_list) {
1654 ScheduleMultiDim< Tensor< GUM_SCALAR > >* pot;
1655 if (xpot_list.contains(xpot))
1656 pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(*xpot,
false);
1658 pot =
new ScheduleMultiDim< Tensor< GUM_SCALAR > >(
1659 std::move(
const_cast< Tensor< GUM_SCALAR >&
>(*xpot)));
1662 new_pot_list.insert(pot);
1669 for (
const auto barren_pot: barren_projected_tensors) {
1670 if (!xnew_pot_list.exists(barren_pot))
delete barren_pot;
1673 return new_pot_list;
1677 template < GUM_Numeric GUM_SCALAR >
1678 void LazyPropagation< GUM_SCALAR >::_produceMessage_(Schedule& schedule,
1682 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[from_id];
1685 for (
const auto other_id: _JT_->neighbours(from_id)) {
1686 if (other_id != to_id) pot_list += _separator_tensors_[
Arc(other_id, from_id)];
1690 const NodeSet& from_clique = _JT_->clique(from_id);
1691 const NodeSet& separator = _JT_->separator(from_id, to_id);
1694 const auto& bn = this->BN();
1696 for (
const auto node: from_clique) {
1697 if (!separator.contains(node)) {
1698 del_vars.
insert(&(bn.variable(node)));
1700 kept_vars.
insert(&(bn.variable(node)));
1706 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(schedule, pot_list, del_vars, kept_vars);
1713 const Arc arc(from_id, to_id);
1715 if (!_arc_to_created_tensors_.exists(arc))
1716 _arc_to_created_tensors_.insert(arc, _ScheduleMultiDimSet_());
1718 for (
auto iter = new_pot_list.beginSafe(); iter != new_pot_list.endSafe(); ++iter) {
1719 const auto pot = *iter;
1721 if (!pot_list.exists(pot)) {
1722 _arc_to_created_tensors_[arc].insert(pot);
1725 auto op = schedule.scheduleMultiDimCreator(pot);
1726 if (op !=
nullptr)
const_cast< ScheduleOperator*
>(op)->makeResultsPersistent(
true);
1730 _separator_tensors_[arc] = std::move(new_pot_list);
1731 _messages_computed_[arc] =
true;
1735 template < GUM_Numeric GUM_SCALAR >
1736 void LazyPropagation< GUM_SCALAR >::_produceMessage_(NodeId from_id, NodeId to_id) {
1738 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[from_id];
1741 for (
const auto other_id: _JT_->neighbours(from_id)) {
1742 if (other_id != to_id) pot_list += _separator_tensors_[
Arc(other_id, from_id)];
1746 const NodeSet& from_clique = _JT_->clique(from_id);
1747 const NodeSet& separator = _JT_->separator(from_id, to_id);
1750 const auto& bn = this->BN();
1752 for (
const auto node: from_clique) {
1753 if (!separator.contains(node)) {
1754 del_vars.
insert(&(bn.variable(node)));
1756 kept_vars.
insert(&(bn.variable(node)));
1762 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(pot_list, del_vars, kept_vars);
1769 const Arc arc(from_id, to_id);
1771 if (!_arc_to_created_tensors_.exists(arc))
1772 _arc_to_created_tensors_.insert(arc, _ScheduleMultiDimSet_());
1774 for (
const auto pot: new_pot_list) {
1775 if (!pot_list.exists(pot)) { _arc_to_created_tensors_[arc].insert(pot); }
1778 _separator_tensors_[arc] = std::move(new_pot_list);
1779 _messages_computed_[arc] =
true;
1783 template < GUM_Numeric GUM_SCALAR >
1784 void LazyPropagation< GUM_SCALAR >::makeInference_() {
1785 if (_use_schedules_) {
1789 for (
const auto node: this->targets()) {
1793 if (_graph_.exists(node)) {
1794 _collectMessage_(schedule, _node_to_clique_[node], _node_to_clique_[node]);
1802 for (
const auto& set: _joint_target_to_clique_)
1803 _collectMessage_(schedule, set.second, set.second);
1806 this->scheduler().execute(schedule);
1809 for (
const auto node: this->targets()) {
1813 if (_graph_.exists(node)) {
1814 _collectMessage_(_node_to_clique_[node], _node_to_clique_[node]);
1822 for (
const auto& set: _joint_target_to_clique_)
1823 _collectMessage_(set.second, set.second);
1828 template < GUM_Numeric GUM_SCALAR >
1829 Tensor< GUM_SCALAR >* LazyPropagation< GUM_SCALAR >::unnormalizedJointPosterior_(NodeId
id) {
1830 if (_use_schedules_) {
1832 return _unnormalizedJointPosterior_(schedule,
id);
1834 return _unnormalizedJointPosterior_(
id);
1839 template < GUM_Numeric GUM_SCALAR >
1840 Tensor< GUM_SCALAR >*
1841 LazyPropagation< GUM_SCALAR >::_unnormalizedJointPosterior_(Schedule& schedule, NodeId
id) {
1842 const auto& bn = this->BN();
1846 if (this->hardEvidenceNodes().
contains(
id)) {
1847 return new Tensor< GUM_SCALAR >(*(this->evidence()[
id]));
1850 auto& scheduler = this->scheduler();
1854 const NodeId clique_of_id = _node_to_clique_[id];
1855 _collectMessage_(schedule, clique_of_id, clique_of_id);
1860 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[clique_of_id];
1863 for (
const auto other: _JT_->neighbours(clique_of_id))
1864 pot_list += _separator_tensors_[
Arc(other, clique_of_id)];
1867 const NodeSet& nodes = _JT_->clique(clique_of_id);
1870 for (
const auto node: nodes) {
1871 if (node !=
id) del_vars.
insert(&(bn.variable(node)));
1876 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(schedule, pot_list, del_vars, kept_vars);
1877 Tensor< GUM_SCALAR >* joint =
nullptr;
1878 ScheduleMultiDim< Tensor< GUM_SCALAR > >* resulting_pot =
nullptr;
1880 if (new_pot_list.size() == 0) {
1881 joint =
new Tensor< GUM_SCALAR >;
1882 for (
const auto var: kept_vars)
1885 if (new_pot_list.size() == 1) {
1886 scheduler.execute(schedule);
1887 resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1888 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*new_pot_list.begin()));
1890 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1891 const IScheduleMultiDim* pot = fast_combination.schedule(schedule, new_pot_list);
1892 scheduler.execute(schedule);
1893 resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1894 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot));
1899 if (pot_list.exists(resulting_pot)) {
1900 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
1902 joint = resulting_pot->exportMultiDim();
1909 bool nonzero_found =
false;
1910 for (Instantiation inst(*joint); !inst.end(); ++inst) {
1911 if (joint->get(inst)) {
1912 nonzero_found =
true;
1916 if (!nonzero_found) {
1920 "some evidence entered into the Bayes "
1921 "net are incompatible (their joint proba = 0)");
1927 template < GUM_Numeric GUM_SCALAR >
1928 Tensor< GUM_SCALAR >* LazyPropagation< GUM_SCALAR >::_unnormalizedJointPosterior_(NodeId
id) {
1929 const auto& bn = this->BN();
1933 if (this->hardEvidenceNodes().
contains(
id)) {
1934 return new Tensor< GUM_SCALAR >(*(this->evidence()[
id]));
1939 NodeId clique_of_id = _node_to_clique_[id];
1940 _collectMessage_(clique_of_id, clique_of_id);
1945 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[clique_of_id];
1948 for (
const auto other: _JT_->neighbours(clique_of_id))
1949 pot_list += _separator_tensors_[
Arc(other, clique_of_id)];
1952 const NodeSet& nodes = _JT_->clique(clique_of_id);
1955 for (
const auto node: nodes) {
1956 if (node !=
id) del_vars.
insert(&(bn.variable(node)));
1961 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(pot_list, del_vars, kept_vars);
1962 Tensor< GUM_SCALAR >* joint =
nullptr;
1964 if (new_pot_list.size() == 0) {
1965 joint =
new Tensor< GUM_SCALAR >;
1966 for (
const auto var: kept_vars)
1968 }
else if (new_pot_list.size() == 1) {
1969 auto sched_joint =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
1970 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*(new_pot_list.begin())));
1974 if (pot_list.exists(sched_joint)) {
1975 joint =
new Tensor< GUM_SCALAR >(sched_joint->multiDim());
1977 joint = sched_joint->exportMultiDim();
1982 new_pot_list.clear();
1985 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
1988 _TensorSet_ xnew_pot_list(new_pot_list.size());
1989 for (
auto xpot: new_pot_list) {
1990 xnew_pot_list.insert(
1991 &(
static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(xpot)->multiDim()));
1994 joint = fast_combination.execute(xnew_pot_list);
2000 for (
const auto pot: new_pot_list)
2001 if (!pot_list.exists(pot))
delete pot;
2006 bool nonzero_found =
false;
2007 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2008 if (joint->get(inst)) {
2009 nonzero_found =
true;
2013 if (!nonzero_found) {
2017 "some evidence entered into the Bayes "
2018 "net are incompatible (their joint proba = 0)");
2024 template < GUM_Numeric GUM_SCALAR >
2025 const Tensor< GUM_SCALAR >& LazyPropagation< GUM_SCALAR >::posterior_(NodeId
id) {
2027 if (
auto p = _target_posteriors_.tryGet(
id)) {
return *(*p); }
2030 auto joint = unnormalizedJointPosterior_(
id);
2031 if (joint->sum() != 1)
2033 _target_posteriors_.insert(
id, joint);
2039 template < GUM_Numeric GUM_SCALAR >
2040 Tensor< GUM_SCALAR >*
2041 LazyPropagation< GUM_SCALAR >::unnormalizedJointPosterior_(
const NodeSet& set) {
2042 if (_use_schedules_) {
2044 return _unnormalizedJointPosterior_(schedule, set);
2046 return _unnormalizedJointPosterior_(set);
2051 template < GUM_Numeric GUM_SCALAR >
2052 Tensor< GUM_SCALAR >*
2053 LazyPropagation< GUM_SCALAR >::_unnormalizedJointPosterior_(Schedule& schedule,
2054 const NodeSet& set) {
2057 NodeSet targets = set, hard_ev_nodes;
2058 for (
const auto node: this->hardEvidenceNodes()) {
2059 if (targets.contains(node)) {
2060 targets.erase(node);
2061 hard_ev_nodes.insert(node);
2065 auto& scheduler = this->scheduler();
2069 const auto& evidence = this->evidence();
2070 if (targets.empty()) {
2071 if (set.size() == 1) {
2072 return new Tensor< GUM_SCALAR >(*evidence[*set.begin()]);
2074 _ScheduleMultiDimSet_ pot_list;
2075 for (
const auto node: set) {
2076 auto new_pot_ev = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[node],
false);
2077 pot_list.insert(new_pot_ev);
2081 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2082 const IScheduleMultiDim* pot = fast_combination.schedule(schedule, pot_list);
2083 auto schedule_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2084 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot));
2085 scheduler.execute(schedule);
2086 auto result = schedule_pot->exportMultiDim();
2096 NodeId clique_of_set;
2097 if (
auto p_clique = _joint_target_to_clique_.tryGet(set)) {
2098 clique_of_set = *p_clique;
2105 for (
const auto node: targets) {
2106 if (!_graph_.exists(node)) {
2108 "The variable " << this->BN().variable(node).name() <<
"(" << node
2109 <<
") does not belong to this optimized inference.")
2115 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
2117 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
2118 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
2119 elim_order.insert(JT_elim_order[i], (
int)i);
2120 NodeId first_eliminated_node = *(targets.begin());
2121 int elim_number = elim_order[first_eliminated_node];
2122 for (
const auto node: targets) {
2123 if (elim_order[node] < elim_number) {
2124 elim_number = elim_order[node];
2125 first_eliminated_node = node;
2129 clique_of_set = _triangulation_->createdJunctionTreeClique(first_eliminated_node);
2133 const NodeSet& clique_nodes = _JT_->clique(clique_of_set);
2134 for (
const auto node: targets) {
2135 if (!clique_nodes.contains(node)) {
2137 this->BN().names(set) <<
"(" << set <<
")"
2138 <<
" is not addressable in this optimized inference.")
2143 _joint_target_to_clique_.
insert(set, clique_of_set);
2147 _collectMessage_(schedule, clique_of_set, clique_of_set);
2152 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[clique_of_set];
2155 for (
const auto other: _JT_->neighbours(clique_of_set))
2156 pot_list += _separator_tensors_[
Arc(other, clique_of_set)];
2159 const NodeSet& nodes = _JT_->clique(clique_of_set);
2162 const auto& bn = this->BN();
2163 for (
const auto node: nodes) {
2164 if (!targets.contains(node)) {
2165 del_vars.
insert(&(bn.variable(node)));
2167 kept_vars.
insert(&(bn.variable(node)));
2173 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(schedule, pot_list, del_vars, kept_vars);
2174 ScheduleMultiDim< Tensor< GUM_SCALAR > >* resulting_pot =
nullptr;
2175 Tensor< GUM_SCALAR >* joint =
nullptr;
2177 if (new_pot_list.size() == 0) {
2178 joint =
new Tensor< GUM_SCALAR >();
2179 for (
const auto var: kept_vars)
2182 if ((new_pot_list.size() == 1) && hard_ev_nodes.empty()) {
2183 scheduler.execute(schedule);
2184 resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2185 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*new_pot_list.begin()));
2189 for (
const auto node: hard_ev_nodes) {
2190 auto new_pot_ev = schedule.insertTable< Tensor< GUM_SCALAR > >(*evidence[node],
false);
2191 new_pot_list.insert(new_pot_ev);
2193 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2194 const auto pot = fast_combination.schedule(schedule, new_pot_list);
2195 scheduler.execute(schedule);
2196 resulting_pot =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2197 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(pot));
2202 if (pot_list.exists(resulting_pot)) {
2203 joint =
new Tensor< GUM_SCALAR >(resulting_pot->multiDim());
2205 joint = resulting_pot->exportMultiDim();
2212 bool nonzero_found =
false;
2213 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2214 if ((*joint)[inst]) {
2215 nonzero_found =
true;
2219 if (!nonzero_found) {
2223 "some evidence entered into the Bayes "
2224 "net are incompatible (their joint proba = 0)");
2231 template < GUM_Numeric GUM_SCALAR >
2232 Tensor< GUM_SCALAR >*
2233 LazyPropagation< GUM_SCALAR >::_unnormalizedJointPosterior_(
const NodeSet& set) {
2238 for (
const auto node: this->hardEvidenceNodes()) {
2239 if (targets.contains(node)) {
2240 targets.erase(node);
2241 hard_ev_nodes.insert(node);
2247 const auto& evidence = this->evidence();
2248 if (targets.empty()) {
2249 if (set.size() == 1) {
2250 return new Tensor< GUM_SCALAR >(*evidence[*set.begin()]);
2252 _TensorSet_ pot_list;
2253 for (
const auto node: set) {
2254 pot_list.insert(evidence[node]);
2258 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2259 const Tensor< GUM_SCALAR >* pot = fast_combination.execute(pot_list);
2261 return const_cast< Tensor< GUM_SCALAR >*
>(pot);
2269 NodeId clique_of_set;
2270 if (
auto p_clique = _joint_target_to_clique_.tryGet(set)) {
2271 clique_of_set = *p_clique;
2278 for (
const auto node: targets) {
2279 if (!_graph_.exists(node)) {
2281 "The variable " << this->BN().variable(node).name() <<
"(" << node
2282 <<
") does not belong to this optimized inference.")
2288 const std::vector< NodeId >& JT_elim_order = _triangulation_->eliminationOrder();
2290 NodeProperty< int > elim_order(Size(JT_elim_order.size()));
2291 for (std::size_t i = std::size_t(0), size = JT_elim_order.size(); i < size; ++i)
2292 elim_order.insert(JT_elim_order[i], (
int)i);
2293 NodeId first_eliminated_node = *(targets.begin());
2294 int elim_number = elim_order[first_eliminated_node];
2295 for (
const auto node: targets) {
2296 if (elim_order[node] < elim_number) {
2297 elim_number = elim_order[node];
2298 first_eliminated_node = node;
2302 clique_of_set = _triangulation_->createdJunctionTreeClique(first_eliminated_node);
2306 const NodeSet& clique_nodes = _JT_->clique(clique_of_set);
2307 for (
const auto node: targets) {
2308 if (!clique_nodes.contains(node)) {
2310 this->BN().names(set) <<
"(" << set <<
")"
2311 <<
" is not addressable in this optimized inference.")
2316 _joint_target_to_clique_.
insert(set, clique_of_set);
2320 _collectMessage_(clique_of_set, clique_of_set);
2325 _ScheduleMultiDimSet_ pot_list = _clique_tensors_[clique_of_set];
2328 for (
const auto other: _JT_->neighbours(clique_of_set))
2329 pot_list += _separator_tensors_[
Arc(other, clique_of_set)];
2332 const NodeSet& nodes = _JT_->clique(clique_of_set);
2335 const auto& bn = this->BN();
2336 for (
const auto node: nodes) {
2337 if (!targets.contains(node)) {
2338 del_vars.
insert(&(bn.variable(node)));
2340 kept_vars.
insert(&(bn.variable(node)));
2346 _ScheduleMultiDimSet_ new_pot_list = _marginalizeOut_(pot_list, del_vars, kept_vars);
2347 Tensor< GUM_SCALAR >* joint =
nullptr;
2348 if (new_pot_list.empty()) {
2349 joint =
new Tensor< GUM_SCALAR >();
2350 for (
const auto var: kept_vars)
2353 if ((new_pot_list.size() == 1) && hard_ev_nodes.empty()) {
2354 auto sched_joint =
const_cast< ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(
2355 static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(*new_pot_list.begin()));
2359 if (pot_list.exists(sched_joint)) {
2360 joint =
new Tensor< GUM_SCALAR >(sched_joint->multiDim());
2362 joint = sched_joint->exportMultiDim();
2367 new_pot_list.clear();
2372 _TensorSet_ xnew_pot_list(new_pot_list.size());
2373 for (
auto xpot: new_pot_list) {
2374 xnew_pot_list.insert(
2375 &(
static_cast< const ScheduleMultiDim< Tensor< GUM_SCALAR >
>* >(xpot)->multiDim()));
2377 for (
const auto node: hard_ev_nodes) {
2378 xnew_pot_list.insert(evidence[node]);
2380 MultiDimCombinationDefault< Tensor< GUM_SCALAR > > fast_combination(_combination_op_);
2381 joint = fast_combination.execute(xnew_pot_list);
2388 for (
const auto pot: new_pot_list)
2389 if (!pot_list.exists(pot))
delete pot;
2393 bool nonzero_found =
false;
2394 for (Instantiation inst(*joint); !inst.end(); ++inst) {
2395 if ((*joint)[inst]) {
2396 nonzero_found =
true;
2400 if (!nonzero_found) {
2404 "some evidence entered into the Bayes "
2405 "net are incompatible (their joint proba = 0)");
2412 template < GUM_Numeric GUM_SCALAR >
2413 const Tensor< GUM_SCALAR >& LazyPropagation< GUM_SCALAR >::jointPosterior_(
const NodeSet& set) {
2415 if (
auto p = _joint_target_posteriors_.tryGet(set)) {
return *(*p); }
2418 auto joint = unnormalizedJointPosterior_(set);
2420 _joint_target_posteriors_.insert(set, joint);
2426 template < GUM_Numeric GUM_SCALAR >
2427 const Tensor< GUM_SCALAR >&
2428 LazyPropagation< GUM_SCALAR >::jointPosterior_(
const NodeSet& wanted_target,
2429 const NodeSet& declared_target) {
2431 if (
auto p = _joint_target_posteriors_.tryGet(wanted_target))
return *(*p);
2437 if (!_joint_target_posteriors_.exists(declared_target)) { jointPosterior_(declared_target); }
2440 const auto& bn = this->BN();
2442 for (
const auto node: declared_target)
2443 if (!wanted_target.contains(node)) del_vars.
insert(&(bn.variable(node)));
2445 =
new Tensor< GUM_SCALAR >(_joint_target_posteriors_[declared_target]->sumOut(del_vars));
2449 _joint_target_posteriors_.insert(wanted_target, pot);
2450 if (
auto clique = _joint_target_to_clique_.tryGet(declared_target)) {
2451 const NodeId clique_id = *clique;
2452 if (!_joint_target_to_clique_.exists(wanted_target))
2453 _joint_target_to_clique_.insert(wanted_target, clique_id);
2459 template < GUM_Numeric GUM_SCALAR >
2460 GUM_SCALAR LazyPropagation< GUM_SCALAR >::evidenceProbability() {
2463 RelevantTensorsFinderType old_relevant_type = _find_relevant_tensor_type_;
2468 if (old_relevant_type != RelevantTensorsFinderType::FIND_ALL) {
2469 _find_relevant_tensor_type_ = RelevantTensorsFinderType::FIND_ALL;
2470 _is_new_jt_needed_ =
true;
2471 this->setOutdatedStructureState_();
2475 this->makeInference();
2483 GUM_SCALAR prob_ev = 1;
2484 for (
const auto root: _roots_) {
2486 NodeId node = *(_JT_->clique(root).begin());
2487 Tensor< GUM_SCALAR >* tmp = unnormalizedJointPosterior_(node);
2488 prob_ev *= tmp->sum();
2492 for (
const auto& projected_cpt: _constants_)
2493 prob_ev *= projected_cpt.second;
2496 _find_relevant_tensor_type_ = old_relevant_type;
2501 template < GUM_Numeric GUM_SCALAR >
2502 Instantiation LazyPropagation< GUM_SCALAR >::mpe() {
2505 RelevantTensorsFinderType old_relevant_type = _find_relevant_tensor_type_;
2510 if (old_relevant_type != RelevantTensorsFinderType::FIND_ALL) {
2511 _find_relevant_tensor_type_ = RelevantTensorsFinderType::FIND_ALL;
2512 _is_new_jt_needed_ =
true;
2513 this->setOutdatedStructureState_();
2517 auto old_projection_op = _projection_op_;
2518 auto new_projection_op = LPMaxprojTensor< GUM_SCALAR >;
2519 bool projection_op_changed = old_projection_op != new_projection_op;
2520 if (projection_op_changed) { this->_setProjectionFunction_(new_projection_op); }
2523 const auto in_target_mode = this->isInTargetMode();
2525 Set< NodeSet > old_joint_targets;
2526 if (in_target_mode) {
2527 old_targets = this->targets();
2528 old_joint_targets = this->jointTargets();
2529 this->eraseAllTargets();
2533 this->makeInference();
2536 Instantiation instantiations;
2537 for (
const auto& ev: this->hardEvidence()) {
2538 const auto& variable = this->BN().variable(ev.first);
2539 instantiations.add(variable);
2540 instantiations.chgVal(variable, ev.second);
2544 NodeProperty< bool > clique2marked = _JT_->nodesPropertyFromVal(
false);
2545 std::function< void(NodeId, NodeId) > diffuse_marks =
2546 [&clique2marked, &diffuse_marks, &instantiations,
this](NodeId clique, NodeId clique_from) {
2547 clique2marked[clique] =
true;
2552 auto clique_nodes = _JT_->clique(clique);
2553 auto pot = unnormalizedJointPosterior_(clique_nodes);
2554 auto pot_argmax = pot->extract(instantiations).argmax();
2556 const auto& new_instantiation = *(pot_argmax.first.begin());
2559 for (
const auto node: clique_nodes) {
2560 const auto& variable = this->BN().variable(node);
2561 if (!instantiations.contains(variable)) {
2562 instantiations.add(variable);
2563 instantiations.chgVal(variable, new_instantiation.val(variable));
2568 for (
const auto neigh: _JT_->neighbours(clique))
2569 if ((neigh != clique_from) && !clique2marked[neigh]) diffuse_marks(neigh, clique);
2574 for (
const auto& cliqueProp: clique2marked) {
2575 const auto clique = cliqueProp.first;
2576 if (!clique2marked[clique]) diffuse_marks(clique, clique);
2581 _find_relevant_tensor_type_ = old_relevant_type;
2584 if (projection_op_changed) { this->_setProjectionFunction_(old_projection_op); }
2587 if (in_target_mode) {
2588 for (
const auto node: old_targets) {
2589 this->addTarget(node);
2591 for (
const auto& set: old_joint_targets) {
2592 this->addJointTarget(set);
2597 return instantiations;
2600 template < GUM_Numeric GUM_SCALAR >
2601 std::pair< Instantiation, GUM_SCALAR > LazyPropagation< GUM_SCALAR >::mpeLog2Posterior() {
2603 const auto instantiation = mpe();
2612 auto proba = (GUM_SCALAR)0.0;
2613 auto node_proba = (GUM_SCALAR)0.0;
2615 for (
const auto node: this->BN().internalDag()) {
2616 const auto& cpt = this->BN().cpt(node);
2617 if (!this->hasSoftEvidence(node)) {
2618 node_proba = cpt[instantiation];
2620 const auto& ev = *(this->evidence()[node]);
2621 node_proba = cpt[instantiation] * ev[instantiation];
2624 if (node_proba == (GUM_SCALAR)0)
2625 return {instantiation, std::numeric_limits< GUM_SCALAR >::lowest()};
2626 proba += (GUM_SCALAR)std::log2(node_proba);
2629 if (!this->hasEvidence())
return {instantiation, proba};
2630 else return {instantiation, proba - std::log2(this->evidenceProbability())};
2633 template < GUM_Numeric GUM_SCALAR >
2634 Tensor< GUM_SCALAR > LPNewmultiTensor(
const Tensor< GUM_SCALAR >& t1,
2635 const Tensor< GUM_SCALAR >& t2) {
2639 template < GUM_Numeric GUM_SCALAR >
2640 Tensor< GUM_SCALAR > LPNewprojTensor(
const Tensor< GUM_SCALAR >& t1,
2642 return t1.sumOut(del_vars);
2645 template < GUM_Numeric GUM_SCALAR >
2646 Tensor< GUM_SCALAR > LPMaxprojTensor(
const Tensor< GUM_SCALAR >& t1,
2648 return t1.maxOut(del_vars);
2651 template < GUM_Numeric GUM_SCALAR >
2652 void LazyPropagation< GUM_SCALAR >::onStateChanged_() {}
The BayesBall algorithm (as described by Schachter).
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>
LazyPropagation(const IBayesNet< GUM_SCALAR > *BN, RelevantTensorsFinderType=RelevantTensorsFinderType::DSEP_BAYESBALL_TENSORS, FindBarrenNodesType=FindBarrenNodesType::FIND_BARREN_NODES, bool use_binary_join_tree=true)
default constructor
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.
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.
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
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...