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

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • 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: 2295
    • 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: 576
    • 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: 2295
    • 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: 2295
    • 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

Offline Son Nguyen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #9 on: April 30, 2018, 14:44:42 »
Is it possible to assign multiple nodes into an array in MATLAB and then call them from there instead of "one-by-one" method?
For example:
A=d.GetNodeByName('A');
B=d.GetNodeByName('B');
Nodes={A;B};
Here we could call out Nodes(i) with i=1:2 in
Nodes(1).EnterFinding(0,0.1); instead of A.EnterFinding(0,0.1);
This is useful in using a "for" loop.

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #10 on: May 05, 2018, 19:10:51 »
Quote
Is it possible to assign multiple nodes into an array in MATLAB and then call them from there instead of "one-by-one" method?

Yes, this should not be a problem. I'm not certain of the exact syntex, but using the HUGIN .NET API it should be something like this:

Code: [Select]
Node []array;
array = new Node[2];
array[0]=A;
array[1]=B;
hope this helps
HUGIN EXPERT A/S

Offline Son Nguyen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #11 on: May 11, 2018, 15:44:39 »
Thank you, that does help me understand more. I have another question: How can we manipulate the table (CPT) of a child node in the network. EnterFinding seems useless in this aspect. The function GetTable does work, but the result is just a data variable with the table type and when I try to access it, I get the "Array formation and indexing are not allowed on .NET objects." error. For short, How could a CPT be modified before we compile the network?

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #12 on: May 16, 2018, 10:00:09 »
The method
Code: [Select]
enterFindingis used for entering evidence on a node. There is also a selectState method to set the node to a specific state.

To change the CPT of the a node you need access the table for the node and manipulate its content. The documentation has a complete example building a network (here referring to the .NET API documentation):
http://download.hugin.com/webdocs/manuals/CS/html/d4648875-d41a-783b-d5f4-638df39ee413.htm#Example%202
Hope this helps
HUGIN EXPERT A/S

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2295
    • View Profile
Re: How can I use HUGIN in Matlab?
« Reply #13 on: February 06, 2020, 20:08:03 »
Due to an update in Matlab (version 2018a), the access to enums has changed. The above code should be updated to something like this:

Code: [Select]

ghapi = NET.addAssembly ('C:\Program Files\Hugin Expert\HUGIN 8.8 (x64)\HDE8.8CS\Lib\hugincs-8.8-2.0-x64.dll');
myEquilibirumEnumHandle = ghapi.AssemblyHandle.GetType('HAPI.Domain+Equilibrium');
myEvidenceModeEnumHandle2 = ghapi.AssemblyHandle.GetType('HAPI.Domain+EvidenceMode');

d = HAPI.Domain ('C:\Program Files\Hugin Expert\HUGIN 8.8 (x64)\Samples\ChestClinic.net', HAPI.DefaultClassParseListener);
L = d.GetNodeByName ('L');
d.Compile ();
L.GetBelief (0)
%ans =
%    0.0550
L.GetBelief (1)
%ans =
%    0.9450

X = d.GetNodeByName ('X');
X.SelectState (0);
d.Propagate (myEquilibirumEnumHandle.GetEnumValues().Get(1), myEvidenceModeEnumHandle2.GetEnumValues().Get(0));
L.GetBelief (0)
% %ans =
% %    0.4887
 L.GetBelief (1)
% %ans =
% %    0.5113
HUGIN EXPERT A/S