Fundamentals of Artificial Intelligence - Lab 1: Expert Fundamentals of Artificial Intelligence - Lab 1: Expert Systems Systems
Fundamentals of Artificial Intelligence - Lab 1: Expert Fundamentals of Artificial Intelligence - Lab 1: Expert Systems Systems
Systems
Student: Filipescu Mihail
In [ ]:
def print_human_read_backchain(backward_chain):
if isinstance(backward_chain, list):
if isinstance(backward_chain, OR):
print("(", end='')
for o in backward_chain:
print(" or ", end='')
print_human_read_backchain(o)
print(")", end='')
elif isinstance(backward_chain, AND):
print("(", end='')
for a in backward_chain:
print(" and ", end='')
print_human_read_backchain(a)
print("(", end='')
else:
print(backward_chain, end='')
def mixed_questions_gen(rules):
data = []
color_opt = get_color_opt(rules)
print("Tourist has: ")
for i in range(len(color_opt)):
print(i, " ", instantiate(color_opt[i], {'x': 'tourist'}))
num = input("Choose by typing number of color." + "?\n ")
data.append(instantiate(color_opt[int(num)], {'x': 'tourist'}))
data = list(forward_chain(rules, data))
return construct_questions(rules, data)
def get_color_opt(rules):
all_antec = []
for rule in rules:
for ant in rule.antecedent():
m = re.search("^.*skin$", ant)
if m != None:
all_antec.append(m.group(0))
return list(set(all_antec))
while True:
goal_reached = False
counter = 0
for leaf in leafes:
# print(leaf,is_actual(leaf, data, rules, noLeafes))
if data and not is_actual(leaf, data, rules, noLeafes):
counter = counter + 1
continue
dataElement = instantiate(leaf, {'x': 'tourist'})
response = check_response(input(dataElement + "?\n "))
if response:
data.append(dataElement)
data = list(forward_chain(rules, data))
if len(set(data).intersection(final_states)) > 0:
goal_reached = True #goal reached
break
elif not response:
noLeafes.append(leaf)
if counter >= len(leafes) or goal_reached:
break
return data
def get_all_final(rules):
final_states = []
for rule in rules:
final_states.append(rule.consequent()[0])
all_antec = []
for rule in rules:
for ant in rule.antecedent():
all_antec.append(ant)
final_states = list(set(final_states).difference(set(all_antec)))
for i in range(len(final_states)):
final_states[i] = instantiate(final_states[i], {'x': 'tourist'})
return set(final_states)
def get_all_leafes(rules):
leafes = []
for rule in rules:
ant = rule.antecedent()
for x in ant:
if check_if_leaf(x, rules):
leafes.append(x)
return list(dict.fromkeys(leafes))
def is_actual(leaf, data, rules, noLeafes):
found_rules = []
for fact in data:
found_rules = found_rules + get_all_rules_with_expr(fact, rules)
if is_present_in_rules(leaf, found_rules) and not is_present_in_data(leaf, data) and
not was_asked_before(leaf, noLeafes):
return True
return False
def check_response(response):
if response == "yes":
return True
elif response == "no":
return False
else:
return None
Task 3
In [ ]:
In [ ]:
Output: ('tim has dark skin', 'tim has good magica affinity', 'tim has heat tolerance', '
tim is a Dunmer', 'tim is an Elf')
Task 4
In [ ]:
def backward_chain(rules, hypothesis, verbose=False):
"""
Outputs the goal tree from having rules and hyphothesis, works like an "encyclopedia"
"""
length = len(rules)
if length==0:
return hypothesis
tree = OR()
Task 5-6-7
In [ ]:
def print_human_read_backchain(backward_chain):
if isinstance(backward_chain, list):
if isinstance(backward_chain, OR):
print("(", end='')
for o in backward_chain:
print(" or ", end='')
print_human_read_backchain(o)
print(")", end='')
elif isinstance(backward_chain, AND):
print("(", end='')
for a in backward_chain:
print(" and ", end='')
print_human_read_backchain(a)
print("(", end='')
else:
print(backward_chain, end='')
def mixed_questions_gen(rules):
data = []
color_opt = get_color_opt(rules)
print("Tourist has: ")
for i in range(len(color_opt)):
print(i, " ", instantiate(color_opt[i], {'x': 'tourist'}))
num = input("Choose by typing number of color." + "?\n ")
data.append(instantiate(color_opt[int(num)], {'x': 'tourist'}))
data = list(forward_chain(rules, data))
def get_color_opt(rules):
all_antec = []
for rule in rules:
for ant in rule.antecedent():
m = re.search("^.*skin$", ant)
if m != None:
all_antec.append(m.group(0))
return list(set(all_antec))
while True:
goal_reached = False
counter = 0
for leaf in leafes:
# print(leaf,is_actual(leaf, data, rules, noLeafes))
if data and not is_actual(leaf, data, rules, noLeafes):
counter = counter + 1
continue
dataElement = instantiate(leaf, {'x': 'tourist'})
response = check_response(input(dataElement + "?\n "))
if response:
data.append(dataElement)
data = list(forward_chain(rules, data))
if len(set(data).intersection(final_states)) > 0:
goal_reached = True #goal reached
break
elif not response:
noLeafes.append(leaf)
if counter >= len(leafes) or goal_reached:
break
return data
def get_all_final(rules):
final_states = []
for rule in rules:
final_states.append(rule.consequent()[0])
all_antec = []
for rule in rules:
for ant in rule.antecedent():
all_antec.append(ant)
final_states = list(set(final_states).difference(set(all_antec)))
for i in range(len(final_states)):
final_states[i] = instantiate(final_states[i], {'x': 'tourist'})
return set(final_states)
def get_all_leafes(rules):
leafes = []
for rule in rules:
ant = rule.antecedent()
for x in ant:
if check_if_leaf(x, rules):
leafes.append(x)
return list(dict.fromkeys(leafes))
def check_response(response):
if response == "yes":
return True
elif response == "no":
return False
else:
return None
Conclusions:
For this lab, I learned how the expert system works by deducting the solution from given facts. It was quite
curious to play around with the basic structure of Goal tree. For me, the backward -chaining was the most
interesting part, while the generation of questions was a little bit more tedious an challenging. All things
considered, by completing this lab, I developed further my programming skills and a more in detpth Expert sys
understanding.
In [ ]:
# required part