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)
def __init__(self, name, variables): self.name = name self.variables = set(variables) self.potential = Distribution.Potential(self.variables) self.visited = False self.neighbors_separator = set() self.num_var = len(self.variables)
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 testBasicDiv(self): assert all(self.potential.table == 1) other = Distribution.Potential(self.nodes, default=2) new = self.potential / other assert all(new.table == 0.5) assert all(self.potential.table == 1) self.potential /= other assert all(new.table == 0.5)
def reinit_potential(self): self.potential = Distribution.Potential(self.var)
def __init__(self, name, cx, cy): self.name = name self.var = cx.variables.intersection(cy.variables) self.potential = Distribution.Potential(self.var) self.visited = False self.neighbors = [cx, cy]
def testBasicMultiply(self): new = self.potential * Distribution.Potential(self.nodes[1:3], default=3) # This just checks their nodesets are equal assert (len(new.nodes) == len(self.potential.nodes))
def testPotentialEQ(self): new = Distribution.Potential(self.nodes) assert (self.potential == new)