Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: cliqset.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // cliqset.h: Definitions for the clique set object.
  12. //
  13. #ifndef _CLIQSET_H_
  14. #define _CLIQSET_H_
  15. #include "gmobj.h"
  16. #include "infer.h"
  17. class GEDGEMBN_CLIQ;
  18. // Counters maintained in the inference engine
  19. struct CLIQSETSTAT
  20. {
  21. long _cReload; // Number of times clique tree was reloaded
  22. long _cCollect; // Number of collect operations
  23. long _cEnterEv; // Number of calls to EnterEvidence
  24. long _cGetBel; // Number of calls to GetBelief
  25. long _cProbNorm; // Number of calls to ProbNorm
  26. CLIQSETSTAT () { Clear(); }
  27. void Clear ()
  28. {
  29. _cReload = 0;
  30. _cCollect = 0;
  31. _cEnterEv = 0;
  32. _cGetBel = 0;
  33. _cProbNorm = 0;
  34. }
  35. };
  36. ////////////////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////////////////
  38. //
  39. // GOBJMBN_CLIQSET:
  40. //
  41. // Since any model may decompose into a set of clique trees
  42. // (assemblages with no interconnections whatever), a CLIQSET
  43. // is defined as the join point or grouping for the clique
  44. // tree "forest".
  45. //
  46. ////////////////////////////////////////////////////////////////////
  47. ////////////////////////////////////////////////////////////////////
  48. class GOBJMBN_CLIQSET: public GOBJMBN_INFER_ENGINE
  49. {
  50. friend class CLIQSETWORK;
  51. public:
  52. GOBJMBN_CLIQSET ( MBNET & model,
  53. REAL rMaxEstimatedSize = -1.0,
  54. int iInferEngID = 0 );
  55. virtual ~ GOBJMBN_CLIQSET ();
  56. virtual INT EType () const
  57. { return EBNO_CLIQUE_SET; }
  58. virtual void Create ();
  59. virtual void Destroy ();
  60. virtual void Reload ();
  61. virtual void EnterEvidence ( GNODEMBN * pgnd, const CLAMP & clamp );
  62. virtual void GetEvidence ( GNODEMBN * pgnd, CLAMP & clamp );
  63. virtual void Infer ();
  64. virtual void GetBelief ( GNODEMBN * pgnd, MDVCPD & mdvBel );
  65. virtual PROB ProbNorm ();
  66. virtual void Dump ();
  67. // Return true if the state of information is impossible
  68. bool BImpossible ();
  69. enum ESTATE // State of the junction tree
  70. {
  71. CTOR, // Just constructed
  72. UNDIR, // Undirected graph created
  73. MORAL, // Moralized
  74. CLIQUED, // Cliques constructed
  75. BUILT, // Fully constructed
  76. CONSISTENT, // Fully propagated
  77. EVIDENCE // Unpropagated evidence present
  78. };
  79. ESTATE EState () const { return _eState; }
  80. MBNET & Model () { return _model; }
  81. INT IInferEngID () const { return _iInferEngID; }
  82. // Force reloading of the clique tree and full inference
  83. void SetReset ( bool bReset = true )
  84. { _bReset = bReset ; }
  85. // Force full collect/distribute cycle
  86. void SetCollect ( bool bCollect = true )
  87. { _bCollect = bCollect; }
  88. // Provide access to inference statistics
  89. CLIQSETSTAT & CqsetStat () { return _cqsetStat; }
  90. protected:
  91. ESTATE _eState; // State of junction tree
  92. // Tallies
  93. int _cCliques; // Number of cliques
  94. int _cCliqueMemberArcs; // Number of clique member arcs
  95. int _cSepsetArcs; // Number of sepsest (arcs)
  96. int _cUndirArcs; // Undirected arcs in moral graph
  97. // Inference control
  98. bool _bCollect; // Is "collect/distribute" pass necessary?
  99. bool _bReset; // Does tree need resetting?
  100. REAL _probNorm; // Residual prob of tree
  101. CLIQSETSTAT _cqsetStat; // Statistics
  102. protected:
  103. bool BCollect() const
  104. { return _bCollect; }
  105. // Cliquing helper functions
  106. int CNeighborUnlinked ( GNODEMBN * pgndmbn, bool bLinkNeighbors = false );
  107. void Eliminate ( GNODEMBN * pgndmbn, CLIQSETWORK & clqsetWork ) ;
  108. void GenerateCliques ( CLIQSETWORK & clqsetWork );
  109. void CreateUndirectedGraph( bool bMarryParents = true );
  110. void DestroyDirectedGraph ();
  111. // Inference and tree maintenance
  112. void Reset ();
  113. void CollectEvidence ();
  114. void DistributeEvidence ();
  115. // Create (but don't init/load) all the clique and sepset marginals
  116. void CreateMarginals();
  117. // Load probabilities into cliques; initialize all the sepsets
  118. void LoadMarginals ();
  119. // Return the "family" or "self" clique for a node
  120. GOBJMBN_CLIQUE * PCliqueFromNode ( GNODEMBN * pgnd,
  121. bool bFamily,
  122. GEDGEMBN_CLIQ * * ppgedgeClique = NULL );
  123. // Typedefs for pointer-to-member-functions; used by Walk(). If bDownwards,
  124. // then object is being enumerated on the way down the tree.
  125. typedef bool (GOBJMBN_CLIQSET::*PFNC_JTREE) ( GOBJMBN_CLIQUE & clique,
  126. bool bDownwards /*= true*/ );
  127. typedef bool (GOBJMBN_CLIQSET::*PFNC_SEPSET) ( GEDGEMBN_SEPSET & sepset,
  128. bool bDownwards /*= true*/ );
  129. // Apply the given member function(s) to all cliques and/or sepsets,
  130. // depth first.
  131. int WalkTree ( bool bDepthFirst,
  132. PFNC_JTREE pfJtree = NULL,
  133. PFNC_SEPSET pfSepset = NULL );
  134. // Apply the given member function to cliques and sepsets, depth first
  135. int WalkDepthFirst ( GOBJMBN_CLIQUE * pClique,
  136. PFNC_JTREE pfJtree = NULL,
  137. PFNC_SEPSET pfSepset = NULL );
  138. int WalkBreadthFirst ( GOBJMBN_CLIQUE * pClique,
  139. PFNC_JTREE pfJtree = NULL,
  140. PFNC_SEPSET pfSepset = NULL );
  141. // Add an undirected arc iff there isn't one already.
  142. bool BAddUndirArc ( GNODEMBN * pgndbnSource, GNODEMBN * pgndbnSink );
  143. // Clique and sepset helper functions used during WalkTree().
  144. bool BCreateClique ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  145. bool BLoadClique ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  146. bool BCreateSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  147. bool BLoadSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  148. bool BCollectEvidenceAtSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  149. bool BDistributeEvidenceAtSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  150. bool BCollectEvidenceAtRoot ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  151. bool BDistributeEvidenceAtRoot ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  152. bool BCollectInitEvidenceAtSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  153. bool BDistributeInitEvidenceAtSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  154. bool BCollectInitEvidenceAtRoot ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  155. bool BDistributeInitEvidenceAtRoot ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  156. // Perform initial inference collect/distribute cycle
  157. void InferInit ();
  158. void CollectEvidenceInit ();
  159. void DistributeEvidenceInit ();
  160. bool BDumpSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  161. bool BDumpClique ( GOBJMBN_CLIQUE & clique, bool bDownwards );
  162. void CheckConsistency ();
  163. bool BConsistentSepset ( GEDGEMBN_SEPSET & sepset, bool bDownwards );
  164. private:
  165. void Clear ();
  166. };
  167. #endif // _CLIQSET_H_