forked from cmnolan/opencog-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formulas.py
132 lines (95 loc) · 3.1 KB
/
formulas.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
try:
from opencog.atomspace import count_to_confidence, confidence_to_count
except ImportError:
from atomspace_remote import count_to_confidence, confidence_to_count
import operator, functools, itertools
from util import concat_lists
DEDUCTION_TERM_WEIGHT = 1.0
INDEPENDENCE_ASSUMPTION_DISCOUNT = 1.0
EXTENSION_TO_INTENSION_DISCOUNT_FACTOR = 1.0
INTENSION_TO_EXTENSION_DISCOUNT_FACTOR = 1.0
def identityFormula(tvs, U):
[(sA, nA)] = tvs
return (sA, nA)
# Won't work with the current control
#def trueFormula(tvs, U):
# [] = tvs
#
# return (1.0, confidence_to_count(1.0))
def deductionSimpleFormula(tvs, U):
(sAB, nAB), (sBC, nBC), (_, nA), (sB, nB), (sC, _) = tvs
# Temporary filtering fix to make sure that nAB >= nA
nA = min(nA, nAB)
sDenominator = low(1 - sB)
nDenominator = low(nB)
w1 = DEDUCTION_TERM_WEIGHT # strength
w2 = 2 - w1 # strength
sAC = (w1 * sAB * sBC
+ w2 * (1 - sAB) * (sC - sB * sBC) / sDenominator)
nAC = INDEPENDENCE_ASSUMPTION_DISCOUNT * nA * nBC / nDenominator
return (sAC, nAC)
def inversionFormula(tvs, U):
(sAB, nAB), (sA, nA), (sB, nB) = tvs
sBA = sAB * sA / low(sB)
nBA = nAB * nB / low(nA)
return (sBA, nBA)
def crispModusPonensFormula(tvs, U):
(sAB, nAB), (sA, nA) = tvs
true = 0.1
if all(x > true for x in [sAB, nAB, sA, nA]):
return (1, confidence_to_count(1))
else:
return (0, 0)
def modusPonensFormula(tvs, U):
(sAB, nAB), (sA, nA) = tvs
# P(B|not A) -- how should we find this?
#BNA = TruthValue(0.5, 0.01)
sBNA, nBNA = (0.5, 0.01)
n2 = min(nAB, nA)
if n2 + nBNA > 0:
s2 = ((sAB * sA * n2 + nBNA +
sBNA * (1 - sA) * nBNA) /
low(n2 + nBNA))
else:
raise NotImplementedError
s2 = BNA.confidence
return (s2, n2)
def notFormula(tvs, U):
[(sA, nA)] = tvs
return (1.0 - sA, nA)
def andSymmetricFormula(tvs, U):
total_strength = 1.0
total_confidence = 1.0
for (s, n) in tvs:
total_strength *= s
total_confidence *= count_to_confidence(n)
return (total_strength, confidence_to_count(total_confidence))
def orFormula(tvs, U):
N = len(tvs)
if N == 1:
return tvs[0]
if N > 2:
# TODO handle via divide-and-conquer or something
raise NotImplementedError("OR calculation not supported for arity > 2")
(sA, nA), (sB, nB) = tvs
A = sA * nB
B = sB * nA
s_tot = sA + sB
n_tot = nA + nB - (A + B) / 2
return (s_tot, n_tot)
def ext2InhFormula(tvs, U):
[(sAB, nAB)] = tvs
sABint = sAB
nABint = nAB * EXTENSION_TO_INTENSION_DISCOUNT_FACTOR
return (sABint, nABint)
def low(n):
return max(n, 0.00001)
# temporal formulas
def beforeFormula(dist1, dist2):
times_event1 = [int(t) for t in dist1.keys()]
times_event2 = [int(t) for t in dist2.keys()]
if all(t_event1 < t_event2 for t_event1 in times_event2 for t_event2 in times_event2):
strength = 1
else:
strength = 0
return strength