def setUp(self):
     cNode = Node.BayesNode(0, 2, index=0, name="cloudy")
     sNode = Node.BayesNode(1, 2, index=1, name="sprinkler")
     rNode = Node.BayesNode(2, 2, index=2, name="rain")
     wNode = Node.BayesNode(3, 2, index=3, name="wetgrass")
     self.nodes = [cNode, sNode, rNode, wNode]
     self.potential = Distribution.Potential(self.nodes, default=1)
    def setUp(self):
        cNode = Node.BayesNode(0, 2, name="cloudy")
        sNode = Node.BayesNode(1, 2, name="sprinkler")
        rNode = Node.BayesNode(2, 2, name="rain")
        wNode = Node.BayesNode(3, 2, name="wetgrass")

        #cloudy
        cNode.add_child(sNode)
        cNode.add_child(rNode)

        #sprinkler
        sNode.add_parent(cNode)
        sNode.add_child(wNode)

        #rain
        rNode.add_parent(cNode)
        rNode.add_child(wNode)

        #wetgrass
        wNode.add_parent(sNode)
        wNode.add_parent(rNode)

        self.nodes = [cNode, sNode, rNode, wNode]

        #create distributions
        #cloudy distribution
        cDistribution = Distribution.DiscreteDistribution(cNode)
        index = cDistribution.generate_index([], [])
        cDistribution[index] = 0.5
        cNode.set_dist(cDistribution)

        #sprinkler
        dist = zeros([cNode.size(), sNode.size()], type=Float32)
        dist[0, ] = 0.5
        dist[1, ] = [0.9, 0.1]
        sDistribution = Distribution.ConditionalDiscreteDistribution(
            nodes=[cNode, sNode], table=dist)
        sNode.set_dist(sDistribution)
        #rain
        dist = zeros([cNode.size(), rNode.size()], type=Float32)
        dist[0, ] = [0.8, 0.2]
        dist[1, ] = [0.2, 0.8]
        rDistribution = Distribution.ConditionalDiscreteDistribution(
            nodes=[cNode, rNode], table=dist)
        rNode.set_dist(rDistribution)
        #wetgrass
        dist = zeros([sNode.size(), rNode.size(), wNode.size()], type=Float32)
        dist[0, 0, ] = [1.0, 0.0]
        dist[1, 0, ] = [0.1, 0.9]
        dist[0, 1, ] = [0.1, 0.9]
        dist[1, 1, ] = [0.01, 0.99]
        wgDistribution = Distribution.ConditionalDiscreteDistribution(
            nodes=[sNode, rNode, wNode], table=dist)
        wNode.set_dist(wgDistribution)
        #create bayes net
        self.bnet = Graph.BayesNet(self.nodes)
        self.engine = Inference.InferenceEngine(self.bnet)
Example #3
0
    def setUp(self):
        a = Node.DirectedNode(1)
        b = Node.DirectedNode(2)
        c = Node.DirectedNode(3)
        d = Node.DirectedNode(4)
        e = Node.DirectedNode(5)

        a.add_child(b)
        a.add_child(c)
        b.add_parent(a)
        c.add_parent(a)
        d.add_child(b)
        b.add_parent(d)
        e.add_child(a)
        a.add_parent(e)

        self.nodes = [a, b, c, d, e]