def cmd(n, m, i, output_dir, dims, timeout, incremental, copy_last): """Assume that an NxM matrix is encoded as a binary number and is passed as I. Try to see whether the scoring pattern can be fit into 2D and save the output embeddings, if it can be.""" output_dir = os.path.join(output_dir, f'{i}') os.makedirs(output_dir, exist_ok=True) output_file = os.path.join(output_dir, f'{dims}D.out') prev_output_file = os.path.join(output_dir, '{}D.out'.format(dims - 1)) if os.path.exists(output_file) and incremental: print( 'Not running for m={}, n={}, i={} as the file {} already exists.'. format(m, n, i, output_file)) else: solved = False if os.path.exists(prev_output_file) and copy_last: with open(prev_output_file) as f: prev_dim_res = f.readline().strip() if prev_dim_res == 'sat': with open(output_file, 'wt') as f: f.write('sat') solved = True if not solved: M = i_to_M(n, m, i) voting_pats = D.sign_mat_to_voting_pats(M) ctx = z3.Context() prob, c_vars, v_vars = D.create_z3_prob( ctx=ctx, n_dim=dims, voting_patterns=voting_pats) prob.set('timeout', timeout) res = prob.check() if str(res) == 'sat': c_vec, v_vec = D.make_vectors_from_z3_soln(prob=prob, c_vars=c_vars, v_vars=v_vars) c_file = output_file + '.c_vecs' v_file = output_file + '.v_vecs' np.savetxt(c_file, c_vec) np.savetxt(v_file, v_vec) with open(output_file, 'wt') as f: f.write(str(res))
def _worker(key_idx): k_id = key_ids[key_idx] M = D.make_M_from_df_generic(df=df, key_name=key, key_value=k_id) voting_pats = D.sign_mat_to_voting_pats(M) unique_voting_pats = list(set(tuple(x) for x in voting_pats)) ctx = z3.Context() prob, _, _ = D.create_z3_prob(ctx=ctx, n_dim=n_dims, voting_patterns=unique_voting_pats) prob.set('timeout', timeout) res = prob.check() return {key: k_id, '{}D_sat'.format(n_dims): str(res)}
def cmd(mat_file, dim, timeout): """Read the partial matrix in MAT_FILE and save embeddings for the file to `mat_file.commenters` and `mat_file.voters` file.""" M = io.loadmat(mat_file)['M_partial'] prob, c_vars, v_vars = D.create_z3_prob( n_dim=dim, voting_patterns=D.sign_mat_to_voting_pats(M)) prob.set('timeout', timeout * 60 * 1000) res = prob.check() if res == z3.sat: c_vecs, v_vecs = D.make_vectors_from_z3_soln(prob, c_vars, v_vars) np.savetxt(mat_file + '.c_vecs', c_vecs) np.savetxt(mat_file + '.v_vecs', v_vecs) print('Done.') else: print('Result for {} was {} instead of "sat"'.format( mat_file, str(res)))
def cmd(mat_file, op_sat_file, dims, timeout): """Read the partial matrix in MAT_FILE and save satisfiability and embeddings to `OP_SAT_FILE`, `OP_SAT_FILE.c_vecs` and `OP_SAT_FILE.v_vecs`.""" M = io.loadmat(mat_file)['M_partial'] prob, c_vars, v_vars = D.create_z3_prob( n_dim=dims, voting_patterns=D.sign_mat_to_voting_pats(M)) prob.set('timeout', timeout * 60 * 1000) res = prob.check() with open(op_sat_file, 'wt') as f: f.write(str(res)) if res == z3.sat: c_vecs, v_vecs = D.make_vectors_from_z3_soln(prob, c_vars, v_vars) np.savetxt(op_sat_file + '.c_vecs', c_vecs) np.savetxt(op_sat_file + '.v_vecs', v_vecs) print('Done.') else: print('Result for {} was {} instead of "sat"'.format( mat_file, str(res)))
def cmd(in_mat_file, op_mat_file, op_loo_file, i_loo, j_loo, sat_2d, sat_1d, seed, guess): """Read M_partial from IN_MAT_FILE and fill in the matrix using Z3. The vote at (i, j) will be removed before filling in the matrix. The resulting matrix will be saved at OP_MAT_FILE and the given LOO entry will be placed along with the original vote in OP_LOO_FILE.""" M_partial = io.loadmat(in_mat_file)['M_partial'] # Leave i, j out. LOO = M_partial[i_loo, j_loo] if guess == 0: M_partial[i_loo, j_loo] = 0 else: M_partial[i_loo, j_loo] = np.sign(guess) voting_pats = D.sign_mat_to_voting_pats(M_partial) if sat_2d == 'unsat' or sat_2d == 'unknown': # Do not even attempt this. with open(in_mat_file + '.Z3.give-up', 'wt') as f: f.write('Not solved because original problem was not 2D-sat.') return elif sat_1d == 'sat': n_dim = 1 else: n_dim = 2 prob, c_vars, v_vars = D.create_z3_prob(n_dim=n_dim, voting_patterns=voting_pats) prob.set('timeout', 10 * 60 * 1000) # Set a 10 minute timeout. res = prob.check() if str(res) == 'sat': c_vec, v_vec = D.make_vectors_from_z3_soln(prob=prob, c_vars=c_vars, v_vars=v_vars) Mhat = c_vec.dot(v_vec.T) io.savemat(op_mat_file, {'Mhat': Mhat}) with open(op_loo_file, 'wt') as f: f.write('{}, {}'.format(LOO, Mhat[i_loo, j_loo])) elif str(res) == 'unknown': # If it got here without making a guess, there is a problem. if guess == 0: with open(op_mat_file + '.Z3.timeout', 'wt') as f: f.write('Timed-out.') else: # Assume that the guess was wrong and guess otherwise. with open(op_loo_file, 'wt') as f: f.write('{}, {}'.format(LOO, -1 * np.sign(guess))) elif str(res) == 'unsat': # If it got here without making a guess, there is a problem. if guess == 0: with open(op_mat_file + '.Z3.error', 'wt') as f: f.write('Problem was unsat?') else: # Assume that the guess was wrong and guess otherwise. with open(op_loo_file, 'wt') as f: f.write('{}, {}'.format(LOO, -1 * np.sign(guess)))