Exemple #1
0
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' ]
Exemple #2
0
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))
Exemple #5
0
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))
Exemple #6
0
# 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',
                 AND('h c d j',
Exemple #7
0
from production import AND, OR, NOT, PASS, FAIL, IF, THEN, \
     match, populate, simplify, variables
from zookeeper import ZOOKEEPER_RULES

# This function, which you need to write, takes in a hypothesis
# that can be determined using a set of rules, and outputs a goal
# tree of which statements it would need to test to prove that
# hypothesis. Refer to the problem set (section 2) for more
# detailed specifications and examples.

# Note that this function is supposed to be a general
# backchainer.  You should not hard-code anything that is
# specific to a particular rule set.  The backchainer will be
# tested on things other than ZOOKEEPER_RULES.

(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(rules, hypothesis):
    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:
Exemple #8
0
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
# 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
Exemple #10
0
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)'))
Exemple #11
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
Exemple #12
0
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
Exemple #13
0
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
Exemple #14
0
# 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_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)'))
Exemple #16
0
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'])
Exemple #17
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)
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
Exemple #18
0
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]
Exemple #19
0
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
Exemple #20
0
                     frozenset) == tree_map(type_encode(result_bc_4),
                                            frozenset))


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 17 ###

# This test checks to make sure that your backchainer produces
# the correct goal tree given the hypothesis 'zot' and using the
# rules defined in ARBITRARY_EXP below.

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', AND('h c d j', OR('h i j', 'i f e', 'i g', 'g i')))


def backchain_to_goal_tree_5_testanswer(val, original_args=None):
    return (tree_map(type_encode(val),
                     frozenset) == tree_map(type_encode(result_bc_5),
Exemple #21
0
#!/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)
Exemple #22
0
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))
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
# 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
Exemple #25
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
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)')),
Exemple #27
0
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(
Exemple #28
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
Exemple #29
0
#### 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)',
Exemple #30
0
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)"))