-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
136 lines (115 loc) · 4.31 KB
/
Node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from NodeType import NodeType
import numpy as np
import random
class Node(object):
"""An SPN node"""
def add_parent(self, parent):
self.parents.append(parent)
def add_child(self, child):
self.children.append(child)
child.add_parent(self)
def update_map_weight_counts(self):
raise NotImplementedError()
def get_value(self, max_mode=False):
raise NotImplementedError()
def __str__(self):
return '{type} node {name}' \
.format(type=self.type.name, name=self.name)
class SumNode(Node):
"""An SPN sum node"""
def __init__(self, name, children=[]):
self.name = name
self.children = children
# Set random weights
self.links = dict()
for child in self.children:
child.add_parent(self)
link = dict()
link['child'] = child
link['weight'] = random.random()
link['count'] = 0
self.links[child.name] = link
self.normalise_weights()
self.parents = []
self.type = NodeType.SUM
self.value = 0.0
def add_child(self, child, weight=None):
self.children.append(child)
child.add_parent(self)
link = dict()
link['child'] = child
link['weight'] = weight if weight else random.random()
link['count'] = 0
self.links[child.name] = link
def normalise_weights(self):
total = np.sum(list(map(lambda l: l['weight'], self.links.values())))
for link in self.links.values():
link['weight'] /= total
def get_value(self, max_mode=False):
if not max_mode:
self.value = 0.0
for child in self.children:
self.value += self.links[child.name]['weight'] * child.get_value()
return self.value
else:
max_child = {'node': None, 'value': None}
for child in self.children:
value = self.links[child.name]['weight'] * child.get_value(max_mode=True)
if not max_child['value'] or max_child['value'] < value:
max_child['node'] = child
max_child['value'] = value
self.value = max_child['value']
return self.value
def update_map_weight_counts(self):
maximum = {'value': 0, 'node': None}
for child in self.children:
val = child.value * self.links[child.name]['weight']
if val >= maximum['value']:
maximum['value'] = val
maximum['node'] = child
self.links[maximum['node'].name]['count'] += 1
maximum['node'].update_map_weight_counts()
def normalise_counts_as_weights(self):
"""Normalise the counts by normalising so they sum to one, and then set that as the weights"""
# Calculates the total by summing the counts of all links
total = np.sum(list(map(lambda l: l['count'], self.links.values())))
if total == 0: # Set all weights and counts to zero
for link in self.links.values():
link['weight'] = 0.0
link['count'] = 0
else:
for link in self.links.values():
link['weight'] = 1.0 * link['count'] / total
link['count'] = 0
class ProdNode(Node):
"""An SPN product node"""
def __init__(self, name, children=[]):
self.name = name
self.parents = []
self.links = dict()
self.children = children
for child in self.children:
child.add_parent(self)
self.links[child.name] = {'child': child, 'count': 0.0}
self.value = 0.0
self.type = NodeType.PRODUCT
def get_value(self, max_mode=False):
self.value = 1.0
for child in self.children:
self.value = self.value * child.get_value(max_mode=max_mode)
return self.value
def update_map_weight_counts(self):
for child in self.children:
child.update_map_weight_counts()
class LeafNode(Node):
"""An SPN leaf node"""
def __init__(self, name, value=1.0):
self.name = name
self.parents = []
self.value = value
self.children = []
self.type = NodeType.LEAF
def get_value(self, max_mode=False):
return self.value
def update_map_weight_counts(self):
pass