def represent(id): data = prefutils.data_from_id(id) bin_array = prefutils.data_to_bin_array(data, dim) #print(bin_array) constraints = [] for i in range(2**dim - 1): constraints.append([y-x for x,y in zip(bin_array[i],bin_array[i+1])]) #constraints.append(data[i]- data[i+1]) #print(constraints) b = [-1] * (2**dim -1) c = [1] * dim res = linprog(c, constraints, b) #, options={"disp": True}) #print(res) if res['success']: return res['x'] else: return []
# let's count primitive pairs that appear in CSPs # I am wondering whether using lists of primitive pairs will be more efficient # that listing the top half of the CSP # there is some savings, but I think it is more efficient to store a CSP in terms of its # n parents and the central flip. dim = 6 id_list = prefutils.get_id_list(dim) data_list = [] for id in id_list: data_list.append(prefutils.data_from_id(id)) #print(data_list) count_list = [0] * 2**dim for data in data_list: count = 0 for i in range(len(data)-1): if data[i] + data[i+1] == data[i] ^ data[i+1]: #print('\t', data[i], data[i+1]) count = count + 1 #print(id, count) count_list[count] = count_list[count] + 1
# corresponds to an A (in order) or a B (out of order) for s in set_list: x = [0] * dim x[s[0]] = 1 y = [0] * dim y[s[1]] = 1 y[s[2]] = 1 pair_dict[s] = (prefutils.bin_array_to_decimal(x), prefutils.bin_array_to_decimal(y)) type_list = [] for pref in pref_list: data = prefutils.data_from_id(pref[0]) type = [] for s in set_list: pair = pair_dict[s] if (data.index(pair[0]) > data.index(pair[1])): t = 'B' else: t = 'A' type.append(t) #print(type) type_list.append(type)
#cur.execute('SELECT id FROM Pref_Six WHERE name IS NULL LIMIT 5000') cur.execute('SELECT id FROM Pref_Five') id_list = [] for (id,) in cur: id_list.append(id) count = [0] * dim middle_dic = {} for id in id_list: data = prefutils.data_from_id(id) #print(data) middle_num = data[2**(dim-1)] middle_data = prefutils.decimal_to_bin_array(data[2**(dim-1)], dim) total = sum(middle_data) # debug: looking at middle entries with exactly 2 ones. # where do they appear? if total == 2: #print(prefutils.data_to_bin_array(data, dim)) print(middle_data) if middle_num in middle_dic: middle_dic[middle_num] = middle_dic[middle_num]+1 else: middle_dic[middle_num] = 1
def run(): dim = 4 conn = mysql.connector.connect(host='localhost', database='mysql', user='******', password='******') cur = conn.cursor(buffered=True) #name_dic = name_dic4a # populate the name_dic # name_dic = {} # cur.execute('SELECT id, name from Pref_Five') # for (id, name) in cur: # name_dic[id] = name keep_running = True while (keep_running): # cur.execute('SELECT id FROM Pref_Six WHERE name IS NULL LIMIT 5000') cur.execute('SELECT id FROM Pref_Five') id_list = [] name_list = [] for (id, ) in cur: id_list.append(id) # keep_running = len(id_list) > 0 keep_running = False if (keep_running): print('processed starting with', id_list[0]) for id in id_list: # for id in [ id_list[0], id_list[1]]: data = prefutils.data_from_id(id) # print('++++++++++++ handling id', id) address = generate_address(data, dim) print(id, address) regen_id = regenerate_id(address, dim) # print(address, id) # print(data) #regen_data = [prefutils.bin_array_to_decimal(x) for x in regen_bin_data] #regen_id = prefutils.halfdata_to_id(regen_data) num_errors = 0 if not regen_id == id: print('error id and regen_id dont match', id, regen_id) num_errors = num_errors + 1 # for a,b in zip(data_bin, regen_bin_data): # print(a,b) # print('name array', address) name = '(' + ','.join(str(x) for x in address) + ')' name_list.append(name) # for id, name in zip(id_list, name_list): # print(id, name) # print(name_list) print(len(name_list)) print(len(list(set(name_list)))) print('num errors', num_errors) # print('adding to DB') param_list = [(name, id) for name, id in zip(name_list, id_list)] # cur.executemany('UPDATE Pref_Five SET name=%s WHERE id=%s', param_list) # cur.executemany('UPDATE Pref_Six SET name=%s WHERE id=%s', param_list) # conn.commit() cur.close()