def q():
    rospy.init_node("discrete_bayesian_query_sample")
    query_func = rospy.ServiceProxy("pgm_learner/discrete/query", DiscreteQuery)


    prize_door = DiscreteNode()
    prize_door.name = "prize_door"
    prize_door.children = ["monty_door"]
    prize_door.outcomes = ["A", "B", "C"]
    prize_door.CPT = [ConditionalProbability(values=prize_door.outcomes,
                                             probabilities=[1.0/3,1.0/3,1.0/3])]
    guest_door = DiscreteNode()
    guest_door.name = "guest_door"
    guest_door.children = ["monty_door"]
    guest_door.outcomes = ["A", "B", "C"]
    guest_door.CPT = [ConditionalProbability(values=guest_door.outcomes,
                                             probabilities=[1.0/3,1.0/3,1.0/3])]
    monty_door = DiscreteNode()
    monty_door.name = "monty_door"
    monty_door.parents = ["prize_door", "guest_door"]
    monty_door.outcomes = ["A", "B", "C"]
    for prize, guest in it.product(["A","B","C"], repeat=2):
        print str([prize, guest])
        print monty_door_prob(prize, guest)
        monty_door.CPT += [ConditionalProbability(values=[prize, guest],
                                                  probabilities=monty_door_prob(prize=prize, guest=guest))]

    req = DiscreteQueryRequest()
    req.nodes = [prize_door, guest_door, monty_door]
    req.evidence = [DiscreteNodeState(node="guest_door", state="A"),
                    DiscreteNodeState(node="monty_door", state="B"),]
    req.query = ["prize_door"]
    print query_func(req)
Ejemplo n.º 2
0
 def test_query(self):
     teacher_nd = NodeData()
     teacher_nd.load(self.teacher_data_path)
     req = DiscreteQueryRequest()
     req.nodes = U.discrete_nodes_to_ros(teacher_nd.Vdata)
     req.evidence = [DiscreteNodeState("Letter", "weak")]
     req.query = ["Grade"]
     res = self.query(req)
     self.assertEqual(len(res.nodes), 1)
     n = res.nodes[0]
     self.assertEqual(n.name, "Grade")
     self.assertListEqual(['A', 'B', 'C'], n.outcomes)
Ejemplo n.º 3
0
 def test_query(self):
     teacher_nd = NodeData()
     teacher_nd.load(self.teacher_data_path)
     req = DiscreteQueryRequest()
     req.nodes = U.discrete_nodes_to_ros(teacher_nd.Vdata)
     req.evidence = [DiscreteNodeState("Letter", "weak")]
     req.query = ["Grade"]
     res = self.query(req)
     self.assertEqual(len(res.nodes), 1)
     n = res.nodes[0]
     self.assertEqual(n.name, "Grade")
     self.assertListEqual(['A','B','C'], n.outcomes)
def q():
    rospy.init_node("discrete_bayesian_query_sample")
    query_func = rospy.ServiceProxy("pgm_learner/discrete/query",
                                    DiscreteQuery)

    prize_door = DiscreteNode()
    prize_door.name = "prize_door"
    prize_door.children = ["monty_door"]
    prize_door.outcomes = ["A", "B", "C"]
    prize_door.CPT = [
        ConditionalProbability(values=prize_door.outcomes,
                               probabilities=[1.0 / 3, 1.0 / 3, 1.0 / 3])
    ]
    guest_door = DiscreteNode()
    guest_door.name = "guest_door"
    guest_door.children = ["monty_door"]
    guest_door.outcomes = ["A", "B", "C"]
    guest_door.CPT = [
        ConditionalProbability(values=guest_door.outcomes,
                               probabilities=[1.0 / 3, 1.0 / 3, 1.0 / 3])
    ]
    monty_door = DiscreteNode()
    monty_door.name = "monty_door"
    monty_door.parents = ["prize_door", "guest_door"]
    monty_door.outcomes = ["A", "B", "C"]
    for prize, guest in it.product(["A", "B", "C"], repeat=2):
        print str([prize, guest])
        print monty_door_prob(prize, guest)
        monty_door.CPT += [
            ConditionalProbability(values=[prize, guest],
                                   probabilities=monty_door_prob(prize=prize,
                                                                 guest=guest))
        ]

    req = DiscreteQueryRequest()
    req.nodes = [prize_door, guest_door, monty_door]
    req.evidence = [
        DiscreteNodeState(node="guest_door", state="A"),
        DiscreteNodeState(node="monty_door", state="B"),
    ]
    req.query = ["prize_door"]
    print query_func(req)