Skip to content
Snippets Groups Projects
Commit ee8edc7c authored by mbaumBielefeld's avatar mbaumBielefeld
Browse files

CPT added and filled with some code

parent 05d1b792
No related branches found
No related tags found
No related merge requests found
from core.BayesNet import * from core.BayesNet import *
from core.Node import * from reasoning.DiscreteNode import DiscreteNode
bn = BayesNet() bn = BayesNet()
n1 = Node("Node1") burglary = DiscreteNode("Burglary", ["Intruder","Safe"])
n2 = Node("Node2") alarm = DiscreteNode("Alarm", ["Ringing", "Silent","Kaputt"])
n3 = Node("Node3") earthquake = DiscreteNode("Earthquake", ["Shaking", "Calm"])
john_calls = DiscreteNode("John calls", ["Calling", "Not Calling"])
mary_calls = DiscreteNode("Mary calls", ["Calling", "Not Calling"])
bn.add_node(n1)
bn.add_node(n2)
bn.add_edge(n1,n2) bn.add_node(burglary)
bn.add_node(alarm)
bn.add_node(earthquake)
bn.add_node(john_calls)
bn.add_node(mary_calls)
n = bn.get_node("Node1")
print n.name
ns = bn.get_nodes(["Node2","Node1"]) bn.add_edge(burglary,alarm)
for n in ns: bn.add_edge(earthquake, alarm)
print n.name bn.add_edge(alarm, john_calls)
bn.add_edge(alarm, mary_calls)
print "Removing existing edge" burglary.set_probability(0.2,[(burglary,"Intruder")])
bn.remove_edge(n1, n2)
print "Removing not existing edge" alarm.set_probability(0.1,[(alarm,"Ringing"),(burglary,"Safe"),(earthquake,"Calm")])
bn.remove_edge(n1, n2)
bn.draw() bn.draw()
...@@ -24,9 +24,7 @@ class BayesNet(object): ...@@ -24,9 +24,7 @@ class BayesNet(object):
def add_edge(self, node_from, node_to): def add_edge(self, node_from, node_to):
if node_from in self.graph.nodes() and node_to in self.graph.nodes(): if node_from in self.graph.nodes() and node_to in self.graph.nodes():
self.graph.add_edge(node_from, node_to) self.graph.add_edge(node_from, node_to)
node_to.announce_parent(node_from)
#raise Exception("Fixme: Adapt CPD of child-node")
else: else:
raise Exception("Tried to add an Edge between two Nodes of which at least one was not contained in the Bayesnet") raise Exception("Tried to add an Edge between two Nodes of which at least one was not contained in the Bayesnet")
......
import abc
class Node(object): class Node(object):
__metaclass__ = abc.ABCMeta
name = "UninitializedName" name = "UninitializedName"
def __init__(self, node_name): def __init__(self, node_name):
self.name = node_name self.name = node_name
@abc.abstractmethod
def announce_parent(self, node):
"""This method will be called by the graph-management to inform nodes which just
became children of other nodes, so they can adapt themselves (e.g. their cpt)"""
return
def __str__(self):
print self.name
return self.name
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
class AbstractCPD(object): class CPD(object):
'''TODO: write doc''' '''TODO: write doc'''
def __init__(self): def __init__(self):
super(AbstractCPD, self).__init__() super(CPD, self).__init__()
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import numpy
from numpy import array
from reasoning.CPD import CPD from reasoning.CPD import CPD
class CPT(CPD): class CPT(CPD):
'''TODO: write doc''' '''TODO: write doc'''
self.values = numpy.array(0)
def __init__(self): def __init__(self, owner):
super(CPT, self).__init__() super(CPT, self).__init__()
def multiplication(self, factor):
def marginalization() self.owner = owner
self.variables = [owner]
size_of_range = len(owner.value_range)
self.values = numpy.ones(size_of_range) / size_of_range
def reduction() def add_parent(self, parent):
self.variables.append(parent)
ax = self.values.ndim
self.values=numpy.expand_dims(self.values,ax)
self.values=numpy.repeat(self.values,len(parent.value_range),axis = ax)
def set_probability(self, value, node_value_pairs):
index = self.get_cpt_index(node_value_pairs)
self.values[tuple(index)]=value
def get_cpt_index(self, node_value_pairs):
nodes, values = zip(*node_value_pairs)
index = []
for node in self.variables:
index_in_values_list = nodes.index(node)
value = values[index_in_values_list]
index.append(node.value_range.index(value))
return index
def multiplication(self, factor):
raise Exception("Called unimplemented function")
def marginalization(self, value_name):
raise Exception("Called unimplemented function")
def division() def reduction(self):
raise Exception("Called unimplemented function")
def division(self, factor):
raise Exception("Called unimplemented function")
def __str__(self):
return str(self.values)
from CPD import AbstractCPD from CPD import CPD
from CPT import CPT from CPT import CPT
from Gauss import Gauss from Gauss import Gauss
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from reasoning.RandomNode import RandomNode from reasoning.RandomNode import RandomNode
from reasoning.CPD.CPT import CPT
class DiscreteNode(RandomNode): class DiscreteNode(RandomNode):
'''TODO: write doc''' '''TODO: write doc'''
def __init__(self): def __init__(self, name, value_range):
super(DiscreteNode, self).__init__() super(DiscreteNode, self).__init__(name)
\ No newline at end of file
self.value_range = value_range
self.cpd = CPT(self)
def announce_parent(self, node):
self.cpd.add_parent(node)
def __str__(self):
return self.name + "\n" + str(self.cpd)
def set_probability(self, value, node_value_pairs):
self.cpd.set_probability(value, node_value_pairs)
...@@ -4,10 +4,11 @@ from core.Node import Node ...@@ -4,10 +4,11 @@ from core.Node import Node
from reasoning.CPD import CPD from reasoning.CPD import CPD
class AbstractRandomNode(Node): class RandomNode(Node):
'''TODO: write doc''' '''TODO: write doc'''
cpd = CPD() cpd = CPD()
def __init__(self): def __init__(self, name):
super(AbstractRandomNode, self).__init__() super(RandomNode, self).__init__(name)
\ No newline at end of file
from DiscreteNode import DiscreteNode from DiscreteNode import DiscreteNode
from GaussNode import GaussNode from GaussNode import GaussNode
from RandomNode import AbstractRandomNode from RandomNode import RandomNode
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment