Exemple #1
0
def get_random_logic_formula():
    alphabet = get_logic_alphabet()
    options = ['fariable', 'implies', 'not']
    options_weights = [0.75, 0.125, 0.125]
    chosen = choice(options, p=options_weights)

    if chosen == 'fariable':
        indexes = range(0, 10000)
        weights = []

        for i in indexes:
            weights.append(0.5**(i + 1))

        index = choice(indexes, p=weights)
        return Node(alphabet[chosen], index=index)

    elif chosen == 'implies':
        child_1 = get_random_logic_formula()
        child_2 = get_random_logic_formula()
        children = [child_1, child_2]
        return Node(alphabet[chosen], children)

    elif chosen == 'not':
        children = [get_random_logic_formula()]
        return Node(alphabet[chosen], children)
Exemple #2
0
def play(logic, times):
    logic.theorems = [choice(logic.theorems)]  # We select one axiom at random

    for i in range(times):
        alphabet = get_logic_alphabet()
        theorem = logic.theorems[-1]
        fariables = get_fariables(to_node(theorem, alphabet))
        fariable = str(choice(fariables))
        formula = fariable
        while formula == fariable:
            formula = str(get_random_logic_formula())
        logic.run_rule('replace_fariable', theorem, fariable, formula)
Exemple #3
0
def get_leaves(node):
    if len(node.children) == 0:
        return [node]

    alphabet = get_logic_alphabet()
    leaves = []

    def to_logic_node(string):
        return to_node(string, alphabet)

    for child in node.children:
        leaves.extend(get_leaves(child))

    leaves = list(map(str, leaves))
    leaves = list(set(leaves))
    leaves = list(map(to_logic_node, leaves))
    return leaves
def play(times = 1):
    alphabet = get_logic_alphabet()
    logic = Saver.load_game('logic')

    for i in range(0, times):
        rule_name = choice(['replace_fariable', 'then'])

        if rule_name == 'then':
            for theorem in logic.theorems:
                logic.run_rule(rule_name, theorem)
        elif rule_name == 'replace_fariable':
            weights = get_weights_from_lens(logic.theorems)
            theorem = choice(logic.theorems, p=weights)
            fariables = get_fariables(to_node(theorem, alphabet))
            fariable = str(choice(fariables))
            formula = str(get_random_logic_formula())
            logic.run_rule(rule_name, theorem, fariable, formula)

    Saver.save_game(logic, 'logic')
def then(game, string):
    from tree.functions.to_node import to_node
    from tree.alphabets.logic import get_logic_alphabet

    alphabet = get_logic_alphabet()
    node = to_node(string, alphabet)

    if not node.symbol.equals(alphabet['implies']):
        return False

    if not string in game.theorems:
        return False

    if len(node.children) < 2:
        return False

    if not str(node.children[0]) in game.theorems:
        return False

    return str(node.children[1])
def replace_fariable(game, theorem, fariable, formula):
    from tree.functions.to_node import to_node
    from tree.alphabets.logic   import get_logic_alphabet

    alphabet = get_logic_alphabet()

    if not theorem in game.theorems:
        return False

    far_as_node = to_node(fariable, alphabet)

    if not far_as_node:
        return False

    if not to_node(formula, alphabet):
        return False

    if not far_as_node.symbol.equals(alphabet['fariable']):
        return False

    return theorem.replace(fariable, formula)
Exemple #7
0
def fit(t1, t2, values1, values2, double_fit):
    alphabet = get_logic_alphabet()
    t1 = to_node(t1, alphabet) if type(t1) == str else t1
    t2 = to_node(t2, alphabet) if type(t2) == str else t2
    root1, root2 = t1.symbol.drawing, t2.symbol.drawing

    if root1 == root2 and root1 != 'F':
        if len(t1.children) == 1:
            return fit(t1.children[0], t2.children[0], values1, values2,
                       double_fit)
        else:
            return fit(t1.children[0], t2.children[0], values1, values2, double_fit) \
               and fit(t1.children[1], t2.children[1], values1, values2, double_fit)
    elif root1 == 'F' and double_fit:
        values1.append((to_string(t1), to_string(t2)))
        return True
    elif root2 == 'F':
        values2.append((to_string(t2), to_string(t1)))
        return True
    else:
        return False
def play(logic, times = 1):
    alphabet = get_logic_alphabet()

    graph = {
        '⇒(F_0, ⇒(F_1, F_0))': [],
        '⇒(⇒(F_0, ⇒(F_1, F_2)), ⇒(⇒(F_0, F_1), ⇒(F_0, F_2)))': [],
        '⇒(⇒(¬(F_0), ¬(F_1)), ⇒(F_1, F_0))': []
    }

    for i in range(0, times):
        rule_name = choice(['replace_fariable', 'then'])

        if rule_name == 'then':
            for theorem in logic.theorems:
                first_len = len(logic.theorems)
                logic.run_rule(rule_name, theorem)
                # print('from then', logic.theorems[-1], '|||', theorem, first_len, len(logic.theorems))
                if first_len != len(logic.theorems):
                    if not logic.theorems[-1] in graph.keys():
                        graph[logic.theorems[-1]] = []

                    a = str(to_node(theorem, alphabet).children[0])
                    graph[logic.theorems[-1]] += [theorem, a]

        elif rule_name == 'replace_fariable':
            weights = get_weights_from_lens(logic.theorems)
            theorem = choice(logic.theorems, p=weights)
            fariables = get_fariables(to_node(theorem, alphabet))
            fariable = str(choice(fariables))
            formula = str(get_random_logic_formula())
            first_len = len(logic.theorems)
            logic.run_rule(rule_name, theorem, fariable, formula)
            # print('from replace', logic.theorems[-1], '|||', theorem, first_len, len(logic.theorems))
            if first_len != len(logic.theorems):
                if not logic.theorems[-1] in graph.keys():
                    graph[logic.theorems[-1]] = []
                graph[logic.theorems[-1]].append(theorem)

    return graph
Exemple #9
0
from fit import single_fit, double_fit
from heapq import heappush, heappop
from tree.functions.to_node         import to_node
from tree.alphabets.logic           import get_logic_alphabet

initial_list = ['⇒(F_0, ⇒(F_1, F_0))', '⇒(⇒(F_0, ⇒(F_1, F_2)), ⇒(⇒(F_0, F_1), ⇒(F_0, F_2)))', '⇒(⇒(¬(F_0), ¬(F_1)), ⇒(F_1, F_0))']
alphabet = get_logic_alphabet()
scorer = Scorer()

def initial_prove(qvq):
	queue = []
	heappush(queue, (1, qvq))

	while len(queue) > 0:
		theorem = heappop(queue)
		if prove(theorem):
			return True

	return False

def prove(qvq, queue):
	fitted = [double_fit(qvq, t) for t in initial_list]
	if any(fitted):
	    return True

	for t in initial_list:
		node = to_node(t, alphabet)
		(left, right), root = node.children, node.symbol.drawing
		if root == '⇒':
			if double_fit(qvq, right):
				heappush(queue, (scorer.score(left), left))
Exemple #10
0
def get_fariables(node):
    alphabet = get_logic_alphabet()
    is_fariable = lambda n: n.symbol.equals(alphabet['fariable'])
    leaves = get_leaves(node)
    return list(filter(is_fariable, leaves))