Ejemplo n.º 1
0
Archivo: lab1.py Proyecto: corcillo/ai
def backchain_to_goal_tree(rules, hypothesis):
    anyMatches = False
    results = OR(hypothesis)
    for rule in rules:
        for statement in rule.consequent():
            matchOutput = match(statement, hypothesis)
            if matchOutput !=None:
                anyMatches = True 
                populatedAnt = populate(rule.antecedent(), matchOutput) #populate antecedent with vocab
                if isinstance(populatedAnt,list):
                    ands = AND()
                    ors = OR()
                    for statement in populatedAnt: #populated Ant is and / or
                        if isinstance(populatedAnt,AND):
                            ands.append(backchain_to_goal_tree(rules,statement))
                        elif isinstance(populatedAnt,OR):
                            ors.append(backchain_to_goal_tree(rules,statement))
                    if len(ands)!=0:
                        results.append(ands)
                    elif len(ors)!=0:
                        results.append(ors)
                else:
                    results.append(backchain_to_goal_tree(rules,populatedAnt))
    if anyMatches == False:
        results.append(OR(hypothesis))
    return simplify(results)
Ejemplo n.º 2
0
def backchain_to_goal_tree(rules, hypothesis,myLeaf = []):
    count = 0
    for i in range(len(rules)):
        bindings = (match(rules[i].consequent()[0],hypothesis))
        if bindings is not None:
            print 'type of bindings',bindings
            andLeaf = AND()
            orLeaf = OR()
            for x in range(len(rules[i].antecedent())):
                print 'in antecedent.....',rules[i].antecedent()[x]
                node = populate((rules[i].antecedent()[x]),bindings)
                if type(rules[i].antecedent()) == AND:
                    andLeaf.append(populate((rules[i].antecedent()[x]),bindings))
                    myLeaf.append(simplify(andLeaf))
                    print 'andLeaf', andLeaf
                if type(rules[i].antecedent()) == OR:
                    orLeaf.append(populate((rules[i].antecedent()[x]),bindings))
                    myLeaf.append(simplify(orLeaf))
                    print 'orLeaf',orLeaf
                
                
                disnode = backchain_to_goal_tree(rules,node)
        else:
            print 'hypothesis'
            #myLeaf.append(hypothesis)
             
    return simplify(myLeaf)
Ejemplo n.º 3
0
def backchain_to_goal_tree(rules, hypothesis):
    treeNode = OR(hypothesis)
    #For every rule in the tree, see which ones have consequents that match my hypothesis
    matches = []
    #iterate through every rule and collect the ones whose consequents match my hypothesis
    for rule in rules:
        #Fill in each consequent of the rule with the correct name of the antecendents
        for consequent in rule.consequent():
            matchAttempt = match(consequent, hypothesis)
            #If we can match one of the consequents, we want to fill in the atecedents with the appropriate hypothesis
            if(matchAttempt != None and not rule in matches):
                for i, expression in enumerate(rule.antecedent()):
                    rule.antecedent()[i] = populate(expression, matchAttempt)
                matches.append(rule)
    #At this point  we have a list of the rules that match, and all of the antecendents are filled in with the variable names we can fill them in with
    treeNode = OR(hypothesis)
    for ruleMatch in matches:
        antecedent = ruleMatch.antecedent()
        if( isinstance(antecedent, AND)):
            node = AND()
        else:
            node = OR()
        for newHypothesis in antecedent:
            node.append(backchain_to_goal_tree(rules, newHypothesis))
        treeNode.append(node)
    return simplify(treeNode)
def backchain_to_goal_tree(rules, hypothesis):
    root = OR(hypothesis)
    for rule in rules:
        for consequent in rule.consequent():
            matchResult = match(consequent, hypothesis)
            if matchResult != None:
                antecedent = rule.antecedent()
                newHypothesis = AND() if isinstance(antecedent, AND) else OR()
                if isinstance(antecedent, str):
                    antecedent = [antecedent]
                for part in antecedent:
                    newHypothesis.append(backchain_to_goal_tree(rules, populate(part, matchResult)))
                root.append(newHypothesis)
    return simplify(root)
Ejemplo n.º 5
0
def backchain_to_goal_tree(rules, hypothesis):
    matching_rules = []
    runningOR = OR()
    for rule in rules:
        match_result = match(rule._action[0], hypothesis)
        if match_result != None:
            runningOR.append(populate(rule._action[0], match_result))
            conditional = rule._conditional
            runningCondition = AND()
            for condition in range(0, len(conditional)):
                condition2 = populate(conditional[condition], match_result)
                runningCondition.append(backchain_to_goal_tree(rules, condition2))
            runningOR.append(runningCondition)
    if len(runningOR) != 0:
        return simplify(runningOR)
    else:
        return hypothesis
Ejemplo n.º 6
0
def backchain_to_goal_tree(rules, hypothesis):
	goal_tree = OR(hypothesis)

	for rule in rules:
		for expr in rule.consequent():
			bindings = match(expr, hypothesis)
			if bindings or bindings == {}: # we have a match
				antecedent = rule.antecedent()
				subtree = AND() if isinstance(antecedent, AND) else OR()

				if isinstance(antecedent, str):
					new_nodes = [populate(antecedent, bindings)]
				else:
					new_nodes = [populate(subexpr, bindings) for subexpr in antecedent]

				for new_node in new_nodes:
					subtree.append(backchain_to_goal_tree(rules, new_node))
				
				goal_tree.append(subtree)

	return simplify(goal_tree)
Ejemplo n.º 7
0
Archivo: lab1.py Proyecto: cookt/mit
def backchain_to_goal_tree(rules, hypothesis):
    tree=OR(hypothesis)
    #print "hypothesis: "+str(hypothesis)
    matches=False
    for rule in rules:
        #print "rule: "+str(rule)
        cons=rule.consequent()
        ant=rule.antecedent()
        #print "cons: "+str(cons)
        for c in cons:
            #print "c:"+str(c)
            if match(c,hypothesis)!=None:
                matches=True
                if isinstance(ant, AND):
                    subtree=AND()
                    for a in ant:
                        subtree.append(backchain_to_goal_tree(rules,populate(a, match(c,hypothesis))))
                elif isinstance(ant, OR):
                    subtree=OR()
                    for a in ant:
                        subtree.append(backchain_to_goal_tree(rules,populate(a,match(c,hypothesis))))
                else:
                   subtree=backchain_to_goal_tree(rules, populate(ant,match(c,hypothesis)))
                tree.append(subtree)     
    if not(match):
        tree.append(hypothesis)
    return simplify(tree)
Ejemplo n.º 8
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:

# Add your rules to this list:
family_rules = [
    IF(AND('person (?x)'), THEN('self (?x) (?x)'), DELETE('person (?x)')),
    IF(AND('parent (?x) (?y)'), THEN('child (?y) (?x)')),
Ejemplo n.º 9
0
def get_sorted_hands_assertions(hands):
    if isinstance(hands, str):
        hands = [hands]
    elif isinstance(hands, tuple):
        hands = list(hands)
    SAME_HAND_RULE = IF(
        OR("(?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
           ),
        THEN(
            "same hand (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ))

    hands_assertions = hands + BASE_ASSERTIONS

    hands_assertions = forward_chain([SAME_HAND_RULE], hands_assertions)

    SORT_HAND_RULE_1 = IF(
        AND(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            OR("(?v2) greater (?v1)",
               AND("same value (?v2) (?v1)", "(?s2) greater (?s1)"))),
        THEN(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v2) (?s2), (?v1) (?s1), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ),
        DELETE(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ))

    SORT_HAND_RULE_2 = IF(
        AND(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            OR("(?v3) greater (?v2)",
               AND("same value (?v3) (?v2)", "(?s3) greater (?s2)"))),
        THEN(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v3) (?s3), (?v2) (?s2), (?v4) (?s4), (?v5) (?s5)"
        ),
        DELETE(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ))

    SORT_HAND_RULE_3 = IF(
        AND(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            OR("(?v4) greater (?v3)",
               AND("same value (?v4) (?v3)", "(?s4) greater (?s3)"))),
        THEN(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v4) (?s4), (?v3) (?s3), (?v5) (?s5)"
        ),
        DELETE(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ))

    SORT_HAND_RULE_4 = IF(
        AND(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            OR("(?v5) greater (?v4)",
               AND("same value (?v5) (?v4)", "(?s5) greater (?s4)"))),
        THEN(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v5) (?s5), (?v4) (?s4)"
        ),
        DELETE(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ))

    SORTED_HAND_RULE = IF(
        AND(
            "(?p) : (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5)",
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            OR("(?v1) greater (?v2)",
               AND("same value (?v1) (?v2)", "(?s1) greater (?s2)")),
            OR("(?v2) greater (?v3)",
               AND("same value (?v2) (?v3)", "(?s2) greater (?s3)")),
            OR("(?v3) greater (?v4)",
               AND("same value (?v3) (?v4)", "(?s3) greater (?s4)")),
            OR("(?v4) greater (?v5)",
               AND("same value (?v4) (?v5)", "(?s4) greater (?s5)"))),
        THEN(
            "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
        ),
        DELETE(
            "same hand (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            "(?p) : (?pv1) (?ps1), (?pv2) (?ps2), (?pv3) (?ps3), (?pv4) (?ps4), (?pv5) (?ps5)"
        ))

    SORT_HAND_RULES = [
        SORT_HAND_RULE_1, SORT_HAND_RULE_2, SORT_HAND_RULE_3, SORT_HAND_RULE_4,
        SORTED_HAND_RULE
    ]

    hands_assertions = forward_chain(SORT_HAND_RULES, hands_assertions)
    return hands_assertions
Ejemplo n.º 10
0
# 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
Ejemplo n.º 11
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
Ejemplo n.º 12
0
# 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
Ejemplo n.º 13
0
            requirements.append( backchain_to_goal_tree(rules, new_hypotheses) )
        else:
            requirements.append( new_hypotheses.__class__(*[backchain_to_goal_tree(rules, h) for h in new_hypotheses]) )

    return simplify(OR(requirements))

def match_hypothesis_against_consequents(consequents, hypothesis):
    for c in consequents:
        binding = match(c, hypothesis)
        if (not isinstance(binding, dict)) and (not binding):
##            print "match_hypothesis_against_consequents: no match found with rules '%s' for hypothesis: '%s'" % (c, hypothesis)
            continue
        else:
            ## Once a binding is found, return.
            return binding
    return None


# Here's an example of running the backward chainer - uncomment
# it to see it work:
#print backchain_to_goal_tree(ZOOKEEPER_RULES, 'opus is a penguin')

if __name__ == '__main__':
    RULES =  IF( AND( '(?x) is a bird',        # Z15
                      '(?x) is a good flyer' ),
                 THEN( '(?x) is an albatross' ))
    assert(backchain_to_goal_tree([RULES], "abc") == 'abc')
    assert(backchain_to_goal_tree([RULES], "a is an albatross") ==
           OR("a is an albatross", AND('a is a bird', 'a is a good flyer') )
    )
Ejemplo n.º 14
0
from production import IF, AND, THEN, FAIL, OR
"""
Chaining is a specific way of thinking, related mostly to maximal inequalities and multiscale considerations,
for bounding the maximum of a collection of (dependent) random variables. In short, it is a method that is often
much more efficient than performing a vanilla union bound. Chaining dates back to work of Kolmogorov, but since
then has been further developed by Dudley, Fernique, Talagrand, and many others.

In this example, we consider collections of variables related to a zookeeper.
"""

## 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")),
Ejemplo n.º 15
0
# the correct goal tree given the hypothesis 'alice is an
# albatross' and using the zookeeper_rules.

def tree_map(lst, fn):
    if isinstance(lst, (list, tuple)):
        return fn([ tree_map(elt, fn) for elt in lst ])
    else:
        return lst

def backchain_to_goal_tree_2_getargs():
    return [ zookeeper_rules, 'alice is an albatross' ]

result_bc_2 = OR('alice is an albatross',
                 AND(OR('alice is a bird',
                        'alice has feathers',
                        AND('alice flies',
                            'alice lays eggs')),
                     'alice is a good flyer'))

def backchain_to_goal_tree_2_testanswer(val, original_val = None):
    return ( tree_map(type_encode(val), frozenset) ==
             tree_map(type_encode(result_bc_2), frozenset))

make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = backchain_to_goal_tree_2_getargs,
          testanswer = backchain_to_goal_tree_2_testanswer,
          expected_val = str(result_bc_2)
          )


### TEST 14 ###
Ejemplo n.º 16
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
Ejemplo n.º 17
0
# 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
Ejemplo n.º 18
0
def get_winning_hand_assertions(classified_hands_assertions):
    DIFFERENT_RANK_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "(?r1) beats (?r2)"), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    STRAIGHT_FLUSH_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) straight_flush",
                "(?v11) greater (?v12)"), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    FOUR_OF_A_KIND_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) four_of_a_kind",
                OR("(?v11) greater (?v12)",
                   AND("same value (?v11) (?v12)", "(?v51) greater (?v52)"))),
            THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    FULL_HOUSE_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) full_house",
                OR("(?v11) greater (?v12)",
                   AND("same value (?v11) (?v12)", "(?v41) greater (?v42)"))),
            THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]
    FLUSH_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) flush",
                OR(
                    "(?v11) greater (?v12)",
                    AND("same value (?v11) (?v12)", "(?v21) greater (?v22)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "(?v31) greater (?v32)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "same value (?v31) (?v32)", "(?v41) greater (?v42)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "same value (?v31) (?v32)", "same value (?v41) (?v42)",
                        "(?v51) greater (?v52)"))), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    STRAIGHT_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) straight",
                "(?v11) greater (?v12)"), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    THREE_OF_A_KIND_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) three_of_a_kind",
                OR(
                    "(?v11) greater (?v12)",
                    AND("same value (?v11) (?v12)", "(?v41) greater (?v42)"),
                    AND("same value (?v11) (?v12)", "same value (?v41) (?v42)",
                        "(?v51) greater (?v52)"))), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    TWO_PAIR_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) two_pair",
                OR(
                    "(?v11) greater (?v12)",
                    AND("same value (?v11) (?v12)", "(?v31) greater (?v32)"),
                    AND("same value (?v11) (?v12)", "same value (?v31) (?v32)",
                        "(?v51) greater (?v52)"))), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]
    ONE_PAIR_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) one_pair",
                OR(
                    "(?v11) greater (?v12)",
                    AND("same value (?v11) (?v12)", "(?v31) greater (?v32)"),
                    AND("same value (?v11) (?v12)", "same value (?v31) (?v32)",
                        "(?v41) greater (?v42)"),
                    AND("same value (?v11) (?v12)", "same value (?v31) (?v32)",
                        "same value (?v41) (?v42)", "(?v51) greater (?v52)"))),
            THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    HIGH_CARD_BEAT_RULE = [
        IF(
            AND(
                "hand rank : (?p1) : (?r1) : (?v11) (?s11), (?v21) (?s21), (?v31) (?s31), (?v41) (?s41), (?v51) (?s51)",
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)",
                "same rank (?r1) (?r2)", "same rank (?r1) high_card",
                OR(
                    "(?v11) greater (?v12)",
                    AND("same value (?v11) (?v12)", "(?v21) greater (?v22)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "(?v31) greater (?v32)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "same value (?v31) (?v32)", "(?v41) greater (?v42)"),
                    AND("same value (?v11) (?v12)", "same value (?v21) (?v22)",
                        "same value (?v31) (?v32)", "same value (?v41) (?v42)",
                        "(?v51) greater (?v52)"))), THEN(),
            DELETE(
                "hand rank : (?p2) : (?r2) : (?v12) (?s12), (?v22) (?s22), (?v32) (?s32), (?v42) (?s42), (?v52) (?s52)"
            ))
    ]

    best_players_rules = flatten([
        DIFFERENT_RANK_BEAT_RULE, STRAIGHT_FLUSH_BEAT_RULE,
        FOUR_OF_A_KIND_BEAT_RULE, FULL_HOUSE_BEAT_RULE, FLUSH_BEAT_RULE,
        STRAIGHT_BEAT_RULE, THREE_OF_A_KIND_BEAT_RULE, TWO_PAIR_BEAT_RULE,
        ONE_PAIR_BEAT_RULE, HIGH_CARD_BEAT_RULE
    ])

    top_hands_assertions = forward_chain(best_players_rules,
                                         classified_hands_assertions)

    BEST_HANDS_RULE = [
        IF(
            "hand rank : (?p) : (?r) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
            THEN(
                "winner (?p) with (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    return forward_chain(BEST_HANDS_RULE, top_hands_assertions)
Ejemplo n.º 19
0
def get_classified_hands_assertions(sorted_hands_assertions):
    HAND_RANK_STRAIGHT_FLUSH_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "(?v1) succeeds (?v2)", "(?v2) succeeds (?v3)",
                "(?v3) succeeds (?v4)", "(?v4) succeeds (?v5)",
                "same suit (?s1) (?s2)", "same suit (?s2) (?s3)",
                "same suit (?s3) (?s4)", "same suit (?s4) (?s5)"),
            THEN(
                "hand rank : (?p) : straight_flush : (?v1) (?s1), (?v2) (?s1), (?v3) (?s1), (?v4) (?s1), (?v5) (?s1)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
    ]

    HAND_RANK_FOUR_OF_A_KIND_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v2) (?v3)",
                "same value (?v3) (?v4)"),
            THEN(
                "hand rank : (?p) : four_of_a_kind : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v2) (?v3)", "same value (?v3) (?v4)",
                "same value (?v4) (?v5)"),
            THEN(
                "hand rank : (?p) : four_of_a_kind : (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5), (?v1) (?s1)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    HAND_RANK_FULL_HOUSE_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v2) (?v3)",
                "same value (?v4) (?v5)"),
            THEN(
                "hand rank : (?p) : full_house : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v3) (?v4)",
                "same value (?v4) (?v5)"),
            THEN(
                "hand rank : (?p) : full_house : (?v3) (?s3), (?v4) (?s4), (?v5) (?s5), (?v1) (?s1), (?v2) (?s2)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    HAND_RANK_FLUSH_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same suit (?s1) (?s2)", "same suit (?s2) (?s3)",
                "same suit (?s3) (?s4)", "same suit (?s4) (?s5)",
                NOT(
                    AND("(?v1) succeeds (?v2)", "(?v2) succeeds (?v3)",
                        "(?v3) succeeds (?v4)", "(?v4) succeeds (?v5)"))),
            THEN(
                "hand rank : (?p) : flush : (?v1) (?s1), (?v2) (?s1), (?v3) (?s1), (?v4) (?s1), (?v5) (?s1)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
    ]

    HAND_RANK_STRAIGHT_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "(?v1) succeeds (?v2)", "(?v2) succeeds (?v3)",
                "(?v3) succeeds (?v4)", "(?v4) succeeds (?v5)",
                NOT(
                    AND("same suit (?s1) (?s2)", "same suit (?s2) (?s3)",
                        "same suit (?s3) (?s4)", "same suit (?s4) (?s5)"))),
            THEN(
                "hand rank : (?p) : straight : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
    ]

    HAND_RANK_THREE_OF_A_KIND_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v2) (?v3)",
                NOT(
                    OR("same value (?v3) (?v4)", "same value (?v4) (?v5)",
                       "same value (?v5) (?v3)"))),
            THEN(
                "hand rank : (?p) : three_of_a_kind : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v2) (?v3)", "same value (?v3) (?v4)",
                NOT(
                    OR("same value (?v1) (?v4)", "same value (?v4) (?v5)",
                       "same value (?v5) (?v1)"))),
            THEN(
                "hand rank : (?p) : three_of_a_kind : (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v1) (?s1), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v3) (?v4)", "same value (?v4) (?v5)",
                NOT(
                    OR("same value (?v1) (?v2)", "same value (?v2) (?v3)",
                       "same value (?v3) (?v1)"))),
            THEN(
                "hand rank : (?p) : three_of_a_kind : (?v3) (?s3), (?v4) (?s4), (?v5) (?s5), (?v1) (?s1), (?v2) (?s2)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    HAND_RANK_TWO_PAIR_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v3) (?v4)",
                NOT(
                    OR("same value (?v1) (?v3)", "same value (?v3) (?v5)",
                       "same value (?v5) (?v1)"))),
            THEN(
                "hand rank : (?p) : two_pair : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)", "same value (?v4) (?v5)",
                NOT(
                    OR("same value (?v1) (?v3)", "same value (?v3) (?v5)",
                       "same value (?v5) (?v1)"))),
            THEN(
                "hand rank : (?p) : two_pair : (?v1) (?s1), (?v2) (?s2), (?v4) (?s4), (?v5) (?s5), (?v3) (?s3)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v2) (?v3)", "same value (?v4) (?v5)",
                NOT(
                    OR("same value (?v1) (?v3)", "same value (?v3) (?v5)",
                       "same value (?v5) (?v1)"))),
            THEN(
                "hand rank : (?p) : two_pair : (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5), (?v1) (?s1)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    HAND_RANK_ONE_PAIR_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v1) (?v2)",
                NOT(
                    OR("same value (?v1) (?v3)", "same value (?v1) (?v4)",
                       "same value (?v1) (?v5)", "same value (?v3) (?v4)",
                       "same value (?v3) (?v5)", "same value (?v4) (?v5)"))),
            THEN(
                "hand rank : (?p) : one_pair : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v2) (?v3)",
                NOT(
                    OR("same value (?v1) (?v3)", "same value (?v1) (?v4)",
                       "same value (?v1) (?v5)", "same value (?v3) (?v4)",
                       "same value (?v3) (?v5)", "same value (?v4) (?v5)"))),
            THEN(
                "hand rank : (?p) : one_pair : (?v2) (?s2), (?v3) (?s3), (?v1) (?s1), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v3) (?v4)",
                NOT(
                    OR("same value (?v1) (?v2)", "same value (?v1) (?v3)",
                       "same value (?v1) (?v5)", "same value (?v2) (?v3)",
                       "same value (?v2) (?v5)", "same value (?v3) (?v5)"))),
            THEN(
                "hand rank : (?p) : one_pair : (?v3) (?s3), (?v4) (?s4), (?v1) (?s1), (?v2) (?s2), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                "same value (?v4) (?v5)",
                NOT(
                    OR("same value (?v1) (?v2)", "same value (?v1) (?v3)",
                       "same value (?v1) (?v4)", "same value (?v2) (?v3)",
                       "same value (?v2) (?v4)", "same value (?v3) (?v4)"))),
            THEN(
                "hand rank : (?p) : one_pair : (?v4) (?s4), (?v5) (?s5), (?v1) (?s1), (?v2) (?s2), (?v3) (?s3)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ))
    ]

    HAND_RANK_HIGH_CARD_RULE = [
        IF(
            AND(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)",
                NOT(
                    OR("same value (?v1) (?v2)", "same value (?v1) (?v3)",
                       "same value (?v1) (?v4)", "same value (?v1) (?v5)",
                       "same value (?v2) (?v3)", "same value (?v2) (?v4)",
                       "same value (?v2) (?v5)", "same value (?v3) (?v4)",
                       "same value (?v3) (?v5)", "same value (?v4) (?v5)")),
                NOT(
                    AND("(?v1) succeeds (?v2)", "(?v2) succeeds (?v3)",
                        "(?v3) succeeds (?v4)", "(?v4) succeeds (?v5)")),
                NOT(
                    AND("same suit (?s1) (?s2)", "same suit (?s2) (?s3)",
                        "same suit (?s3) (?s4)", "same suit (?s4) (?s5)"))),
            THEN(
                "hand rank : (?p) : high_card : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            ),
            DELETE(
                "sorted (?p) : (?v1) (?s1), (?v2) (?s2), (?v3) (?s3), (?v4) (?s4), (?v5) (?s5)"
            )),
    ]

    hand_rank_classifier_rules = flatten([
        HAND_RANK_STRAIGHT_FLUSH_RULE, HAND_RANK_FOUR_OF_A_KIND_RULE,
        HAND_RANK_FULL_HOUSE_RULE, HAND_RANK_FLUSH_RULE,
        HAND_RANK_STRAIGHT_RULE, HAND_RANK_THREE_OF_A_KIND_RULE,
        HAND_RANK_TWO_PAIR_RULE, HAND_RANK_ONE_PAIR_RULE,
        HAND_RANK_HIGH_CARD_RULE
    ])

    hand_rank_classifier_assertions = forward_chain(hand_rank_classifier_rules,
                                                    sorted_hands_assertions)

    return purge("(?v1) succeeds (?v2)", hand_rank_classifier_assertions)
Ejemplo n.º 20
0
def purge(pattern, assertions):
    purge_rule = IF(AND(pattern), THEN(), DELETE(pattern))
    return forward_chain([purge_rule], assertions)
Ejemplo n.º 21
0
def backchain_to_goal_tree(rules, hypothesis):
    goal_tree = OR(hypothesis)
    for rule in rules:
        consequent = rule.consequent()[0]
        matching = match(consequent, hypothesis)
        if matching or matching == {}:
            if isinstance(rule.antecedent(), AND):
                leaf_tree = AND()
                for antecedent in rule.antecedent():
                    if populate(antecedent, matching):
                        leaf_tree.append(
                            backchain_to_goal_tree(
                                rules, populate(antecedent, matching)))
                        goal_tree.append(leaf_tree)
                    else:
                        leaf_tree.append(
                            backchain_to_goal_tree(rules, antecedent))
            elif isinstance(rule.antecedent(), OR):
                leaf_tree = OR()
                for antecedent in rule.antecedent():
                    if populate(antecedent, matching):
                        leaf_tree.append(
                            backchain_to_goal_tree(
                                rules, populate(antecedent, matching)))
                        goal_tree.append(leaf_tree)
                    else:
                        leaf_tree.append(
                            backchain_to_goal_tree(rules, antecedent))
            else:
                goal_tree.append(
                    backchain_to_goal_tree(
                        rules, populate(rule.antecedent(), matching)))

    return simplify(goal_tree)
Ejemplo n.º 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) )
Ejemplo n.º 23
0
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_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)"))
Ejemplo n.º 24
0
# 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
from production import AND, OR, NOT, PASS, FAIL, IF, THEN, \
     match, populate, simplify, variables
from zookeeper import ZOOKEEPER_RULES
import string
# 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.
rule1 = (
    IF(AND('(?x) has (?y)', '(?x) has (?z)'), THEN('(?x) has (?y) and (?z)')),
    IF(AND('(?x) has rhythm and music'),
       THEN('(?x) could not ask for anything more')),
)
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)')))
antelist = list()
liste = list()


def myfunc3(rule, deneme):
    eleman = AND()
    for liste in populate(rule.antecedent(), deneme):
        if not liste in antelist:
            antelist.append(liste)
            eleman.append(liste)
    return eleman
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)"))
repeat_rule = IF(OR('person (?x)'), THEN('repeat (?x) (?x)'))
Ejemplo n.º 27
0
          getargs=backchain_to_goal_tree_1_getargs,
          testanswer=backchain_to_goal_tree_1_testanswer,
          expected_val='[ \'stuff\' ]',
          name="backchain_to_goal_tree")

### TEST 11 ###


def backchain_to_goal_tree_2_getargs():
    return [ZOOKEEPER_RULES, 'alice is an albatross']


result_bc_2 = OR(
    'alice is an albatross',
    AND(
        OR('alice is a bird', 'alice has feathers',
           AND('alice flies', 'alice lays eggs')), 'alice is a good flyer'))


def backchain_to_goal_tree_2_testanswer(val, original_val=None):
    return (tree_map(type_encode(val),
                     frozenset) == tree_map(type_encode(result_bc_2),
                                            frozenset))


# This test checks to make sure that your backchainer produces
# the correct goal tree given the hypothesis 'alice is an
# albatross' and using the ZOOKEEPER_RULES.

make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=backchain_to_goal_tree_2_getargs,
Ejemplo n.º 28
0
# 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'])
#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' ])
Ejemplo n.º 29
0
# 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

def same_anchor_rule(num=2):
    statements = []
Ejemplo n.º 30
0
ANSWER_2 = '4'

ANSWER_3 = '2'  #rule2

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)") )
#person_rule = IF( OR ( 'person (?x)','person (?y)' ), THEN( 'same-person (?x) (?x)' ) )
#same = IF( OR ( 'person (?x) (?y)','person (?y) (?x)', 'person (?x) (?z)', 'person (?z) (?y)' ), THEN( 'same (?x) (?x)' ) )
#sibling = IF( AND('parent (?x) (?y)', 'parent (?x) (?z)','person (?y)', 'person (?z)'), THEN ("sibling (?z) (?y)", "sibling (?y) (?z)") )
Ejemplo n.º 31
0
def create_statement(statements, rule):
    if isinstance(rule, AND):
        return AND(statements)
    elif isinstance(rule, OR):
        return OR(statements)
Ejemplo n.º 32
0
# Which rule fires second?

ANSWER_5 = '2'

# 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
Ejemplo n.º 33
0
from production import IF, AND, THEN, FAIL, OR

jew_rule1 = IF( AND( '(?x) wears kipa',
                '(?x) speaks Jewish',
                '(?x) has height 160',
                '(?x) has Judaism religion',
                '(?x) has yellow skin',
                '(?x) is medium familiar with Luna-City'
                ), THEN('(?x) is jew'))

jew_rule2 = IF( AND( '(?x) wears kipa',
                '(?x) speaks Jewish',
                '(?x) has Judaism religion'
                ), THEN('(?x) is jew'))

jew_rules = [jew_rule1, jew_rule2]

american_rule1 = IF( AND( '(?x) wears suit',
                '(?x) speaks English',
                '(?x) has height 165',
                '(?x) has Catholic religion',
                '(?x) has white skin',
                '(?x) is high familiar with Luna-City'
                ), THEN('(?x) is american'))

american_rule2 = IF( AND( '(?x) speaks English',
                '(?x) has white skin'
                ), THEN('(?x) is american'))

american_rules = [american_rule1, american_rule2]
Ejemplo n.º 34
0
def flatten(list_of_lists):
    return [x for sublist in list_of_lists for x in sublist]


hand_rank_assertions = [
    "straight_flush beats four_of_a_kind", "four_of_a_kind beats full_house",
    "full_house beats flush", "flush beats straight",
    "straight beats three_of_a_kind", "three_of_a_kind beats two_pair",
    "two_pair beats one_pair", "one_pair beats high_card",
    "rank straight_flush", "rank four_of_a_kind", "rank full_house",
    "rank flush", "rank straight", "rank three_of_a_kind", "rank two_pair",
    "rank one_pair", "rank high_card"
]

HAND_RANK_TRANSITIVITY_RULE = IF(AND("(?a) beats (?b)", "(?b) beats (?c)"),
                                 THEN("(?a) beats (?c)"))

HAND_RANK_EQUALITY_RULE = IF("rank (?a)", THEN("same rank (?a) (?a)"))

hand_rank_rules = [HAND_RANK_EQUALITY_RULE, HAND_RANK_TRANSITIVITY_RULE]

hand_rank_assertions = forward_chain(hand_rank_rules, hand_rank_assertions)

# Card Values : A(h high, l low), 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K
# Card Suits : H, D, S, C
card_value_assertions = [
    "Ah succeeds K",
    "K succeeds Q",
    "Q succeeds J",
    "J succeeds 10",
Ejemplo n.º 35
0
# 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
Ejemplo n.º 36
0
# 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