1 /** 2 * 3 * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU) 4 * info_at_agrum_dot_org 5 * 6 * This library is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with this library. If not, see <http://www.gnu.org/licenses/>. 18 * 19 */ 20 21 22 #include <iostream> 23 #include <string> 24 25 #include <gumtest/AgrumTestSuite.h> 26 #include <gumtest/testsuite_utils.h> 27 28 #include <agrum/BN/BayesNet.h> 29 #include <agrum/BN/io/BIFXML/BIFXMLBNReader.h> 30 #include <agrum/tools/variables/labelizedVariable.h> 31 32 // The graph used for the tests: 33 // 1 2_ 1 -> 3 34 // / \ / / 1 -> 4 35 // 3 4 / 3 -> 5 36 // \ / / 4 -> 5 37 // 5_/ 2 -> 4 38 // 2 -> 5 39 40 namespace gum_tests { 41 42 class BIFXMLBNReaderTestSuite: public CxxTest::TestSuite { 43 public: testConstuctor()44 void testConstuctor() { 45 std::string file = GET_RESSOURCES_PATH("bifxml/BNBIFXMLReader_file1.bifxml"); 46 gum::BayesNet< double > net; 47 48 gum::BIFXMLBNReader< double >* reader = 0; 49 TS_GUM_ASSERT_THROWS_NOTHING(reader = new gum::BIFXMLBNReader< double >(&net, file)); 50 TS_GUM_ASSERT_THROWS_NOTHING(delete reader); 51 } 52 testRead_file1()53 void testRead_file1() { 54 std::string file = GET_RESSOURCES_PATH("bifxml/BNBIFXMLReader_file1.bifxml"); 55 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 56 57 TS_ASSERT_DIFFERS(net, nullptr) 58 59 gum::BIFXMLBNReader< double > reader(net, file); 60 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 61 62 if (net != nullptr) { 63 TS_ASSERT(net->empty()) 64 delete net; 65 } 66 } 67 testRead_file2_float()68 void testRead_file2_float() { 69 std::string file = GET_RESSOURCES_PATH("bifxml/BNBIFXMLReader_file2.bifxml"); 70 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 71 gum::BIFXMLBNReader< double > reader(net, file); 72 73 gum::Size isOK = (gum::Size)0; 74 TS_GUM_ASSERT_THROWS_NOTHING(isOK = reader.proceed()); 75 TS_ASSERT_EQUALS(isOK, (gum::Size)0) 76 77 TS_ASSERT_DIFFERS(net, nullptr) 78 79 if (net != nullptr) { 80 TS_ASSERT(!net->empty()) 81 TS_ASSERT_EQUALS(net->size(), (gum::Size)2) 82 gum::NodeId node_1 = 0, node_2 = 0; 83 84 for (const auto node: net->nodes()) 85 if (net->variable(node).name() == "n1") 86 node_1 = node; 87 else 88 node_2 = node; 89 90 const gum::DiscreteVariable& var_1 = net->variable(node_1); 91 92 TS_ASSERT_EQUALS(var_1.name(), "n1") 93 TS_ASSERT_EQUALS(var_1.domainSize(), (gum::Size)2) 94 95 const gum::Potential< double >& proba_1 = net->cpt(node_1); 96 TS_ASSERT_EQUALS(proba_1.domainSize(), (gum::Size)2) 97 98 gum::Instantiation inst_1(proba_1); 99 inst_1.setFirst(); 100 TS_ASSERT_DELTA(proba_1[inst_1], 0.2f, 0.001f) 101 inst_1.setLast(); 102 TS_ASSERT_DELTA(proba_1[inst_1], 0.8f, 0.001f) 103 104 const gum::DiscreteVariable& var_2 = net->variable(node_2); 105 TS_ASSERT_EQUALS(var_2.name(), "n2") 106 TS_ASSERT_EQUALS(var_2.domainSize(), (gum::Size)2) 107 108 const gum::Potential< double >& proba_2 = net->cpt(node_2); 109 TS_ASSERT_EQUALS(proba_2.domainSize(), (gum::Size)2) 110 111 gum::Instantiation inst_2(proba_2); 112 inst_2.setFirst(); 113 TS_ASSERT_DELTA(proba_2[inst_2], 0.3f, 0.001f) 114 inst_2.setLast(); 115 TS_ASSERT_DELTA(proba_2[inst_2], 0.7f, 0.001f) 116 delete net; 117 } 118 } 119 testRead_dog_double()120 void testRead_dog_double() { 121 // from Charniak, Bayesian networks Without Tears, AI Magazine, 1991 122 std::string file = GET_RESSOURCES_PATH("bifxml/dog.bifxml"); 123 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 124 gum::BIFXMLBNReader< double > reader(net, file); 125 126 gum::Size isOK = (gum::Size)0; 127 TS_GUM_ASSERT_THROWS_NOTHING(isOK = reader.proceed()); 128 TS_ASSERT_EQUALS(isOK, (gum::Size)0) 129 130 TS_ASSERT_DIFFERS(net, nullptr) 131 132 if (net != nullptr) { 133 TS_ASSERT_EQUALS(net->size(), (gum::Size)5) 134 135 const gum::Potential< double >& proba = net->cpt(net->idFromName("dog-out")); 136 137 TS_ASSERT_EQUALS(proba.domainSize(), (gum::Size)8) 138 139 delete (net); 140 } 141 } 142 testRead_file2_double()143 void testRead_file2_double() { 144 std::string file = GET_RESSOURCES_PATH("bifxml/BNBIFXMLReader_file2.bifxml"); 145 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 146 gum::BIFXMLBNReader< double > reader(net, file); 147 148 gum::Size isOK = (gum::Size)0; 149 TS_GUM_ASSERT_THROWS_NOTHING(isOK = reader.proceed()); 150 TS_ASSERT_EQUALS(isOK, (gum::Size)0) 151 152 TS_ASSERT_DIFFERS(net, nullptr) 153 154 if (net != nullptr) { 155 TS_ASSERT_EQUALS(net->size(), (gum::Size)2) 156 gum::NodeId node_1 = 0, node_2 = 0; 157 158 for (const auto node: net->nodes()) 159 if (net->variable(node).name() == "n1") 160 node_1 = node; 161 else 162 node_2 = node; 163 164 const gum::DiscreteVariable& var_1 = net->variable(node_1); 165 166 TS_ASSERT_EQUALS(var_1.name(), "n1") 167 168 TS_ASSERT_EQUALS(var_1.domainSize(), (gum::Size)2) 169 170 const gum::Potential< double >& proba_1 = net->cpt(node_1); 171 172 TS_ASSERT_EQUALS(proba_1.domainSize(), (gum::Size)2) 173 174 gum::Instantiation inst_1(proba_1); 175 176 inst_1.setFirst(); 177 178 TS_ASSERT_DELTA(proba_1[inst_1], 0.2f, 0.001f) 179 180 inst_1.setLast(); 181 182 TS_ASSERT_DELTA(proba_1[inst_1], 0.8f, 0.001f) 183 184 const gum::DiscreteVariable& var_2 = net->variable(node_2); 185 186 TS_ASSERT_EQUALS(var_2.name(), "n2") 187 188 TS_ASSERT_EQUALS(var_2.domainSize(), (gum::Size)2) 189 190 const gum::Potential< double >& proba_2 = net->cpt(node_2); 191 192 TS_ASSERT_EQUALS(proba_2.domainSize(), (gum::Size)2) 193 194 gum::Instantiation inst_2(proba_2); 195 196 inst_2.setFirst(); 197 198 TS_ASSERT_DELTA(proba_2[inst_2], 0.3f, 0.001f) 199 200 inst_2.setLast(); 201 202 TS_ASSERT_DELTA(proba_2[inst_2], 0.7f, 0.001f) 203 204 delete net; 205 } 206 } 207 testRead_file3()208 void testRead_file3() { 209 std::string file = GET_RESSOURCES_PATH("bifxml/BNBIFXMLReader_file3.bifxml"); 210 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 211 gum::BIFXMLBNReader< double > reader(net, file); 212 213 gum::Size isOK = (gum::Size)0; 214 TS_GUM_ASSERT_THROWS_NOTHING(isOK = reader.proceed()); 215 TS_ASSERT_EQUALS(isOK, (gum::Size)0) 216 TS_ASSERT_DIFFERS(net, nullptr) 217 218 if (net != nullptr) { 219 gum::HashTable< std::string, gum::NodeId > idMap; 220 221 for (const auto node: net->nodes()) 222 idMap.insert(net->variable(node).name(), node); 223 224 const gum::DiscreteVariable& var_1 = net->variable(idMap["n1"]); 225 226 TS_ASSERT_EQUALS(var_1.name(), "n1") 227 228 TS_ASSERT_EQUALS(var_1.domainSize(), (gum::Size)2) 229 230 TS_ASSERT_EQUALS(var_1.label(0), "0") 231 232 TS_ASSERT_EQUALS(var_1.label(1), "1") 233 234 const gum::Potential< double >& proba_1 = net->cpt(idMap["n1"]); 235 236 TS_ASSERT_EQUALS(proba_1.domainSize(), (gum::Size)2) 237 238 gum::Instantiation inst_1(proba_1); 239 240 inst_1.setFirst(); 241 242 TS_ASSERT_DELTA(proba_1[inst_1], 0.2f, 0.001f) 243 244 inst_1.setLast(); 245 246 TS_ASSERT_DELTA(proba_1[inst_1], 0.8f, 0.001f) 247 248 const gum::DiscreteVariable& var_2 = net->variable(idMap["n2"]); 249 250 TS_ASSERT_EQUALS(var_2.name(), "n2") 251 252 TS_ASSERT_EQUALS(var_2.domainSize(), (gum::Size)2) 253 254 TS_ASSERT_EQUALS(var_2.label(0), "foo") 255 256 TS_ASSERT_EQUALS(var_2.label(1), "bar") 257 258 const gum::Potential< double >& proba_2 = net->cpt(idMap["n2"]); 259 260 TS_ASSERT_EQUALS(proba_2.domainSize(), (gum::Size)2) 261 262 gum::Instantiation inst_2(proba_2); 263 264 inst_2.setFirst(); 265 266 TS_ASSERT_DELTA(proba_2[inst_2], 0.3f, 0.001f) 267 268 inst_2.setLast(); 269 270 TS_ASSERT_DELTA(proba_2[inst_2], 0.7f, 0.001f) 271 272 const gum::DiscreteVariable& var_3 = net->variable(idMap["n3"]); 273 274 TS_ASSERT_EQUALS(var_3.name(), "n3") 275 276 TS_ASSERT_EQUALS(var_3.domainSize(), (gum::Size)2) 277 278 TS_ASSERT_EQUALS(var_3.label(0), "0") 279 280 TS_ASSERT_EQUALS(var_3.label(1), "1") 281 282 TS_ASSERT(net->dag().existsArc(idMap["n1"], idMap["n3"])) 283 284 const gum::Potential< double >& proba_3 = net->cpt(idMap["n3"]); 285 286 TS_ASSERT_EQUALS(proba_3.domainSize(), (gum::Size)4) 287 288 gum::Instantiation inst_3(proba_3); 289 290 inst_3.chgVal(var_1, 0); 291 292 inst_3.chgVal(var_3, 0); 293 294 TS_ASSERT_DELTA(proba_3[inst_3], 0.1f, 0.001f) 295 296 inst_3.chgVal(var_3, 1); 297 298 TS_ASSERT_DELTA(proba_3[inst_3], 0.9f, 0.001f) 299 300 inst_3.chgVal(var_1, 1); 301 302 inst_3.chgVal(var_3, 0); 303 304 TS_ASSERT_DELTA(proba_3[inst_3], 0.9f, 0.001f) 305 306 inst_3.chgVal(var_3, 1); 307 308 TS_ASSERT_DELTA(proba_3[inst_3], 0.1f, 0.001f) 309 310 const gum::DiscreteVariable& var_4 = net->variable(idMap["n4"]); 311 312 TS_ASSERT_EQUALS(var_4.name(), "n4") 313 314 TS_ASSERT_EQUALS(var_4.domainSize(), (gum::Size)2) 315 316 TS_ASSERT_EQUALS(var_4.label(0), "0") 317 318 TS_ASSERT_EQUALS(var_4.label(1), "1") 319 320 TS_ASSERT(net->dag().existsArc(idMap["n1"], idMap["n4"])) 321 322 TS_ASSERT(net->dag().existsArc(idMap["n2"], idMap["n4"])) 323 324 const gum::Potential< double >& proba_4 = net->cpt(idMap["n4"]); 325 326 TS_ASSERT_EQUALS(proba_4.domainSize(), (gum::Size)8) 327 328 gum::Instantiation inst_4(proba_4); 329 330 inst_4.chgVal(var_1, 0); 331 332 inst_4.chgVal(var_2, 0); 333 334 inst_4.chgVal(var_4, 0); 335 336 TS_ASSERT_DELTA(proba_4[inst_4], 0.4f, 0.001f) 337 338 inst_4.chgVal(var_4, 1); 339 340 TS_ASSERT_DELTA(proba_4[inst_4], 0.6f, 0.001f) 341 342 inst_4.chgVal(var_1, 1); 343 344 inst_4.chgVal(var_2, 0); 345 346 inst_4.chgVal(var_4, 0); 347 348 TS_ASSERT_DELTA(proba_4[inst_4], 0.5f, 0.001f) 349 350 inst_4.chgVal(var_4, 1); 351 352 TS_ASSERT_DELTA(proba_4[inst_4], 0.5f, 0.001f) 353 354 inst_4.chgVal(var_1, 0); 355 356 inst_4.chgVal(var_2, 1); 357 358 inst_4.chgVal(var_4, 0); 359 360 TS_ASSERT_DELTA(proba_4[inst_4], 0.5f, 0.001f) 361 362 inst_4.chgVal(var_4, 1); 363 364 TS_ASSERT_DELTA(proba_4[inst_4], 0.5f, 0.001f) 365 366 inst_4.chgVal(var_1, 1); 367 368 inst_4.chgVal(var_2, 1); 369 370 inst_4.chgVal(var_4, 0); 371 372 TS_ASSERT_EQUALS(proba_4[inst_4], 1) 373 374 inst_4.chgVal(var_4, 1); 375 376 TS_ASSERT_EQUALS(proba_4[inst_4], 0) 377 378 const gum::DiscreteVariable& var_5 = net->variable(idMap["n5"]); 379 380 TS_ASSERT_EQUALS(var_5.name(), "n5") 381 382 TS_ASSERT_EQUALS(var_5.domainSize(), (gum::Size)3) 383 384 TS_ASSERT_EQUALS(var_5.label(0), "space") 385 386 TS_ASSERT_EQUALS(var_5.label(1), "final") 387 388 TS_ASSERT_EQUALS(var_5.label(2), "frontiere") 389 390 TS_ASSERT(net->dag().existsArc(idMap["n2"], idMap["n5"])) 391 392 TS_ASSERT(net->dag().existsArc(idMap["n3"], idMap["n5"])) 393 394 const gum::Potential< double >& proba_5 = net->cpt(idMap["n5"]); 395 396 TS_ASSERT_EQUALS(proba_5.domainSize(), (gum::Size)12) 397 398 gum::Instantiation inst_5(proba_5); 399 400 inst_5.chgVal(var_3, 0); 401 402 inst_5.chgVal(var_2, 0); 403 404 inst_5.chgVal(var_5, 0); 405 406 TS_ASSERT_DELTA(proba_5[inst_5], 0.3f, 0.001f) 407 408 inst_5.chgVal(var_5, 1); 409 410 TS_ASSERT_DELTA(proba_5[inst_5], 0.6f, 0.001f) 411 412 inst_5.chgVal(var_5, 2); 413 414 TS_ASSERT_DELTA(proba_5[inst_5], 0.1f, 0.001f) 415 416 inst_5.chgVal(var_2, 0); 417 418 inst_5.chgVal(var_3, 1); 419 420 inst_5.chgVal(var_5, 0); 421 422 TS_ASSERT_DELTA(proba_5[inst_5], 0.5f, 0.001f) 423 424 inst_5.chgVal(var_5, 1); 425 426 TS_ASSERT_DELTA(proba_5[inst_5], 0.5f, 0.001f) 427 428 inst_5.chgVal(var_5, 2); 429 430 TS_ASSERT_DELTA(proba_5[inst_5], 0.0f, 0.001f) 431 432 inst_5.chgVal(var_2, 1); 433 434 inst_5.chgVal(var_3, 0); 435 436 inst_5.chgVal(var_5, 0); 437 438 TS_ASSERT_DELTA(proba_5[inst_5], 0.4f, 0.001f) 439 440 inst_5.chgVal(var_5, 1); 441 442 TS_ASSERT_DELTA(proba_5[inst_5], 0.6f, 0.001f) 443 444 inst_5.chgVal(var_5, 2); 445 446 TS_ASSERT_DELTA(proba_5[inst_5], 0.0f, 0.001f) 447 448 inst_5.chgVal(var_2, 1); 449 450 inst_5.chgVal(var_3, 1); 451 452 inst_5.chgVal(var_5, 0); 453 454 TS_ASSERT_DELTA(proba_5[inst_5], 0.5f, 0.001f) 455 456 inst_5.chgVal(var_5, 1); 457 458 TS_ASSERT_DELTA(proba_5[inst_5], 0.5f, 0.001f) 459 460 inst_5.chgVal(var_5, 2); 461 462 TS_ASSERT_EQUALS(proba_5[inst_5], 0) 463 464 const gum::DiscreteVariable& var_6 = net->variable(idMap["n6"]); 465 466 TS_ASSERT_EQUALS(var_6.name(), "n6") 467 468 TS_ASSERT_EQUALS(var_6.domainSize(), (gum::Size)2) 469 470 TS_ASSERT_EQUALS(var_6.label(0), "0") 471 472 TS_ASSERT_EQUALS(var_6.label(1), "1") 473 474 TS_ASSERT(net->dag().existsArc(idMap["n1"], idMap["n6"])) 475 476 TS_ASSERT(net->dag().existsArc(idMap["n5"], idMap["n6"])) 477 478 const gum::Potential< double >& proba_6 = net->cpt(idMap["n6"]); 479 480 TS_ASSERT_EQUALS(proba_6.domainSize(), (gum::Size)12) 481 482 gum::Instantiation inst_6(proba_6); 483 484 inst_6.chgVal(var_6, 0); 485 486 inst_6.chgVal(var_1, 0); 487 488 inst_6.chgVal(var_5, 0); 489 490 TS_ASSERT_DELTA(proba_6[inst_6], 0.1f, 0.001f) 491 492 inst_6.chgVal(var_5, 1); 493 494 TS_ASSERT_DELTA(proba_6[inst_6], 0.2f, 0.001f) 495 496 inst_6.chgVal(var_5, 2); 497 498 TS_ASSERT_DELTA(proba_6[inst_6], 0.3f, 0.001f) 499 500 inst_6.chgVal(var_1, 1); 501 502 inst_6.chgVal(var_5, 0); 503 504 TS_ASSERT_DELTA(proba_6[inst_6], 0.4f, 0.001f) 505 506 inst_6.chgVal(var_5, 1); 507 508 TS_ASSERT_DELTA(proba_6[inst_6], 0.5f, 0.001f) 509 510 inst_6.chgVal(var_5, 2); 511 512 TS_ASSERT_DELTA(proba_6[inst_6], 0.6f, 0.001f) 513 514 inst_6.chgVal(var_6, 1); 515 516 inst_6.chgVal(var_1, 0); 517 518 inst_6.chgVal(var_5, 0); 519 520 TS_ASSERT_DELTA(proba_6[inst_6], 0.7f, 0.001f) 521 522 inst_6.chgVal(var_5, 1); 523 524 TS_ASSERT_DELTA(proba_6[inst_6], 0.8f, 0.001f) 525 526 inst_6.chgVal(var_5, 2); 527 528 TS_ASSERT_DELTA(proba_6[inst_6], 0.9f, 0.001f) 529 530 inst_6.chgVal(var_1, 1); 531 532 inst_6.chgVal(var_5, 0); 533 534 TS_ASSERT_EQUALS(proba_6[inst_6], 1) 535 536 inst_6.chgVal(var_5, 1); 537 538 TS_ASSERT_EQUALS(proba_6[inst_6], 0) 539 540 inst_6.chgVal(var_5, 2); 541 542 TS_ASSERT_EQUALS(proba_6[inst_6], 0) 543 544 delete net; 545 } 546 } 547 testAlarm()548 void testAlarm() { 549 std::string file = GET_RESSOURCES_PATH("bifxml/alarm.bifxml"); 550 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 551 gum::BIFXMLBNReader< double > reader(net, file); 552 553 gum::Size isOK = (gum::Size)0; 554 TS_GUM_ASSERT_THROWS_NOTHING(isOK = reader.proceed()); 555 TS_ASSERT_EQUALS(isOK, (gum::Size)0) 556 557 gum::HashTable< std::string, gum::NodeId > idMap; 558 559 for (const auto node: net->nodes()) 560 idMap.insert(net->variable(node).name(), node); 561 562 // The node wich we'll test 563 TS_ASSERT(idMap.exists("HISTORY")) 564 // It's parent 565 TS_ASSERT(idMap.exists("LVFAILURE")) 566 567 if (idMap.exists("HISTORY") && idMap.exists("LVFAILURE")) { 568 const gum::DiscreteVariable& history = net->variable(idMap["HISTORY"]); 569 TS_ASSERT_EQUALS(history.domainSize(), (gum::Size)2) 570 TS_ASSERT_EQUALS(history.label(0), "TRUE") 571 TS_ASSERT_EQUALS(history.label(1), "FALSE") 572 TS_ASSERT(net->dag().existsArc(idMap["LVFAILURE"], idMap["HISTORY"])) 573 574 const gum::Potential< double >& historyCPT = net->cpt(idMap["HISTORY"]); 575 TS_ASSERT_EQUALS(historyCPT.domainSize(), (gum::Size)4) 576 TS_ASSERT(historyCPT.contains(net->variable(idMap["HISTORY"]))) 577 TS_ASSERT(historyCPT.contains(net->variable(idMap["LVFAILURE"]))) 578 579 gum::Instantiation historyInst(historyCPT); 580 historyInst.chgVal(history, 0); 581 historyInst.chgVal(net->variable(idMap["LVFAILURE"]), 0); 582 TS_ASSERT_DELTA(historyCPT[historyInst], 0.9f, 0.0001f) 583 historyInst.chgVal(history, 1); 584 historyInst.chgVal(net->variable(idMap["LVFAILURE"]), 0); 585 TS_ASSERT_DELTA(historyCPT[historyInst], 0.1f, 0.0001f) 586 historyInst.chgVal(history, 0); 587 historyInst.chgVal(net->variable(idMap["LVFAILURE"]), 1); 588 TS_ASSERT_DELTA(historyCPT[historyInst], 0.01f, 0.0001f) 589 historyInst.chgVal(history, 1); 590 historyInst.chgVal(net->variable(idMap["LVFAILURE"]), 1); 591 TS_ASSERT_DELTA(historyCPT[historyInst], 0.99f, 0.0001f) 592 } 593 594 // The node wich we'll test 595 TS_ASSERT(idMap.exists("ERRLOWOUTPUT")) 596 // It's Children 597 TS_ASSERT(idMap.exists("HRBP")) 598 599 if (idMap.exists("ERRLOWOUTPUT") && idMap.exists("HRBP")) { 600 const gum::DiscreteVariable& errlowoutput = net->variable(idMap["ERRLOWOUTPUT"]); 601 TS_ASSERT_EQUALS(errlowoutput.domainSize(), (gum::Size)2) 602 TS_ASSERT_EQUALS(errlowoutput.label(0), "TRUE") 603 TS_ASSERT_EQUALS(errlowoutput.label(1), "FALSE") 604 TS_ASSERT(net->dag().existsArc(idMap["ERRLOWOUTPUT"], idMap["HRBP"])) 605 606 const gum::Potential< double >& errlowoutputCPT = net->cpt(idMap["ERRLOWOUTPUT"]); 607 TS_ASSERT_EQUALS(errlowoutputCPT.domainSize(), (gum::Size)2) 608 TS_ASSERT(errlowoutputCPT.contains(errlowoutput)) 609 610 gum::Instantiation errlowoutputInst(errlowoutputCPT); 611 errlowoutputInst.chgVal(errlowoutput, 0); 612 TS_ASSERT_DELTA(errlowoutputCPT[errlowoutputInst], 0.05f, 0.001f) 613 errlowoutputInst.chgVal(errlowoutput, 1); 614 TS_ASSERT_DELTA(errlowoutputCPT[errlowoutputInst], 0.95f, 0.001f) 615 } 616 617 // The nide wich we'll test 618 TS_ASSERT(idMap.exists("LVEDVOLUME")) 619 // It's parents 620 TS_ASSERT(idMap.exists("HYPOVOLEMIA")) 621 TS_ASSERT(idMap.exists("LVFAILURE")) 622 623 if (idMap.exists("LVEDVOLUME") && idMap.exists("HYPOVOLEMIA") && idMap.exists("LVFAILURE")) { 624 const gum::DiscreteVariable& lvedvolume = net->variable(idMap["LVEDVOLUME"]); 625 const gum::DiscreteVariable& hypovolemia = net->variable(idMap["HYPOVOLEMIA"]); 626 const gum::DiscreteVariable& lvfailure = net->variable(idMap["LVFAILURE"]); 627 // checking label order 628 TS_ASSERT_EQUALS(lvedvolume.label(0), "LOW") 629 TS_ASSERT_EQUALS(lvedvolume.label(1), "NORMAL") 630 TS_ASSERT_EQUALS(lvedvolume.label(2), "HIGH") 631 TS_ASSERT_EQUALS(hypovolemia.label(0), "TRUE") 632 TS_ASSERT_EQUALS(hypovolemia.label(1), "FALSE") 633 TS_ASSERT_EQUALS(lvfailure.label(0), "TRUE") 634 TS_ASSERT_EQUALS(lvfailure.label(1), "FALSE") 635 636 const gum::Potential< double >& cpt = net->cpt(idMap["LVEDVOLUME"]); 637 gum::Instantiation inst(cpt); 638 639 gum::Instantiation var_inst; 640 var_inst << lvedvolume; 641 642 inst.chgVal(hypovolemia, 0); 643 inst.chgVal(lvfailure, 0); 644 float array_1[] = {0.95f, 0.04f, 0.01f}; 645 size_t i = 0; 646 647 for (inst.setFirstIn(var_inst); !inst.end(); inst.incIn(var_inst), ++i) { 648 TS_ASSERT_DELTA(cpt[inst], array_1[i], 0.001f) 649 } 650 651 inst.chgVal(hypovolemia, 1); 652 inst.chgVal(lvfailure, 0); 653 float array_2[] = {0.98f, 0.01f, 0.01f}; 654 i = 0; 655 656 for (inst.setFirstIn(var_inst); !inst.end(); inst.incIn(var_inst), ++i) { 657 TS_ASSERT_DELTA(cpt[inst], array_2[i], 0.001f) 658 } 659 660 inst.chgVal(hypovolemia, 0); 661 inst.chgVal(lvfailure, 1); 662 float array_3[] = {0.01f, 0.09f, 0.9f}; 663 i = 0; 664 665 for (inst.setFirstIn(var_inst); !inst.end(); inst.incIn(var_inst), ++i) { 666 TS_ASSERT_DELTA(cpt[inst], array_3[i], 0.001f) 667 } 668 669 inst.chgVal(hypovolemia, 1); 670 inst.chgVal(lvfailure, 1); 671 float array_4[] = {0.05f, 0.9f, 0.05f}; 672 i = 0; 673 674 for (inst.setFirstIn(var_inst); !inst.end(); inst.incIn(var_inst), ++i) { 675 TS_ASSERT_DELTA(cpt[inst], array_4[i], 0.001f) 676 } 677 } 678 679 delete net; 680 } 681 testUnexisting()682 void testUnexisting() { 683 std::string file = "Schmurtz"; 684 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 685 686 TS_GUM_ASSERT_THROWS_NOTHING(gum::BIFXMLBNReader< double > reader(net, file)); 687 gum::BIFXMLBNReader< double > reader(net, file); 688 TS_ASSERT_THROWS(reader.proceed(), gum::IOError) 689 690 if (net) delete net; 691 } 692 Carpo()693 void Carpo() { 694 std::string file = GET_RESSOURCES_PATH("bifxml/carpo.bifxml"); 695 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 696 gum::BIFXMLBNReader< double > reader(net, file); 697 698 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 699 700 if (net) delete net; 701 } 702 testDiabetes()703 void testDiabetes() { 704 std::string file = GET_RESSOURCES_PATH("bifxml/Diabetes.bifxml"); 705 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 706 gum::BIFXMLBNReader< double > reader(net, file); 707 708 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 709 710 if (net) delete net; 711 } 712 testHailfinder()713 void testHailfinder() { 714 std::string file = GET_RESSOURCES_PATH("bifxml/hailfinder.bifxml"); 715 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 716 gum::BIFXMLBNReader< double > reader(net, file); 717 718 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 719 720 if (net) delete net; 721 } 722 testInsurance()723 void testInsurance() { 724 std::string file = GET_RESSOURCES_PATH("bifxml/insurance.bifxml"); 725 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 726 gum::BIFXMLBNReader< double > reader(net, file); 727 728 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 729 730 if (net) delete net; 731 } 732 testLink()733 void testLink() { 734 std::string file = GET_RESSOURCES_PATH("bifxml/Link.bifxml"); 735 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 736 gum::BIFXMLBNReader< double > reader(net, file); 737 738 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 739 740 if (net) delete net; 741 } 742 testMildew()743 void testMildew() { 744 std::string file = GET_RESSOURCES_PATH("bifxml/Mildew.bifxml"); 745 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 746 gum::BIFXMLBNReader< double > reader(net, file); 747 748 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 749 750 if (net) delete net; 751 } 752 testMunin1()753 void testMunin1() { 754 std::string file = GET_RESSOURCES_PATH("bifxml/Munin1.bifxml"); 755 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 756 gum::BIFXMLBNReader< double > reader(net, file); 757 758 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 759 760 if (net) delete net; 761 } 762 testPigs()763 void testPigs() { 764 std::string file = GET_RESSOURCES_PATH("bifxml/Pigs.bifxml"); 765 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 766 gum::BIFXMLBNReader< double > reader(net, file); 767 768 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 769 770 if (net) delete net; 771 } 772 testWater()773 void testWater() { 774 std::string file = GET_RESSOURCES_PATH("bifxml/Water.bifxml"); 775 gum::BayesNet< double >* net = new gum::BayesNet< double >(); 776 gum::BIFXMLBNReader< double > reader(net, file); 777 778 TS_GUM_ASSERT_THROWS_NOTHING(reader.proceed()); 779 780 if (net) delete net; 781 } 782 }; 783 } // namespace gum_tests 784