'flush beats straight', 'full-house beats flush', 'straight-flush beats full-house' ) # Fill in this rule so that it finds all other combinations of # which poker hands beat which, transitively. For example, it # should be able to deduce that a three-of-a-kind beats a pair, # because a three-of-a-kind beats two-pair, which beats a pair. transitive_rule = IF( AND('(?x) beats (?y)', '(?y) beats (?z)'), THEN('(?x) beats (?z)') ) # You can test your rule like this: # print forward_chain([transitive_rule], poker_data) # Here's some other data sets for the rule. The tester uses # these, so don't change them. TEST_RESULTS_TRANS1 = forward_chain([transitive_rule], [ 'a beats b', 'b beats c' ]) TEST_RESULTS_TRANS2 = forward_chain([transitive_rule], [ 'rock beats scissors', 'scissors beats paper', 'paper beats rock' ]) # Problem 1.3.2: Family relations # First, define all your rules here individually. That is, give # them names by assigning them to variables. This way, you'll be # able to refer to the rules by name and easily rearrange them if # you need to. same_identity = IF(OR('male (?x)', 'female (?x)'), THEN('same_identity (?x) (?x)')) sibling = IF(AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('same_identity (?y) (?z)')), THEN('sibling (?y) (?z)')) brother = IF(AND('sibling (?x) (?y)', 'male (?x)'), THEN('brother (?x) (?y)'))
ANSWER_4 = '0' ANSWER_5 = '3' ANSWER_6 = '1' ANSWER_7 = '0' #### Part 2: Transitive Rule ######################################### transitive_rule = IF( AND('(?x) beats (?y)','(?y) beats (?z)'), THEN('(?x) beats (?z)')) # You can test your rule by uncommenting these print statements: print forward_chain([transitive_rule], abc_data) print forward_chain([transitive_rule], poker_data) print forward_chain([transitive_rule], minecraft_data) #### Part 3: Family Relations ######################################### # Define your rules here: rule1=IF(('person (?x)'), THEN ('self (?x) (?x)')) rule2=IF( AND( 'parent (?x) (?y)','parent (?x) (?z)',NOT ('self (?y) (?z)')), THEN ('sibling (?y) (?z)')) rule3=IF( AND('parent (?x) (?y)'), THEN ('child (?y) (?x)')) rule4=IF( AND( 'parent (?x) (?y)','parent (?z) (?w)', 'sibling (?x) (?z)',NOT ('self (?y) (?w)')), THEN ('cousin (?y) (?w)'),('cousin (?w) (?y)'))
family_rules = [ repeat_rule, parent_child_rule, child_sibling_rule, grandparent_grandchild_rule, cousin_rule ] # Uncomment this to test your data on the Simpsons family: # print(forward_chain(family_rules, simpsons_data, verbose=False)) # These smaller datasets might be helpful for debugging: # pprint(forward_chain(family_rules, sibling_test_data, verbose=True)) # pprint(forward_chain(family_rules, grandparent_test_data, verbose=True)) # The following should generate 14 cousin relationships, representing 7 pairs # of people who are cousins: harry_potter_family_cousins = [ relation for relation in forward_chain( family_rules, harry_potter_family_data, verbose=False) if "cousin" in relation ] # To see if you found them all, uncomment this line: #print(harry_potter_family_cousins) #### Part 4: Backward Chaining ######################################### # Import additional methods for backchaining from production import PASS, FAIL, match, populate, simplify, variables def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list
answers.append(tree.cargo.format(name).encode('utf8')) ask_questions(tree.left, name) else: ask_questions(tree.right, name) run_client = True while run_client: answers = [] print('Give name') name = raw_input('Name: ') type_of_chain = raw_input('Forward or Backward chain?(F/B):') if type_of_chain == "F": ask_questions(rules.tree, name) result = forward_chain(rules.RULES, answers) if len(result) != 0: nationality = result[-1].split()[-1] if nationality in possible_results: print(result[-1]) else: print "Unknown" else: print "Unknown" else: nationality = raw_input('Enter nationality:') print backchain_to_goal_tree(rules.RULES, name + ' is a ' + nationality) HOW_MANY_HOURS_THIS_PSET_TOOK = '' WHAT_I_FOUND_INTERESTING = ''
'straight beats three-of-a-kind', 'flush beats straight', 'full-house beats flush', 'straight-flush beats full-house') # Fill in this rule so that it finds all other combinations of # which poker hands beat which, transitively. For example, it # should be able to deduce that a three-of-a-kind beats a pair, # because a three-of-a-kind beats two-pair, which beats a pair. transitive_rule = IF(AND('(?x) beats (?y)', '(?y) beats (?z)'), THEN('(?x) beats (?z)')) # You can test your rule like this: # print forward_chain([transitive_rule], poker_data) # Here's some other data sets for the rule. The tester uses # these, so don't change them. TEST_RESULTS_TRANS1 = forward_chain([transitive_rule], ['a beats b', 'b beats c']) TEST_RESULTS_TRANS2 = forward_chain( [transitive_rule], ['rock beats scissors', 'scissors beats paper', 'paper beats rock']) # Problem 1.3.2: Family relations # First, define all your rules here individually. That is, give # them names by assigning them to variables. This way, you'll be # able to refer to the rules by name and easily rearrange them if # you need to. same_identity_rule = IF(OR('male (?x)', 'female (?x)'), THEN('same-identity (?x) (?x)')) sibling_rule = IF(
"straight-flush beats full-house", ) # Fill in this rule so that it finds all other combinations of # which poker hands beat which, transitively. For example, it # should be able to deduce that a three-of-a-kind beats a pair, # because a three-of-a-kind beats two-pair, which beats a pair. transitive_rule = IF(AND("(?x) beats (?y)", "(?y) beats (?z)"), THEN("(?x) beats (?z)")) # You can test your rule like this: # print(forward_chain([transitive_rule], poker_data)) # Here's some other data sets for the rule. The tester uses # these, so don't change them. TEST_RESULTS_TRANS1 = forward_chain([transitive_rule], ["a beats b", "b beats c"]) TEST_RESULTS_TRANS2 = forward_chain( [transitive_rule], ["rock beats scissors", "scissors beats paper", "paper beats rock"], ) # Problem 1.3.2: Family relations # First, define all your rules here individually. That is, give # them names by assigning them to variables. This way, you'll be # able to refer to the rules by name and easily rearrange them if # you need to. # Then, put them together into a list in order, and call it # family_rules.