def backchain_to_goal_tree_4_getargs(): return [ [ IF( AND( '(?x) has (?y)', '(?x) has (?z)' ), THEN( '(?x) has (?y) and (?z)' ) ), IF( '(?x) has rhythm and music', THEN( '(?x) could not ask for anything more' ) ) ], 'gershwin could not ask for anything more' ]
def backchain_to_goal_tree_4_getargs(): return [ [ IF(AND("(?x) has (?y)", "(?x) has (?z)"), THEN("(?x) has (?y) and (?z)")), IF( "(?x) has rhythm and music", THEN("(?x) could not ask for anything more"), ), ], "gershwin could not ask for anything more", ]
def make_rule(row, facts): mark, rule = row.split(':') conditions, consequence = rule.split('=>') conditions = [c.strip() for c in conditions.split(',')] conditions = [facts[cond] for cond in conditions] consequence = facts[consequence.strip()] return IF(AND(*conditions), THEN(consequence))
def same_anchor_rule(num=2): statements = [] consequent = "consistent" for i in range(0,num): binding = "(?%c)"%chr(i+CHAR_OFFSET) statements.append( "IsA(%s, (?x))"%binding) consequent += " %s"%binding return IF (AND (statements), THEN(consequent))
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ #find a rule which has the hypothesis as the conclusion and testing its premises # Rule Z14: IF( AND( '(?x) is a bird', #in rule Z3 '(?x) does not fly', #opposite of rule Z4 '(?x) swims', '(?x) has black and white color'), THEN('(?x) is a penguin')) # penguin = opus? #OR('opus is a penguin', AND( #OR('opus is a bird', 'opus has feathers'), #AND('opus flies', 'opus lays eggs')) #'opus does not fly', #'opus swims', #'opus has black and white color' )) #Rule Z14: if results = [hypothesis] for rule in rules: consequent = rule.consequent() for expr in consequent: bindings = match(expr, hypothesis) if bindings or expr == hypothesis: antecedent = rule.antecedent() if isinstance(antecedent, str): new_hypothesis = populate(antecedent, bindings) results.append( backchain_to_goal_tree(rules, new_hypothesis)) results.append(new_hypothesis) else: statements = [ populate(ante_expr, bindings) for ante_expr in antecedent ] new_results = [] for statement in statements: new_results.append( backchain_to_goal_tree(rules, statement)) results.append(create_statement(new_results, antecedent)) return simplify(OR(results))
# the correct goal tree given the hypothesis 'gershwin could not # ask for anything more' and using the rules defined in # backchain_to_goal_tree_4_getargs() above. make_test(type = 'FUNCTION_ENCODED_ARGS', getargs = backchain_to_goal_tree_4_getargs, testanswer = backchain_to_goal_tree_4_testanswer, expected_val = str(result_bc_4) ) ### TEST 14 ### ARBITRARY_EXP = ( IF( AND( 'a (?x)', 'b (?x)' ), THEN( 'c d' '(?x) e' )), IF( OR( '(?y) f e', '(?y) g' ), THEN( 'h (?y) j' )), IF( AND( 'h c d j', 'h i j' ), THEN( 'zot' )), IF( '(?z) i', THEN( 'i (?z)' )) ) def backchain_to_goal_tree_5_getargs(): return [ ARBITRARY_EXP, 'zot' ] result_bc_5 = OR('zot',
# Which rule fires second? ANSWER_5 = '0' # Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ('two-pair beats pair', 'three-of-a-kind beats two-pair', '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
# Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ( 'two-pair beats pair', 'three-of-a-kind beats two-pair', '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
ANSWER_2 = '4' ANSWER_3 = '2' ANSWER_4 = '0' ANSWER_5 = '3' ANSWER_6 = '1' ANSWER_7 = '0' #### Part 2: Transitive Rule ######################################### # Fill this in with your rule transitive_rule = IF(AND('(?x) beats (?y)', '(?y) beats (?z)'), THEN('(?x) beats (?z)')) # You can test your rule by uncommenting these pretty print statements # and observing the results printed to your screen after executing lab1.py # pprint(forward_chain([transitive_rule], abc_data)) # pprint(forward_chain([transitive_rule], poker_data)) # pprint(forward_chain([transitive_rule], minecraft_data)) #### Part 3: Family Relations ######################################### # Define your rules here. We've given you an example rule whose lead you can follow: same_rule = IF(AND('person (?x)', 'person (?x)'), THEN('same (?x) (?x)')) friend_rule = IF(AND("person (?x)", "person (?y)"), THEN("friend (?x) (?y)", "friend (?y) (?x)"))
#### Part 1: Multiple Choice ######################################### ANSWER_1 = '2' ANSWER_2 = 'no' ANSWER_3 = '2' ANSWER_4 = '1' ANSWER_5 = '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: self_rule = IF('person (?x)', THEN('self (?x) (?x)')) sibling_rule = IF( AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('self (?y) (?z)')), THEN('sibling (?y) (?z)', 'sibling (?z) (?y)'))
ANSWER_3 = '2' ANSWER_4 = '0' ANSWER_5 = '3' ANSWER_6 = '1' ANSWER_7 = '0' #### Part 2: Transitive Rule ######################################### # Fill this in with your rule transitive_rule = IF(AND('(?y) beats (?z)', '(?x) beats (?y)'), THEN('(?x) beats (?z)') ) # You can test your rule by uncommenting these pretty print statements # and observing the results printed to your screen after executing lab1.py # pprint(forward_chain([transitive_rule], abc_data)) # pprint(forward_chain([transitive_rule], poker_data)) # pprint(forward_chain([transitive_rule], minecraft_data)) #### Part 3: Family Relations ######################################### # Define your rules here. We've given you an example rule whose lead you can follow: friend_rule = IF( AND("person (?x)", "person (?y)"), THEN ("friend (?x) (?y)", "friend (?y) (?x)") ) same_identity = IF(OR('person (?x)'), THEN('same (?x) (?x)'))
# Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ( 'two-pair beats pair', 'three-of-a-kind beats two-pair', '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
#!/usr/bin/env python2 from production import IF, AND, OR, NOT, THEN, DELETE, forward_chain theft_rule = IF('you have (?x)', THEN('we have (?x)'), DELETE('you have (?x)')) data = ('you have apple', 'you have orange', 'you have pear') print forward_chain([theft_rule], data, verbose=False)
# Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ( 'two-pair beats pair', 'three-of-a-kind beats two-pair', '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) 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
ANSWER_2 = '' ANSWER_3 = '' ANSWER_4 = '' ANSWER_5 = '' ANSWER_6 = '' ANSWER_7 = '' #### Part 2: Transitive Rule ######################################### # Fill this in with your rule transitive_rule = IF(AND(), THEN()) # You can test your rule by uncommenting these pretty print statements # and observing the results printed to your screen after executing lab1.py # pprint(forward_chain([transitive_rule], abc_data)) # pprint(forward_chain([transitive_rule], poker_data)) # pprint(forward_chain([transitive_rule], minecraft_data)) #### Part 3: Family Relations ######################################### # Define your rules here. We've given you an example rule whose lead you can follow: friend_rule = IF(AND("person (?x)", "person (?y)"), THEN("friend (?x) (?y)", "friend (?y) (?x)")) # Add your rules to this list: family_rules = [friend_rule]
# Problem 1.2: Multiple choice # Which part of a rule may change the data? # 1. the antecedent # 2. the consequent # 3. both ANSWER_1 = '2' # A rule-based system about Monty Python's "Dead Parrot" sketch # uses the following rules: from production import IF, AND, OR, NOT, THEN, DELETE, forward_chain rule1 = IF( AND( '(?x) is a Norwegian Blue parrot', '(?x) is motionless' ), THEN( '(?x) is not dead' ) ) rule2 = IF( NOT( '(?x) is dead' ), THEN( '(?x) is pining for the fjords' ) ) data = ( 'Polly is a Norwegian Blue parrot', 'Polly is motionless' ) print forward_chain([rule1, rule2], data, verbose=True) # and the following initial data: # # Will this system produce the datum 'Polly is pining for the # fjords'? Answer 'yes' or 'no'. ANSWER_2 = 'no' # Which rule contains a programming error? Answer '1' or '2'.
# Author: Leilani H. Gilpin # Date: 30 December 2019 # Section: 1 # Email: [email protected] # Description: Rule file for conceptual primitives and reasonableness monitor code. # Based on the MIT 6.034 Lab 1: Rule-Based Systems from production import IF, AND, OR, NOT, THEN, DELETE, forward_chain, pretty_goal_tree from production import PASS, FAIL, match, populate, simplify, variables from data import * import pprint CHAR_OFFSET = 97 # RULES FOR FORWARD CHAINING same_IsA_rule = IF('(?x) IsA (?y)', THEN('self (?x) (?x)')) same_location_rule = IF('(?x) AtLocation (?y)', THEN('self (?x) (?x)')) consistent_anchor_rule = IF(AND("(?x) IsA (?y)", "(?z) IsA (?y)", NOT("self (?x) (?z)")), THEN("(?x) consistent (?z)")) consistent_location_rule = IF(AND("(?x) AtLocation (?y)", "(?z) AtLocation (?y)", NOT("self (?x) (?z)")), #NOT("(?z) sameLocation (?y)")), THEN("(?x) sameLocation (?z)")) anchor_rules = [same_IsA_rule, consistent_anchor_rule] location_rules = [same_location_rule, consistent_location_rule] # make consistent rules? # make size rules
# Which rule fires second? ANSWER_5 = '0' # Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ('two-pair beats pair', 'three-of-a-kind beats two-pair', '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']) """ You will be given data that includes three kinds of statements: - 'male x': x is male - 'female x': x is female
# Which rule fires second? ANSWER_5 = '0' # Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ('two-pair beats pair', 'three-of-a-kind beats two-pair', '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) # pprint(forward_chain([transitive_rule], abc_data)) # pprint(forward_chain([transitive_rule], poker_data)) # pprint(forward_chain([transitive_rule], minecraft_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'])
# Which rule fires second? ANSWER_5 = '0' # Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ('two-pair beats pair', 'three-of-a-kind beats two-pair', '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
ANSWER_2 = '4' ANSWER_3 = '2' ANSWER_4 = '0' ANSWER_5 = '3' ANSWER_6 = '1' ANSWER_7 = '0' #### Part 2: Transitive Rule ######################################### # Fill this in with your rule transitive_rule = IF(AND('(?x) beats (?y)', '(?y) beats (?z)'), THEN('(?x) beats (?z)')) # You can test your rule by uncommenting these pretty print statements # and observing the results printed to your screen after executing lab1.py #pprint(forward_chain([transitive_rule], abc_data)) #pprint(forward_chain([transitive_rule], poker_data)) #pprint(forward_chain([transitive_rule], minecraft_data)) #### Part 3: Family Relations ######################################### # Define your rules here. We've given you an example rule whose lead you can follow: friend_rule = IF(AND("person (?x)", "person (?y)"), THEN("friend (?x) (?y)", "friend (?y) (?x)")) self = IF('parent (?y) (?x)', THEN('self (?x) (?x)'))
# Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ( 'two-pair beats pair', 'three-of-a-kind beats two-pair', '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
def transitive_rule_poker_testanswer(val, original_val=None): if repr(transitive_rule) == repr(IF(AND(), THEN())): raise NotImplementedError return (set(val) == set(poker_answer))
from production import IF, AND, THEN, FAIL, OR # ZOOKEEPER RULES ZOOKEEPER_RULES = ( IF( AND('(?x) has hair'), # Z1 THEN('(?x) is a mammal')), IF( AND('(?x) gives milk'), # Z2 THEN('(?x) is a mammal')), IF( AND('(?x) has feathers'), # Z3 THEN('(?x) is a bird')), IF( AND( '(?x) flies', # Z4 '(?x) lays eggs'), THEN('(?x) is a bird')), IF( AND( '(?x) is a mammal', # Z5 '(?x) eats meat'), THEN('(?x) is a carnivore')), IF( AND( '(?x) is a mammal', # Z6 '(?x) has pointed teeth', '(?x) has claws', '(?x) has forward-pointing eyes'), THEN('(?x) is a carnivore')), IF(
# Problem 1.3.1: Poker hands # You're given this data about poker hands: poker_data = ( 'two-pair beats pair', 'three-of-a-kind beats two-pair', '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' ])
#### Part 1: Multiple Choice ######################################### ANSWER_1 = '2' ANSWER_2 = 'no' ANSWER_3 = '2' ANSWER_4 = '1' ANSWER_5 = '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: self = IF('person (?x)', THEN('self (?x) (?x)')) sibling = IF( AND( 'parent (?x) (?y)',
ANSWER_3 = '2' ANSWER_4 = '0' ANSWER_5 = '3' ANSWER_6 = '1' ANSWER_7 = '0' #### Part 2: Transitive Rule ######################################### # transitive_rule = IF( AND(), THEN() ) 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: # Add your rules to this list: family_rules = [ IF('person (?x)', THEN('self (?x) (?x)')), IF(AND('parent (?p) (?x)', 'parent (?p) (?y)', NOT('self (?x) (?y)')), THEN('sibling (?x) (?y)')),