package com.bayesserver.examples;
import com.bayesserver.*;
import com.bayesserver.inference.*;
public class DecisionGraphExample
{
public static void main(String[] args) throws Exception {
Network network = new Network();
State oilDry = new State("Dry");
State oilWet = new State("Wet");
State oilSoaking = new State("Soaking");
Variable oil = new Variable("Oil", oilDry, oilWet, oilSoaking);
Node nodeOil = new Node(oil);
network.getNodes().add(nodeOil);
State testResultClosed = new State("Closed");
State testResultOpen = new State("Open");
State testResultDiffuse = new State("Diffuse");
Variable testResult = new Variable("Test Result", testResultClosed, testResultOpen, testResultDiffuse);
Node nodeTestResult = new Node(testResult);
network.getNodes().add(nodeTestResult);
State testYes = new State("Yes");
State testNo = new State("No");
Variable test = new Variable("Test?", VariableValueType.DISCRETE, VariableKind.DECISION);
test.getStates().add(testYes);
test.getStates().add(testNo);
Node nodeTest = new Node(test);
network.getNodes().add(nodeTest);
State drillYes = new State("Yes");
State drillNo = new State("No");
Variable drill = new Variable("Drill?", VariableValueType.DISCRETE, VariableKind.DECISION);
drill.getStates().add(drillYes);
drill.getStates().add(drillNo);
Node nodeDrill = new Node(drill);
network.getNodes().add(nodeDrill);
Variable drillUtility = new Variable("Drill utility", VariableValueType.CONTINUOUS, VariableKind.UTILITY);
Node nodeDrillUtility = new Node(drillUtility);
network.getNodes().add(nodeDrillUtility);
Variable testUtility = new Variable("Test utility", VariableValueType.CONTINUOUS, VariableKind.UTILITY);
Node nodeTestUtility = new Node(testUtility);
network.getNodes().add(nodeTestUtility);
Variable meu = new Variable("MEU", VariableValueType.CONTINUOUS, VariableKind.UTILITY);
Node nodeMeu = new Node(meu);
network.getNodes().add(nodeMeu);
NetworkLinkCollection links = network.getLinks();
links.add(new Link(nodeOil, nodeTestResult));
links.add(new Link(nodeOil, nodeDrillUtility));
links.add(new Link(nodeTestResult, nodeDrill));
links.add(new Link(nodeTest, nodeTestResult));
links.add(new Link(nodeTest, nodeDrill));
links.add(new Link(nodeTest, nodeTestUtility));
links.add(new Link(nodeDrill, nodeDrillUtility));
links.add(new Link(nodeDrillUtility, nodeMeu));
links.add(new Link(nodeTestUtility, nodeMeu));
Table tableOil = nodeOil.newDistribution().getTable();
tableOil.set(0.5, oilDry);
tableOil.set(0.3, oilWet);
tableOil.set(0.2, oilSoaking);
nodeOil.setDistribution(tableOil);
Table tableTestResult = nodeTestResult.newDistribution().getTable();
double third = 1.0 / 3.0;
new TableIterator(
tableTestResult,
new Node[] { nodeOil, nodeTest, nodeTestResult }
).copyFrom(
new double[]{
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);
Table tableTest = nodeTest.newDistribution().getTable();
tableTest.normalize(true);
nodeTest.setDistribution(tableTest);
Table tableDrill = nodeDrill.newDistribution().getTable();
tableDrill.normalize(true);
nodeDrill.setDistribution(tableDrill);
CLGaussian gaussianDrillUtility = (CLGaussian)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);
CLGaussian gaussianTestUtility = (CLGaussian)nodeTestUtility.newDistribution();
gaussianTestUtility.setMean(testUtility, -10.0, testYes);
gaussianTestUtility.setMean(testUtility, 0.0, testNo);
nodeTestUtility.setDistribution(gaussianTestUtility);
CLGaussian gaussianMeu = (CLGaussian)nodeMeu.newDistribution();
gaussianMeu.setWeight(meu, drillUtility, 1.0);
gaussianMeu.setWeight(meu, testUtility, 1.0);
nodeMeu.setDistribution(gaussianMeu);
InferenceFactory factory = new RelevanceTreeInferenceFactory();
Inference inference = factory.createInferenceEngine(network);
QueryOptions queryOptions = factory.createQueryOptions();
QueryOutput queryOutput = factory.createQueryOutput();
queryOptions.setDecisionAlgorithm(DecisionAlgorithm.SINGLE_POLICY_UPDATING_LIGHT);
Table queryOil = new Table(oil);
Table queryDrill = new Table(drill);
CLGaussian queryMeu = new CLGaussian(meu);
CLGaussian queryJoint = new CLGaussian(new Variable[] { meu, oil });
QueryDistributionCollection queryDistributions = inference.getQueryDistributions();
queryDistributions.add(queryOil);
queryDistributions.add(queryDrill);
queryDistributions.add(queryMeu);
queryDistributions.add(queryJoint);
inference.query(queryOptions, queryOutput);
double oilDryValue = queryOil.get(oilDry);
System.out.println(String.format("Oil = Dry\t%s", oilDryValue));
double meuValue = queryMeu.getMean(meu);
System.out.println(String.format("MEU\t%s", meuValue));
double drillYesValue = queryDrill.get(drillYes);
System.out.println(String.format("Drill? = Yes\t%s", drillYesValue));
double meuOilDry = queryJoint.getMean(meu, oilDry);
System.out.println(String.format("MEU Oil=Dry\t%s", meuOilDry));
}
}