-
Notifications
You must be signed in to change notification settings - Fork 0
/
Certainty Factors.py
79 lines (66 loc) · 2.34 KB
/
Certainty Factors.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
###############################################
# Edmund Felicidario
# Certainty Factors
# CMPS 3560 - Artificial Intelligence
# 22 March 2022
###############################################
def isInDB( literal, DB ):
for p in DB:
lit, cf = p
if lit == literal:
return True
def getCF( literal, DB ):
for p in DB:
lit, cf = p
if lit == literal:
return cf
DB = [ ("A", 1.00),
("B", 0.70),
("C", 0.75),
("D", 0.80),
("E", 0.50),
("M", -1.0) ] # ... [-1,1]
KB = [ (("Y", "D"), "Z", 0.7),
(("A", "B", "E"), "Y", 0.95),
(("A"), "X", 1.0),
(("C"), "L", 1.0),
(("L", "M"), "N", 1.0) ]
count = 1 # Number of times we've iterated over the whole rule set
changes = True
while changes:
changes = False # Set the flag that there have been no changes to false
print( "Starting iteration " + str(count))
for p in KB: # For each rule in the set of rules...
antecedent, consequent, cf = p
print( "Consdier a rule where: " )
print( antecedent )
print( "implies: " )
print( consequent )
print( " with CF" )
print( cf )
# Determine if all literals in antecedent are also in KB
satisfied = True # Flag for entire premise being satisfied
for q in antecedent: # A and B -> C ... ["A", "B"]
# q will be a string
# KB is a list of strings
if not isInDB(q, DB):
satisfied = False # Flag as false, all clauses must be inferred
# If it passes the above, then antecedent is satisfied
# ...and consequent should be entailed
if satisfied and not isInDB( consequent, DB): # TODO
tmp = 10000
for r in antecedent:
if getCF(r, DB) < tmp:
tmp = getCF(r, DB)
DB.append( (consequent, cf*tmp) )
print ( DB )
changes = True
print( "Antecedent is in DB, consequent is implied, DB is now: ")
print(DB)
elif satisfied and isInDB( consequent, DB ):
print( "Consequent is implied, but was already in DB" )
else:
print( "Consequent is not implied" )
count = count + 1
print( "No more changes. DB is: " )
print(DB)