Exemplo n.º 1
0
def consistent_score_mapped_chess(exs, var_ids, m):
    max_score = 0
    max_exs = []
    max_rules = []
    subd_exs = sub_vars_chess(exs, var_ids)  # examples been replaced variables
    mapped_subd_exs = []  # list of plterms
    inv_m = {v: k for k, v in m.items()}  # inverted map
    for e in subd_exs:
        e_mapped = apply_mapping_chess(e, m)
        mapped_subd_exs.append(LL.PlTerm(e_mapped))
    mapped_subd_term = LL.PlTerm(mapped_subd_exs)
    con_sol = LL.abduceChessInstFeature(
        mapped_subd_term)  # consistent solution

    if con_sol:
        #max_rules = copy.deepcopy(con_sol.py())
        max_subd_exs = con_sol[0].py()  #Only the first solution is used
        for k in range(len(max_subd_exs)):
            # map back
            max_exs.append(apply_mapping_chess(max_subd_exs[k], inv_m))
        max_score = len(max_exs)
        re = consist_result(max_score, max_exs, m, [])
        return re
    else:
        return None
Exemplo n.º 2
0
def consistent_score_mapped(exs, var_ids, m):
	max_score = 0
	max_exs = []
	max_rules = []
	subd_exs = sub_vars(exs, var_ids)  # examples been replaced variables

	mapped_subd_exs = []  # list of plterms
	inv_m = {v: k for k, v in m.items()}  # inverted map
	null_pos = []
	for e in subd_exs:
		e_symbs_n = apply_mapping(e, m)
		(e_symbs, n_pos) = remove_nulls(e_symbs_n)
		null_pos.append(n_pos)
		mapped_subd_exs.append(LL.PlTerm(e_symbs))
	mapped_subd_term = LL.PlTerm(mapped_subd_exs)
	con_sol = LL.abduceConInsts(mapped_subd_term)  # consistent solution

	if con_sol:
		max_rules = copy.deepcopy(con_sol.py())
		max_subd_exs = con_sol.py().copy()
		max_exs = []
		for k in range(0, len(max_subd_exs)):
			# add nulls back
			max_subd_exs_n = add_nulls(max_subd_exs[k], null_pos[k])
			# map back
			max_exs.append(apply_mapping(max_subd_exs_n, inv_m))
		abduced_exs = exs.copy()
		for i in range(0, len(max_exs)):
			abduced_exs[i] = max_exs[i]
		max_score = len(max_exs)
		re = consist_result(max_score, abduced_exs, m, max_rules, [])
		return re
	else:
		return None
Exemplo n.º 3
0
def evaluate_data(model, chessboards_true, chessboards_false, mapping):
    correct_cnt_true = 0
    correct_cnt_false = 0
    exs_true = translate_image_to_label(model, chessboards_true, mapping)
    exs_false = translate_image_to_label(model, chessboards_false, mapping)
    for ex in exs_true:
        re = LL.evalChess(LL.PlTerm(ex))
        if re == True:
            correct_cnt_true += 1
    for ex in exs_false:
        re = LL.evalChess(LL.PlTerm(ex))
        if re == False:
            correct_cnt_false += 1
    #Get MLP validation result
    if len(exs_true) != 0:
        true_accuracy = correct_cnt_true / len(exs_true)
    else:
        true_accuracy = 99
    if len(exs_false) != 0:
        false_accuracy = correct_cnt_false / len(exs_false)
    else:
        false_accuracy = 99
    total_accuracy = (correct_cnt_true + correct_cnt_false) / (len(exs_true) +
                                                               len(exs_false))
    return true_accuracy, false_accuracy, total_accuracy
Exemplo n.º 4
0
def test_legit_inst():
    print('VI. Test veryfication of legitimate instance')
    ex = []
    ex.append(LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1]))
    ex.append(LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 0, '+']))
    ex.append(LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 'U']))
    ex.append(LL.PlTerm([1, '+', '+', 1, 1, '=', 1, 1]))
    t = time.time()
    for i in range(0, len(ex)):
        print(ex[i])
        print(LL.legitInst(ex[i]))
    print(time.time() - t)
    print('----------END------------\n')
Exemplo n.º 5
0
def test_gen_rand_feature():
    print('I. Test generating random features (addition rules)')
    print('==================\n')
    for i in range(1, 11):
        print(i)
        t = LL.genRandFeature()
        print(t)
    print('----------END------------\n')
Exemplo n.º 6
0
def test_eval_feature():
    print('III. Test evaluate example with given feature')
    print('==================\n')
    print('*******Feature: ')
    f = LL.PlTerm([
        'my_op([1],[1],[1,0])', 'my_op([0],[0],[0])', 'my_op([1],[0],[1])',
        'my_op([0],[1],[1])'
    ])
    print(f)
    print('*******Examples: ')
    ex1 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1])
    ex2 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 0])
    ex3 = LL.PlTerm(['A', 'A', 'B', 'A', 'A', 'C', 'A', 'A', 'D'])
    re1 = LL.evalInstFeature(ex1, f)
    re2 = LL.evalInstFeature(ex2, f)
    re3 = LL.evalInstFeature(ex3, f)
    print('****example 1 & result: ')
    print(ex1)
    print(re1)
    print('****example 2 & result: ')
    print(ex2)
    print(re2)
    print('****example 3 & result: ')
    print(ex3)
    print(re3)
    print('----------END------------\n')
Exemplo n.º 7
0
def test_create_PlTerms():
    print(
        'II. Test creating constants (number, lower-case letters started string/compounds) and variables (strings started with upper-case letters, strings started with \'_\' ): '
    )
    print('==================\n')
    t1 = LL.PlTerm()
    print(t1)
    t2 = LL.PlTerm([1, 2, 'X', 'X', 'good(day)'])
    print(t2)
    t3 = LL.PlTerm([1, 2, 'X', 'X', ['good(day)', '_d'], '_d', []])
    print(t3)
    print(
        "*******If the term reference hasn't been messed up, the outputs stay the same"
    )
    print(t1)
    print(t2)
    print(t3)
    print('*******Test creating list of variables')
    a = LL.PlTerm('A')
    b = LL.PlTerm([a, a, a, a, a, a])
    c = LL.PlTerm([b, b, b])
    d = LL.PlTerm(c.py())
    print('*******a: ')
    print(a)
    print('*******b & c: c and b should contain same variables as a')
    print(b)
    print(c)
    print(
        '*******d: all d\'s elements should be same variable, but different from a'
    )
    print(d)
    print('----------END------------\n')
Exemplo n.º 8
0
def get_mlp_vector(equation, model, rules, abduced_map, shape=(28, 28, 1)):
    h = shape[0]
    w = shape[1]
    d = shape[2]
    model_output = np.argmax(model.predict(equation.reshape(-1, h, w, d)), axis=1)
    model_labels = []
    for out in model_output:
        model_labels.append(abduced_map[out])
    #print(model_labels)
    vector = []
    for rule in rules:
        ex = LL.PlTerm(model_labels)
        f = LL.PlTerm(rule)
        if LL.evalInstFeature(ex, f):
            vector.append(1)
        else:
            vector.append(0)
    return vector
Exemplo n.º 9
0
def LL_init(pl_file_path="src/prolog/learn.chr"):
    print("Initializing prolog...")
    assert os.path.exists(pl_file_path), "%s is not exist" % pl_file_path
    LL.init("-G10g -M6g")  # must initialise prolog engine first!
    LL.consult(pl_file_path)  # consult the background knowledge file
    LL.call("prolog_stack_property(global, limit(X)), writeln(X)"
            )  #test if stack changed
    print("Prolog has alreadly initialized.")
Exemplo n.º 10
0
def test_abduction():
    print(
        'IV. Test instance abduction given feature and label, the instance should contain at least 1 variable.'
    )
    print('==================\n')
    print('*******feature:')
    f = LL.PlTerm([
        'my_op([1],[1],[1,0])', 'my_op([0],[0],[0])', 'my_op([1],[0],[1])',
        'my_op([0],[1],[1])'
    ])
    ex1 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 'U'])  # U for unknown
    re1 = LL.abduceInstFeature(ex1, f, True)
    re2 = LL.abduceInstFeature(ex1, f, False)
    print(f)
    print('*******ex1 & result when assuming ex1 is POSITIVE+++:')
    print('****ex1')
    print(ex1)
    print('****abduced')
    print(re1)
    print('*******result when assuming ex1 is NEGATIVE---:')
    print(re2)

    ex2 = LL.PlTerm(['U1', 'U2', 'U3', 'U4', '_5', '_6', 'U7', '_8',
                     'U9'])  # U and _ standing for unknown variables
    re2 = LL.abduceInstFeature(ex2, f, True)
    print('\n****ex2')
    print(ex2)
    print('****abduced when assuming ex2 is POSITIVE+++')
    print(re2)

    print()
    f = LL.PlTerm([])
    ex2 = LL.PlTerm(['A', '_', 'A', 'A', '_', 'A'])
    re3 = LL.abduceInstFeature(ex2, f, True)
    print('**** abducing when feature is [] (empty):')
    print('***ex: ')
    print(ex2)
    print('***result: ')
    print(re3)

    print('----------END------------\n')
Exemplo n.º 11
0
def LL_init(pl_file_path):
    print("\n** Initializing prolog **")
    assert os.path.exists(pl_file_path), "%s is not exist" % pl_file_path
    # must initialise prolog engine first!
    LL.init("--quiet --nosignals --stack_limit=10G")
    # consult the background knowledge file
    LL.consult(pl_file_path)
    #test if stack changed
    LL.call("prolog_stack_property(global, limit(X)), writeln(X)")
    print("Prolog has alreadly initialized")
Exemplo n.º 12
0
def test_parse_inst_feature():
    print('VI. Test parsing instance to feature')
    t = time.time()
    ex1 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1])
    ex2 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 0])
    f1 = LL.parseInstFeature(ex1)
    f2 = LL.parseInstFeature(ex2)
    ex3 = LL.PlTerm([1, '+', 1, '=', 1, 0])
    f3 = LL.parseInstFeature(ex3)
    print(time.time() - t)
    print(ex1)
    print('parsed to:')
    print(f1)
    print(ex2)
    print('parsed to:')
    print(f2)
    print(ex3)
    print('parsed to:')
    print(f3)
    print('----------END------------\n')
Exemplo n.º 13
0
import LogicLayer
from PresentLayer import Present
import sys
import getopt
from multiprocessing import Queue

if __name__ == '__main__':
    ip = 'localhost'
    port = 23333

    try:
        options, args = getopt.getopt(sys.argv[1:], "i:p:", ['ip=', 'port='])
        for n, v in options:
            if n in ('-i', '--ip'):
                ip = v
            elif n in ('-p', '--port'):
                port = int(v)
    except:
        options = None

    send = Queue()
    recv = Queue()
    conn = LogicLayer.Logic((ip, port), recv, send)
    if not conn.conn():
        exit(1)
    conn.start()
    client = Present(recv, send)
    conn.close()
Exemplo n.º 14
0
	def to_feature(self):
		feat = LL.conInstsFeature(self.abduced_exs_mapped)
		return LL_feature(self.abduced_map, feat)
Exemplo n.º 15
0
	def eval(self, ex):
		# apply mapping
		ex_symbs_n = apply_mapping(ex, self.mapping)
		(ex_symbs, n_pos) = remove_nulls(ex_symbs_n)
		ex_term = LL.PlTerm(ex_symbs)  # LL term
		return LL.evalInstFeature(ex_term, LL.PlTerm(self.rules))
Exemplo n.º 16
0
def consistent_score(exs, var_ids, maps):
	# for debug
	#print('vars: ', end = '\t')
	#print(var_ids)

	max_score = 0
	max_exs = []
	max_map = {}
	max_rules = []
	max_subd_exs_ids = []
	max_null_pos = []

	subd_exs_all = sub_vars(exs, var_ids)  # examples been replaced variables
	subd_exs_ids = []
	#count = 0
	t = time.time()
	for i in range(0, len(subd_exs_all)):
		subd_exs_ids.append(i)

		subd_exs = []
		for j in subd_exs_ids:
			subd_exs.append(subd_exs_all[j])

		got_con_sol = False
		# do mapping and evaluation, TODO: possible for multithread
		for m in maps:
			#LL.gc()
			#LL.trimStacks() # IMPORTANT!!
			mapped_subd_exs = []  # list of plterms

			null_pos = []
			for e in subd_exs:
				e_symbs_n = apply_mapping(e, m)
				(e_symbs, n_pos) = remove_nulls(e_symbs_n)
				null_pos.append(n_pos)
				mapped_subd_exs.append(LL.PlTerm(e_symbs))
			mapped_subd_term = LL.PlTerm(mapped_subd_exs)
			con_sol = LL.abduceConInsts(
				mapped_subd_term)  # consistent solution

			if con_sol:
				got_con_sol = True
			if con_sol and max_score < len(subd_exs):
				max_rules = copy.deepcopy(con_sol.py())
				max_score = len(subd_exs)
				max_subd_exs_ids = subd_exs_ids.copy()
				max_map = m.copy()
				max_null_pos = null_pos.copy()

				if max_score == len(mapped_subd_exs):
					break
		if not got_con_sol:
			subd_exs_ids.pop()

	abduced_exs = exs.copy()
	inv_m = {v: k for k, v in max_map.items()}  # inverted map
	#print(max_subd_exs_ids)
	for j in range(0, len(max_subd_exs_ids)):
		# add nulls back
		ex_n = add_nulls(max_rules[j].copy(), max_null_pos[j])
		mapped_ex = apply_mapping(ex_n, inv_m)
		abduced_exs[max_subd_exs_ids[j]] = mapped_ex
	'''
	print('elapsed time: ', end = '\t')
	print(time.time() - t, end = '\t')
	print('score:', end = '\t')
	print(max_score)
	'''
	re = consist_result(max_score, abduced_exs, max_map, max_rules,
						max_subd_exs_ids)
	return re
Exemplo n.º 17
0
def test_consistent_abduce():
    print('VII. Test consistent abduction')
    ex1_1 = LL.PlTerm([1, 1, '_', 1, '_', '_', 1, '_'])
    ex1_2 = LL.PlTerm([1, 1, '_', 1, '_', '_', 0, '_'])
    ex1_3 = LL.PlTerm([1, '_', '_', 0, '_', '_', '_', '_'])
    ex2_1 = LL.PlTerm(['_', '_', '_', '_', '_'])
    ex2_2 = LL.PlTerm([0, '_', 0, '_', '_', '_'])
    ex3 = LL.PlTerm([1, '_', '_', '_', 1, 0])
    ex4 = LL.PlTerm([1, '_', 1, 1, '_', 1, '_', '_'])
    ex5 = LL.PlTerm([1, '+', 0, '=', 1])
    ex6 = LL.PlTerm([1, 1, '+', 1, 1, '=', 1, 1, 0])
    ex7 = LL.PlTerm([0, '+', 1, '=', '_'])
    exs1 = LL.PlTerm([ex1_1, ex2_1, ex3, ex4, ex5, ex6, ex7])
    exs2 = LL.PlTerm([ex1_2, ex2_1, ex3, ex4, ex5, ex6, ex7])
    exs3 = LL.PlTerm([ex1_3, ex2_2, ex3, ex4, ex5, ex6, ex7])
    exs4 = LL.PlTerm([ex1_2, ex3, ex4])
    exs5 = LL.PlTerm([[1, 1, '+', 1, 1, '=', 1, 0], [0, '+', 0, '=', 0],
                      [1, '+', 1, '=', 1, 0], [1, '+', 1, 1, '=', 1, 0, 0],
                      [1, '+', 0, '=', 1]])
    t = time.time()
    re1 = LL.abduceConInsts(exs1)
    re2 = LL.abduceConInsts(exs2)
    re3 = LL.abduceConInsts(exs3)
    re4 = LL.abduceConInsts(exs4)
    re5 = LL.abduceConInsts(exs5)
    print(time.time() - t)
    print('*****ex1:')
    print(exs1)
    print('***result1:')
    if re1:
        print(re1.py())
    else:
        print(re1)
    print('*****ex2:')
    print(exs2)
    print('***result2:')
    if re2:
        print(re2.py())
    else:
        print(re2)
    print('*****ex3:')
    print(exs3)
    print('***result3:')
    if re3:
        print(re3.py())
    else:
        print(re3)
    print('*****ex4:')
    print(exs4)
    print('***result4:')
    if re4:
        print(re4.py())
    else:
        print(re4)
    print('*****ex5:')
    print(exs5)
    print('***result5:')
    if re5:
        print(re5.py())
    else:
        print(re5)

    print('----------END------------\n')
Exemplo n.º 18
0
def test_chess_abduction():
    print(
        'IV. Test instance abduction given feature and label, the instance should contain at least 1 variable.'
    )
    print('==================\n')

    chess1_1 = LL.PlTerm([4, 0, 0])
    chess1_2 = LL.PlTerm([6, 1, 0])
    chess1_3 = LL.PlTerm([1, 2, 0])
    chess1_4 = LL.PlTerm([3, 3, 1])
    chess1_5 = LL.PlTerm([5, 3, 1])
    chess1_6 = LL.PlTerm([3, 6, 'X'])
    chess1_7 = LL.PlTerm([7, 4, 2])
    chess1_8 = LL.PlTerm([0, 5, 'Y'])
    chess_1_all = LL.PlTerm([
        chess1_1, chess1_2, chess1_3, chess1_4, chess1_5, chess1_6, chess1_7,
        chess1_8
    ])

    vars_1 = LL.PlTerm(['X', 'Y'])
    labels_1 = LL.PlTerm(False)
    inst_1 = LL.PlTerm([chess_1_all, vars_1, labels_1])

    chess2_1 = LL.PlTerm([4, 0, 0])
    chess2_2 = LL.PlTerm([6, 1, 0])
    chess2_3 = LL.PlTerm([1, 2, 0])
    chess2_4 = LL.PlTerm([3, 3, 1])
    chess2_5 = LL.PlTerm([5, 3, 1])
    chess2_6 = LL.PlTerm([3, 6, 'X2'])
    chess2_7 = LL.PlTerm([7, 4, 2])
    chess2_8 = LL.PlTerm([0, 5, 2])
    chess_2_all = LL.PlTerm([
        chess2_1, chess2_2, chess2_3, chess2_4, chess2_5, chess2_6, chess2_7,
        chess2_8
    ])

    vars_2 = LL.PlTerm(['X2'])
    labels_2 = LL.PlTerm(True)
    inst_2 = LL.PlTerm([chess_2_all, vars_2, labels_2])

    insts = LL.PlTerm([inst_1, inst_2])

    print(insts)
    #[[([[4,0,0], [6,1,0], [1,2,0], [3,3,1], [5,3,1], [3,6,X], [7,4,2], [0,5,2]]),[X],true] ]

    re1 = LL.abduceChessInstFeature(insts)

    print('result ---:')
    print(re1)

    print('----------END------------\n')
Exemplo n.º 19
0
from flask import Flask, request, Response, jsonify
import jsonpickle
import numpy as np
import cv2
import LogicLayer
import json

import main_flask_client as adapterClient

# Initialize the Flask application
app = Flask(__name__)

logicLayer_inst = LogicLayer.LogicLayer()


@app.route('/check_visuals_proxy', methods=['GET', 'POST'])
def check_visuals_proxy():
    data = request.data
    formData = request.form
    frameCount = int(formData["frameCount"])
    imgBytes = request.files["fileUpload"].read()
    annotations_jsonStr = formData["annotations"]

    annotations_deserialized = json.loads(annotations_jsonStr)

    # request.form to get form parameter
    filePath = f"screenShot_{frameCount}.png"
    outImg = open(filePath, "wb")
    outImg.write(imgBytes)

    img_numpy = np.fromstring(imgBytes, dtype="uint8")
Exemplo n.º 20
0
    insts = LL.PlTerm([inst_1, inst_2])

    print(insts)
    #[[([[4,0,0], [6,1,0], [1,2,0], [3,3,1], [5,3,1], [3,6,X], [7,4,2], [0,5,2]]),[X],true] ]

    re1 = LL.abduceChessInstFeature(insts)

    print('result ---:')
    print(re1)

    print('----------END------------\n')


if __name__ == "__main__":

    LL.init("-G10g -M6g")  # must initialise prolog engine first!
    LL.consult('src/prolog/learn.chr')  # consult the background knowledge file
    #LL.call("current_prolog_flag(table_space, X), writeln(X)")  #test if stack changed
    LL.call("prolog_stack_property(global, limit(X)), writeln(X)"
            )  #test if stack changed
    '''
    test_gen_rand_feature() # test random feature generation
    test_create_PlTerms() # test PlTerm creation, including variables and constants
    test_eval_feature() # test evaluating examples with feature
    test_abduction()

    test_legit_inst() # test evaluation of legitimate instance mapping
    test_parse_inst_feature() # test parsing instance to feature
    
    test_consistent_abduce() # test consistent abduction for a list of instances