pgm_learner_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 
6 import rospy
7 from pgm_learner.msg import DiscreteNode, ConditionalProbability
8 from pgm_learner.srv import (DiscreteParameterEstimation,
9  DiscreteParameterEstimationResponse,
10  DiscreteQuery,
11  DiscreteQueryResponse,
12  DiscreteStructureEstimation,
13  DiscreteStructureEstimationResponse,
14  LinearGaussianParameterEstimation,
15  LinearGaussianParameterEstimationResponse,
16  LinearGaussianStructureEstimation,
17  LinearGaussianStructureEstimationResponse,
18  )
19 import pgm_learner.msg_utils as U
20 
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
26 
27 
28 class PGMLearnerServer(object):
29  def __init__(self):
30  self.learner = PGMLearner()
31  rospy.Service("~discrete/parameter_estimation", DiscreteParameterEstimation, self.discrete_parameter_estimation_cb)
32  rospy.Service("~discrete/query", DiscreteQuery, self.discrete_query_cb)
33  rospy.Service("~discrete/structure_estimation", DiscreteStructureEstimation, self.discrete_structure_estimation_cb)
34  rospy.Service("~linear_gaussian/parameter_estimation", LinearGaussianParameterEstimation, self.lg_parameter_estimation_cb)
35  rospy.Service("~linear_gaussian/structure_estimation", LinearGaussianStructureEstimation, self.lg_structure_estimation_cb)
36 
38  skel = U.graph_skeleton_from_ros(req.graph)
39  skel.toporder()
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))
43 
44  def discrete_query_cb(self, req):
45  nd = U.discrete_nodedata_from_ros(req.nodes)
46  skel = U.graph_skeleton_from_node_data(nd)
47  skel.toporder()
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}
52 
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()
57  node = DiscreteNode()
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)
62  return res
63 
65  states = [{ns.node: ns.state for ns in s.node_states} for s in req.states]
66  pvalparam = 0.05 # default value
67  indegree = 1 # default value
68  if req.pvalparam != 0.0:
69  pvalparam = req.pvalparam
70  if req.indegree != 0:
71  indegree = req.indegree
72  res = self.learner.discrete_constraint_estimatestruct(states,
73  pvalparam=pvalparam,
74  indegree=indegree)
75  return DiscreteStructureEstimationResponse(U.graph_skeleton_to_ros(res))
76 
78  skel = U.graph_skeleton_from_ros(req.graph)
79  skel.toporder()
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))
84 
86  states = [{ns.node: ns.state for ns in s.node_states} for s in req.states]
87  rospy.logdebug(states)
88  pvalparam = 0.05 # default value
89  bins = 10 # default value
90  indegree = 1 # default value
91  if req.pvalparam != 0.0:
92  pvalparam = req.pvalparam
93  if req.bins != 0:
94  bins = req.bins
95  if req.indegree != 0:
96  indegree = req.indegree
97  rospy.logdebug("bins: %d, pvalparam: %f, indegree: %d" % (bins, pvalparam, indegree))
98  res = self.learner.lg_constraint_estimatestruct(states,
99  pvalparam=pvalparam,
100  bins=bins,
101  indegree=indegree)
102  rospy.logdebug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
103  rospy.logdebug(res.V)
104  rospy.logdebug(res.E)
105  return LinearGaussianStructureEstimationResponse(U.graph_skeleton_to_ros(res))
106 
107 
108 if __name__ == '__main__':
109  rospy.init_node("pgm_learner")
111  rospy.spin()


pgm_learner
Author(s): Yuki Furuta
autogenerated on Wed Jul 10 2019 03:47:12