コード例 #1
0
ファイル: lab1.py プロジェクト: wsnalice/6.034_Fall_15
#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:
sel = IF( 'person (?x)' , THEN('self (?x) (?x)'))
sib_sib = IF( 'sibling (?x) (?y)' , THEN( 'sibling (?y) (?x)' ) )
chi_par = IF( 'child (?y) (?x)' , THEN( 'parent (?x) (?y)' ))
par_chi = IF( 'parent (?x) (?y)' , THEN( 'child (?y) (?x)' ))
cou_cou = IF( 'cousin (?x) (?y)' , THEN( 'cousin (?y) (?x)' ) )
gpa_gch = IF( 'grandparent (?y) (?x)' , THEN( 'grandchild (?x) (?y)' ))
gch_gpa = IF( 'grandchild (?y) (?x)' , THEN( 'grandparent (?x) (?y)' ))
sib = IF( AND(AND('parent (?x) (?y)','parent (?x) (?z)'),NOT ('self (?y) (?z)')), THEN('sibling (?y) (?z)') )
cou = IF( AND( AND('parent (?x) (?y)','parent (?z) (?w)'),'sibling (?x) (?z)' ), THEN('cousin (?y) (?w)') )
gpa = IF( AND('parent (?x) (?y)','parent (?y) (?z)'),THEN('grandparent (?x) (?z)'))


# Add your rules to this list:
family_rules = [sel,sib_sib,chi_par,par_chi,cou_cou,gpa_gch,gch_gpa,sib,cou,gpa]

# 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:
#print forward_chain(family_rules, sibling_test_data, verbose=True)
#print forward_chain(family_rules, grandparent_test_data, verbose=True)

# The following should generate 14 cousin relationships, representing 7 pairs
コード例 #2
0
# 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)' ) )
mother_rule = IF( AND('parent (?y) (?x)','female (?y)' ), THEN( 'mother (?y) (?x)') ) #mother rule
father_rule = IF( AND('parent (?y) (?x)','male (?y)' ), THEN( 'father (?y) (?x)') ) #father rule    
son_rule = IF( AND('male (?x)','parent (?y) (?x)' ), THEN( 'son (?x) (?y)') ) #son rule
daughter_rule = IF( AND('female (?x)','parent (?y) (?x)'), THEN( 'daughter (?x) (?y)') )
grandparent_rule = IF( AND('parent (?y) (?x)','parent (?z) (?y)' ), THEN( 'grandparent (?z) (?x)') )
grandchild_rule = IF( AND('parent (?y) (?x)','parent (?z) (?y)' ), THEN( 'grandchild (?x) (?z)') )
brother_rule = IF( AND('male (?x)','parent (?y) (?x)','parent (?y) (?z)',NOT('same-identity (?x) (?z)') ), THEN( 'brother (?x) (?z)') )
sister_rule = IF( AND('female (?x)','parent (?y) (?x)','parent (?y) (?z)',NOT('same-identity (?x) (?z)') ), THEN( 'sister (?x) (?z)') )
cousin_rule1 = IF( AND('parent (?y) (?b)','parent (?z) (?a)',NOT( 'same-identity (?y) (?z)') , OR( 'brother (?y) (?z)', 'sister (?y) (?z)' )), THEN( 'cousin (?a) (?b)') )
#cousin_rule2 = IF( AND('parent (?y) (?b)','parent (?z) (?a)',NOT( 'same-identity (?y) (?z)') , OR( 'brother (?y) (?z)', 'sister (?y) (?z)' )), THEN( 'cousin (?b) (?a)') )


# Then, put them together into a list in order, and call it
# family_rules.
family_rules =  [same_identity, mother_rule, father_rule, son_rule, daughter_rule,grandparent_rule, grandchild_rule, brother_rule, sister_rule,cousin_rule1]
                
# Some examples to try it on:
# Note: These are used for testing, so DO NOT CHANGE
simpsons_data = ("male bart",
                 "female lisa",
                 "female maggie",
                 "female marge",
コード例 #3
0
                                    ['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_ident_rule = IF(OR('male (?x)', 'female (?x)'),
                     THEN('same_ident (?x) (?x)'))
sibling_rule = IF(
    AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('same_ident (?y) (?z)')),
    THEN('sibling (?y) (?z)', 'sibling (?z) (?y)'))
brother_rule = IF(AND('sibling (?y) (?z)', 'male (?y)'),
                  THEN('brother (?y) (?z)'))
sister_rule = IF(AND('sibling (?y) (?z)', 'female (?y)'),
                 THEN('sister (?y) (?z)'))
mother_rule = IF(AND('parent (?x) (?y)', 'female (?x)'),
                 THEN('mother (?x) (?y)'))
father_rule = IF(AND('parent (?x) (?y)', 'male (?x)'),
                 THEN('father (?x) (?y)'))
son_rule = IF(AND('parent (?x) (?y)', 'male (?y)'), THEN('son (?y) (?x)'))
daughter_rule = IF(AND('parent (?x) (?y)', 'female (?y)'),
                   THEN('daughter (?y) (?x)'))
cousin_rule = IF(
    AND('parent (?x) (?y)', 'parent (?z) (?a)', 'sibling (?x) (?z)'),
    THEN('cousin (?y) (?a)', 'cousin (?a) (?y)'))
コード例 #4
0
mother_rule = IF(AND('female (?x)', 'parent (?x) (?y)'),
                 THEN('mother (?x) (?y)'))
father_rule = IF(AND('male (?x)', 'parent (?x) (?y)'),
                 THEN('father (?x) (?y)'))
son_rule = IF(AND('parent (?x) (?y)', 'male (?y)'), THEN('son (?y) (?x)'))
daughter_rule = IF(AND('parent (?x) (?y)', 'female (?y)'),
                   THEN('daughter (?y) (?x)'))
sibling_rule = IF(
    AND('parent (?x) (?y)', 'parent (?x) (?z)'),
    THEN('sibling (?y) (?z)'),
)
same_identity = IF(AND('sibling (?x) (?y)', 'sibling (?y) (?x)'),
                   THEN('same-identity (?x) (?x)'))
brother_rule = IF(
    AND('male (?x)', 'sibling (?x) (?y)', NOT('same-identity (?x) (?y)')),
    THEN('brother (?x) (?y)'))
sister_rule = IF(
    AND('female (?x)', 'sibling (?x) (?y)', NOT('same-identity (?x) (?y)')),
    THEN('sister (?x) (?y)'))
cousin_rule = IF(
    AND('parent (?p) (?x)', 'parent (?q) (?y)',
        OR('sibling (?p) (?q)', 'sibling (?q) (?p)'),
        NOT('same-identity (?p) (?q)'), NOT('same-identity (?x) (?y)')),
    THEN('cousin (?x) (?y)', 'cousin (?y) (?x)'))
grandparent_rule = IF(
    AND('parent (?p1) (?x)', 'parent (?p2) (?y)', 'sibling p1 p2'),
    THEN('cousin (?x) (?y)'))
grandchild_rule = IF(
    AND('parent (?p1) (?x)', 'parent (?p2) (?y)', 'sibling p1 p2'),
    THEN('cousin (?x) (?y)'),
コード例 #5
0
# family_rules.

# 'brother x y': x is the brother of y (sharing at least one parent)
# 'sister x y': x is the sister of y (sharing at least one parent)
# 'mother x y': x is the mother of y
# 'father x y': x is the father of y
# 'son x y': x is the son of y
# 'daughter x y': x is the daughter of y
# 'cousin x y': x and y are cousins (a parent of x and a parent of y are siblings)
# 'grandparent x y': x is the grandparent of y
# 'grandchild x y': x is the grandchild of y

family_rules = [
    IF(
        AND(OR("male (?x)", "female (?x)", "parent (?x)"),
            NOT("same-identity (?x) (?x)")), THEN("same-identity (?x) (?x)")),
    IF(
        AND("parent (?x) (?z)", "parent (?x) (?y)", "male (?y)",
            NOT("same-identity (?y) (?z)")), THEN("brother (?y) (?z)")),
    IF(
        AND("parent (?x) (?z)", "parent (?x) (?y)", "female (?y)",
            NOT("same-identity (?y) (?z)")), THEN("sister (?y) (?z)")),
    IF(AND("parent (?x) (?y)", "female (?x)"), THEN("mother (?x) (?y)")),
    IF(AND("parent (?x) (?y)", "male (?x)"), THEN("father (?x) (?y)")),
    IF(AND("parent (?x) (?y)", "male (?y)"), THEN("son (?y) (?x)")),
    IF(AND("parent (?x) (?y)", "female (?y)"), THEN("daughter (?y) (?x)")),
    IF(AND("parent (?x) (?y)", "parent (?y) (?z)"),
       THEN("grandparent (?x) (?z)")),
    IF(AND("parent (?x) (?y)", "parent (?y) (?z)"),
       THEN("grandchild (?z) (?x)")),
    IF(
コード例 #6
0
# 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)"))

child_rule = IF(AND('parent (?x) (?y)'), THEN('child (?y) (?x)'))

sibling_rule = IF(
    AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('same (?y) (?z)')),
    THEN('sibling (?y) (?z)', 'sibling (?z) (?y)'))

cousin_rule = IF(
    AND('parent (?u) (?x)', 'parent (?v) (?y)',
        OR('sibling (?u) (?v)', 'sibling (?v) (?u)'), NOT('sibling (?x) (?y)'),
        NOT('sibling (?y) (?x)')), THEN('cousin (?x) (?y)',
                                        'cousin (?y) (?x)'))

grandparent_rule = IF(AND('parent (?u) (?y)', 'parent (?x) (?u)'),
                      THEN('grandparent (?x) (?y)'))

grandchild_rule = IF(AND('grandparent (?y) (?x)'),
                     THEN('grandchild (?x) (?y)'))

greatgrandparent_rule = IF(
コード例 #7
0

# 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 = IF ( OR('male (?x)',
                 'female (?x)'),
            THEN ('same (?x) (?x)'))

sibling = IF( AND ('parent (?x) (?y)',
                   'parent (?x) (?z)',
                  NOT('same (?y) (?z)')),
              THEN ('sibling (?y) (?z)'))

father = IF( AND ('parent (?x) (?y)',
                  'male (?x)'),
             THEN ('father (?x) (?y)'))

mother = IF( AND ('parent (?x) (?y)',
                  'female (?x)'),
             THEN ('mother (?x) (?y)'))

son = IF( AND ('parent (?x) (?y)',
                  'male (?y)'),
             THEN ('son (?y) (?x)'))

daughter = IF( AND ('parent (?x) (?y)',
コード例 #8
0
ファイル: lab1.py プロジェクト: ericaschwa/AI_practice
# 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.
family_rules = [
    IF('male (?x)', THEN('same (?x) (?x)')),
    IF('female (?x)', THEN('same (?x) (?x)')),
    IF(AND('sibling (?x) (?y)', 'male (?y)'), THEN('brother (?y) (?x)')),
    IF(AND('sibling (?x) (?y)', 'female (?y)'), THEN('sister (?y) (?x)')),
    IF(AND('parent (?x) (?y)', 'female (?x)'), THEN('mother (?x) (?y)')),
    IF(AND('parent (?x) (?y)', 'male (?x)'), THEN('father (?x) (?y)')),
    IF(AND('parent (?x) (?y)', 'male (?y)'), THEN('son (?y) (?x)')),
    IF(AND('parent (?x) (?y)', 'female (?y)'), THEN('daughter (?y) (?x)')),
    IF(AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('same (?y) (?z)')),
       THEN('sibling (?y) (?z)')),
    IF(AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('same (?y) (?z)')),
       THEN('sibling (?z) (?y)')),
    IF(AND('parent (?a) (?b)', 'parent (?c) (?d)', 'sibling (?a) (?c)'),
       THEN('cousin (?b) (?d)')),
    IF(AND('parent (?a) (?b)', 'parent (?c) (?d)', 'sibling (?a) (?c)'),
       THEN('cousin (?d) (?b)')),
    IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
       THEN('grandparent (?x) (?z)')),
    IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
       THEN('grandchild (?z) (?x)')),
]

# Some examples to try it on:
# Note: These are used for testing, so DO NOT CHANGE
コード例 #9
0
ファイル: lab1.py プロジェクト: yeyimilk/MIT-6.034-AI
# 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:
self_rule = IF('person (?x)', THEN('self (?x) (?x)'))
friend_rule = IF(AND("person (?x)", "person (?y)", NOT("self (?x) ?(y)")),
                 THEN("friend (?x) (?y)", "friend (?y) (?x)"))
sibling_rule = IF(
    AND('parent (?z) (?x)', 'parent (?z) (?y)', NOT('self (?x) (?y)')),
    THEN('sibling (?x) (?y)'))
sibling_rule_two = IF('sibling (?x) (?y)', THEN('sibling (?y) (?x)'))

child_rule = IF('parent (?x) (?y)', THEN('child (?y) (?x)'))
grandchild_rule = IF(
    AND('child (?x) (?y)', 'child (?y) (?p)', NOT('self (?x) ?(y)')),
    THEN('grandchild (?x) (?p)'))

cousin_rule = IF(
    AND('sibling (?a) (?b)', 'parent (?a) (?x)', 'parent (?b) (?y)',
        NOT('self (?x) (?y)'), NOT('sibling (?x) (?y)')),
    THEN('cousin (?x) (?y)'))
コード例 #10
0
#### 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_rule = IF("person (?x)", THEN("same (?x) (?x)"))

child_rule = IF(AND("parent (?y) (?x)"), THEN("child (?x) (?y)"))

sibling_rule = IF(
    AND(
        "parent (?z) (?x)",
        "parent (?z) (?y)",
        NOT(
            "same (?x) (?y)"
        )
    ),
    THEN(
        "sibling (?x) (?y)"
    )
)

grandparent_rule = IF(
    AND(
        "parent (?z) (?x)",
        "parent (?y) (?z)"
        ),
    THEN(
        "grandparent (?y) (?x)"
    )
コード例 #11
0
ファイル: lab1.py プロジェクト: cleonty/MIT6.034exercixes
# 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.
same_identity_rule = IF(OR('male (?x)', 'female (?x)'),
                        THEN('same-identity (?x) (?x)'))

brother_rule = IF(
    AND('male (?x)', 'parent (?z) (?x)', 'parent (?z) (?y)',
        NOT('same-identity (?x) (?y)')), THEN('brother (?x) (?y)'))

brother_rule_2 = IF('brother (?x) (?y)', THEN('sister (?y) (?x)'))

sister_rule = IF(
    AND('female (?x)', 'parent (?z) (?x)', 'parent (?z) (?y)',
        NOT('same-identity (?x) (?y)')), THEN('sister (?x) (?y)'))
sister_rule_2 = IF('sister (?x) (?y)', THEN('brother (?y) (?x)'))
mother_rule = IF(AND('female (?x)', 'parent (?x) (?y)'),
                 THEN('mother (?x) (?y)'))
father_rule = IF(AND('male (?x)', 'parent (?x) (?y)'),
                 THEN('father (?x) (?y)'))
daughter_rule = IF(AND('female (?x)', 'parent (?y) (?x)'),
                   THEN('daughter (?x) (?y)'))
son_rule = IF(AND('male (?x)', 'parent (?y) (?x)'), THEN('son (?x) (?y)'))
sibling_rule = IF(
コード例 #12
0
# 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.

family_rules = [
#equals
IF( OR('male (?x)', 'female (?x)'), THEN('equals (?x) (?x)') ),

#sibling
IF( AND('parent (?x) (?y)', 'parent (?x) (?z)', NOT('equals (?z) (?y)')), THEN('sibling (?y) (?z)', 'sibling (?z) (?y)') ),
#brother
IF( AND('male (?x)'  , 'sibling (?x) (?y)'), THEN('brother (?x) (?y)') ),
#sister
IF( AND('female (?x)', 'sibling (?x) (?y)'), THEN('sister (?x) (?y)') ),

#father
IF( AND('male (?x)'  , 'parent (?x) (?y)'), THEN('father (?x) (?y)') ),
#mother
IF( AND('female (?x)', 'parent (?x) (?y)'), THEN('mother (?x) (?y)') ),

#son
IF( AND('male (?x)'  , 'parent (?y) (?x)'), THEN('son (?x) (?y)') ),
#daughter
IF( AND('female (?x)', 'parent (?y) (?x)'), THEN('daughter (?x) (?y)') ),
コード例 #13
0
ファイル: lab1.py プロジェクト: Arinzeokeke/6034ai-psets
    '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)'))
brother_sister_rule = IF( AND( 'male (?x)', 'female (?y)', 'parent (?z) (?x)', 'parent (?z) (?y)'),
    THEN('brother (?x) (?y)', 'sister (?y) (?x)', 'sibling (?x) (?y)', 'sibling (?y) (?x)'))

brother_brother_rule = IF( AND( 'male (?x)', 'male (?y)', 'parent (?z) (?x)', 'parent (?z) (?y)', NOT('same-identity (?x) (?y)')),
    THEN('brother (?x) (?y)', 'brother (?y) (?x)', 'sibling (?x) (?y)', 'sibling (?y) (?x)'))

sister_sister_rule = IF( AND( 'female (?x)', 'female (?y)', 'parent (?z) (?x)', 'parent (?z) (?y)', NOT('same-identity (?x) (?y)')),
    THEN('sister (?x) (?y)', 'sister (?y) (?x)', 'sibling (?x) (?y)', 'sibling (?y) (?x)'))

father_rule = IF( AND('parent (?x) (?y)', 'male (?x)'),
    THEN('father (?x) (?y)'))

mother_rule = IF( AND('parent (?x) (?y)', 'female (?x)'),
    THEN('mother (?x) (?y)'))

son_rule = IF( AND('parent (?x) (?y)', 'male (?y)'),
  THEN('son (?y) (?x)'))

daughter_rule = IF( AND('parent (?x) (?y)', 'female (?y)'),
コード例 #14
0
    [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.
rule_id = IF(OR('male (?x)', 'female (?x)'), THEN('same-id (?x) (?x)'))
rule_brother = IF(
    AND('male (?x)', 'parent (?y) (?x)', 'parent (?y) (?z)',
        NOT('same-id (?x) (?z)')),
    THEN('brother (?x) (?z)', 'sibling (?x) (?z)', 'sibling (?z) (?x)'))
rule_sister = IF(
    AND('female (?x)', 'parent (?y) (?x)', 'parent (?y) (?z)',
        NOT('same-id (?x) (?z)')),
    THEN('sister (?x) (?z)', 'sibling (?x) (?z)', 'sibling (?z) (?x)'))
rule_mother = IF(AND('female (?x)', 'parent (?x) (?y)'),
                 THEN('mother (?x) (?y)'))
rule_father = IF(AND('male (?x)', 'parent (?x) (?y)'),
                 THEN('father (?x) (?y)'))
rule_son = IF(AND('male (?x)', 'parent (?y) (?x)'), THEN('son (?x) (?y)'))
rule_daughter = IF(AND('female (?x)', 'parent (?y) (?x)'),
                   THEN('daughter (?x) (?y)'))
rule_cousin = IF(
    AND('parent (?p1) (?c1)', 'parent (?p2) (?c2)', 'sibling (?p1) (?p2)'),
    THEN('cousin (?c1) (?c2)', 'cousin (?c2) (?c1)'))
コード例 #15
0
ファイル: lab1.py プロジェクト: eewayhsu/6.034
# 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.

identity_rule = IF(OR('male (?x)',
                      'female (?x)'),
                    THEN('same-identity (?x) (?x)'))


siblings_rule =  IF(AND ('parent (?x) (?y)',
                        'parent (?x) (?z)',
                        NOT('same-identity (?y) (?z)')),
                        THEN('siblings (?y) (?z)'))


brother_rule =  IF(AND ('male (?x)',
                        'siblings (?x) (?y)'),
                          THEN ('brother (?x) (?y)'))

sister_rule =   IF(AND ('female (?x)',
                        'siblings (?x) (?y)'),
                          THEN ('sister (?x) (?y)'))


father_rule =   IF(AND ('male (?x)',
                        'parent (?x) (?y)'),
                          THEN ('father (?x) (?y)'))
コード例 #16
0
ファイル: lab1.py プロジェクト: farrigaba/6.034
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)'))

child_rule = IF('parent (?x) (?y)', THEN('child (?y) (?x)'))

cousin_rule = IF(
    AND('sibling (?x) (?y)', 'sibling (?y) (?x)', 'parent (?x) (?z)',
        'parent (?y) (?a)', NOT('self (?z) (?a)')),
    THEN('cousin (?z) (?a)', 'cousin (?a) (?z)'))

grand_rule = IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
                THEN('grandparent (?x) (?z)', 'grandchild (?z) (?x)'))

# Add your rules to this list:
family_rules = [self_rule, sibling_rule, child_rule, cousin_rule, grand_rule]
コード例 #17
0
ファイル: lab1.py プロジェクト: bmewing/mit6034
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.
ident = IF(OR('male (?x)', 'female (?x)'), THEN('identical (?x) (?x)'))
sibling = IF(AND('parent (?p) (?x)', 'parent (?p) (?y)', NOT('identical (?x) (?y)')),
              THEN('sibling (?x) (?y)'))
brother = IF(AND('male (?x)', 'sibling (?x) (?y)', NOT('identical (?x) (?y)')), THEN('brother (?x) (?y)'))
sister = IF(AND('female (?x)', 'sibling (?x) (?y)', NOT('identical (?x) (?y)')), THEN('sister (?x) (?y)'))
mother = IF(AND('female (?p)', 'parent (?p) (?x)'), THEN('mother (?p) (?x)'))
father = IF(AND('male (?p)', 'parent (?p) (?x)'), THEN('father (?p) (?x)'))
daughter = IF(AND('female (?x)', 'parent (?p) (?x)'), THEN('daughter (?x) (?p)'))
son = IF(AND('male (?x)', 'parent (?p) (?x)'), THEN('son (?x) (?p)'))
cousin = IF(AND('sibling (?p) (?q)', 'parent (?p) (?x)', 'parent (?q) (?y)'), THEN('cousin (?x) (?y)'))
grandp = IF(AND('parent (?g) (?p)', 'parent (?p) (?x)'), THEN('grandparent (?g) (?x)'))
grandc = IF(AND('parent (?g) (?p)', 'parent (?p) (?x)'), THEN('grandchild (?x) (?g)'))

# Then, put them together into a list in order, and call it
# family_rules.
family_rules = [ident, sibling, brother, sister, mother, father, daughter, son, cousin, grandp, grandc]
コード例 #18
0
ファイル: lab1.py プロジェクト: zimengpan/MIT_AI_Python
# 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'.
ANSWER_3 = '2'

# If you're uncertain of these answers, look in tests.py for an
コード例 #19
0
ファイル: lab1.py プロジェクト: graceyin1218/6.034-labs
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)', 'parent (?x) (?z)', NOT('self (?y) (?z)')),
    THEN('sibling (?y) (?z)', 'sibling (?z) (?y)'))
child = IF('parent (?x) (?y)', THEN('child (?y) (?x)'))
grandparent_grandchild = IF(
    AND('parent (?x) (?y)', 'parent (?y) (?z)'),
    THEN('grandparent (?x) (?z)', 'grandchild (?z) (?x)'))
cousin = IF(
    AND('parent (?x) (?y)', 'parent (?a) (?b)', 'sibling (?x) (?a)',
        NOT('sibling (?y) (?b)')), THEN('cousin (?y) (?b)',
                                        'cousin (?b) (?y)'))

# Add your rules to this list:
family_rules = [self, sibling, child, grandparent_grandchild, cousin]

# Uncomment this to test your data on the Simpsons family:
#print forward_chain(family_rules, simpsons_data, verbose=False)
コード例 #20
0
ファイル: lab1.py プロジェクト: asolove/education
- 'daughter x y': x is the daughter of y
- 'cousin x y': x and y are cousins (a parent of x and a parent of y are siblings)
- 'grandparent x y': x is the grandparent of y
- 'grandchild x y': x is the grandchild of y
"""

# 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_person_rule = IF(OR('male (?x)', 'female (?x)'), THEN('same (?x) (?x)'))
brother_rule = IF(
    AND('male (?x)', 'parent (?y) (?x)', 'parent (?y) (?z)',
        NOT('same (?x) (?z)')), THEN('brother (?x) (?z)'))
sister_rule = IF(
    AND('female (?x)', 'parent (?y) (?x)', 'parent (?y) (?z)',
        NOT('same (?x) (?z)')), THEN('sister (?x) (?z)'))
mother_rule = IF(AND('female (?m)', 'parent (?m) (?k)'),
                 THEN('mother (?m) (?k)'))
father_rule = IF(AND('male (?f)', 'parent (?f) (?k)'),
                 THEN('father (?f) (?k)'))
son_rule = IF(AND('male (?k)', 'parent (?p) (?k)'), THEN('son (?k) (?p)'))
daughter_rule = IF(AND('female (?k)', 'parent (?p) (?k)'),
                   THEN('daughter (?k) (?p)'))
sibling_rule = IF(OR('brother (?x) (?y)', 'sister (?x) (?y)'),
                  THEN('sibling (?x) (?y)'))
cousin_rule = IF(
    AND('parent (?x) (?k1)', 'parent (?y) (?k2)', 'sibling (?x) (?y)'),
    THEN('cousin (?k1) (?k2)'))
コード例 #21
0
ファイル: lab1.py プロジェクト: yingtian991221/mit-courses
# Then, put them together into a list in order, and call it
# family_rules.
family_rules = [
    IF(AND('male (?x)', 'parent (?y) (?x)'), THEN('son (?x) (?y)')),
    IF(AND('female (?x)', 'parent (?y) (?x)'), THEN('daughter (?x) (?y)')),
    IF(AND('male (?x)', 'parent (?x) (?y)'), THEN('father (?x) (?y)')),
    IF(AND('female (?x)', 'parent (?x) (?y)'), THEN('mother (?x) (?y)')),
    IF('parent (?y) (?x)', THEN('same-identity (?x) (?x)')),
    IF(AND('father (?x) (?y)', 'male (?y)'), THEN('son (?y) (?x)')),
    IF(AND('father (?x) (?y)', 'female (?y)'), THEN('daughter (?y) (?x)')),
    IF(AND('mother (?x) (?y)', 'male (?y)'), THEN('son (?y) (?x)')),
    IF(AND('mother (?x) (?y)', 'female (?y)'), THEN('daughter (?y) (?x)')),
    IF(
        AND('parent (?x) (?y)', 'parent (?x) (?z)', 'male (?y)',
            NOT('same-identity (?y) (?z)')), THEN('brother (?y) (?z)')),
    IF(
        AND('parent (?x) (?y)', 'parent (?x) (?z)', 'female (?y)',
            NOT('same-identity (?y) (?z)')), THEN('sister (?y) (?z)')),
    IF(
        AND('parent (?x) (?y)', 'parent (?z) (?w)',
            OR('brother (?x) (?z)', 'sister (?x) (?z)'),
            NOT('same-identity (?y) (?w)')), THEN('cousin (?y) (?w)')),
    IF(
        AND('parent (?x) (?y)', 'parent (?z) (?w)',
            OR('brother (?x) (?z)', 'sister (?x) (?z)'),
            NOT('same-identity (?y) (?w)')), THEN('cousin (?w) (?y)')),
    IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
       THEN('grandparent (?x) (?z)')),
    IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
       THEN('grandchild (?z) (?x)')),
コード例 #22
0
ファイル: rules.py プロジェクト: shaydamoezzi/anomaly-explain
# 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

def same_anchor_rule(num=2):
    statements = []
コード例 #23
0
    [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)'))

brother = IF(
    AND('male (?x)', 'parent (?z) (?x)', 'parent (?z) (?y)',
        NOT('same identity (?x) (?y)')), THEN('brother (?x) (?y)'))

sister = IF(
    AND('female (?x)', 'parent (?z) (?x)', 'parent (?z) (?y)',
        NOT('same identity (?x) (?y)')), THEN('sister (?x) (?y)'))

mother = IF(AND('female (?x)', 'parent (?x) (?y)'), THEN('mother (?x) (?y)'))

father = IF(AND('male (?x)', 'parent (?x) (?y)'), THEN('father (?x) (?y)'))

son = IF(AND('male (?x)', 'parent (?y) (?x)'), THEN('son (?x) (?y)'))

daughter = IF(AND('female (?x)', 'parent (?y) (?x)'),
              THEN('daughter (?x) (?y)'))

sibling = IF(OR('brother (?x) (?y)', 'sister (?x) (?y)'),
#Same-identity rule
same_identity = IF(OR('male (?x)', 'female (?x)'),
                   THEN('same-identity (?x) (?x)'))

#Primero las reglas 'verticales'
grandparent = IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
                 THEN('grandparent (?x) (?z)'))
child = IF('parent (?x) (?y)', THEN('child (?y) (?x)'))
grandchild = IF(AND('child (?y) (?x)', 'child (?z) (?y)'),
                THEN('grandchild (?z) (?x)'))

#Despues las reglas 'horizontales'
sibling = IF(
    AND('parent (?x) (?y)', 'parent (?x) (?z)',
        NOT('same-identity (?y) (?z)')), THEN('sibling (?y) (?z)'))
cousin = IF(
    AND('grandparent (?x) (?y)', 'grandparent (?x) (?z)',
        NOT('sibling (?y) (?z)'), NOT('same-identity (?y) (?z)')),
    THEN('cousin (?y) (?z)')
)  #Creo que no es necesario chequear "sibling z y" porque sibling tiene preferencia total sobre esta regla

#Por ultimo las reglas que incluyen el sexo de la persona
brother = IF(AND('sibling (?x) (?y)', 'male (?x)'),
             THEN('brother (?x) (?y)'))  #Ver simetria...
sister = IF(AND('sibling (?x) (?y)', 'female (?x)'), THEN('sister (?x) (?y)'))
mother = IF(AND('parent (?x) (?y)', 'female (?x)'), THEN('mother (?x) (?y)'))
father = IF(AND('parent (?x) (?y)', 'male (?x)'), THEN('father (?x) (?y)'))
son = IF(AND('child (?x) (?y)', 'male (?x)'), THEN('son (?x) (?y)'))
daughter = IF(AND('child (?x) (?y)', 'female (?x)'),
              THEN('daughter (?x) (?y)'))
コード例 #25
0
#  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)'))

sibling_rule = IF(AND('parent (?z) (?x)',
                      'parent (?z) (?y)',
                      NOT('same (?x) (?y)')),
                 THEN('sibling (?x) (?y)', 'sibling (?y) (?x)'))

child_rule = IF('parent (?y) (?x)', THEN('child (?x) (?y)'))

cousin_rule = IF(AND('parent (?z1) (?x)',
                       'parent (?z2) (?y)',
                       'sibling (?z2) (?z1)',
                     NOT('sibling (?x) (?y)')),
                THEN('cousin (?x) (?y)', 'cousin (?y) (?x)'))

grandparent_rule = IF(AND('parent (?x) (?a)',
                          'parent (?a) (?y)'),
                      THEN('grandparent (?x) (?y)','grandchild (?y) (?x)'))

コード例 #26
0
ファイル: lab1.py プロジェクト: zakidane/machine-learning-MIT
child_xy = 'child (?x) (?y)'
parent_yx = 'parent (?y) (?x)'
cousin_xy = 'cousin (?x) (?y)'
grandparent_xy = 'grandparent (?x) (?y)'
grandchild_xy = 'grandchild (?x) (?y)'
# Then, put them together into a list in order, and call it
# family_rules.
family_rules = [
    IF(AND(parent_yx, 'male (?y)'), THEN('father (?y) (?x)')),
    IF(AND(parent_yx, 'female (?y)'), THEN('mother (?y) (?x)')),
    IF(AND(parent_yx, 'male (?x)'), THEN('son (?x) (?y)')),
    IF(AND(parent_yx, 'female (?x)'), THEN('daughter (?x) (?y)')),
    IF(parent_yx, THEN('same-person (?x) (?x)')),
    IF(
        AND(parent_yx, 'parent (?y) (?z)', 'female (?x)',
            NOT('same-person (?x) (?z)')), THEN('sister (?x) (?z)')),
    IF(
        AND(parent_yx, 'parent (?y) (?z)', 'male (?x)',
            NOT('same-person (?x) (?z)')), THEN('brother (?x) (?z)')),
    IF(
        AND(parent_yx, 'parent (?x) (?z)', 'parent (?y) (?a)',
            'parent (?a) (?b)', NOT('same-person (?z) (?b)'),
            NOT('same-person (?x) (?a)')), THEN('cousin (?z) (?b)')),
    IF(
        AND(parent_yx, 'parent (?x) (?z)', 'parent (?y) (?a)',
            'parent (?a) (?b)', NOT('same-person (?z) (?b)'),
            NOT('same-person (?x) (?a)')), THEN('cousin (?b) (?z)')),
]
# fill me in

# Some examples to try it on:
コード例 #27
0
ファイル: lab1.py プロジェクト: QuestionC/mit6034
    '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.
family_rules = [ 
        IF( 'male (?x)', THEN( 'self (?x) (?x)')),
        IF( 'female (?x)', THEN( 'self (?x) (?x)')),
        IF( AND('parent (?p) (?x)', 'parent (?p) (?y)', NOT('self (?x) (?y)')),
            THEN( 'sibling (?x) (?y)')),
        IF( AND('sibling (?x) (?y)', 'male (?x)'),
            THEN( 'brother (?x) (?y)')),
        IF( AND('sibling (?x) (?y)', 'female (?x)'),
            THEN( 'sister (?x) (?y)')),
        IF( AND('parent (?p) (?x)', 'female (?p)'),
            THEN( 'mother (?p) (?x)')),
        IF( AND('parent (?p) (?x)', 'male (?p)'),
            THEN( 'father (?p) (?x)')),
        IF( AND('parent (?p) (?x)', 'male (?x)'),
            THEN( 'son (?x) (?p)')),
        IF( AND('parent (?p) (?x)', 'female (?x)'),
            THEN( 'daughter (?x) (?p)')),
        IF( AND('sibling (?px) (?py)', 'parent (?px) (?x)', 'parent (?py) (?y)'),
            THEN( 'cousin (?x) (?y)')),
コード例 #28
0
                IF( AND('parent (?x) (?y)',
                        'female (?y)'),    
                THEN(   'daughter (?y) (?x)') ),
    
                IF( AND('parent (?x) (?y)',
                        'male (?x)'),    
                THEN(   'father (?x) (?y)') ),
    
                IF( AND('parent (?x) (?y)',
                        'female (?x)'),    
                THEN(   'mother (?x) (?y)') ),

                IF( AND('parent (?x) (?y)',
                        'parent (?x) (?z)',
                        'male (?z)',
                        NOT('self (?z) (?y)')),
                THEN(   'brother (?z) (?y)')),
                
                IF( AND('parent (?x) (?y)',
                        'parent (?x) (?z)',
                        'female (?z)',
                        NOT('self (?z) (?y)')),
                THEN(   'sister (?z) (?y)' )),
    
                IF( OR('brother (?z) (?y)',
                       'sister (?z) (?y)'),
                THEN( 'sibling (?z) (?y)' )),
    
                IF(AND('parent (?x) (?y)',
                       'parent (?w) (?z)',
                       'sibling (?x) (?w)'),
コード例 #29
0
# 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)',
        'parent (?x) (?z)',
        NOT('self (?y) (?z)'),
    ), THEN('sibling (?y) (?z)'))

child = IF('parent (?x) (?y)', THEN('child (?y) (?x)'))

cousin = IF(
    AND('parent (?a) (?x)', 'parent (?b) (?y)', 'sibling (?a) (?b)',
        NOT('self (?a) (?b)')),
    # don't add NOT('self (?x) (?y)')
    THEN('cousin (?x) (?y)'))

grandchild = IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
                THEN('grandchild (?z) (?x)'))

grandparent = IF(AND('parent (?x) (?y)', 'parent (?y) (?z)'),
                 THEN('grandparent (?x) (?z)'))
コード例 #30
0
    [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(
    AND("parent (?x) (?y)", "parent (?x) (?z)",
        NOT("same-identity (?y) (?z)")), THEN("sibling (?y) (?z)"))
brother_rule = IF(AND("sibling (?x) (?y)", "male (?x)"),
                  THEN("brother (?x) (?y)"))
sister_rule = IF(AND("sibling (?x) (?y)", "female (?x)"),
                 THEN("sister (?x) (?y)"))

father_rule = IF(AND("parent (?x) (?y)", "male (?x)"),
                 THEN("father (?x) (?y)"))
mother_rule = IF(AND("parent (?x) (?y)", "female (?x)"),
                 THEN("mother (?x) (?y)"))

son_rule = IF(AND("parent (?x) (?y)", "male (?y)"), THEN("son (?y) (?x)"))
daughter_rule = IF(AND("parent (?x) (?y)", "female (?y)"),
                   THEN("daughter (?y) (?x)"))

cousin_rule = IF(