inference.bas

来自「用VB和MSBN实现线的一个贝叶斯网络的简单例子」· BAS 代码 · 共 74 行

BAS
74
字号
Attribute VB_Name = "Inference"

Option Explicit

' Inference: an Overview
Sub InferenceExample()
    
    Dim aMSBN As New MSBN3Lib.MSBN
    
    'Load the Auto model
    Dim modelAuto As MSBN3Lib.Model
    Set modelAuto = aMSBN.Models.Add("Auto", FileName:="auto.dsc", ErrorFilename:="loaderror.log")
     
    'Call the model's inference engine "inferAuto"
    Dim inferAuto As MSBN3Lib.Engine
    Set inferAuto = modelAuto.Engine
    
    ' Check a probability
    Debug.Print "The probability that the car will start is "
    Debug.Print inferAuto.Belief("EngineStart", "yes")
    Debug.Print
    
    
    
    ' Add some evidence and check some probabilities
    Debug.Print "Tell it that the car, in fact, doesn't start."
    inferAuto.Evidence.Add "EngineStart", "no"
    
    Debug.Print "The probability that the car will start is "
    Debug.Print inferAuto.Belief("EngineStart", "yes")
    Debug.Print "The probability that the battery is good is "
    Debug.Print inferAuto.Belief("Battery", "good")
    Debug.Print
    
    ' Add some more evidence and check some probabilities
    Debug.Print "Tell it the lights don't work"
    inferAuto.Evidence.Add "LightsShine", "don't work"
    Debug.Print "The probability that the battery is good is "
    Debug.Print inferAuto.Belief("Battery", "good")
    Debug.Print
    
    
    ' Change some evidence and check some probabilities
    Debug.Print "Tell it the lights do work"
    inferAuto.Evidence.Set "LightsShine", "work"
    Debug.Print "The probability that the battery is good is "
    Debug.Print inferAuto.Belief("Battery", "good")
    Debug.Print

    'For every node and every state, show the probability
    Dim aNode As MSBN3Lib.Node
    Dim aState As MSBN3Lib.state
    Debug.Print "Nodes and probabilities"
    For Each aNode In modelAuto.ModelNodes
        Debug.Print aNode.Name,
        For Each aState In aNode.States
            Debug.Print "P("; aState.Name; ")="; inferAuto.Belief(aNode, aState),
        Next aState
        Debug.Print
    Next aNode
    Debug.Print
    
    'Show a recommendation
    Debug.Print "Knowing the state of "; inferAuto.Recommendations.Keys(0); "would be most useful."
    Debug.Print
    
    'List all recommendations
    Debug.Print "Node", "Utility"
    For Each aNode In inferAuto.Recommendations.KeyObjects
        Debug.Print aNode.Name, inferAuto.Recommendations(aNode)
    Next aNode

End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?