def write_data(input_file, output_file, features):

    cnf, _ = dimacs_to_cnf(input_file)

    variables = open(output_file[0], 'w')
    relations = open(output_file[1], 'w')

    relations_str = ''
    variables_str = ''

    and_children_list = []
    or_children_list = []

    num_feature = 50

    feature_OR = features['Or']
    feature_AND = features['And']
    feature_G = features['Global']
    # feature_leaf: [1,2,3]
    feature_leaf = {'1': features['a'], '2': features['b'],
                    '3': features['c'], '4': features['d'],
                    '5': features['e'], '6': features['f'],
                    '7': features['g'], '8': features['h'],
                    '9': features['i'], '10': features['j'],
                    '11': features['k'], '12': features['l']
                    }
    feature_leaf['-1'] = -feature_leaf['1']
    feature_leaf['-2'] = -feature_leaf['2']
    feature_leaf['-3'] = -feature_leaf['3']
    feature_leaf['-4'] = -feature_leaf['4']
    feature_leaf['-5'] = -feature_leaf['5']
    feature_leaf['-6'] = -feature_leaf['6']
    feature_leaf['-7'] = -feature_leaf['7']
    feature_leaf['-8'] = -feature_leaf['8']
    feature_leaf['-9'] = -feature_leaf['9']
    feature_leaf['-10'] = -feature_leaf['10']
    feature_leaf['-11'] = -feature_leaf['11']
    feature_leaf['-12'] = -feature_leaf['12']


    # add global var
    feature = feature_G
    label = 0
    variables_str += str(0) + '\t'
    for j in range(len(feature)):
        variables_str += str(feature[j]) + '\t'
    variables_str += str(label) + '\n'

    var_id = 1
    pseudo_clause = [i[0] for i in cnf]
    for l in pseudo_clause:
        variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, feature_leaf[str(l)]))) + '\t1\n')
        var_id += 1
    variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, feature_AND))) + '\t3\n')
    var_id += 1

    for i in range(1, var_id):
        relations_str += (str(i) + '\t' + '0\n')
    for i in range(1, var_id-1):
        relations_str += (str(i) + '\t' + str(var_id-1) + '\n')

    relations.write(relations_str)
    variables.write(variables_str)
    relations.close()
    variables.close()
Exemple #2
0
def write_data(input_file, output_file, features):

    cnf, _ = dimacs_to_cnf(input_file)

    variables = open(output_file[0], 'w')
    relations = open(output_file[1], 'w')

    relations_str = ''
    variables_str = ''

    feature_leaf = {}

    for i in range(
            len(DIGIT_TO_SYM)):# DIGIT_TO_SYM = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
        feature_leaf[str(i)] = features[DIGIT_TO_SYM[i]]
        feature_leaf[str(-i)] = -features[DIGIT_TO_SYM[i]]


    # add global var
    feature = features['Global']
    label = 0
    variables_str += str(0) + '\t'
    variables_str += '\t'.join(list(map(str, feature))) + '\t'
    variables_str += str(label) + '\n'

    # record known vars
    if '.s' not in input_file: #the formula
        known_var = {}
        var_id = 1
        and_vars = []
        or_vars = []
        or_children = {}
        for c in cnf:
            or_vars.append(var_id)
            or_children[var_id] = []
            variables_str += (
                        str(var_id) + '\t' + '\t'.join(list(map(str, features['Or']))) + '\t' + str(type_map['Or']) + '\n')
            current_or = var_id
            var_id += 1
            for l in c:
                if l not in known_var.keys():
                    known_var[l] = var_id
                    variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, feature_leaf[str(l)]))) + '\t' + str(
                        type_map['Symbol']) + '\n')
                    this_var_id = var_id
                    var_id += 1
                else:
                    this_var_id = known_var[l]
                or_children[current_or].append(this_var_id)
        and_vars.append(var_id)
        variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, features['And']))) + '\t' + str(
                        type_map['And']) + '\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t' + '0\n')
        for or_var in or_vars:
            for or_child in or_children[or_var]:
                relations_str += (str(or_child) + '\t' + str(or_var) + '\n')
        for or_var in or_vars:
            relations_str += (str(or_var) + '\t' + str(and_vars[0]) + '\n')
    else: #the assignment
        var_id = 1
        pseudo_clause = [i[0] for i in cnf]
        for l in pseudo_clause:
            variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, feature_leaf[str(l)]))) + '\t' + str(
                type_map['Symbol']) + '\n')
            var_id += 1
        variables_str += (str(var_id) + '\t' + '\t'.join(list(map(str, features['And']))) + '\t' + str(
                        type_map['And']) + '\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t'+ str(0) + '\n')
        for i in range(1, var_id-1):
            relations_str += (str(i) + '\t' + str(var_id-1) + '\n')

    relations.write(relations_str)
    variables.write(variables_str)
    relations.close()
    variables.close()
Exemple #3
0
def write_data(input_file, output_file, features):

    cnf, _ = dimacs_to_cnf(input_file)

    variables = open(output_file[0], 'w')
    relations = open(output_file[1], 'w')

    relations_str = ''
    variables_str = ''

    and_children_list = []
    or_children_list = []

    num_feature = 50

    feature_OR = features['Or']
    feature_AND = features['And']
    feature_G = features['Global']
    # feature_leaf: [1,2,3]
    feature_leaf = {
        '1': features['a'],
        '2': features['b'],
        '3': features['c'],
        '4': features['d'],
        '5': features['e'],
        '6': features['f'],
        '7': features['g'],
        '8': features['h'],
        '9': features['i'],
        '10': features['j'],
        '11': features['k'],
        '12': features['l']
    }
    feature_leaf['-1'] = -feature_leaf['1']
    feature_leaf['-2'] = -feature_leaf['2']
    feature_leaf['-3'] = -feature_leaf['3']
    feature_leaf['-4'] = -feature_leaf['4']
    feature_leaf['-5'] = -feature_leaf['5']
    feature_leaf['-6'] = -feature_leaf['6']
    feature_leaf['-7'] = -feature_leaf['7']
    feature_leaf['-8'] = -feature_leaf['8']
    feature_leaf['-9'] = -feature_leaf['9']
    feature_leaf['-10'] = -feature_leaf['10']
    feature_leaf['-11'] = -feature_leaf['11']
    feature_leaf['-12'] = -feature_leaf['12']

    # add global var
    feature = feature_G
    label = 0
    variables_str += str(0) + '\t'
    for j in range(len(feature)):
        variables_str += str(feature[j]) + '\t'
    variables_str += str(label) + '\n'

    # record known vars
    if '.s' not in input_file:
        known_var = {}
        var_id = 1
        and_vars = []
        or_vars = []
        or_children = {}
        for c in cnf:
            or_vars.append(var_id)
            or_children[var_id] = []
            variables_str += (str(var_id) + '\t' +
                              '\t'.join(list(map(str, feature_OR))) + '\t2\n')
            current_or = var_id
            var_id += 1
            for l in c:
                if l not in known_var.keys():
                    known_var[l] = var_id
                    variables_str += (str(var_id) + '\t' + '\t'.join(
                        list(map(str, feature_leaf[str(l)]))) + '\t1\n')
                    this_var_id = var_id
                    var_id += 1
                else:
                    this_var_id = known_var[l]
                or_children[current_or].append(this_var_id)
        and_vars.append(var_id)
        variables_str += (str(var_id) + '\t' +
                          '\t'.join(list(map(str, feature_AND))) + '\t3\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t' + '0\n')
        for or_var in or_vars:
            for or_child in or_children[or_var]:
                relations_str += (str(or_child) + '\t' + str(or_var) + '\n')
        for or_var in or_vars:
            relations_str += (str(or_var) + '\t' + str(and_vars[0]) + '\n')
    else:
        var_id = 1
        pseudo_clause = [i[0] for i in cnf]
        for l in pseudo_clause:
            variables_str += (str(var_id) + '\t' +
                              '\t'.join(list(map(str, feature_leaf[str(l)]))) +
                              '\t1\n')
            var_id += 1
        variables_str += (str(var_id) + '\t' +
                          '\t'.join(list(map(str, feature_AND))) + '\t3\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t' + '0\n')
        for i in range(1, var_id - 1):
            relations_str += (str(i) + '\t' + str(var_id - 1) + '\n')

    relations.write(relations_str)
    variables.write(variables_str)
    relations.close()
    variables.close()
Exemple #4
0
def write_data(input_file, output_file, features):
    ddnnf = open(input_file, 'r')
    variables = open(output_file[0], 'w')
    relations = open(output_file[1], 'w')

    and_children = open(output_file[2], 'w')
    or_children = open(output_file[3], 'w')

    relations_str = ''
    variables_str = ''

    and_children_list = []
    or_children_list = []

    num_feature = 50

    feature_OR = features['Or']
    feature_AND = features['And']
    feature_G = features['Global']

    file_id = input_file.split('/')[-1].split('.')[-3] if '.s' in input_file else input_file.split('/')[-1].split('.')[-2]
    file_id = int(file_id)
    cnf, _ = dimacs_to_cnf(input_file.replace('vrd_ddnnf_raw', 'vrd_raw').replace('nnf', 'cnf'))
    _, r_container = prepare_clauses(clauses, annotation[idx2filename[file_id]], converter, objs)
    cnf = [r_container.get_original_repr(c) for c in cnf]

    # add global var
    feature = feature_G
    label = 0
    variables_str += str(0) + '\t'
    for j in range(len(feature)):
        variables_str += str(feature[j]) + '\t'
    variables_str += str(label) + '\n'

    line_num = -1
    for line in ddnnf.readlines():
        if line_num > -1:
            line = line.split()
            type = line[0]
            children = line[1:]
            if type == 'L':
                feature = _feature_leaf(r_container.get_original_repr([int(children[0])])[0])
                label = 1  # leaf node
            elif type == 'O':
                feature = feature_OR
                label = 2  # OR node
                or_children_list.append([])
                for child in children[2:]:
                    child = int(child)
                    or_children_list[-1].append(child + 1)
                    relations_str += str(child + 1) + '\t' + str(line_num + 1) + '\n'
            elif type == 'A':
                feature = feature_AND
                label = 3  # AND node
                and_children_list.append([])
                for child in children[1:]:
                    child = int(child)
                    and_children_list[-1].append(child + 1)
                    relations_str += str(child + 1) + '\t' + str(line_num + 1) + '\n'

            variables_str += str(line_num + 1) + '\t'
            for j in range(len(feature)):
                variables_str += str(feature[j]) + '\t'
            variables_str += str(label) + '\n'

        line_num += 1

    # add edge for global variable
    for j in range(line_num):
        relations_str += str(j + 1) + '\t' + str(0) + '\n'

    relations.write(relations_str)
    variables.write(variables_str)
    relations.close()
    variables.close()

    json.dump(and_children_list, and_children)
    json.dump(or_children_list, or_children)
Exemple #5
0
def write_data(input_file, output_file, features):
    print(input_file)
    file_id = input_file.split('/')[-1].split(
        '.')[-3] if '.s' in input_file else input_file.split('/')[-1].split(
            '.')[-2]
    file_id = int(file_id)
    print(file_id)
    cnf, _ = dimacs_to_cnf(input_file)
    _, r_container = prepare_clauses(clauses,
                                     annotation[idx2filename[file_id]],
                                     converter, objs)
    cnf = [r_container.get_original_repr(c) for c in cnf]

    variables = open(output_file[0], 'w')
    relations = open(output_file[1], 'w')

    relations_str = ''
    variables_str = ''

    feature_OR = features['Or']
    feature_AND = features['And']
    feature_G = features['Global']

    # add global var
    feature = feature_G
    label = 0
    variables_str += str(0) + '\t'
    for j in range(len(feature)):
        variables_str += str(feature[j]) + '\t'
    variables_str += str(label) + '\n'

    # record known vars
    if '.s' not in input_file:
        known_var = {}
        var_id = 1
        and_vars = []
        or_vars = []
        or_children = {}
        for c in cnf:
            or_vars.append(var_id)
            or_children[var_id] = []
            variables_str += (str(var_id) + '\t' +
                              '\t'.join(list(map(str, feature_OR))) + '\t2\n')
            current_or = var_id
            var_id += 1
            for l in c:
                if l not in known_var.keys():
                    known_var[l] = var_id
                    variables_str += (str(var_id) + '\t' + '\t'.join(
                        list(map(str, _feature_leaf(int(l))))) + '\t1\n')
                    this_var_id = var_id
                    var_id += 1
                else:
                    this_var_id = known_var[l]
                or_children[current_or].append(this_var_id)
        and_vars.append(var_id)
        variables_str += (str(var_id) + '\t' +
                          '\t'.join(list(map(str, feature_AND))) + '\t3\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t' + '0\n')
        for or_var in or_vars:
            for or_child in or_children[or_var]:
                relations_str += (str(or_child) + '\t' + str(or_var) + '\n')
        for or_var in or_vars:
            relations_str += (str(or_var) + '\t' + str(and_vars[0]) + '\n')
    else:
        var_id = 1
        pseudo_clause = [i[0] for i in cnf]
        for l in pseudo_clause:
            variables_str += (
                str(var_id) + '\t' +
                '\t'.join(list(map(str, _feature_leaf(int(l))))) + '\t1\n')
            var_id += 1
        variables_str += (str(var_id) + '\t' +
                          '\t'.join(list(map(str, feature_AND))) + '\t3\n')
        var_id += 1

        for i in range(1, var_id):
            relations_str += (str(i) + '\t' + '0\n')
        for i in range(1, var_id - 1):
            relations_str += (str(i) + '\t' + str(var_id - 1) + '\n')

    relations.write(relations_str)
    variables.write(variables_str)
    relations.close()
    variables.close()