Hello Bayes

Startpage
Tutorial

This document instructs you how to create a small Visual Basic.net Project called "HelloBayes" which uses the Hugin API ActiveX Server (please see the original HelloBayes for details). The program reads a belief network designed in the Hugin tool, allows the application-user to enter evidence, propagates it and displays the resulting probabilities. Note, that the use of the Hugin ActiveX API in Visual Basic.net is not officially supported.

The HelloBayes Visual Basic.net Project

The project is created in exactly the same way as the original HelloBayes. After the above steps it is time to add code turning the form into a useful application. The code is divided into four different sections: the declaration section of the module, initial operations performed on loading the form, operations performed when closing the form and operations performed when one of the option buttons selects a DNA-match? state.

Declaration section of the module

A global variable d is declared which will refer to the murder network. To hold a network it is necessary to allocate a domain structure of type HAPI.Domain which keeps track of the nodes belonging to the network, the compilation status of the network etc. Furthermore, the section declares constants in order to be able to symbolically identify the states of the two nodes in the network.
Option Explicit
Dim d As HAPI.Domain
Dim glob as new HAPI.Globals
Private Const MurderYes = 0
Private Const MurderNo = 1
Private Const DNAMatch = 0
Private Const DNANoMatch = 1

On the Load of the Form

The Form_Load() event is triggered when the form is loaded. We use it to initialize the global variables and display the guilty probability in the initial no state of the DNA-match? node.
Private Sub Form_Load()
    ' A collection to hold the found parseErrors
     Dim hc as new HuginCollection
     Dim parseErrors As Object ' really a VB 6.0 Collection

    ' Create the Vb 6.0 Collection
     parseErrors=hc.toCollection()

    ' Load the domain from the murder.net file and compile it.
    Set d = glob.LoadDomainFromNet("D:\VB Projects\HelloBayes\murder.net", parseErrors, 10)
    d.Compile
    
     ' Initially the DNANoMatch state is selected - display the probability 
    Call DNAState_Click(DNANoMatch)
    Exit Sub
End Sub

First we need to create an instance of the HAPI.Globals, as VB.net does not allow for singleton instances. This instance is then used when the domain variable is initialized by calling the API function LoadDomainFromNet with the path of the murder network and a HuginCollection object in case of parse errors as arguments. Here, we need to use a special HuginCollection, to create a VB 6.0 Collection, since the Collection class in VB.net is not compatible with VB 6.0 Collections. Depending on the location of your murder.net file you might have to use a different path. Hereafter the domain is compiled which triangulates the graph and constructs the junction tree. The domain is now ready for propagation.

Initially the DNANoMatch option button is selected. We call the sub procedure described in the next section to display the guilty probability.

When closing the Form

A Domain object holds a reference to all the Nodes contained in it, and these nodes again hold a reference to the Domain object. Therefore the garbage collector in Visual Basic cannot determine if a Domain objects has been released by an application. This is a domain should be explicitly deleted once you are done using it.
Private Sub Form_Terminate()
    ' We are done using the domain - explicitly delete it.
    d.Delete
End Sub

Clicking on one of the option buttons

The DNAState_Click event is triggered whenever the selected DNAState is changed.
Private Sub DNAState_Click(Index As Integer)
    Dim n As HAPI.Node
    
    ' Fetch the node with label "DNA-match?" and name "DNA" from the domain
    Set n = d.GetNodeByName("DNA")
    
    ' Instantiate the Node with state "Index" as evidence and sum propagate it
    n.SelectState (Index) 
    Call d.Propagate(hEquilibriumSum, hModeNormal)
    
    ' Fetch the "Murderer?" node and display the probability for MurderYes
    Set n = d.GetNodeByName("M")
    GuiltyProb.Caption = Format(n.Belief(MurderYes) * 100, "##0.00000000") & "%"
End Sub

To instantiate the DNA-match? node we first fetch it from the domain by calling GetNodeByName with the DNA-match? node's name ("DNA") as parameter. Hereafter we enter the state selected by the DNAState options as a finding and sum-propagate the information in the network. Finally the probability for the suspect to be guilty is displayed by calling GetBelief for the Murderer? node with MurderYes as the desired state.

You should now be able to to compile and run the application. The low probability in case of a DNA match indicates that such a case needs substantial strengthening before going to trial. Hopefully the probabilities will persuade Colombo to rely on more sophisticated investigation methods!