Author Topic: batch propagation  (Read 13938 times)

Offline FranzisA

  • Newbie
  • *
  • Posts: 4
    • View Profile
batch propagation
« on: September 01, 2013, 17:46:34 »
Hello everyone,

I am working with the Hugin API for Java and would like to find a way to batch propagate a rather large amount of cases through a given network and save above all the expected utility as an output.
I found the function LoadAndPropagate in the example/test folder of the Hugin installation folder. I am trying to modify this function, so that I can not only propagate one case but many cases and save the output to a new file.
I am wondering if someone has some experience with this problem?

Would be great to get some help!
Best!

-- To specify my concern:
I have trouble to adjust the method "domain.propagate" to the function parseCases. The output always returns only one result from the evidence propagation but not the results for each case.
« Last Edit: September 01, 2013, 19:58:29 by FranzisA »

Offline Martin

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 613
    • View Profile
Re: batch propagation
« Reply #1 on: September 09, 2013, 11:37:50 »
Dear FranzisA

This should give you a good start at solving the task.

To propagate a number of cases read from a data file in batch one could do the following:
1) parse the data file into the Domain, this inserts a number of cases in the Domain object, which we can refer to by case index.
Note: In order to successfully parse a data file, the data file _must_ strictly be a valid hugin data file (see the API reference manual http://download.hugin.com/webdocs/manuals/api-manual.pdf ch. 11.3 Data files).
2) iterate: enter a case then propagate then perform other actions

In code such a function could look like this:

Code: [Select]
void doBatch() throws ExceptionHugin {
 //parse data file into domain
 domain.parseCases("path-to-my-data.dat", new DefaultClassParseListener());

 //get number of cases
 int numCases = domain.getNumberOfCases();

 //now iterate, propagate each case and perform some actions
 for (int i = 0; i < numCases; ++i) {
  try {
   domain.enterCase(i);
   domain.propagate(Domain.H_EQUILIBRIUM_SUM, Domain.H_EVIDENCE_MODE_NORMAL);
   //perform some actions, e.g. record beliefs utilities etc
   //...
  } catch (ExceptionHugin eh) {
   System.out.println("Exception processing case #" + i + ": " + eh.getMessage());
  }
 }
}
Hugin Expert A/S

Offline FranzisA

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: batch propagation
« Reply #2 on: September 23, 2013, 21:04:00 »
Dear Martin Karlsen,

thank you very much for your reply! Meanwhile I managed to parse cases (given in a valid case file) and retrieve the utility for each case and save it in an output file as vector.

Another interesting task would be to handle a net file with more than one utility node. Of course it works like above, if I just want to know the overall expected utility. But if I want to retrieve the utitlity for each node seperatly, I need to iterate over the cases AND over the nodes. I am working on this question right now. Maybe you also have a hint for this problem?

Best regards

Offline Martin

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 613
    • View Profile
Re: batch propagation
« Reply #3 on: September 24, 2013, 09:25:37 »
Yes that would be exactly the way to do it!

If you know the names of the set of utility nodes beforehand, you could structure your code like so:

Code: [Select]
//reference the utility nodes un1 and un2
UtilityNode un1 = (UtilityNode)domain.getNodeByName("un1");
UtilityNode un2 = (UtilityNode)domain.getNodeByName("un2");
...
...
//iterate all cases
for (int i = 0; i < numCases; ++i) {
 //enter case #i, propagate, SPU etc.
 ...
 //report utilities for un1 and un2
 System.out.println("utility 1: " + un1.getExpectedUtility());
 System.out.println("utility 2: " + un2.getExpectedUtility());
 ...
}


By the way, since you are working with a LIMID.

If your network contains uninstantiated decisions after propagating a case then you might want to perform SPU (single policy update) before reading out beliefs and utilities.

SPU takes the entered evidence into account to compute new policies for uninstantiated decisions which may improve overall expected utility of the decision problem (and influence beliefs for the nodes that depends on uninstantiated decisions).

See chapter 9.3 "Inference in LIMIDs: Computing optimal policies" in the API reference manual for details about SPU.
http://download.hugin.com/webdocs/manuals/api-manual.pdf

In the Java API the SPU function is Domain.updatePolicies()
Hugin Expert A/S

Offline FranzisA

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: batch propagation
« Reply #4 on: September 24, 2013, 17:03:13 »
Thank you very much for the hint about the function getNodeByName and also about the SPU issue.
I will try to save the "utility-vectors" in seperate files by retrieving the values from the referenced utillty nodes.
Great help, thanks!