StructuredBayesBallTestSuite.h 9.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES   *
 *   {prenom.nom}@lip6.fr                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
20
#include <agrum/BN/algorithms/BayesBall.h>
21
#include <agrum/BN/inference/variableElimination.h>
22

Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
23 24
#include <agrum/PRM/inference/SVE.h>
#include <agrum/PRM/inference/structuredBayesBall.h>
25

Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
26
#include <agrum/PRM/o3prm/O3prmReader.h>
27

28 29
#include <cxxtest/AgrumTestSuite.h>

30 31 32
namespace gum_tests {
  class StructuredBayesBallTestSuite : public CxxTest::TestSuite {
    private:
33
    gum::prm::PRM<double>*          prm;
Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
34
    gum::prm::PRMInference<double>* prm_inf;
35 36
    gum::prm::PRMSystem<double>*    sys;
    gum::prm::PRM<double>*          small;
Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
37
    gum::prm::PRMInference<double>* small_inf;
38
    gum::prm::PRMSystem<double>*    small_sys;
39

40
    public:
41
    void setUp() {
42 43 44
      {
        gum::prm::o3prm::O3prmReader<double> reader;
        reader.readFile( GET_RESSOURCES_PATH( "o3prm/inference.o3prm" ) );
45 46
        prm = reader.prm();
        sys = &( prm->getSystem( "aSys" ) );
47 48 49 50
        prm_inf = new gum::prm::SVE<double>( *prm, *sys );
      }
      {
        gum::prm::o3prm::O3prmReader<double> reader;
51 52
        reader.readFile( GET_RESSOURCES_PATH( "o3prm/printers_systems.o3prm" ) );
        small = reader.prm();
Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
53
        small_sys = &( small->getSystem( "smallSys" ) );
54 55
        small_inf = new gum::prm::SVE<double>( *small, *small_sys );
      }
56 57 58
    }

    void tearDown() {
59 60 61 62
      delete prm;
      delete prm_inf;
      delete small;
      delete small_inf;
63 64 65
    }

    void testConstructors() {
66 67 68 69 70 71 72
      gum::prm::StructuredBayesBall<double>* bb = 0;
      TS_GUM_ASSERT_THROWS_NOTHING(
          bb = new gum::prm::StructuredBayesBall<double>( *prm_inf ) );
      TS_GUM_ASSERT_THROWS_NOTHING( delete bb );
      TS_GUM_ASSERT_THROWS_NOTHING(
          bb = new gum::prm::StructuredBayesBall<double>( *small_inf ) );
      TS_GUM_ASSERT_THROWS_NOTHING( delete bb );
73 74 75 76 77
    }

    /// Checking that when a root is queried and there is no evidence, the
    /// requisite nodes set contains only the root node.
    void testRootsNoObs() {
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      gum::prm::StructuredBayesBall<double>* bb = 0;
      TS_GUM_ASSERT_THROWS_NOTHING(
          bb = new gum::prm::StructuredBayesBall<double>( *prm_inf ) );

      for ( auto i = sys->begin(); i != sys->end(); ++i ) {
        for ( auto a = ( *( i.val() ) ).begin(); a != ( *( i.val() ) ).end();
              ++a ) {
          if ( ( *( i.val() ) )
                   .type()
                   .dag()
                   .parents( ( *( a.val() ) ).id() )
                   .empty() ) {
            TS_GUM_ASSERT_THROWS_NOTHING(
                bb->compute( i.val(), ( *( a.val() ) ).id() ) );

            for ( auto j = sys->begin(); j != sys->end(); ++j ) {
              if ( ( j.val() ) != ( i.val() ) ) {
95
                TS_ASSERT( !bb->exists( j.val() ) );
96 97 98
              } else if ( bb->exists( j.val() ) ) {
                TS_ASSERT_EQUALS( bb->requisiteNodes( j.val() ).size(),
                                  (gum::Size)1 );
99 100
                TS_ASSERT( bb->requisiteNodes( j.val() ).contains(
                    ( *( a.val() ) ).id() ) );
101 102 103
              } else {
                TS_ASSERT( false );
              }
104
            }
105
          }
106
        }
107
      }
108

109
      TS_GUM_ASSERT_THROWS_NOTHING( delete bb );
110 111 112 113 114
    }

    /// Checking that when a root is queried and there is no evidence, the
    /// requisite nodes set contains only the root node.
    void testRootsNoObsSmall() {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      gum::prm::StructuredBayesBall<double>* bb = 0;
      TS_GUM_ASSERT_THROWS_NOTHING(
          bb = new gum::prm::StructuredBayesBall<double>( *small_inf ) );

      for ( auto i = small_sys->begin(); i != small_sys->end(); ++i ) {
        for ( auto a = ( *( i.val() ) ).begin(); a != ( *( i.val() ) ).end();
              ++a ) {
          if ( ( *( i.val() ) )
                   .type()
                   .dag()
                   .parents( ( *( a.val() ) ).id() )
                   .empty() ) {
            TS_GUM_ASSERT_THROWS_NOTHING(
                bb->compute( i.val(), ( *( a.val() ) ).id() ) );

130
            for ( gum::prm::PRMSystem<double>::iterator j = small_sys->begin();
131 132 133
                  j != small_sys->end();
                  ++j ) {
              if ( ( j.val() ) != ( i.val() ) ) {
134
                TS_ASSERT( !bb->exists( j.val() ) );
135 136 137
              } else if ( bb->exists( j.val() ) ) {
                TS_ASSERT_EQUALS( bb->requisiteNodes( j.val() ).size(),
                                  (gum::Size)1 );
138 139
                TS_ASSERT( bb->requisiteNodes( j.val() ).contains(
                    ( *( a.val() ) ).id() ) );
140 141 142
              } else {
                TS_ASSERT( false );
              }
143
            }
144
          }
145
        }
146
      }
147

148
      TS_GUM_ASSERT_THROWS_NOTHING( delete bb );
149 150
    }

Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
151 152
    /// Checking that when a root is queried and there is evidence on each leaf
    /// node,
153 154 155 156 157 158
    /// the
    /// requisite nodes set contains all nodes
    //       void /*test*/RootsObs() { // do not know what to do for now ...
    //         gum::prm::StructuredBayesBall<double>* bb = 0;
    //         TS_GUM_ASSERT_THROWS_NOTHING(bb = new
    //         gum::prm::StructuredBayesBall<double>(*prm_inf));
159
    //         for (gum::prm::PRMSystem<double>::iterator i = sys->begin(); i !=
160
    //         sys->end(); ++i) {
161
    //           for (gum::prm::PRMInstance::iterator a = (**i).begin(); a !=
162 163 164
    //           (**i).end(); ++a) {
    //             if ( (**i).type().dag().children((**a).id()).empty() and (not
    //             (**i).hasRefAttr((**a).id())) ) {
Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
165 166
    //               gum::prm::PRMInference<double>::Chain chain =
    //               std::make_pair(*i,
167 168 169 170 171 172 173 174 175 176 177 178
    //               *a);
    //               Potential<prm_float> e;
    //               e.add((**a).type().variable());
    //               e.fill((prm_float) 0.0);
    //               Instantiation inst(e);
    //               inst.setFirst();
    //               e.set(inst, (prm_float)1.0);
    //               prm_inf->addEvidence(chain, e);
    //               TS_ASSERT(bb->__isHardEvidence(*i, (**a).id()));
    //             }
    //           }
    //         }
179
    //         for (gum::prm::PRMSystem<double>::iterator i = sys->begin(); i !=
180
    //         sys->end(); ++i) {
181
    //           for (gum::prm::PRMInstance::iterator a = (**i).begin(); a !=
182 183 184
    //           (**i).end(); ++a) {
    //             if ((**i).type().dag().parents((**a).id()).empty()) {
    //               TS_GUM_ASSERT_THROWS_NOTHING(bb->compute(*i, (**a).id()));
185
    //               std::vector<gum::prm::PRMInstance*> stack;
186 187 188
    //               stack.push_back(*i);
    //               gum::Size count = 0;
    //               while (not stack.empty()) {
189
    //                 gum::prm::PRMInstance* inst = stack.back();
190 191 192 193 194 195 196 197
    //                 stack.pop_back();
    //                 TS_ASSERT(bb->exists(inst));
    //                 if (not bb->exists(inst)) {
    //                   GUM_TRACE_VAR(inst->name());
    //                   std::string dot = ".";
    //                   GUM_TRACE((**i).name() + dot + (**a).safeName());
    //                 }
    //                 count += inst->size();
198
    //                 for (gum::prm::PRMInstance::InvRefIterator iter =
199
    //                 inst->beginInvRef(); iter != inst->endInvRef(); ++iter) {
200
    //                   typedef std::vector< std::pair<gum::prm::PRMInstance*,
201
    //                   std::string> >::iterator Iter;
Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
202 203
    //                   for (Iter jter = (**iter).begin(); jter !=
    //                   (**iter).end();
204 205 206 207 208 209 210 211 212 213 214 215
    //                   ++jter) {
    //                     stack.push_back(jter->first);
    //                   }
    //                 }
    //               }
    //               TS_ASSERT(bb->requisiteNodes(*i).size() <= count);
    //               // std::cerr << std::endl;
    //             }
    //           }
    //         }
    //         TS_GUM_ASSERT_THROWS_NOTHING(delete bb);
    //       }
216
  };
217

Pierre-Henri Wuillemin's avatar
Pierre-Henri Wuillemin committed
218
}  // namespace gum_tests