ecco qui un paio di esempi funzionanti (non ho smanettato troppo con Hugin visto che al mio gruppo è stato assegnato il progetto sulla Latent Semantic Analysis):
This first example is concerned with loading a Bayesian network or an influence diagram. Once the Bayesian network or influence diagram has been loaded, the corresponding domain is triangulated using the minimum fill-in-weight heuristic and the compilation process is completed. Next, the members of each clique of the junction tree(s) are printed on standard output. Finally, a propagation of evidence is performed and the resulting posterior marginals are printed on standard output.
import COM.hugin.HAPI.*;
import java.util.ListIterator;
class MyParseListener implements ClassParseListener
{
public void parseError (int line, String msg)
{
System.out.println ("Parse error in line " + line + ": " + msg);
}
public void insertClass (String className, ClassCollection cc)
{
try {
cc.parseClasses (className + ".oobn", this);
}
catch (ExceptionHugin e) {
System.out.println ("Parsing failed: " + e.getMessage ());
}
}
}
class LAP
{
static MyParseListener parseListener = new MyParseListener ();
/**
* Load a Bayesian network, compile it, and propagate evidence.
*/
public LAP (String fileName)
{
try {
ClassCollection cc = new ClassCollection ();
cc.parseClasses (fileName + ".oobn", parseListener);
// Unfold the Class to a Domain that can be compiled and
// used for inference, etc.
COM.hugin.HAPI.Class main = cc.getClassByName (fileName);
if (main == null)
System.out.println ("Class not found: " + fileName);
else
{
Domain domain = main.createDomain ();
domain.openLogFile (fileName + ".log");
domain.triangulate (Domain.H_TM_FILL_IN_WEIGHT);
domain.compile ();
printJunctionTrees (domain.getJunctionTrees ());
domain.propagate (Domain.H_EQUILIBRIUM_SUM,
Domain.H_EVIDENCE_MODE_NORMAL);
printNodeMarginals (domain);
domain.closeLogFile ();
domain.saveAsKB (fileName + ".hkb");
}
}
catch (ExceptionHugin e) {
System.out.println ("Exception caught:");
System.out.println (e.getMessage ());
}
catch (Exception e) {
System.out.println ("General exception:");
System.out.println (e.getMessage ());
}
}
/**
* Print the cliques of the junction tree(s).
*/
public void printJunctionTrees (JunctionTreeList list)
throws ExceptionHugin
{
System.out.println ("Cliques: ");
ListIterator jtit = list.listIterator ();
while (jtit.hasNext ())
{
JunctionTree jt = (JunctionTree) jtit.next ();
ListIterator cliqueit = jt.getCliques ().listIterator ();
while (cliqueit.hasNext ())
printNodes (((Clique) cliqueit.next ()).getMembers ());
}
System.out.println ();
}
/**
* Print the marginal distribution of each variable in the domain.
*/
public void printNodeMarginals (Domain domain)
throws ExceptionHugin
{
ListIterator it = domain.getNodes ().listIterator ();
while (it.hasNext ())
{
Node node = (Node) it.next ();
System.out.println (node.getLabel () + "(" + node.getName () + ")");
if (node.getCategory () == Domain.H_CATEGORY_CHANCE)
{
if (node.getKind () == Domain.H_KIND_CONTINUOUS)
{
System.out.println ("-Mean : " +
((ContinuousChanceNode) node).getMean ());
System.out.println ("-Variance: " +
((ContinuousChanceNode) node).getVariance ());
}
else if (node.getKind () == Domain.H_KIND_DISCRETE)
for (int i = 0; i < ((DiscreteChanceNode) node).getNumberOfStates (); i++)
System.out.println ("-" +
((DiscreteChanceNode) node). getStateLabel (i) +
" " +
((DiscreteChanceNode) node).getBelief (i));
}
else if (node.getCategory () == Domain.H_CATEGORY_DECISION)
for (int i = 0; i < ((DiscreteDecisionNode) node).getNumberOfStates (); i++)
System.out.println ("-" +
((DiscreteDecisionNode) node).getStateLabel (i) +
" " +
((DiscreteDecisionNode) node).getExpectedUtility (i));
}
}
/**
* Print the name of each node in the list.
*/
static void printNodes (NodeList list)
throws ExceptionHugin
{
ListIterator it = list.listIterator ();
while (it.hasNext ())
{
Node node = (Node) it.next ();
System.out.print (node.getName () + " ");
}
System.out.println ();
}
}
/**
* Load a Hugin NET file and perform a single propagation of evidence.
* Print the results.
*/
class LoadAndPropagate
{
static public void main (String args[])
{
new LAP (args[0]);
}
}
------------------
The second example describes how a Bayesian network can be constructed using the Hugin Java API. The Bayesian network constructed consists of three numbered nodes. Two of the nodes take on values 0, 1, and 2. The third node is the sum of the two other nodes. Once the Bayesian network is constructed, the network is saved to a NET specification file and an initial propagation is performed. Finally, the marginals of the nodes are printed on standard output.
import COM.hugin.HAPI.*;
import java.awt.event.*;
import java.util.ListIterator;
import java.awt.geom.Point2D;
class BAP
{
protected Domain domain;
/**
* Build a Bayesian network and propagate evidence.
*/
public BAP ()
{
try {
domain = new Domain ();
buildNetwork ();
domain.saveAsNet ("builddomain.net");
domain.compile ();
propagateEvidenceInNetwork ();
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* Propagate evidence in domain.
*/
protected void propagateEvidenceInNetwork ()
{
try {
domain.propagate (Domain.H_EQUILIBRIUM_SUM,
Domain.H_EVIDENCE_MODE_NORMAL);
printNodeMarginals (domain);
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* print node marginals.
*/
protected void printNodeMarginals (Domain d)
{
try {
ListIterator it = domain.getNodes ().listIterator ();
while (it.hasNext ())
{
DiscreteChanceNode node = (DiscreteChanceNode) it.next ();
System.out.println (node.getLabel ());
for (int i = 0, n = node.getNumberOfStates (); i < n; i++)
System.out.println ("-" + node.getStateLabel (i)
+ " " + node.getBelief (i));
}
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* Constructs numbered discrete chance node.
*/
protected NumberedDCNode constructNDC (String label, String name, int n)
{
try {
NumberedDCNode node = new NumberedDCNode (domain);
node.setNumberOfStates (n);
for (int i = 0; i < n; i++)
node.setStateValue (i, i);
for (int i = 0; i < n; i++)
node.setStateLabel (i, (new Integer (i)).toString ());
node.setLabel (label);
node.setName (name);
return node;
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
return null;
}
/**
* Build the structure.
*/
protected void buildStructure
(NumberedDCNode A, NumberedDCNode B, NumberedDCNode C)
{
try {
C.addParent (A);
C.addParent (B);
A.setPosition (new Point2D.Double (100, 200));
B.setPosition (new Point2D.Double (200, 200));
C.setPosition (new Point2D.Double (150, 50));
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* Expression for C
*/
protected void buildExpressionForC
(NumberedDCNode A, NumberedDCNode B, NumberedDCNode C)
{
try {
NodeList modelNodes = new NodeList ();
Model model = new Model (C, modelNodes);
NodeExpression exprA = new NodeExpression (A);
NodeExpression exprB = new NodeExpression (B);
AddExpression exprC = new AddExpression (exprA, exprB);
model.setExpression (0, exprC);
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* Specify the prior distribution of A and B.
*/
protected void specifyDistributions (NumberedDCNode A, NumberedDCNode B)
{
try {
Table table;
table = A.getTable ();
double [] data = new double[3];
data[0] = 0.1;
data[1] = 0.2;
data[2] = 0.7;
table.setData (data);
table = B.getTable ();
table.setDataItem (0, 0.2);
table.setDataItem (1, 0.2);
table.setDataItem (2, 0.6);
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
/**
* Build the Bayesian network.
*/
protected void buildNetwork ()
{
try {
domain.setNodeSize (new Point2D.Double (50, 30));
NumberedDCNode A = constructNDC ("A", "A", 3);
NumberedDCNode B = constructNDC ("B", "B", 3);
NumberedDCNode C = constructNDC ("C", "C", 5);
buildStructure (A, B, C);
buildExpressionForC (A, B, C);
specifyDistributions (A, B);
}
catch (ExceptionHugin e) {
System.out.println (e.getMessage ());
}
}
}
/**
* Build a Bayesian network and perform a propagation of evidence.
* Print the results.
*/
class BuildAndPropagate
{
static public void main (String args[])
{
new BAP ();
}
}