Ejemplo n.º 1
0
"""
@Author: Joris van Vugt, Moira Berens

Entry point for testing the variable elimination algorithm

"""
from read_bayesnet import BayesNet
from variable_elim import *
from heuristics import *

if __name__ == '__main__':
    # the class BayesNet represents a Bayesian network from a .bif file
    # in several variables
    net = BayesNet('child.bif')

    # these are the variables that should be used for variable elimination
    print 'values', net.values
    print 'parents', net.parents
    print 'nodes', net.nodes


    # Make your variable elimination code in a seperate file: 'variable_elim'.
    # you can call this file as follows:
    ve = VariableElimination(net)

    # If variables are known beforehand, you can represent them in the following way:
    evidence = {'Sick': 'no', 'CO2Report': '<7.5', 'ChestXray': 'Plethoric'}

    # determine you heuristics before you call the run function. This can be done in this file or in a seperate file
    # The heuristics either specifying the elimination ordering (list) or it is a function that determines the elimination ordering
    # given the network. An simple example is:
Ejemplo n.º 2
0
"""
@Author: Joris van Vugt, Moira Berens

Entry point for testing the variable elimination algorithm

"""
from read_bayesnet import BayesNet
from variable_elim import VariableElimination

if __name__ == '__main__':
    # the class BayesNet represents a Bayesian network from a .bif file
    # in several variables
    # net = BayesNet('earthquake.bif')
    net = BayesNet('cancer.bif')
    # net = BayesNet('asia.bif')

    # these are the variables that should be used for variable elimination

    # print '============='
    # print 'values',net.values
    # for x in net.values:
    #     if len(net.values[x])>2:
    #         print '!!!!!!!!!!!!!!!!!!!!!!!!! Danger'

    # print '============='
    # print 'probabilities'
    # for x in net.probabilities:
    #     print '---',x,'---\n',net.probabilities[x]

    # print '============='
    # print 'parents', net.parents
Ejemplo n.º 3
0
"""
@Author: Joris van Vugt, Moira Berens, Leonieke van den Bulk

Entry point for the creation of the variable elimination algorithm in Python 3.
Code to read in Bayesian Networks has been provided. We assume you have installed the pandas package.

"""
from read_bayesnet import BayesNet
from variable_elim import VariableElimination

if __name__ == '__main__':
    # The class BayesNet represents a Bayesian network from a .bif file in several variables
    net = BayesNet(
        'earthquake.bif'
    )  # Format and other networks can be found on http://www.bnlearn.com/bnrepository/

    # These are the variables read from the network that should be used for variable elimination
    #print("Nodes:")
    #print(net.nodes)
    #print("Values:")
    #print(net.values)
    #print("Parents:")
    #print(net.parents)
    #print("Probabilities:")
    #print(net.probabilities)
    #for i in net.probabilities.keys():
    #    print(i)
    #    print(net.probabilities[i])

    # Make your variable elimination code in the seperate file: 'variable_elim'.
    # You use this file as follows:
Ejemplo n.º 4
0
"""
@Author: Joris van Vugt, Moira Berens, Leonieke van den Bulk

Entry point for the creation of the variable elimination algorithm in Python 3.
Code to read in Bayesian Networks has been provided. We assume you have installed the pandas package.

"""
from read_bayesnet import BayesNet
from variable_elim import VariableElimination

if __name__ == '__main__':
    # The class BayesNet represents a Bayesian network from a .bif file in several variables
    net = BayesNet('earthquake.bif')
    # Format and other networks can be found on http://www.bnlearn.com/bnrepository/

    # These are the variables read from the network that should be used for variable elimination
    # print("Nodes:")
    # print(net.nodes)
    # print("Values:")
    # print(net.values)
    # print("Parents:")
    # print(net.parents)
    # print("Probabilities:")
    # print(net.probabilities)

    # Make your variable elimination code in a seperate file: 'variable_elim'.
    # You can call this file as follows:
    ve = VariableElimination(net)

    # Set the node to be queried as follows:
    query = 'Alarm'
Ejemplo n.º 5
0
"""
@Author: Joris van Vugt, Moira Berens

Entry point for testing the variable elimination algorithm

"""
from read_bayesnet import BayesNet
from variable_elim import *
import time

if __name__ == '__main__':
    # the class BayesNet represents a Bayesian network from a .bif file
    # in several variables
    start = time.time()
    net = BayesNet('munin.bif')  #

    # these are the variables that should be used for variable elimination
    #print ('values', net.values)
    #print ('probabilities', net.probabilities)
    #print ('parents', net.parents)
    #print ('nodes', net.nodes)

    # Make your variable elimination code in a seperate file: 'variable_elim'.
    # you can call this file as follows:
    #ve = VariableElimination(net)

    # If variables are known beforehand, you can represent them in the following way:
    # evidence = {'Burglary': 'True'}

    # determine you heuristics before you call the run function. This can be done in this file or in a seperate file
    # The heuristics either specifying the elimination ordering (list) or it is a function that determines the elimination ordering
Ejemplo n.º 6
0
                formulas[formulas.index(factor)] = None

        while None in formulas:
            formulas.remove(None)
        return newFactor, formulas

    def removeFromNet(self, network, node):
        for probs in network.probabilities:
            if node in network.probabilities[probs]:
                network.probabilities[probs] = network.probabilities[
                    probs].drop(node, axis=1)

    def properPrint(self, probabilities):
        for prob in probabilities:
            print(prob)
            print(network.probabilities[prob])

    def properWrite(self, formulas):
        for factor in formulas:
            print(self.network.probabilities[factor])


network = BayesNet('cancer.bif')
VE = VariableElimination(network)
query = "Cancer"
elim_order = VE.elimination_order(query, VE.fewest_factors())
observed = {"Pollution": "low"}
result = VE.run(query, observed, elim_order)
print("Result")
print(VE.network.probabilities[result])