pgm_learner_server.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
00004 
00005 
00006 import rospy
00007 from pgm_learner.msg import DiscreteNode, ConditionalProbability
00008 from pgm_learner.srv import (DiscreteParameterEstimation,
00009                              DiscreteParameterEstimationResponse,
00010                              DiscreteQuery,
00011                              DiscreteQueryResponse,
00012                              DiscreteStructureEstimation,
00013                              DiscreteStructureEstimationResponse,
00014                              LinearGaussianParameterEstimation,
00015                              LinearGaussianParameterEstimationResponse,
00016                              LinearGaussianStructureEstimation,
00017                              LinearGaussianStructureEstimationResponse,
00018                              )
00019 import pgm_learner.msg_utils as U
00020 
00021 from libpgm.nodedata import NodeData
00022 from libpgm.graphskeleton import GraphSkeleton
00023 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
00024 from libpgm.pgmlearner import PGMLearner
00025 from libpgm.tablecpdfactorization import TableCPDFactorization
00026 
00027 
00028 class PGMLearnerServer(object):
00029     def __init__(self):
00030         self.learner = PGMLearner()
00031         rospy.Service("~discrete/parameter_estimation", DiscreteParameterEstimation, self.discrete_parameter_estimation_cb)
00032         rospy.Service("~discrete/query", DiscreteQuery, self.discrete_query_cb)
00033         rospy.Service("~discrete/structure_estimation", DiscreteStructureEstimation, self.discrete_structure_estimation_cb)
00034         rospy.Service("~linear_gaussian/parameter_estimation", LinearGaussianParameterEstimation, self.lg_parameter_estimation_cb)
00035         rospy.Service("~linear_gaussian/structure_estimation", LinearGaussianStructureEstimation, self.lg_structure_estimation_cb)
00036 
00037     def discrete_parameter_estimation_cb(self, req):
00038         skel = U.graph_skeleton_from_ros(req.graph)
00039         skel.toporder()
00040         data = U.graph_states_dict_from_ros(req.states)
00041         res = self.learner.discrete_mle_estimateparams(skel, data)
00042         return DiscreteParameterEstimationResponse(U.discrete_nodes_to_ros(res.Vdata))
00043 
00044     def discrete_query_cb(self, req):
00045         nd = U.discrete_nodedata_from_ros(req.nodes)
00046         skel = U.graph_skeleton_from_node_data(nd)
00047         skel.toporder()
00048         bn = DiscreteBayesianNetwork(skel, nd)
00049         fn = TableCPDFactorization(bn)
00050         q = {n: nd.Vdata[n]["vals"] for n in req.query}
00051         ev = {ns.node: ns.state for ns in req.evidence}
00052 
00053         rospy.loginfo("resolving query %s with evidence %s" % (q, ev))
00054         ans = fn.condprobve(query=q, evidence=ev)
00055         rospy.loginfo("%s -> %s" % (ans.scope, ans.vals))
00056         res = DiscreteQueryResponse()
00057         node = DiscreteNode()
00058         node.name = ans.scope[0]
00059         node.outcomes=q[node.name]
00060         node.CPT.append(ConditionalProbability(node.outcomes, ans.vals))
00061         res.nodes.append(node)
00062         return res
00063 
00064     def discrete_structure_estimation_cb(self, req):
00065         states = [{ns.node: ns.state for ns in s.node_states} for s in req.states]
00066         pvalparam = 0.05 # default value
00067         indegree = 1 # default value
00068         if req.pvalparam != 0.0:
00069             pvalparam = req.pvalparam
00070         if req.indegree != 0:
00071             indegree = req.indegree
00072         res = self.learner.discrete_constraint_estimatestruct(states,
00073                                                               pvalparam=pvalparam,
00074                                                               indegree=indegree)
00075         return DiscreteStructureEstimationResponse(U.graph_skeleton_to_ros(res))
00076 
00077     def lg_parameter_estimation_cb(self, req):
00078         skel = U.graph_skeleton_from_ros(req.graph)
00079         skel.toporder()
00080         data = U.graph_states_dict_from_ros(req.states)
00081         res = self.learner.lg_mle_estimateparams(skel, data)
00082         rospy.logdebug("parameter estimation: %s" % res.Vdata)
00083         return LinearGaussianParameterEstimationResponse(U.linear_gaussian_nodes_to_ros(res.Vdata))
00084 
00085     def lg_structure_estimation_cb(self, req):
00086         states = [{ns.node: ns.state for ns in s.node_states} for s in req.states]
00087         rospy.logdebug(states)
00088         pvalparam = 0.05 # default value
00089         bins = 10 # default value
00090         indegree = 1 # default value
00091         if req.pvalparam != 0.0:
00092             pvalparam = req.pvalparam
00093         if req.bins != 0:
00094             bins = req.bins
00095         if req.indegree != 0:
00096             indegree = req.indegree
00097         rospy.logdebug("bins: %d, pvalparam: %f, indegree: %d" % (bins, pvalparam, indegree))
00098         res = self.learner.lg_constraint_estimatestruct(states,
00099                                                         pvalparam=pvalparam,
00100                                                         bins=bins,
00101                                                         indegree=indegree)
00102         rospy.logdebug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
00103         rospy.logdebug(res.V)
00104         rospy.logdebug(res.E)
00105         return LinearGaussianStructureEstimationResponse(U.graph_skeleton_to_ros(res))
00106 
00107 
00108 if __name__ == '__main__':
00109     rospy.init_node("pgm_learner")
00110     n = PGMLearnerServer()
00111     rospy.spin()


pgm_learner
Author(s): Yuki Furuta
autogenerated on Wed Jul 10 2019 03:24:11