7 from pgm_learner.msg
import DiscreteNode, ConditionalProbability
8 from pgm_learner.srv
import (DiscreteParameterEstimation,
9 DiscreteParameterEstimationResponse,
11 DiscreteQueryResponse,
12 DiscreteStructureEstimation,
13 DiscreteStructureEstimationResponse,
14 LinearGaussianParameterEstimation,
15 LinearGaussianParameterEstimationResponse,
16 LinearGaussianStructureEstimation,
17 LinearGaussianStructureEstimationResponse,
21 from libpgm.nodedata
import NodeData
22 from libpgm.graphskeleton
import GraphSkeleton
23 from libpgm.discretebayesiannetwork
import DiscreteBayesianNetwork
24 from libpgm.pgmlearner
import PGMLearner
25 from libpgm.tablecpdfactorization
import TableCPDFactorization
38 skel = U.graph_skeleton_from_ros(req.graph)
40 data = U.graph_states_dict_from_ros(req.states)
41 res = self.learner.discrete_mle_estimateparams(skel, data)
42 return DiscreteParameterEstimationResponse(U.discrete_nodes_to_ros(res.Vdata))
45 nd = U.discrete_nodedata_from_ros(req.nodes)
46 skel = U.graph_skeleton_from_node_data(nd)
48 bn = DiscreteBayesianNetwork(skel, nd)
49 fn = TableCPDFactorization(bn)
50 q = {n: nd.Vdata[n][
"vals"]
for n
in req.query}
51 ev = {ns.node: ns.state
for ns
in req.evidence}
53 rospy.loginfo(
"resolving query %s with evidence %s" % (q, ev))
54 ans = fn.condprobve(query=q, evidence=ev)
55 rospy.loginfo(
"%s -> %s" % (ans.scope, ans.vals))
56 res = DiscreteQueryResponse()
58 node.name = ans.scope[0]
59 node.outcomes=q[node.name]
60 node.CPT.append(ConditionalProbability(node.outcomes, ans.vals))
61 res.nodes.append(node)
65 states = [{ns.node: ns.state
for ns
in s.node_states}
for s
in req.states]
68 if req.pvalparam != 0.0:
69 pvalparam = req.pvalparam
71 indegree = req.indegree
72 res = self.learner.discrete_constraint_estimatestruct(states,
75 return DiscreteStructureEstimationResponse(U.graph_skeleton_to_ros(res))
78 skel = U.graph_skeleton_from_ros(req.graph)
80 data = U.graph_states_dict_from_ros(req.states)
81 res = self.learner.lg_mle_estimateparams(skel, data)
82 rospy.logdebug(
"parameter estimation: %s" % res.Vdata)
83 return LinearGaussianParameterEstimationResponse(U.linear_gaussian_nodes_to_ros(res.Vdata))
86 states = [{ns.node: ns.state
for ns
in s.node_states}
for s
in req.states]
87 rospy.logdebug(states)
91 if req.pvalparam != 0.0:
92 pvalparam = req.pvalparam
96 indegree = req.indegree
97 rospy.logdebug(
"bins: %d, pvalparam: %f, indegree: %d" % (bins, pvalparam, indegree))
98 res = self.learner.lg_constraint_estimatestruct(states,
102 rospy.logdebug(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
103 rospy.logdebug(res.V)
104 rospy.logdebug(res.E)
105 return LinearGaussianStructureEstimationResponse(U.graph_skeleton_to_ros(res))
108 if __name__ ==
'__main__':
109 rospy.init_node(
"pgm_learner")
def discrete_query_cb(self, req)
def lg_structure_estimation_cb(self, req)
def discrete_structure_estimation_cb(self, req)
def discrete_parameter_estimation_cb(self, req)
def lg_parameter_estimation_cb(self, req)