Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

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

cgPerera
2014-07-15
2014-07-16
  • cgPerera
    cgPerera
    2014-07-15

    Hi,

    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.setName("HasVisitedAsia");
    newNode1.setDescription("Visited asia or not");
    newNode1.appendState("Yes");
    newNode1.appendState("No");
    PotentialTable auxCPT1 = newNode1.getProbabilityFunction();
    auxCPT1.addVariable(newNode1);
    auxCPT1.addValueAt(0, 0.01f); auxCPT1.addValueAt(1, 0.99f);
    net.addNode(newNode1);

    ProbabilisticNode newNode2 = new ProbabilisticNode();
    newNode2.setName("IsSmoker");
    newNode2.setDescription("Is a smoker or not");
    newNode2.appendState("Yes");
    newNode2.appendState("No");
    PotentialTable auxCPT2 = newNode2.getProbabilityFunction();
    auxCPT2.addVariable(newNode2);
    net.addNode(newNode2);
    auxCPT2.addValueAt(0, 0.3f); auxCPT2.addValueAt(1, 0.7f);

    ProbabilisticNode newNode3 = new ProbabilisticNode();
    newNode3.setName("HasTuberculosis");
    newNode3.setDescription("Has Tuverculosis or not");
    newNode3.appendState("Yes");
    newNode3.appendState("No");
    PotentialTable auxCPT3 = newNode3.getProbabilityFunction();
    auxCPT3.addVariable(newNode3);
    net.addNode(newNode3);
    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/asia.net"), net);

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

    When I compare the asia.net file saved by me and the asia.net file provided in examples\bn\net, I can see that my asia.net contains some unnecessary 0s. A fraction of the asia.net 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.