7 from libpgm.discretebayesiannetwork
import DiscreteBayesianNetwork
8 from libpgm.graphskeleton
import GraphSkeleton
9 from libpgm.nodedata
import NodeData
10 from libpgm.tablecpdfactorization
import TableCPDFactorization
13 from pgm_learner.msg
import DiscreteNode, DiscreteNodeState, ConditionalProbability
14 from pgm_learner.srv
import DiscreteQuery, DiscreteQueryRequest
17 skel = GraphSkeleton()
18 skel.V = [
"prize_door",
"guest_door",
"monty_door"]
19 skel.E = [[
"prize_door",
"monty_door"],
20 [
"guest_door",
"monty_door"]]
27 "children": [
"monty_door"],
28 "vals": [
"A",
"B",
"C"],
29 "cprob": [1.0/3, 1.0/3, 1.0/3],
34 "children": [
"monty_door"],
35 "vals": [
"A",
"B",
"C"],
36 "cprob": [1.0/3, 1.0/3, 1.0/3],
40 "parents": [
"prize_door",
"guest_door"],
42 "vals": [
"A",
"B",
"C"],
44 "['A', 'A']": [0., 0.5, 0.5],
45 "['B', 'B']": [0.5, 0., 0.5],
46 "['C', 'C']": [0.5, 0.5, 0.],
47 "['A', 'B']": [0., 0., 1.],
48 "['A', 'C']": [0., 1., 0.],
49 "['B', 'A']": [0., 0., 1.],
50 "['B', 'C']": [1., 0., 0.],
51 "['C', 'A']": [0., 1., 0.],
52 "['C', 'B']": [1., 0., 0.],
56 bn = DiscreteBayesianNetwork(skel, nd)
57 fn = TableCPDFactorization(bn)
60 "prize_door": [
"A",
"B",
"C"],
67 res = fn.condprobve(query, evidence)
76 ret.insert(ord(prize)-ord(
"A"), 0.)
78 s = set([
"A",
"B",
"C"])
79 d = s.difference([prize, guest])
80 pos = ord(d.pop()) - ord(
"A")
86 rospy.init_node(
"discrete_bayesian_query_sample")
87 query_func = rospy.ServiceProxy(
"pgm_learner/discrete/query", DiscreteQuery)
90 prize_door = DiscreteNode()
91 prize_door.name =
"prize_door" 92 prize_door.children = [
"monty_door"]
93 prize_door.outcomes = [
"A",
"B",
"C"]
94 prize_door.CPT = [ConditionalProbability(values=prize_door.outcomes,
95 probabilities=[1.0/3,1.0/3,1.0/3])]
96 guest_door = DiscreteNode()
97 guest_door.name =
"guest_door" 98 guest_door.children = [
"monty_door"]
99 guest_door.outcomes = [
"A",
"B",
"C"]
100 guest_door.CPT = [ConditionalProbability(values=guest_door.outcomes,
101 probabilities=[1.0/3,1.0/3,1.0/3])]
102 monty_door = DiscreteNode()
103 monty_door.name =
"monty_door" 104 monty_door.parents = [
"prize_door",
"guest_door"]
105 monty_door.outcomes = [
"A",
"B",
"C"]
106 for prize, guest
in it.product([
"A",
"B",
"C"], repeat=2):
107 print str([prize, guest])
109 monty_door.CPT += [ConditionalProbability(values=[prize, guest],
112 req = DiscreteQueryRequest()
113 req.nodes = [prize_door, guest_door, monty_door]
114 req.evidence = [DiscreteNodeState(node=
"guest_door", state=
"A"),
115 DiscreteNodeState(node=
"monty_door", state=
"B"),]
116 req.query = [
"prize_door"]
117 print query_func(req)
119 if __name__ ==
'__main__':
def monty_door_prob(prize, guest)