forum.hugin.com

User Forums => FAQ => Topic started by: Anders L Madsen on March 08, 2011, 09:51:19

Title: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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
Title: Re: How can I use HUGIN in Matlab?
Post by: Francois 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
Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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 (http://download.hugin.com/webdocs/manuals/api-manual.pdf). For instance:

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

Title: Re: How can I use HUGIN in Matlab?
Post by: Francois 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.

Title: Re: How can I use HUGIN in Matlab?
Post by: Frank Jensen on June 27, 2013, 16:29:55
No, this is not possible.
Title: Re: How can I use HUGIN in Matlab?
Post by: MatteoV 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?
Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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?
Title: Re: How can I use HUGIN in Matlab?
Post by: MatteoV 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!!!

Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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
Title: Re: How can I use HUGIN in Matlab?
Post by: Son Nguyen 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.
Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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
Title: Re: How can I use HUGIN in Matlab?
Post by: Son Nguyen 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?
Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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 (http://download.hugin.com/webdocs/manuals/CS/html/d4648875-d41a-783b-d5f4-638df39ee413.htm#Example%202)
Hope this helps
Title: Re: How can I use HUGIN in Matlab?
Post by: Anders L Madsen 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