Visual Basic Example Code

Below is an example VB program that does the same thing as the "Demo" program that ships with Netica C-API.  There is a Visual Studio project for it, called "Netica Demo for VB" within the " Netica\Netica xxx\Programming Examples " folder of the Netica download package. (more info on programming Netica in VB)

 

Sub Main()

On Error GoTo Failed

 

Dim app As Netica.Application

app = New Netica.Application

app.Visible = True

 

Dim net_file_name As String

net_file_name = System.AppDomain.CurrentDomain.BaseDirectory() & "..\..\..\ChestClinic.dne"

Dim net As Netica.Bnet

net = app.ReadBNet(app.NewStream(net_file_name))

net.Compile()

 

Dim TB As Netica.BNode

TB = net.Nodes.Item("Tuberculosis")

Dim belief As Double

belief = TB.GetBelief("present")

MsgBox("The probability of tuberculosis is " & belief)

 

net.Nodes.Item("XRay").EnterFinding("abnormal")

belief = TB.GetBelief("present")

MsgBox("Given an abnormal X-Ray, the probability of tuberculosis is " & belief)

 

net.Nodes.Item("VisitAsia").EnterFinding("visit")

belief = TB.GetBelief("present")

MsgBox("Given abnormal X-Ray and visit to Asia, the probability of tuberculosis is " & belief)

 

net.Nodes.Item("Cancer").EnterFinding("present")

belief = TB.GetBelief("present")

MsgBox("Given abnormal X-Ray, Asia visit, and lung cancer, the probability of tuberculosis is " & belief)

 

net.Delete()

If Not app.UserControl Then

app.Quit()

End If

 

Exit Sub

Failed:

 

MsgBox("NeticaDemo: Error " & (Err.Number And &H7FFFS) & ": " & Err.Description)

 

End Sub

 

 

Below is an example that reads in a net from the Examples folder (you may have to change the path), then reads in cases and does belief updating.

------------------------------

 

Sub Main()

On Error GoTo Failed

 

Dim app As Netica.Application

app = New Netica.Application

Dim casefile As Streamer

Dim net As Bnet

Set netfile = app.NewStream("C:\Netica Data\BNs\Car_Diagnosis_0_Learned.dne")

Set casefile = app.NewStream("C:\Netica Data\Cases\Good Cases\Car Cases 10.cas")

Set net = app.ReadBNet(netfile)

net.AutoUpdate = 1

net.Compile

Dim lights_node As Bnode

Set lights_node = net.Node("Lights")

Dim lights_dim As Long

lights_dim = lights_node.GetStateIndex("dim")

Dim id As Long

Dim fr As Double

Dim caseposn As Long

Dim done As Boolean

done = False

caseposn = FirstCase

Do

net.RetractFindings

net.ReadFindings case_posn:=caseposn, stream:=casefile, IDNum:=id, freq:=fr

net.ReadFindings case_posn:=caseposn, stream:=casefile, nodes:=net.Nodes, IDNum:=id, freq:=fr

If caseposn = NoMoreCases Then

done = True

Else

MsgBox "Belief in Lights dim = " & lights_node.GetBelief(lights_dim)

End If

caseposn = NextCase

Loop Until done

net.Delete

Exit Sub

 

Failed:

MsgBox "Error " & ((err.Number And &H7FFF) - 10000) & ": " & err.Description

End Sub

===============================================================================

EXAMPLES ON HOW TO SET CPT TABLE ENTRIES:

-----------------------------------------

Here is how you could set the CPTs of the "Chest Clinic" example from the manual:

 

Dim VisitAsia As BNode, Tuberculosis As BNode, Smoking As BNode

Dim Cancer As BNode, XRay As BNode, TbOrCa As BNode

Set VisitAsia = net.Node("VisitAsia")

...

Set TbOrCa = net.Node("TbOrCa")

 

 

Dim p(0 To 1) As Single

 

p(0) = 0.01: p(1) = 0.99: VisitAsia.CPTable("") = p

 

p(0) = 0.05: p(1) = 0.95: Tuberculosis.CPTable(Array(0)) = p

p(0) = 0.01: p(1) = 0.99: Tuberculosis.CPTable(Array(1)) = p

 

p(0) = 0.5: p(1) = 0.5: Smoking.CPTable("") = p

 

p(0) = 0.1: p(1) = 0.9: Cancer.CPTable(Array(0)) = p:

p(0) = 0.01: p(1) = 0.99: Cancer.CPTable(Array(1)) = p

 

p(0) = 0.98: p(1) = 0.02: XRay.CPTable(Array(0)) = p

p(0) = 0.05: p(1) = 0.95: XRay.CPTable(Array(1)) = p

 

p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(0, 0)) = p:

p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(0, 1)) = p:

p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(1, 0)) = p:

p(0) = 0: p(1) = 1: TbOrCa.CPTable(Array(1, 1)) = p:

 

Here are 6 alternate ways to set the CPT of the TbOrCa node.

 

Dim p(0 To 1) As Single

Dim s(0 To 1) As Integer

s(1) = 0: s(0) = 0: p(0) = 1: p(1) = 0: TbOrCa.CPTable(s) = p:

s(0) = 1: TbOrCa.CPTable(s) = p:

s(1) = 1: s(0) = 0: TbOrCa.CPTable(s) = p:

s(0) = 1: p(0) = 0: p(1) = 1: TbOrCa.CPTable(s) = p:

 

 

Dim p(0 To 1) As Single

p(0) = 1: p(1) = 0: TbOrCa.CPTable("present,present") = p:

p(0) = 1: p(1) = 0: TbOrCa.CPTable("present,absent") = p:

p(0) = 1: p(1) = 0: TbOrCa.CPTable("absent,present") = p:

p(0) = 0: p(1) = 1: TbOrCa.CPTable("absent,absent") = p:

 

 

TbOrCa.StateFuncTable("present,present") = "true":

TbOrCa.StateFuncTable("present,absent") = "true":

TbOrCa.StateFuncTable("absent,present") = "true":

TbOrCa.StateFuncTable("absent,absent") = "false":

 

 

TbOrCa.CPTable("*,*") = Array(1, 0)

TbOrCa.CPTable("absent,absent") = Array(0, 1)

 

 

TbOrCa.StateFuncTable("*,*") = "true"

TbOrCa.StateFuncTable("absent,absent") = "false"

 

 

TbOrCa.Equation = "TbOrCa (Tuberculosis, Cancer) = (Tuberculosis || Cancer)"

TbOrCa.EquationToTable num_samples:=1, samp_unc:=False, add_exist:=False