Author Topic: How can I use HUGIN in Matlab?  (Read 16571 times)

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2278
    • View Profile
How can I use HUGIN in Matlab?
« on: March 08, 2011, 09:51:19 »
Using the HUGIN .NET API it is possible to make use of HUGIN functionality in Matlab. The appropriate HUGIN dll file should be loaded as an assembly into Matlab.

The following example illustrates how to load the ChestClinic example from the Samples directory of the HUGIN installation and print the belief in node L:
Code: [Select]
ghapi = NET.addAssembly ('C:\Program Files\Hugin Expert\Hugin Researcher 7.4\HDE7.4CS\Lib\hugincs-7.4-2.0-x64.dll');
d = HAPI.Domain ('C:\Program Files\Hugin Expert\Hugin Researcher 7.4\Samples\ChestClinic.net', HAPI.DefaultClassParseListener);
L = d.GetNodeByName ('L');
d.Compile ();
L.GetBelief (0)
ans =
    0.0550
L.GetBelief (1)
ans =
    0.9450

The next code fragment shows how to select a state of a node, propagate the evidence and show the posterior beliefs in node L:
Code: [Select]
X = d.GetNodeByName ('X');
X.SelectState (0);
d.Propagate (HAPI.Equilibrium.H_EQUILIBRIUM_SUM, HAPI.EvidenceMode.H_EVIDENCE_MODE_NORMAL);
L.GetBelief (0)
ans =
    0.4887
L.GetBelief (1)
ans =
    0.5113
« Last Edit: March 14, 2011, 22:22:49 by Anders L Madsen »
HUGIN EXPERT A/S

Offline Francois

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #1 on: June 27, 2011, 17:29:01 »
Thank you Anders for the sample of code, it is very helpful!

What if I wanted to set X to a value in between 0 and 1 (X ray could be ambiguous) and set X to 90% YES / 10% NO.  Using the GUI it is simple, right click on the node and click enter likehood..., but in Matlab I have no idea. Could you show us how to do that?

Thank you in advance,

Francois
« Last Edit: June 27, 2011, 17:30:32 by Francois »

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2278
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #2 on: June 30, 2011, 14:45:09 »
Quote
What if I wanted to set X to a value in between 0 and 1 (X ray could be ambiguous) and set X to 90% YES / 10% NO.  Using the GUI it is simple, right click on the node and click enter likehood..., but in Matlab I have no idea. Could you show us how to do that?

First: you should be very careful with likelihood evidence. Entering a likelihood potential (0.9,0.1) does not fixed the distribution over X to (0.9,0.1). It is another potential over X giving different weights to the states of X and it is combined with any other information there may be on X.

To enter likelihood evidence you should use the "enterFinding" function. You can read about this in section 8.2 of the HUGIN API Reference manual: http://download.hugin.com/webdocs/manuals/api-manual.pdf. For instance:

Code: [Select]
L.enterFinding (0, 0.9)
L.enterFinding (1, 0.1)

« Last Edit: June 30, 2011, 14:47:49 by Anders L Madsen »
HUGIN EXPERT A/S

Offline Francois

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #3 on: June 25, 2013, 16:08:30 »
Is there a way to fix the distribution of a node? In the same maner as Select State does, it fix the state not taking into account other information from the network.


Offline Frank Jensen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 575
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #4 on: June 27, 2013, 16:29:55 »
No, this is not possible.

Offline MatteoV

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #5 on: February 09, 2018, 19:01:50 »
Hi,

Thank you for this topic and short code.
I need to use Hugin with Matlab for a case study. I do know that using C++ I can specify the prior distribution of a node (A) by using a simple code as follows:

Table *table = A->getTable ();
  NumberList data = table->getData ();

  data[0] = 0.5;
  data[1] = 0.4;
  data[2] = 0.1;
  table->setData (data);

Within the Matlab framework, thanks to your suggestion, I am able to get the name name, Select the state (SelectState) and retrieving the belief of the network node by using GetBelief. However, I am not able to assign the prior distribution of to a node. In fact, I get the following error message "No appropriate method, property, or field 'GetTable' for class 'HAPI.Domain'." or "No appropriate method, property, or field 'getTable' for class 'HAPI.Domain'. "

I do have this problem also with other command, such as getName, getMean etc.

Do you kindly have any suggestion for this?

Thanks a lot!

Any possible solution?

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2278
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #6 on: February 09, 2018, 19:50:57 »
Hi,

Domain does not have a table. You need to access the node. Can you send the code that produces the error?
« Last Edit: February 10, 2018, 17:58:59 by Anders L Madsen »
HUGIN EXPERT A/S

Offline MatteoV

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #7 on: February 15, 2018, 16:33:26 »
Dear Anders,

Please find attached the .m file and the corresponding Hugin file.

Hope this helps!

Thank you!!!


Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2278
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #8 on: February 17, 2018, 22:02:54 »
Hello,

This line of code is not correct:
Code: [Select]
A_table=d.GetTable ('A');
You should invoke GetTable on A as in:
Code: [Select]
A_table=A.GetTable ();
The node A has a table (a representation of its CPT).

hope this helps
Anders
HUGIN EXPERT A/S