def q_without_ros(): skel = GraphSkeleton() skel.V = ["prize_door", "guest_door", "monty_door"] skel.E = [["prize_door", "monty_door"], ["guest_door", "monty_door"]] skel.toporder() nd = NodeData() nd.Vdata = { "prize_door": { "numoutcomes": 3, "parents": None, "children": ["monty_door"], "vals": ["A", "B", "C"], "cprob": [1.0/3, 1.0/3, 1.0/3], }, "guest_door": { "numoutcomes": 3, "parents": None, "children": ["monty_door"], "vals": ["A", "B", "C"], "cprob": [1.0/3, 1.0/3, 1.0/3], }, "monty_door": { "numoutcomes": 3, "parents": ["prize_door", "guest_door"], "children": None, "vals": ["A", "B", "C"], "cprob": { "['A', 'A']": [0., 0.5, 0.5], "['B', 'B']": [0.5, 0., 0.5], "['C', 'C']": [0.5, 0.5, 0.], "['A', 'B']": [0., 0., 1.], "['A', 'C']": [0., 1., 0.], "['B', 'A']": [0., 0., 1.], "['B', 'C']": [1., 0., 0.], "['C', 'A']": [0., 1., 0.], "['C', 'B']": [1., 0., 0.], }, }, } bn = DiscreteBayesianNetwork(skel, nd) fn = TableCPDFactorization(bn) query = { "prize_door": ["A","B","C"], } evidence = { "guest_door": "A", "monty_door": "B", } res = fn.condprobve(query, evidence) print res.vals print res.scope print res.card print res.stride
def q_without_ros(): skel = GraphSkeleton() skel.V = ["prize_door", "guest_door", "monty_door"] skel.E = [["prize_door", "monty_door"], ["guest_door", "monty_door"]] skel.toporder() nd = NodeData() nd.Vdata = { "prize_door": { "numoutcomes": 3, "parents": None, "children": ["monty_door"], "vals": ["A", "B", "C"], "cprob": [1.0 / 3, 1.0 / 3, 1.0 / 3], }, "guest_door": { "numoutcomes": 3, "parents": None, "children": ["monty_door"], "vals": ["A", "B", "C"], "cprob": [1.0 / 3, 1.0 / 3, 1.0 / 3], }, "monty_door": { "numoutcomes": 3, "parents": ["prize_door", "guest_door"], "children": None, "vals": ["A", "B", "C"], "cprob": { "['A', 'A']": [0., 0.5, 0.5], "['B', 'B']": [0.5, 0., 0.5], "['C', 'C']": [0.5, 0.5, 0.], "['A', 'B']": [0., 0., 1.], "['A', 'C']": [0., 1., 0.], "['B', 'A']": [0., 0., 1.], "['B', 'C']": [1., 0., 0.], "['C', 'A']": [0., 1., 0.], "['C', 'B']": [1., 0., 0.], }, }, } bn = DiscreteBayesianNetwork(skel, nd) fn = TableCPDFactorization(bn) query = { "prize_door": ["A", "B", "C"], } evidence = { "guest_door": "A", "monty_door": "B", } res = fn.condprobve(query, evidence) print res.vals print res.scope print res.card print res.stride
def construct(self): skel = GraphSkeleton() skel.V = self.nodes.keys() skel.E = [] for node, ndata in self.nodes.iteritems(): if ndata['parents']: for p in ndata['parents']: skel.E.append([p, node]) self.nodes[p]['children'].append(node) for node, ndata in self.nodes.iteritems(): if len(ndata['children']) == 0: ndata['children'] = None data = NodeData() data.Vdata = self.nodes skel.toporder() bn = DiscreteBayesianNetwork(skel, data) return bn
def __init__(self, nodes): self.nodes = {} self.children = defaultdict(list) self.parents = defaultdict(list) self.outputs = {} for name, node_spec in nodes.iteritems(): node_type = node_spec["type"] if node_type == "inferred": parents = node_spec["parents"] # store the relationship between these elements for parent in parents: normalised = normalise_name(parent) self.parents[name].append(normalised) self.children[normalised].append(name) truth_table = parse_truth_table(node_spec["p"], parents) node = make_node(truth_table, parents, node_type) self.nodes[name] = node if node_type == "fsm_input": node = make_node([1.0, 0.0], None, node_type) self.nodes[name] = node if node_type == "sensor_input": proxy_node = make_node([1.0, 0.0], None, "proxy") proxy_name = "_proxy_%s" % name self.nodes[proxy_name] = proxy_node self.children[proxy_name].append(name) node = make_node({ "['T']": [1.0, 0.0], "['F']": [0.0, 1.0] }, [proxy_name], node_type) self.nodes[name] = node if node_type == "output": self.outputs[name] = node_spec for node in self.nodes: if len(self.children[node]) > 0: self.nodes[node]["children"] = self.children[node] else: self.nodes[node]["children"] = None # certainty scaling self.event_caution = 0.0 og = OrderedSkeleton() og.V = self.nodes.keys() edges = [] for k, children in self.children.iteritems(): for child in children: edges.append((k, child)) og.E = edges og.toporder() nd = NodeData() nd.Vdata = self.nodes #logging.debug(pprint.pformat(nd.Vdata)) self.net = DiscreteBayesianNetwork(og, nd) self.factor_net = TableCPDFactorization(self.net)
def discrete_nodedata_from_ros(nodes): nd = NodeData() nd.Vdata = {n.name: dict_from_ros_discrete_node(n) for n in nodes} return nd