Beispiel #1
0
    def discrete_query_cb(self, req):
        nd = U.discrete_nodedata_from_ros(req.nodes)
        skel = U.graph_skeleton_from_node_data(nd)
        skel.toporder()
        bn = DiscreteBayesianNetwork(skel, nd)
        fn = TableCPDFactorization(bn)
        q = {n: nd.Vdata[n]["vals"] for n in req.query}
        ev = {ns.node: ns.state for ns in req.evidence}

        rospy.loginfo("resolving query %s with evidence %s" % (q, ev))
        ans = fn.condprobve(query=q, evidence=ev)
        rospy.loginfo("%s -> %s" % (ans.scope, ans.vals))
        res = DiscreteQueryResponse()
        node = DiscreteNode()
        node.name = ans.scope[0]
        node.outcomes = q[node.name]
        node.CPT.append(ConditionalProbability(node.outcomes, ans.vals))
        res.nodes.append(node)
        return res
Beispiel #2
0
def discrete_node_from_dict(name, d):
    n = DiscreteNode()
    n.name = str(name)
    n.outcomes = map(str, d["vals"])
    if d["parents"]:
        n.parents = map(str, d["parents"])
    if d["children"]:
        n.children = map(str, d["children"])
    cprob = d["cprob"]
    if isinstance(cprob, dict):
        n.CPT = [ConditionalProbability(values=eval(k), probabilities=v) for k,v in (d["cprob"]).items()]
    else:
        n.CPT = [ConditionalProbability(values=map(str, d["vals"]), probabilities=cprob)]
    return n
Beispiel #3
0
def discrete_node_from_dict(name, d):
    n = DiscreteNode()
    n.name = str(name)
    n.outcomes = map(str, d["vals"])
    if d["parents"]:
        n.parents = map(str, d["parents"])
    if d["children"]:
        n.children = map(str, d["children"])
    cprob = d["cprob"]
    if isinstance(cprob, dict):
        n.CPT = [
            ConditionalProbability(values=eval(k), probabilities=v)
            for k, v in (d["cprob"]).items()
        ]
    else:
        n.CPT = [
            ConditionalProbability(values=map(str, d["vals"]),
                                   probabilities=cprob)
        ]
    return n
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)
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)