Impact analysis in Python

# __author__ = 'Bayes Server'
# __version__= '0.1'

from jpype import *  # pip install jpype1   (for Java 7, pip install jpype1 0.6.3, and remove convertStrings=False)

# TODO change path to Bayes Server jar file
classpath = 'C:\\Program Files\\Bayes Server\\Bayes Server 8.19\\API\\Java\\bayesserver-8.19.jar'

startJVM(getDefaultJVMPath(), '-Djava.class.path=%s' % classpath, convertStrings=False)

bayes = JPackage('com.bayesserver')
bayes_data = bayes.data
bayes_inference = bayes.inference
bayes_analysis = bayes.analysis

# // TODO change path to Waste network
network_path = 'C:\\ProgramData\\Bayes Server 8.19\\Sample Networks\\Waste.bayes'

network = bayes.Network()
network.load(network_path)

variables = network.getVariables()

# discrete
burning_regimen = variables.get('Burning Regimen', True)
waste_type = variables.get('Waste type', True)
filter_state = variables.get('Filter state', True)

# continuous
filter_efficiency = variables.get('Filter efficiency', True)
dust_emission = variables.get('Dust emission', True)
metals_in_waste = variables.get('Metals in waste', True)
co2_concentration = variables.get('CO2 concentration', True)
light_penetrability = variables.get('Light penetrability', True)
metals_emission = variables.get('Metals emission', True)

filter_state_intact = filter_state.getStates().get('Intact', True)
waste_type_industrial = waste_type.getStates().get('Industrial', True)

evidence = bayes_inference.DefaultEvidence(network)
evidence.set(light_penetrability, java.lang.Double(0.2))
evidence.set(co2_concentration, java.lang.Double(-1.0))
evidence.setState(waste_type_industrial)

options = bayes_analysis.ImpactOptions()
options.setSubsetMethod(bayes_analysis.ImpactSubsetMethod.EXCLUDE)
options.setMaxEvidenceSubsetSize(1)

evidence_to_analyse = [light_penetrability, co2_concentration, waste_type]

output = bayes_analysis.Impact.calculate(
    network,
    filter_state,
    filter_state_intact,
    evidence,
    java.util.Arrays.asList(evidence_to_analyse),
    options
)

print('P(Intact) (all evidence) = {}'.format(output.getHypothesis().getStateProbabilityAll()))
print('P(Intact) (no evidence) = {}'.format(output.getHypothesis().getStateProbabilityNone()))

print()

for item in output.getItems():
    print(','.join([str(b) for b in item.getEvidenceFlags()]))
    print('\tP(Intact)\t{}'.format(item.getStateProbability()))

# Expected output...

# P(Intact)(all evidence) = 0.8806625135255
# P(Intact)(no evidence) = 0.95

# False, True, True
#       P(Intact)   0.95
# True, False, True
#       P(Intact)   0.447566691128873
# True, True, False
#       P(Intact)    0.858460027839224

shutdownJVM()