00001
00002
00003
00004
00005 import itertools as it
00006
00007 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
00008 from libpgm.graphskeleton import GraphSkeleton
00009 from libpgm.nodedata import NodeData
00010 from libpgm.tablecpdfactorization import TableCPDFactorization
00011
00012 import rospy
00013 from pgm_learner.msg import DiscreteNode, DiscreteNodeState, ConditionalProbability
00014 from pgm_learner.srv import DiscreteQuery, DiscreteQueryRequest
00015
00016 def q_without_ros():
00017 skel = GraphSkeleton()
00018 skel.V = ["prize_door", "guest_door", "monty_door"]
00019 skel.E = [["prize_door", "monty_door"],
00020 ["guest_door", "monty_door"]]
00021 skel.toporder()
00022 nd = NodeData()
00023 nd.Vdata = {
00024 "prize_door": {
00025 "numoutcomes": 3,
00026 "parents": None,
00027 "children": ["monty_door"],
00028 "vals": ["A", "B", "C"],
00029 "cprob": [1.0/3, 1.0/3, 1.0/3],
00030 },
00031 "guest_door": {
00032 "numoutcomes": 3,
00033 "parents": None,
00034 "children": ["monty_door"],
00035 "vals": ["A", "B", "C"],
00036 "cprob": [1.0/3, 1.0/3, 1.0/3],
00037 },
00038 "monty_door": {
00039 "numoutcomes": 3,
00040 "parents": ["prize_door", "guest_door"],
00041 "children": None,
00042 "vals": ["A", "B", "C"],
00043 "cprob": {
00044 "['A', 'A']": [0., 0.5, 0.5],
00045 "['B', 'B']": [0.5, 0., 0.5],
00046 "['C', 'C']": [0.5, 0.5, 0.],
00047 "['A', 'B']": [0., 0., 1.],
00048 "['A', 'C']": [0., 1., 0.],
00049 "['B', 'A']": [0., 0., 1.],
00050 "['B', 'C']": [1., 0., 0.],
00051 "['C', 'A']": [0., 1., 0.],
00052 "['C', 'B']": [1., 0., 0.],
00053 },
00054 },
00055 }
00056 bn = DiscreteBayesianNetwork(skel, nd)
00057 fn = TableCPDFactorization(bn)
00058
00059 query = {
00060 "prize_door": ["A","B","C"],
00061 }
00062 evidence = {
00063 "guest_door": "A",
00064 "monty_door": "B",
00065 }
00066
00067 res = fn.condprobve(query, evidence)
00068 print res.vals
00069 print res.scope
00070 print res.card
00071 print res.stride
00072
00073 def monty_door_prob(prize, guest):
00074 if prize == guest:
00075 ret = [0.5,0.5]
00076 ret.insert(ord(prize)-ord("A"), 0.)
00077 else:
00078 s = set(["A","B","C"])
00079 d = s.difference([prize, guest])
00080 pos = ord(d.pop()) - ord("A")
00081 ret = [0.,0.]
00082 ret.insert(pos, 1.)
00083 return ret
00084
00085 def q():
00086 rospy.init_node("discrete_bayesian_query_sample")
00087 query_func = rospy.ServiceProxy("pgm_learner/discrete/query", DiscreteQuery)
00088
00089
00090 prize_door = DiscreteNode()
00091 prize_door.name = "prize_door"
00092 prize_door.children = ["monty_door"]
00093 prize_door.outcomes = ["A", "B", "C"]
00094 prize_door.CPT = [ConditionalProbability(values=prize_door.outcomes,
00095 probabilities=[1.0/3,1.0/3,1.0/3])]
00096 guest_door = DiscreteNode()
00097 guest_door.name = "guest_door"
00098 guest_door.children = ["monty_door"]
00099 guest_door.outcomes = ["A", "B", "C"]
00100 guest_door.CPT = [ConditionalProbability(values=guest_door.outcomes,
00101 probabilities=[1.0/3,1.0/3,1.0/3])]
00102 monty_door = DiscreteNode()
00103 monty_door.name = "monty_door"
00104 monty_door.parents = ["prize_door", "guest_door"]
00105 monty_door.outcomes = ["A", "B", "C"]
00106 for prize, guest in it.product(["A","B","C"], repeat=2):
00107 print str([prize, guest])
00108 print monty_door_prob(prize, guest)
00109 monty_door.CPT += [ConditionalProbability(values=[prize, guest],
00110 probabilities=monty_door_prob(prize=prize, guest=guest))]
00111
00112 req = DiscreteQueryRequest()
00113 req.nodes = [prize_door, guest_door, monty_door]
00114 req.evidence = [DiscreteNodeState(node="guest_door", state="A"),
00115 DiscreteNodeState(node="monty_door", state="B"),]
00116 req.query = ["prize_door"]
00117 print query_func(req)
00118
00119 if __name__ == '__main__':
00120 q()