Author Topic: How to pass database using JAVA HUgin API for EM learning.  (Read 59965 times)

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #15 on: September 19, 2011, 17:49:28 »
thank you.
I added try-catch with a printStackTrace call and the output is:
"COM.hugin.HAPI.ExceptionUsage: An API function was called with an invalid argument (e.g., a NULL object was passed to a function that expects a non-NULL argument).
   at COM.hugin.HAPI.ExceptionHugin.throwException(ExceptionHugin.java:115)
   at COM.hugin.HAPI.Domain.learnStructure(Domain.java:1451)
   at Prova.main(Test.java:80)"

to the line 80 of Test.java there is :
myDomain.learnStructure();

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #16 on: September 20, 2011, 12:04:16 »
What about the data file? If there is no data entered into the domain, then this error message may be produced. Save the cases to file before calling Domain.learnStructure and inspect the file (or post it here).
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #17 on: September 20, 2011, 18:39:24 »
I think, my error is this, but I cannot understand how I pass cases to the domain.
For the my code, I haven't data file but I have the data of my database.
In the while loops after
p=myDomain.newCase();
I save the cases with Domain.saveCase(String) in a txt file but I have a file empty.
How I save the cases???



Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #18 on: September 21, 2011, 10:13:57 »
Please read section 11.1 on data and entering data of the HUGIN API Reference Manual and study the example sent to you previously.

You need to use Domain.saveCases (and not Domain.saveCase) to debug.
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #19 on: September 21, 2011, 17:36:22 »
thanks..........
I used
Domain.saveCases();
and I could see the .dat file:

# - one - two

1 - "car" - #0
1 - #1 - "red"
1 - #2 - #2
1 - #3 - #3
1 - "house" - #4
1 - "computer" - "green"

the. dat file generated doesn't contain all the values but it has in some position #0,#1,#2..............
Why???
If I print with System.out.println() those values I have the right values

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #20 on: September 21, 2011, 18:29:02 »
Please attach the data file.

The tokens "#0, #1, ..."  refers to state indexes. Chapter 11 of the HUGIN API Reference Manual describes the learning process and the structure of the data files in great detail. PLEASE read this chapter.
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #21 on: September 29, 2011, 16:48:01 »
thanks....
I have always the same error:
"COM.hugin.HAPI.ExceptionUsage: An API function was called with an invalid argument (e.g., a NULL object was passed to a function that expects a non-NULL argument).
   at COM.hugin.HAPI.ExceptionHugin.throwException(ExceptionHugin.java:115)
   at COM.hugin.HAPI.Domain.learnStructure(Domain.java:1451)
   at Prova.main(Test.java:80)"
In my code:
1 - in the while loop I pass the value of record of my database to the states of nodes
2 - I use Domain.saveCases(java.lang.String fileName, NodeList nodes, int[] cases, boolean caseCounts, java.lang.String separator, java.lang.String missingData) to save the cases in a file "cases.dat"
3 - I use Domain.parseCases(java.lang.String fileName, ParseListener parseListener) to pass the cases stored in "cases.dat" and enters the cases into this Domain.
4 - I use Domain.leanStructure for PC-algorithm
5 - I use Domain.compile()
6 - I use Domain.learnTables()

This is a part of my new code:
...........................
int cases[]=new int[30];
int p=0;
int z=0;
boolean caseCount=true;
int statenode = 0;
NodeList nodes = new NodeList();
nodes.add(one);
nodes.add(two);
Resulset rset = stmt.executeQuery("SELECT one FROM table;");
Resulset rset2 = stmt2.executeQuery("SELECT two FROM table;");

while(rset.next()&&rset2.next()){
p=myDomain.newCase();
cases[z]=p;
one.setCaseState(p,statenode);
two.setCaseState(p,statenode);
a = rset.getString("one");
b = rset2.getString("two");
one.setStateLabel(statenode,a);
two.setStateLabel(statenode,b);
statenode++;
z++
}

two.addParent(one);
myDomain.saveCases("cases.dat",nodes,cases,caseCount," "," ");
myDomain.parseCases("cases.dat",new DefaultClassParseListener());
myDomain.learnStructure();
myDomain.compile();
myDomain.learnTables();
........................
« Last Edit: September 29, 2011, 17:02:43 by jack »

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #22 on: September 30, 2011, 09:27:11 »
PLEASE attach the "cases.dat" file.
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #23 on: September 30, 2011, 13:07:05 »
This is my new file "cases.dat":

#   one   two

1   "car"   "yellow"
1   "pen"   "red"
1   "train"   "white"
1   "bag"     "black"   
1   "house"   "blue"
1   "computer"   "green"

If I use "pen","bag","yellow" and "black" two or three times instead of "train","bag","yellow"and "white", I have values #0,#1,.........

This is my old file "cases.dat":
#  one  two

1  "car"  #0
1  #1  "red"
1  #2  #2
1  #3  #3
1  "house"  #4
1  "computer"  "green"


Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #24 on: October 01, 2011, 14:32:51 »
As described in the example I posted, you should not use "addParent" when invoking "learnStructure".

Page 157 of the HUGIN API Reference Manual states under the description of "learnStructure":

Quote
This function creates directed links (found by the PC algorithm) between the
nodes of domain, which is assumed to contain only discrete chance nodes
and no edges.

Hence, it is an usage error to use "addParent" before invoking "learnStructure".
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #25 on: October 03, 2011, 17:48:58 »
OK..................
Thanks, thanks thanks..........................
Now I have a problem to the print of tables.
I use to the print tables the metod "printTable (Table CPT)" of post "How to pass .dat file using JAVA HUgin API for EM learning.":

//print tables for all discrete chance nodes
    public static void printTable (Table CPT) throws ExceptionHugin {
   NodeList nodes = CPT.getNodes();
   int[] configuration = new int[nodes.size()];
   int tableSize = CPT.getSize();
   for (int i = 0; i < tableSize; i++) {
       CPT.getConfiguration(configuration, i);
       for (int j = 0; j < nodes.size(); j++) {
      Node n = (Node)nodes.get(j);
      System.out.print(n.getName() + "=" + configuration[j] + " ");
       }
       System.out.println(":= " + CPT.getDataItem(i));
       }
    }

Now in output I have the print of table but the values ​​are not equal to those obtained by Hugin GUI:

table of node "one" with hugin GUI:
car          := 0.166667
pen         :=0.5
house     :=0.166667
computer:=0.166667

table of node "one" with hugin java output:
one=0      := 0.16666666666666669
one=1      := 0.16666666666666669
one=2      := 0.16666666666666669
one=3      := 0.16666666666666669
one=4      := 0.16666666666666669
one=5      := 0.16666666666666669

but the value of one=1,one=2,one=3 is "pen" and then I should have a single value with probability value 0.5 as well in hugin gui

« Last Edit: October 03, 2011, 20:46:13 by jack »

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #26 on: October 06, 2011, 18:52:00 »
Please study again the example I posted earlier. It illustrates how you first create the nodes and their states of the network. Once the nodes are created you either use "learnStructure" followed by "learnTables" or create the structure (using "addParent") and followed by "learnTables" to estimate the parameters from data.

In the code you explicitly create one state for each data item. Also, you assume that each node should have 40 states.
HUGIN EXPERT A/S

Offline jack

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #27 on: October 09, 2011, 14:02:26 »
ok now everything works well.
But I have yet another question.
I do not know a priori the number of states of the node, but if I don't set first setNumberOfStates I cannot call setStateLabel.
How can I solve this problem?
thaanks,thanks,thanks very very much

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How to pass database using JAVA HUgin API for EM learning.
« Reply #28 on: October 12, 2011, 13:36:18 »
You need to determine the number of states of each node and their labels prior to creating the Domain object. You cannot do this on the fly (or at least you have to be very careful not to create two or more states for the same label). This best approach is to make two passes through the data. One for determining the number of states and labels and the second pass for entering the data to the Domain object.
HUGIN EXPERT A/S