Error in saving and retrieving the Probabilistic Network created using the API

  • cgPerera

    cgPerera - 2014-07-15


    I am trying to create the Asia network using the functions in API. I am using unbbayes.example.TextMode as the reference to create the Nodes and Edges. As an example, creation of the first 3 nodes look like below.

    ProbabilisticNetwork net = new ProbabilisticNetwork("Asia_Network");

    ProbabilisticNode newNode1 = new ProbabilisticNode();
    newNode1.setDescription("Visited asia or not");
    PotentialTable auxCPT1 = newNode1.getProbabilityFunction();
    auxCPT1.addValueAt(0, 0.01f); auxCPT1.addValueAt(1, 0.99f);

    ProbabilisticNode newNode2 = new ProbabilisticNode();
    newNode2.setDescription("Is a smoker or not");
    PotentialTable auxCPT2 = newNode2.getProbabilityFunction();
    auxCPT2.addValueAt(0, 0.3f); auxCPT2.addValueAt(1, 0.7f);

    ProbabilisticNode newNode3 = new ProbabilisticNode();
    newNode3.setDescription("Has Tuverculosis or not");
    PotentialTable auxCPT3 = newNode3.getProbabilityFunction();
    ProbabilisticNode asiaNode = (ProbabilisticNode)net.getNode("HasVisitedAsia");
    net.addEdge(new Edge(asiaNode, newNode3));
    auxCPT3.addValueAt(0, 0.05f); auxCPT3.addValueAt(1, 0.95f);
    auxCPT3.addValueAt(2, 0.01f); auxCPT3.addValueAt(3, 0.99f);

    After adding all the nodes and edges, I am saving the network using
    new NetIO().save(new File("net/"), net);

    Then I try to retrieve the saved network using
    ProbabilisticNetwork netWk = (ProbabilisticNetwork)new NetIO().load(new File("net/"));
    This causes a null pointer exception.

    When I compare the file saved by me and the file provided in examples\bn\net, I can see that my contains some unnecessary 0s. A fraction of the file saved is as follows.

    potential (HasVisitedAsia)
    data = ( 0.01 0.99 );
    0.0 0.0 ); // unnecessary zeros

    potential (HasTuberculosis | HasVisitedAsia)
    data = (( 0.05 0.95 )
    ( 0.01 0.99 ));
    ( 0.0 0.0 ) // unnecessary zeros
    ( 0.0 0.0 ));

    I cannot figure out whether I am making a mistake in creating or saving the network. Your help is highly appreciated.

    Thanks in advance.

    Last edit: cgPerera 2014-07-15
    • Laecio

      Laecio - 2014-07-16

      Hy Pereira,

      The method addValueAt adds a new cell in the PotentialTable on the position that you specify. So, when you use auxCPT1.addValueAt(0, 0.01f), you add a new cell on position 0, with the value 0.01f. The older cells are shift by one position and because of this, the final table ends with the double of necessary cells:

      potential (HasVisitedAsia)
      data = ( 0.01 0.99 );
      0.0 0.0 );

      You should use the method setValue instead:

      auxCPT1.setValue(0, 0.01f); auxCPT1.setValue(1, 0.99f);
      auxCPT2.setValue(0, 0.3f); auxCPT2.setValue(1, 0.7f);
      auxCPT3.setValue(0, 0.05f); auxCPT3.setValue(1, 0.95f);
      auxCPT3.setValue(2, 0.01f); auxCPT3.setValue(3, 0.99f);

      Please, try this.

      Good job, and feel comfortable to ask other questions.

      Best Regards,
      Laécio Santos
      UnBBayes Team

  • cgPerera

    cgPerera - 2014-07-16

    Thank you very much for the suggestion Laécio. It works fine now.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks