import jpype
import jpype.imports
from jpype.types import *
classpath = "lib/bayesserver-10.8.jar"
jpype.startJVM(classpath=[classpath])
from com.bayesserver import *
from com.bayesserver.inference import *
network = Network()
oilDry = State('Dry')
oilWet = State('Wet')
oilSoaking = State('Soaking')
oil = Variable('Oil', [oilDry, oilWet, oilSoaking])
nodeOil = Node(oil)
network.getNodes().add(nodeOil)
testResultClosed = State('Closed')
testResultOpen = State('Open')
testResultDiffuse = State('Diffuse')
testResult = Variable('Test Result', [testResultClosed, testResultOpen, testResultDiffuse])
nodeTestResult = Node(testResult)
network.getNodes().add(nodeTestResult)
testYes = State('Yes')
testNo = State('No')
test = Variable('Test?', VariableValueType.DISCRETE, VariableKind.DECISION)
test.getStates().add(testYes)
test.getStates().add(testNo)
nodeTest = Node(test)
network.getNodes().add(nodeTest)
drillYes = State('Yes')
drillNo = State('No')
drill = Variable('Drill?', VariableValueType.DISCRETE, VariableKind.DECISION)
drill.getStates().add(drillYes)
drill.getStates().add(drillNo)
nodeDrill = Node(drill)
network.getNodes().add(nodeDrill)
drillUtility = Variable('Drill utility', VariableValueType.CONTINUOUS, VariableKind.UTILITY)
nodeDrillUtility = Node(drillUtility)
network.getNodes().add(nodeDrillUtility)
testUtility = Variable('Test utility', VariableValueType.CONTINUOUS, VariableKind.UTILITY)
nodeTestUtility = Node(testUtility)
network.getNodes().add(nodeTestUtility)
meu = Variable('MEU', VariableValueType.CONTINUOUS, VariableKind.UTILITY)
nodeMeu = Node(meu)
network.getNodes().add(nodeMeu)
links = network.getLinks()
links.add(Link(nodeOil, nodeTestResult))
links.add(Link(nodeOil, nodeDrillUtility))
links.add(Link(nodeTestResult, nodeDrill))
links.add(Link(nodeTest, nodeTestResult))
links.add(Link(nodeTest, nodeDrill))
links.add(Link(nodeTest, nodeTestUtility))
links.add(Link(nodeDrill, nodeDrillUtility))
links.add(Link(nodeDrillUtility, nodeMeu))
links.add(Link(nodeTestUtility, nodeMeu))
tableOil = nodeOil.newDistribution().getTable()
tableOil.set(0.5, [oilDry])
tableOil.set(0.3, [oilWet])
tableOil.set(0.2, [oilSoaking])
nodeOil.setDistribution(tableOil)
tableTestResult = nodeTestResult.newDistribution().getTable()
third = 1.0 / 3.0
TableIterator(
tableTestResult,
[nodeOil, nodeTest, nodeTestResult]
).copyFrom(
[0.1, 0.3, 0.6, third, third, third, 0.3, 0.4, 0.3, third, third, third, 0.5, 0.4, 0.1, third, third, third])
nodeTestResult.setDistribution(tableTestResult)
tableTest = nodeTest.newDistribution().getTable()
tableTest.normalize(True)
nodeTest.setDistribution(tableTest)
tableDrill = nodeDrill.newDistribution().getTable()
tableDrill.normalize(True)
nodeDrill.setDistribution(tableDrill)
gaussianDrillUtility = nodeDrillUtility.newDistribution()
gaussianDrillUtility.setMean(drillUtility, -70.0, [oilDry, drillYes])
gaussianDrillUtility.setMean(drillUtility, 0.0, [oilDry, drillNo])
gaussianDrillUtility.setMean(drillUtility, 50.0, [oilWet, drillYes])
gaussianDrillUtility.setMean(drillUtility, 0.0, [oilWet, drillNo])
gaussianDrillUtility.setMean(drillUtility, 200.0, [oilSoaking, drillYes])
gaussianDrillUtility.setMean(drillUtility, 0.0, [oilSoaking, drillNo])
nodeDrillUtility.setDistribution(gaussianDrillUtility)
gaussianTestUtility = nodeTestUtility.newDistribution()
gaussianTestUtility.setMean(testUtility, -10.0, [testYes])
gaussianTestUtility.setMean(testUtility, 0.0, [testNo])
nodeTestUtility.setDistribution(gaussianTestUtility)
gaussianMeu = nodeMeu.newDistribution()
gaussianMeu.setWeight(meu, drillUtility, 1.0)
gaussianMeu.setWeight(meu, testUtility, 1.0)
nodeMeu.setDistribution(gaussianMeu)
factory = RelevanceTreeInferenceFactory()
inference = factory.createInferenceEngine(network)
queryOptions = factory.createQueryOptions()
queryOutput = factory.createQueryOutput()
queryOptions.setDecisionAlgorithm(DecisionAlgorithm.SINGLE_POLICY_UPDATING_LIGHT)
queryOil = Table(oil)
queryDrill = Table(drill)
queryMeu = CLGaussian(meu)
queryJoint = CLGaussian([meu, oil])
queryDistributions = inference.getQueryDistributions()
queryDistributions.add(QueryDistribution(queryOil))
queryDistributions.add(QueryDistribution(queryDrill))
queryDistributions.add(QueryDistribution(queryMeu))
queryDistributions.add(QueryDistribution(queryJoint))
inference.query(queryOptions, queryOutput)
oilDryValue = queryOil.get([oilDry])
print('Oil = Dry\t{}'.format(oilDryValue))
meuValue = queryMeu.getMean(meu)
print('MEU\t{}'.format(meuValue))
drillYesValue = queryDrill.get([drillYes])
print('Drill? = Yes\t{}'.format(drillYesValue))
meuOilDry = queryJoint.getMean(meu, [oilDry])
print('MEU Oil=Dry\t{}'.format(meuOilDry))